Fix theoretical int truncation
[chromium-blink-merge.git] / components / wifi / wifi_service.cc
blob340a4b554fed3a04e96f0a538e2eaf1c9342d1d8
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.
5 #include "components/wifi/wifi_service.h"
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"
12 namespace wifi {
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) {}
21 WiFiService::NetworkProperties::~NetworkProperties() {}
23 scoped_ptr<base::DictionaryValue> WiFiService::NetworkProperties::ToValue(
24 bool network_list) const {
25 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue());
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);
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);
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 (FrequencySet::const_iterator it = this->frequency_set.begin();
43 it != this->frequency_set.end();
44 ++it) {
45 frequency_list->AppendInteger(*it);
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);
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 base::Value* value_extra = base::JSONReader::Read(json_extra);
58 value->Set(type, value_extra);
61 return value.Pass();
64 bool WiFiService::NetworkProperties::UpdateFromValue(
65 const base::DictionaryValue& value) {
66 const base::DictionaryValue* wifi = NULL;
67 std::string network_type;
68 // Get network type and make sure that it is WiFi (if specified).
69 if (value.GetString(onc::network_config::kType, &network_type)) {
70 if (network_type != onc::network_type::kWiFi)
71 return false;
72 type = network_type;
74 if (value.GetDictionary(onc::network_type::kWiFi, &wifi)) {
75 wifi->GetString(onc::wifi::kSecurity, &security);
76 wifi->GetString(onc::wifi::kSSID, &ssid);
77 wifi->GetString(onc::wifi::kPassphrase, &password);
78 wifi->GetBoolean(onc::wifi::kAutoConnect, &auto_connect);
79 return true;
81 return false;
84 std::string WiFiService::NetworkProperties::MacAddressAsString(
85 const uint8 mac_as_int[6]) {
86 // mac_as_int is big-endian. Write in byte chunks.
87 // Format is XX:XX:XX:XX:XX:XX.
88 static const char* const kMacFormatString = "%02x:%02x:%02x:%02x:%02x:%02x";
89 return base::StringPrintf(kMacFormatString,
90 mac_as_int[0],
91 mac_as_int[1],
92 mac_as_int[2],
93 mac_as_int[3],
94 mac_as_int[4],
95 mac_as_int[5]);
98 bool WiFiService::NetworkProperties::OrderByType(const NetworkProperties& l,
99 const NetworkProperties& r) {
100 if (l.connection_state != r.connection_state)
101 return l.connection_state < r.connection_state;
102 // This sorting order is needed only for browser_tests, which expect this
103 // network type sort order: ethernet < wifi < vpn < cellular.
104 if (l.type == r.type)
105 return l.guid < r.guid;
106 if (l.type == onc::network_type::kEthernet)
107 return true;
108 if (r.type == onc::network_type::kEthernet)
109 return false;
110 return l.type > r.type;
113 } // namespace wifi