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 NetworkProperties network_properties
;
18 network_properties
.connection_state
= onc::connection_state::kConnected
;
19 network_properties
.guid
= "stub_wifi1_guid";
20 network_properties
.name
= "wifi1";
21 network_properties
.type
= onc::network_type::kWiFi
;
22 network_properties
.frequency
= 0;
23 network_properties
.ssid
= "wifi1";
24 network_properties
.security
= onc::wifi::kWEP_PSK
;
25 network_properties
.signal_strength
= 40;
26 network_properties
.json_extra
=
28 " \"MacAddress\": \"00:11:22:AA:BB:CC\","
30 " \"Gateway\": \"0.0.0.1\","
31 " \"IPAddress\": \"0.0.0.0\","
32 " \"RoutingPrefix\": 0,"
36 " \"Frequency\": 2400,"
37 " \"FrequencyList\": [2400]"
40 networks_
.push_back(network_properties
);
43 NetworkProperties network_properties
;
44 network_properties
.connection_state
= onc::connection_state::kNotConnected
;
45 network_properties
.guid
= "stub_wifi2_guid";
46 network_properties
.name
= "wifi2_PSK";
47 network_properties
.type
= onc::network_type::kWiFi
;
48 network_properties
.frequency
= 5000;
49 network_properties
.frequency_set
.insert(2400);
50 network_properties
.frequency_set
.insert(5000);
51 network_properties
.ssid
= "wifi2_PSK";
52 network_properties
.security
= onc::wifi::kWPA_PSK
;
53 network_properties
.signal_strength
= 80;
54 networks_
.push_back(network_properties
);
58 FakeWiFiService::~FakeWiFiService() {
61 void FakeWiFiService::Initialize(
62 scoped_refptr
<base::SequencedTaskRunner
> task_runner
) {
65 void FakeWiFiService::UnInitialize() {
68 void FakeWiFiService::GetProperties(const std::string
& network_guid
,
69 base::DictionaryValue
* properties
,
71 NetworkList::iterator network_properties
= FindNetwork(network_guid
);
72 if (network_properties
== networks_
.end()) {
73 *error
= "Error.InvalidNetworkGuid";
76 properties
->Swap(network_properties
->ToValue(false).get());
79 void FakeWiFiService::GetManagedProperties(
80 const std::string
& network_guid
,
81 base::DictionaryValue
* managed_properties
,
84 *error
= kErrorWiFiService
;
87 void FakeWiFiService::GetState(const std::string
& network_guid
,
88 base::DictionaryValue
* properties
,
90 NetworkList::iterator network_properties
= FindNetwork(network_guid
);
91 if (network_properties
== networks_
.end()) {
92 *error
= "Error.InvalidNetworkGuid";
95 properties
->Swap(network_properties
->ToValue(true).get());
98 void FakeWiFiService::SetProperties(
99 const std::string
& network_guid
,
100 scoped_ptr
<base::DictionaryValue
> properties
,
101 std::string
* error
) {
102 NetworkList::iterator network_properties
= FindNetwork(network_guid
);
103 if (network_properties
== networks_
.end() ||
104 !network_properties
->UpdateFromValue(*properties
)) {
105 *error
= "Error.DBusFailed";
109 void FakeWiFiService::CreateNetwork(
111 scoped_ptr
<base::DictionaryValue
> properties
,
112 std::string
* network_guid
,
113 std::string
* error
) {
114 NetworkProperties network_properties
;
115 if (network_properties
.UpdateFromValue(*properties
)) {
116 network_properties
.guid
= network_properties
.ssid
;
117 networks_
.push_back(network_properties
);
118 *network_guid
= network_properties
.guid
;
120 *error
= "Error.DBusFailed";
124 void FakeWiFiService::GetVisibleNetworks(const std::string
& network_type
,
125 base::ListValue
* network_list
,
126 bool include_details
) {
127 for (NetworkList::const_iterator it
= networks_
.begin();
128 it
!= networks_
.end();
130 if (network_type
.empty() || network_type
== onc::network_type::kAllTypes
||
131 it
->type
== network_type
) {
132 scoped_ptr
<base::DictionaryValue
> network(it
->ToValue(!include_details
));
133 network_list
->Append(network
.release());
138 void FakeWiFiService::RequestNetworkScan() {
139 NotifyNetworkListChanged(networks_
);
142 void FakeWiFiService::StartConnect(const std::string
& network_guid
,
143 std::string
* error
) {
144 NetworkList::iterator network_properties
= FindNetwork(network_guid
);
145 if (network_properties
== networks_
.end()) {
146 *error
= "Error.InvalidNetworkGuid";
149 DisconnectAllNetworksOfType(network_properties
->type
);
150 network_properties
->connection_state
= onc::connection_state::kConnected
;
152 NotifyNetworkListChanged(networks_
);
153 NotifyNetworkChanged(network_guid
);
156 void FakeWiFiService::StartDisconnect(const std::string
& network_guid
,
157 std::string
* error
) {
158 NetworkList::iterator network_properties
= FindNetwork(network_guid
);
159 if (network_properties
== networks_
.end()) {
160 *error
= "Error.InvalidNetworkGuid";
163 network_properties
->connection_state
= onc::connection_state::kNotConnected
;
165 NotifyNetworkListChanged(networks_
);
166 NotifyNetworkChanged(network_guid
);
169 void FakeWiFiService::GetKeyFromSystem(const std::string
& network_guid
,
170 std::string
* key_data
,
171 std::string
* error
) {
172 *error
= "not-found";
175 void FakeWiFiService::SetEventObservers(
176 scoped_refptr
<base::MessageLoopProxy
> message_loop_proxy
,
177 const NetworkGuidListCallback
& networks_changed_observer
,
178 const NetworkGuidListCallback
& network_list_changed_observer
) {
179 message_loop_proxy_
.swap(message_loop_proxy
);
180 networks_changed_observer_
= networks_changed_observer
;
181 network_list_changed_observer_
= network_list_changed_observer
;
184 void FakeWiFiService::RequestConnectedNetworkUpdate() {
187 NetworkList::iterator
FakeWiFiService::FindNetwork(
188 const std::string
& network_guid
) {
189 for (NetworkList::iterator it
= networks_
.begin(); it
!= networks_
.end();
191 if (it
->guid
== network_guid
)
194 return networks_
.end();
197 void FakeWiFiService::DisconnectAllNetworksOfType(const std::string
& type
) {
198 for (NetworkList::iterator it
= networks_
.begin(); it
!= networks_
.end();
200 if (it
->type
== type
)
201 it
->connection_state
= onc::connection_state::kNotConnected
;
205 void FakeWiFiService::SortNetworks() {
206 // Sort networks, so connected/connecting is up front, then by type:
207 // Ethernet, WiFi, Cellular, VPN
208 networks_
.sort(NetworkProperties::OrderByType
);
211 void FakeWiFiService::NotifyNetworkListChanged(const NetworkList
& networks
) {
212 WiFiService::NetworkGuidList current_networks
;
213 for (NetworkList::const_iterator it
= networks
.begin(); it
!= networks
.end();
215 current_networks
.push_back(it
->guid
);
218 message_loop_proxy_
->PostTask(
219 FROM_HERE
, base::Bind(network_list_changed_observer_
, current_networks
));
222 void FakeWiFiService::NotifyNetworkChanged(const std::string
& network_guid
) {
223 WiFiService::NetworkGuidList
changed_networks(1, network_guid
);
224 message_loop_proxy_
->PostTask(
225 FROM_HERE
, base::Bind(networks_changed_observer_
, changed_networks
));