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/fake_wifi_service.h"
8 #include "base/json/json_reader.h"
9 #include "base/message_loop/message_loop.h"
10 #include "components/onc/onc_constants.h"
14 FakeWiFiService::FakeWiFiService() {
15 // Populate data expected by unit test.
17 WiFiService::NetworkProperties network_properties
;
18 network_properties
.connection_state
= onc::connection_state::kConnected
;
19 network_properties
.guid
= "stub_ethernet";
20 network_properties
.name
= "eth0";
21 network_properties
.type
= onc::network_type::kEthernet
;
22 network_properties
.json_extra
=
24 " \"Authentication\": \"None\""
26 networks_
.push_back(network_properties
);
29 WiFiService::NetworkProperties network_properties
;
30 network_properties
.connection_state
= onc::connection_state::kConnected
;
31 network_properties
.guid
= "stub_wifi1";
32 network_properties
.name
= "wifi1";
33 network_properties
.type
= onc::network_type::kWiFi
;
34 network_properties
.frequency
= 0;
35 network_properties
.ssid
= "stub_wifi1";
36 network_properties
.security
= onc::wifi::kWEP_PSK
;
37 network_properties
.signal_strength
= 0;
38 networks_
.push_back(network_properties
);
41 WiFiService::NetworkProperties network_properties
;
42 network_properties
.connection_state
= onc::connection_state::kConnected
;
43 network_properties
.guid
= "stub_vpn1";
44 network_properties
.name
= "vpn1";
45 network_properties
.type
= onc::network_type::kVPN
;
46 networks_
.push_back(network_properties
);
49 WiFiService::NetworkProperties network_properties
;
50 network_properties
.connection_state
= onc::connection_state::kNotConnected
;
51 network_properties
.guid
= "stub_wifi2";
52 network_properties
.name
= "wifi2_PSK";
53 network_properties
.type
= onc::network_type::kWiFi
;
54 network_properties
.frequency
= 5000;
55 network_properties
.frequency_set
.insert(2400);
56 network_properties
.frequency_set
.insert(5000);
57 network_properties
.ssid
= "wifi2_PSK";
58 network_properties
.security
= onc::wifi::kWPA_PSK
;
59 network_properties
.signal_strength
= 80;
60 networks_
.push_back(network_properties
);
63 WiFiService::NetworkProperties network_properties
;
64 network_properties
.connection_state
= onc::connection_state::kNotConnected
;
65 network_properties
.guid
= "stub_cellular1";
66 network_properties
.name
= "cellular1";
67 network_properties
.type
= onc::network_type::kCellular
;
68 network_properties
.json_extra
=
70 " \"ActivationState\": \"not-activated\","
71 " \"NetworkTechnology\": \"GSM\","
72 " \"RoamingState\": \"home\""
74 networks_
.push_back(network_properties
);
78 FakeWiFiService::~FakeWiFiService() {
81 void FakeWiFiService::Initialize(
82 scoped_refptr
<base::SequencedTaskRunner
> task_runner
) {
85 void FakeWiFiService::UnInitialize() {
88 void FakeWiFiService::GetProperties(const std::string
& network_guid
,
89 base::DictionaryValue
* properties
,
91 WiFiService::NetworkList::iterator network_properties
=
92 FindNetwork(network_guid
);
93 if (network_properties
!= networks_
.end()) {
94 properties
->Swap(network_properties
->ToValue(false).get());
96 *error
= "Error.DBusFailed";
100 void FakeWiFiService::GetManagedProperties(
101 const std::string
& network_guid
,
102 base::DictionaryValue
* managed_properties
,
103 std::string
* error
) {
104 const std::string network_properties
=
106 " \"ConnectionState\": {"
107 " \"Active\": \"NotConnected\","
108 " \"Effective\": \"Unmanaged\""
110 " \"GUID\": \"stub_wifi2\","
112 " \"Active\": \"wifi2_PSK\","
113 " \"Effective\": \"UserPolicy\","
114 " \"UserPolicy\": \"My WiFi Network\""
117 " \"Active\": \"WiFi\","
118 " \"Effective\": \"UserPolicy\","
119 " \"UserPolicy\": \"WiFi\""
122 " \"AutoConnect\": {"
123 " \"Active\": false,"
124 " \"UserEditable\": true"
128 " \"Effective\": \"Unmanaged\""
130 " \"FrequencyList\" : {"
131 " \"Active\": [2400, 5000],"
132 " \"Effective\": \"Unmanaged\""
135 " \"Effective\": \"UserSetting\","
136 " \"UserEditable\": true,"
137 " \"UserSetting\": \"FAKE_CREDENTIAL_VPaJDV9x\""
140 " \"Active\": \"wifi2_PSK\","
141 " \"Effective\": \"UserPolicy\","
142 " \"UserPolicy\": \"wifi2_PSK\""
145 " \"Active\": \"WPA-PSK\","
146 " \"Effective\": \"UserPolicy\","
147 " \"UserPolicy\": \"WPA-PSK\""
149 " \"SignalStrength\": {"
151 " \"Effective\": \"Unmanaged\""
155 scoped_ptr
<base::DictionaryValue
> properties_value(
156 reinterpret_cast<base::DictionaryValue
*>(
157 base::JSONReader::Read(network_properties
)));
158 managed_properties
->MergeDictionary(properties_value
.get());
161 void FakeWiFiService::GetState(const std::string
& network_guid
,
162 base::DictionaryValue
* properties
,
163 std::string
* error
) {
164 WiFiService::NetworkList::iterator network_properties
=
165 FindNetwork(network_guid
);
166 if (network_properties
== networks_
.end()) {
167 *error
= "Error.InvalidParameter";
171 const std::string network_state
=
173 " \"ConnectionState\": \"NotConnected\","
174 " \"GUID\": \"stub_wifi2\","
175 " \"Name\": \"wifi2_PSK\","
176 " \"Type\": \"WiFi\","
178 " \"Security\": \"WPA-PSK\","
179 " \"SignalStrength\": 80"
182 scoped_ptr
<base::DictionaryValue
> properties_value(
183 reinterpret_cast<base::DictionaryValue
*>(
184 base::JSONReader::Read(network_state
)));
185 properties
->MergeDictionary(properties_value
.get());
188 void FakeWiFiService::SetProperties(
189 const std::string
& network_guid
,
190 scoped_ptr
<base::DictionaryValue
> properties
,
191 std::string
* error
) {
192 WiFiService::NetworkList::iterator network_properties
=
193 FindNetwork(network_guid
);
194 if (network_properties
== networks_
.end() ||
195 !network_properties
->UpdateFromValue(*properties
)) {
196 *error
= "Error.DBusFailed";
200 void FakeWiFiService::CreateNetwork(
202 scoped_ptr
<base::DictionaryValue
> properties
,
203 std::string
* network_guid
,
204 std::string
* error
) {
205 WiFiService::NetworkProperties network_properties
;
206 if (network_properties
.UpdateFromValue(*properties
)) {
207 network_properties
.guid
= network_properties
.ssid
;
208 networks_
.push_back(network_properties
);
209 *network_guid
= network_properties
.guid
;
211 *error
= "Error.DBusFailed";
215 void FakeWiFiService::GetVisibleNetworks(const std::string
& network_type
,
216 base::ListValue
* network_list
) {
217 for (WiFiService::NetworkList::const_iterator it
= networks_
.begin();
218 it
!= networks_
.end();
220 if (network_type
.empty() || network_type
== onc::network_type::kAllTypes
||
221 it
->type
== network_type
) {
222 scoped_ptr
<base::DictionaryValue
> network(it
->ToValue(true));
223 network_list
->Append(network
.release());
228 void FakeWiFiService::RequestNetworkScan() {
229 NotifyNetworkListChanged(networks_
);
232 void FakeWiFiService::StartConnect(const std::string
& network_guid
,
233 std::string
* error
) {
234 NetworkList::iterator network_properties
= FindNetwork(network_guid
);
235 if (network_properties
!= networks_
.end()) {
236 DisconnectAllNetworksOfType(network_properties
->type
);
237 network_properties
->connection_state
= onc::connection_state::kConnected
;
239 NotifyNetworkListChanged(networks_
);
240 NotifyNetworkChanged(network_guid
);
242 *error
= "configure-failed";
246 void FakeWiFiService::StartDisconnect(const std::string
& network_guid
,
247 std::string
* error
) {
248 WiFiService::NetworkList::iterator network_properties
=
249 FindNetwork(network_guid
);
250 if (network_properties
!= networks_
.end()) {
251 network_properties
->connection_state
= onc::connection_state::kNotConnected
;
253 NotifyNetworkListChanged(networks_
);
254 NotifyNetworkChanged(network_guid
);
256 *error
= "not-found";
260 void FakeWiFiService::GetKeyFromSystem(const std::string
& network_guid
,
261 std::string
* key_data
,
262 std::string
* error
) {
263 *error
= "not-found";
266 void FakeWiFiService::SetEventObservers(
267 scoped_refptr
<base::MessageLoopProxy
> message_loop_proxy
,
268 const NetworkGuidListCallback
& networks_changed_observer
,
269 const NetworkGuidListCallback
& network_list_changed_observer
) {
270 message_loop_proxy_
.swap(message_loop_proxy
);
271 networks_changed_observer_
= networks_changed_observer
;
272 network_list_changed_observer_
= network_list_changed_observer
;
275 void FakeWiFiService::RequestConnectedNetworkUpdate() {
278 WiFiService::NetworkList::iterator
FakeWiFiService::FindNetwork(
279 const std::string
& network_guid
) {
280 for (WiFiService::NetworkList::iterator it
= networks_
.begin();
281 it
!= networks_
.end();
283 if (it
->guid
== network_guid
)
286 return networks_
.end();
289 void FakeWiFiService::DisconnectAllNetworksOfType(const std::string
& type
) {
290 for (WiFiService::NetworkList::iterator it
= networks_
.begin();
291 it
!= networks_
.end();
293 if (it
->type
== type
)
294 it
->connection_state
= onc::connection_state::kNotConnected
;
298 void FakeWiFiService::SortNetworks() {
299 // Sort networks, so connected/connecting is up front, then by type:
300 // Ethernet, WiFi, Cellular, VPN
301 networks_
.sort(WiFiService::NetworkProperties::OrderByType
);
304 void FakeWiFiService::NotifyNetworkListChanged(
305 const WiFiService::NetworkList
& networks
) {
306 WiFiService::NetworkGuidList current_networks
;
307 for (WiFiService::NetworkList::const_iterator it
= networks
.begin();
308 it
!= networks
.end();
310 current_networks
.push_back(it
->guid
);
313 message_loop_proxy_
->PostTask(
314 FROM_HERE
, base::Bind(network_list_changed_observer_
, current_networks
));
317 void FakeWiFiService::NotifyNetworkChanged(const std::string
& network_guid
) {
318 WiFiService::NetworkGuidList
changed_networks(1, network_guid
);
319 message_loop_proxy_
->PostTask(
320 FROM_HERE
, base::Bind(networks_changed_observer_
, changed_networks
));