Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2013 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/wifi_service.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 WiFiService::NetworkProperties::NetworkProperties() | |
| 15 : connection_state(onc::connection_state::kNotConnected), | |
| 16 security(onc::wifi::kNone), | |
| 17 signal_strength(0), | |
| 18 auto_connect(false), | |
| 19 frequency(WiFiService::kFrequencyUnknown) {} | |
| 20 | |
| 21 WiFiService::NetworkProperties::~NetworkProperties() {} | |
| 22 | |
| 23 scoped_ptr<base::DictionaryValue> WiFiService::NetworkProperties::ToValue( | |
| 24 bool network_list) const { | |
| 25 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue()); | |
| 26 | |
| 27 value->SetString(onc::network_config::kGUID, guid); | |
| 28 value->SetString(onc::network_config::kName, name); | |
| 29 value->SetString(onc::network_config::kConnectionState, connection_state); | |
| 30 value->SetString(onc::network_config::kType, type); | |
| 31 | |
| 32 if (type == onc::network_type::kWiFi) { | |
| 33 scoped_ptr<base::DictionaryValue> wifi(new base::DictionaryValue()); | |
| 34 wifi->SetString(onc::wifi::kSecurity, security); | |
| 35 wifi->SetInteger(onc::wifi::kSignalStrength, signal_strength); | |
| 36 | |
| 37 // Network list expects subset of data. | |
| 38 if (!network_list) { | |
| 39 if (frequency != WiFiService::kFrequencyUnknown) | |
| 40 wifi->SetInteger(onc::wifi::kFrequency, frequency); | |
| 41 scoped_ptr<base::ListValue> frequency_list(new base::ListValue()); | |
| 42 for (FrequencyList::const_iterator it = this->frequency_list.begin(); | |
| 43 it != this->frequency_list.end(); | |
| 44 ++it) { | |
| 45 frequency_list->AppendInteger(*it); | |
| 46 } | |
| 47 if (!frequency_list->empty()) | |
| 48 wifi->Set(onc::wifi::kFrequencyList, frequency_list.release()); | |
| 49 if (!bssid.empty()) | |
| 50 wifi->SetString(onc::wifi::kBSSID, bssid); | |
| 51 wifi->SetString(onc::wifi::kSSID, ssid); | |
| 52 } | |
| 53 value->Set(onc::network_type::kWiFi, wifi.release()); | |
| 54 } else { | |
| 55 // Add properites from json extra if present. | |
| 56 if (!json_extra.empty()) { | |
| 57 Value* value_extra = base::JSONReader::Read(json_extra); | |
| 58 value->Set(type, value_extra); | |
| 59 } | |
| 60 } | |
| 61 return value.Pass(); | |
| 62 } | |
| 63 | |
| 64 bool WiFiService::NetworkProperties::UpdateFromValue( | |
| 65 const base::DictionaryValue& value) { | |
| 66 const base::DictionaryValue* wifi = NULL; | |
| 67 std::string wifi_security; | |
| 68 if (value.GetDictionary(onc::network_type::kWiFi, &wifi) && | |
| 69 wifi->GetString(onc::wifi::kSecurity, &wifi_security)) { | |
| 70 security = wifi_security; | |
| 71 return true; | |
| 72 } | |
| 73 return false; | |
| 74 } | |
| 75 | |
| 76 std::string WiFiService::NetworkProperties::MacAddressAsString( | |
| 77 const uint8 mac_as_int[6]) { | |
| 78 // mac_as_int is big-endian. Write in byte chunks. | |
| 79 // Format is XX:XX:XX:XX:XX:XX. | |
| 80 static const char* const kMacFormatString = "%02x:%02x:%02x:%02x:%02x:%02x"; | |
| 81 return base::StringPrintf(kMacFormatString, | |
| 82 mac_as_int[0], | |
| 83 mac_as_int[1], | |
| 84 mac_as_int[2], | |
| 85 mac_as_int[3], | |
| 86 mac_as_int[4], | |
| 87 mac_as_int[5]); | |
| 88 } | |
| 89 | |
| 90 bool WiFiService::NetworkProperties::OrderByType(const NetworkProperties& l, | |
| 91 const NetworkProperties& r) { | |
| 92 if (l.connection_state != r.connection_state) | |
| 93 return l.connection_state < r.connection_state; | |
| 94 // This sorting order is needed only for browser_tests, which expect this | |
| 95 // network type sort order: ethernet < wifi < vpn < cellular. | |
| 96 if (l.type == r.type) | |
| 97 return l.guid < r.guid; | |
| 98 if (l.type == onc::network_type::kEthernet) | |
| 99 return true; | |
| 100 if (r.type == onc::network_type::kEthernet) | |
| 101 return false; | |
| 102 return l.type > r.type; | |
| 103 } | |
| 104 } // namespace wifi | |
|
Jói
2013/11/20 11:04:18
nit: blank line before
mef
2013/11/20 16:19:35
Done.
| |
| OLD | NEW |