Allow only one bookmark to be added for multiple fast starring
[chromium-blink-merge.git] / components / wifi / network_properties.cc
blob68af45247ee47f1dc99ce99295ffd3dad2bab29a
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.
5 #include "components/wifi/network_properties.h"
7 #include "base/message_loop/message_loop.h"
8 #include "base/strings/string_number_conversions.h"
9 #include "base/strings/stringprintf.h"
10 #include "components/onc/onc_constants.h"
12 namespace wifi {
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) {
22 NetworkProperties::~NetworkProperties() {
25 scoped_ptr<base::DictionaryValue> NetworkProperties::ToValue(
26 bool network_list) const {
27 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue());
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);
35 // For now, assume all WiFi services are connectable.
36 value->SetBoolean(onc::network_config::kConnectable, true);
38 scoped_ptr<base::DictionaryValue> wifi(new base::DictionaryValue());
39 wifi->SetString(onc::wifi::kSecurity, security);
40 wifi->SetInteger(onc::wifi::kSignalStrength, signal_strength);
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);
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 wifi->SetString(onc::wifi::kHexSSID,
58 base::HexEncode(ssid.c_str(), ssid.size()));
60 value->Set(onc::network_type::kWiFi, wifi.release());
62 return value.Pass();
65 bool NetworkProperties::UpdateFromValue(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 value.GetString(onc::network_config::kName, &name);
76 value.GetString(onc::network_config::kGUID, &guid);
77 value.GetString(onc::network_config::kConnectionState, &connection_state);
78 wifi->GetString(onc::wifi::kSecurity, &security);
79 wifi->GetString(onc::wifi::kSSID, &ssid);
80 wifi->GetString(onc::wifi::kPassphrase, &password);
81 wifi->GetBoolean(onc::wifi::kAutoConnect, &auto_connect);
82 return true;
84 return false;
87 std::string NetworkProperties::MacAddressAsString(const uint8 mac_as_int[6]) {
88 // mac_as_int is big-endian. Write in byte chunks.
89 // Format is XX:XX:XX:XX:XX:XX.
90 static const char* const kMacFormatString = "%02x:%02x:%02x:%02x:%02x:%02x";
91 return base::StringPrintf(kMacFormatString,
92 mac_as_int[0],
93 mac_as_int[1],
94 mac_as_int[2],
95 mac_as_int[3],
96 mac_as_int[4],
97 mac_as_int[5]);
100 bool NetworkProperties::OrderByType(const NetworkProperties& l,
101 const NetworkProperties& r) {
102 if (l.connection_state != r.connection_state)
103 return l.connection_state < r.connection_state;
104 // This sorting order is needed only for browser_tests, which expect this
105 // network type sort order: ethernet < wifi < vpn < cellular.
106 if (l.type == r.type)
107 return l.guid < r.guid;
108 if (l.type == onc::network_type::kEthernet)
109 return true;
110 if (r.type == onc::network_type::kEthernet)
111 return false;
112 return l.type > r.type;
115 } // namespace wifi