1 // Copyright 2014 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #include "components/wifi/network_properties.h" 6 7 #include "base/json/json_reader.h" 8 #include "base/message_loop/message_loop.h" 9 #include "base/strings/stringprintf.h" 10 #include "components/onc/onc_constants.h" 11 12 namespace wifi { 13 14 NetworkProperties::NetworkProperties() 15 : connection_state(onc::connection_state::kNotConnected), 16 security(onc::wifi::kSecurityNone), 17 signal_strength(0), 18 auto_connect(false), 19 frequency(kFrequencyUnknown) { 20 } 21 22 NetworkProperties::~NetworkProperties() { 23 } 24 25 scoped_ptr<base::DictionaryValue> NetworkProperties::ToValue( 26 bool network_list) const { 27 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue()); 28 29 value->SetString(onc::network_config::kGUID, guid); 30 value->SetString(onc::network_config::kName, name); 31 value->SetString(onc::network_config::kConnectionState, connection_state); 32 DCHECK(type == onc::network_type::kWiFi); 33 value->SetString(onc::network_config::kType, type); 34 35 // For now, assume all WiFi services are connectable. 36 value->SetBoolean(onc::network_config::kConnectable, true); 37 38 scoped_ptr<base::DictionaryValue> wifi(new base::DictionaryValue()); 39 wifi->SetString(onc::wifi::kSecurity, security); 40 wifi->SetInteger(onc::wifi::kSignalStrength, signal_strength); 41 42 // Network list expects subset of data. 43 if (!network_list) { 44 if (frequency != kFrequencyUnknown) 45 wifi->SetInteger(onc::wifi::kFrequency, frequency); 46 scoped_ptr<base::ListValue> frequency_list(new base::ListValue()); 47 for (FrequencySet::const_iterator it = this->frequency_set.begin(); 48 it != this->frequency_set.end(); 49 ++it) { 50 frequency_list->AppendInteger(*it); 51 } 52 if (!frequency_list->empty()) 53 wifi->Set(onc::wifi::kFrequencyList, frequency_list.release()); 54 if (!bssid.empty()) 55 wifi->SetString(onc::wifi::kBSSID, bssid); 56 wifi->SetString(onc::wifi::kSSID, ssid); 57 } 58 value->Set(onc::network_type::kWiFi, wifi.release()); 59 60 if (!network_list && !json_extra.empty()) { 61 base::Value* value_extra = base::JSONReader::Read(json_extra); 62 CHECK(value_extra); 63 base::DictionaryValue* value_dictionary; 64 if (value_extra->GetAsDictionary(&value_dictionary)) 65 value->MergeDictionary(value_dictionary); 66 } 67 68 return value.Pass(); 69 } 70 71 bool NetworkProperties::UpdateFromValue(const base::DictionaryValue& value) { 72 const base::DictionaryValue* wifi = NULL; 73 std::string network_type; 74 // Get network type and make sure that it is WiFi (if specified). 75 if (value.GetString(onc::network_config::kType, &network_type)) { 76 if (network_type != onc::network_type::kWiFi) 77 return false; 78 type = network_type; 79 } 80 if (value.GetDictionary(onc::network_type::kWiFi, &wifi)) { 81 value.GetString(onc::network_config::kName, &name); 82 value.GetString(onc::network_config::kGUID, &guid); 83 value.GetString(onc::network_config::kConnectionState, &connection_state); 84 wifi->GetString(onc::wifi::kSecurity, &security); 85 wifi->GetString(onc::wifi::kSSID, &ssid); 86 wifi->GetString(onc::wifi::kPassphrase, &password); 87 wifi->GetBoolean(onc::wifi::kAutoConnect, &auto_connect); 88 return true; 89 } 90 return false; 91 } 92 93 std::string NetworkProperties::MacAddressAsString(const uint8 mac_as_int[6]) { 94 // mac_as_int is big-endian. Write in byte chunks. 95 // Format is XX:XX:XX:XX:XX:XX. 96 static const char* const kMacFormatString = "%02x:%02x:%02x:%02x:%02x:%02x"; 97 return base::StringPrintf(kMacFormatString, 98 mac_as_int[0], 99 mac_as_int[1], 100 mac_as_int[2], 101 mac_as_int[3], 102 mac_as_int[4], 103 mac_as_int[5]); 104 } 105 106 bool NetworkProperties::OrderByType(const NetworkProperties& l, 107 const NetworkProperties& r) { 108 if (l.connection_state != r.connection_state) 109 return l.connection_state < r.connection_state; 110 // This sorting order is needed only for browser_tests, which expect this 111 // network type sort order: ethernet < wifi < vpn < cellular. 112 if (l.type == r.type) 113 return l.guid < r.guid; 114 if (l.type == onc::network_type::kEthernet) 115 return true; 116 if (r.type == onc::network_type::kEthernet) 117 return false; 118 return l.type > r.type; 119 } 120 121 } // namespace wifi 122