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/json/json_reader.h"
8 #include "base/message_loop/message_loop.h"
9 #include "base/strings/string_number_conversions.h"
10 #include "base/strings/stringprintf.h"
11 #include "components/onc/onc_constants.h"
15 NetworkProperties::NetworkProperties()
16 : connection_state(onc::connection_state::kNotConnected
),
17 security(onc::wifi::kSecurityNone
),
20 frequency(kFrequencyUnknown
) {
23 NetworkProperties::~NetworkProperties() {
26 scoped_ptr
<base::DictionaryValue
> NetworkProperties::ToValue(
27 bool network_list
) const {
28 scoped_ptr
<base::DictionaryValue
> value(new base::DictionaryValue());
30 value
->SetString(onc::network_config::kGUID
, guid
);
31 value
->SetString(onc::network_config::kName
, name
);
32 value
->SetString(onc::network_config::kConnectionState
, connection_state
);
33 DCHECK(type
== onc::network_type::kWiFi
);
34 value
->SetString(onc::network_config::kType
, type
);
36 // For now, assume all WiFi services are connectable.
37 value
->SetBoolean(onc::network_config::kConnectable
, true);
39 scoped_ptr
<base::DictionaryValue
> wifi(new base::DictionaryValue());
40 wifi
->SetString(onc::wifi::kSecurity
, security
);
41 wifi
->SetInteger(onc::wifi::kSignalStrength
, signal_strength
);
43 // Network list expects subset of data.
45 if (frequency
!= kFrequencyUnknown
)
46 wifi
->SetInteger(onc::wifi::kFrequency
, frequency
);
47 scoped_ptr
<base::ListValue
> frequency_list(new base::ListValue());
48 for (FrequencySet::const_iterator it
= this->frequency_set
.begin();
49 it
!= this->frequency_set
.end();
51 frequency_list
->AppendInteger(*it
);
53 if (!frequency_list
->empty())
54 wifi
->Set(onc::wifi::kFrequencyList
, frequency_list
.release());
56 wifi
->SetString(onc::wifi::kBSSID
, bssid
);
57 wifi
->SetString(onc::wifi::kSSID
, ssid
);
58 wifi
->SetString(onc::wifi::kHexSSID
,
59 base::HexEncode(ssid
.c_str(), ssid
.size()));
61 value
->Set(onc::network_type::kWiFi
, wifi
.release());
63 if (!network_list
&& !json_extra
.empty()) {
64 base::Value
* value_extra
= base::JSONReader::Read(json_extra
);
66 base::DictionaryValue
* value_dictionary
;
67 if (value_extra
->GetAsDictionary(&value_dictionary
))
68 value
->MergeDictionary(value_dictionary
);
74 bool NetworkProperties::UpdateFromValue(const base::DictionaryValue
& value
) {
75 const base::DictionaryValue
* wifi
= NULL
;
76 std::string network_type
;
77 // Get network type and make sure that it is WiFi (if specified).
78 if (value
.GetString(onc::network_config::kType
, &network_type
)) {
79 if (network_type
!= onc::network_type::kWiFi
)
83 if (value
.GetDictionary(onc::network_type::kWiFi
, &wifi
)) {
84 value
.GetString(onc::network_config::kName
, &name
);
85 value
.GetString(onc::network_config::kGUID
, &guid
);
86 value
.GetString(onc::network_config::kConnectionState
, &connection_state
);
87 wifi
->GetString(onc::wifi::kSecurity
, &security
);
88 wifi
->GetString(onc::wifi::kSSID
, &ssid
);
89 wifi
->GetString(onc::wifi::kPassphrase
, &password
);
90 wifi
->GetBoolean(onc::wifi::kAutoConnect
, &auto_connect
);
96 std::string
NetworkProperties::MacAddressAsString(const uint8 mac_as_int
[6]) {
97 // mac_as_int is big-endian. Write in byte chunks.
98 // Format is XX:XX:XX:XX:XX:XX.
99 static const char* const kMacFormatString
= "%02x:%02x:%02x:%02x:%02x:%02x";
100 return base::StringPrintf(kMacFormatString
,
109 bool NetworkProperties::OrderByType(const NetworkProperties
& l
,
110 const NetworkProperties
& r
) {
111 if (l
.connection_state
!= r
.connection_state
)
112 return l
.connection_state
< r
.connection_state
;
113 // This sorting order is needed only for browser_tests, which expect this
114 // network type sort order: ethernet < wifi < vpn < cellular.
115 if (l
.type
== r
.type
)
116 return l
.guid
< r
.guid
;
117 if (l
.type
== onc::network_type::kEthernet
)
119 if (r
.type
== onc::network_type::kEthernet
)
121 return l
.type
> r
.type
;