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"
8 #include "base/json/json_reader.h"
9 #include "base/message_loop/message_loop.h"
10 #include "components/onc/onc_constants.h"
14 // Fake implementation of WiFiService used to satisfy expectations of
15 // networkingPrivateApi browser test.
16 class FakeWiFiService
: public WiFiService
{
19 // Populate data expected by unit test.
21 WiFiService::NetworkProperties network_properties
;
22 network_properties
.connection_state
= onc::connection_state::kConnected
;
23 network_properties
.guid
= "stub_ethernet";
24 network_properties
.name
= "eth0";
25 network_properties
.type
= onc::network_type::kEthernet
;
26 network_properties
.json_extra
=
28 " \"Authentication\": \"None\""
30 networks_
.push_back(network_properties
);
33 WiFiService::NetworkProperties network_properties
;
34 network_properties
.connection_state
= onc::connection_state::kConnected
;
35 network_properties
.guid
= "stub_wifi1";
36 network_properties
.name
= "wifi1";
37 network_properties
.type
= onc::network_type::kWiFi
;
38 network_properties
.frequency
= 0;
39 network_properties
.ssid
= "stub_wifi1";
40 network_properties
.security
= onc::wifi::kWEP_PSK
;
41 network_properties
.signal_strength
= 0;
42 networks_
.push_back(network_properties
);
45 WiFiService::NetworkProperties network_properties
;
46 network_properties
.connection_state
= onc::connection_state::kConnected
;
47 network_properties
.guid
= "stub_vpn1";
48 network_properties
.name
= "vpn1";
49 network_properties
.type
= onc::network_type::kVPN
;
50 networks_
.push_back(network_properties
);
53 WiFiService::NetworkProperties network_properties
;
54 network_properties
.connection_state
=
55 onc::connection_state::kNotConnected
;
56 network_properties
.guid
= "stub_wifi2";
57 network_properties
.name
= "wifi2_PSK";
58 network_properties
.type
= onc::network_type::kWiFi
;
59 network_properties
.frequency
= 5000;
60 network_properties
.frequency_set
.insert(2400);
61 network_properties
.frequency_set
.insert(5000);
62 network_properties
.ssid
= "wifi2_PSK";
63 network_properties
.security
= onc::wifi::kWPA_PSK
;
64 network_properties
.signal_strength
= 80;
65 networks_
.push_back(network_properties
);
68 WiFiService::NetworkProperties network_properties
;
69 network_properties
.connection_state
=
70 onc::connection_state::kNotConnected
;
71 network_properties
.guid
= "stub_cellular1";
72 network_properties
.name
= "cellular1";
73 network_properties
.type
= onc::network_type::kCellular
;
74 network_properties
.json_extra
=
76 " \"ActivateOverNonCellularNetwork\": false,"
77 " \"ActivationState\": \"not-activated\","
78 " \"NetworkTechnology\": \"GSM\","
79 " \"RoamingState\": \"home\""
81 networks_
.push_back(network_properties
);
85 virtual void Initialize(
86 scoped_refptr
<base::SequencedTaskRunner
> task_runner
) OVERRIDE
{}
88 virtual void UnInitialize() OVERRIDE
{}
90 virtual void GetProperties(const std::string
& network_guid
,
91 base::DictionaryValue
* properties
,
92 std::string
* error
) OVERRIDE
{
93 NetworkList::iterator network_properties
= FindNetwork(network_guid
);
94 if (network_properties
!= networks_
.end()) {
95 properties
->Swap(network_properties
->ToValue(false).get());
97 *error
= "Error.DBusFailed";
101 virtual void GetManagedProperties(const std::string
& network_guid
,
102 base::DictionaryValue
* managed_properties
,
103 std::string
* error
) OVERRIDE
{
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 virtual void GetState(const std::string
& network_guid
,
162 base::DictionaryValue
* properties
,
163 std::string
* error
) OVERRIDE
{
164 NetworkList::iterator network_properties
= FindNetwork(network_guid
);
165 if (network_properties
== networks_
.end()) {
166 *error
= "Error.InvalidParameter";
170 const std::string network_state
=
172 " \"ConnectionState\": \"NotConnected\","
173 " \"GUID\": \"stub_wifi2\","
174 " \"Name\": \"wifi2_PSK\","
175 " \"Type\": \"WiFi\","
177 " \"Security\": \"WPA-PSK\","
178 " \"SignalStrength\": 80"
181 scoped_ptr
<base::DictionaryValue
> properties_value(
182 reinterpret_cast<base::DictionaryValue
*>(
183 base::JSONReader::Read(network_state
)));
184 properties
->MergeDictionary(properties_value
.get());
187 virtual void SetProperties(const std::string
& network_guid
,
188 scoped_ptr
<base::DictionaryValue
> properties
,
189 std::string
* error
) OVERRIDE
{
190 NetworkList::iterator network_properties
= FindNetwork(network_guid
);
191 if (network_properties
== networks_
.end() ||
192 !network_properties
->UpdateFromValue(*properties
)) {
193 *error
= "Error.DBusFailed";
197 virtual void CreateNetwork(bool shared
,
198 scoped_ptr
<base::DictionaryValue
> properties
,
199 std::string
* network_guid
,
200 std::string
* error
) OVERRIDE
{
201 WiFiService::NetworkProperties network_properties
;
202 if (network_properties
.UpdateFromValue(*properties
)) {
203 network_properties
.guid
= network_properties
.ssid
;
204 networks_
.push_back(network_properties
);
205 *network_guid
= network_properties
.guid
;
207 *error
= "Error.DBusFailed";
211 virtual void GetVisibleNetworks(const std::string
& network_type
,
212 base::ListValue
* network_list
) OVERRIDE
{
213 for (WiFiService::NetworkList::const_iterator it
= networks_
.begin();
214 it
!= networks_
.end();
216 if (network_type
.empty() ||
217 network_type
== onc::network_type::kAllTypes
||
218 it
->type
== network_type
) {
219 scoped_ptr
<base::DictionaryValue
> network(it
->ToValue(true));
220 network_list
->Append(network
.release());
225 virtual void RequestNetworkScan() OVERRIDE
{
226 NotifyNetworkListChanged(networks_
);
229 virtual void StartConnect(const std::string
& network_guid
,
230 std::string
* error
) OVERRIDE
{
231 NetworkList::iterator network_properties
= FindNetwork(network_guid
);
232 if (network_properties
!= networks_
.end()) {
233 DisconnectAllNetworksOfType(network_properties
->type
);
234 network_properties
->connection_state
= onc::connection_state::kConnected
;
236 NotifyNetworkListChanged(networks_
);
237 NotifyNetworkChanged(network_guid
);
239 *error
= "configure-failed";
243 virtual void StartDisconnect(const std::string
& network_guid
,
244 std::string
* error
) OVERRIDE
{
245 NetworkList::iterator network_properties
= FindNetwork(network_guid
);
246 if (network_properties
!= networks_
.end()) {
247 network_properties
->connection_state
=
248 onc::connection_state::kNotConnected
;
250 NotifyNetworkListChanged(networks_
);
251 NotifyNetworkChanged(network_guid
);
253 *error
= "not-found";
257 virtual void GetKeyFromSystem(const std::string
& network_guid
,
258 std::string
* key_data
,
259 std::string
* error
) OVERRIDE
{
260 *error
= "not-found";
263 virtual void SetEventObservers(
264 scoped_refptr
<base::MessageLoopProxy
> message_loop_proxy
,
265 const NetworkGuidListCallback
& networks_changed_observer
,
266 const NetworkGuidListCallback
& network_list_changed_observer
) OVERRIDE
{
267 message_loop_proxy_
.swap(message_loop_proxy
);
268 networks_changed_observer_
= networks_changed_observer
;
269 network_list_changed_observer_
= network_list_changed_observer
;
272 virtual void RequestConnectedNetworkUpdate() OVERRIDE
{ }
275 NetworkList::iterator
FindNetwork(const std::string
& network_guid
) {
276 for (NetworkList::iterator it
= networks_
.begin(); it
!= networks_
.end();
278 if (it
->guid
== network_guid
)
281 return networks_
.end();
284 void DisconnectAllNetworksOfType(const std::string
& type
) {
285 for (NetworkList::iterator it
= networks_
.begin(); it
!= networks_
.end();
287 if (it
->type
== type
)
288 it
->connection_state
= onc::connection_state::kNotConnected
;
292 void SortNetworks() {
293 // Sort networks, so connected/connecting is up front, then by type:
294 // Ethernet, WiFi, Cellular, VPN
295 networks_
.sort(WiFiService::NetworkProperties::OrderByType
);
298 void NotifyNetworkListChanged(const NetworkList
& networks
) {
299 WiFiService::NetworkGuidList current_networks
;
300 for (WiFiService::NetworkList::const_iterator it
= networks
.begin();
301 it
!= networks
.end();
303 current_networks
.push_back(it
->guid
);
306 message_loop_proxy_
->PostTask(
308 base::Bind(network_list_changed_observer_
, current_networks
));
311 void NotifyNetworkChanged(const std::string
& network_guid
) {
312 WiFiService::NetworkGuidList
changed_networks(1, network_guid
);
313 message_loop_proxy_
->PostTask(
315 base::Bind(networks_changed_observer_
, changed_networks
));
318 NetworkList networks_
;
319 scoped_refptr
<base::MessageLoopProxy
> message_loop_proxy_
;
320 NetworkGuidListCallback networks_changed_observer_
;
321 NetworkGuidListCallback network_list_changed_observer_
;
324 WiFiService
* WiFiService::CreateForTest() { return new FakeWiFiService(); }