Roll src/third_party/WebKit b559582:d13e636 (svn 193437:193439)
[chromium-blink-merge.git] / components / wifi / fake_wifi_service.cc
bloba35d002d4d78f654d02d538502d2be5bccb8aa2d
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"
7 #include "base/bind.h"
8 #include "base/message_loop/message_loop.h"
9 #include "components/onc/onc_constants.h"
11 namespace wifi {
13 FakeWiFiService::FakeWiFiService() {
14 // Populate data expected by unit test.
16 NetworkProperties network_properties;
17 network_properties.connection_state = onc::connection_state::kConnected;
18 network_properties.guid = "stub_wifi1_guid";
19 network_properties.name = "wifi1";
20 network_properties.type = onc::network_type::kWiFi;
21 network_properties.frequency = 0;
22 network_properties.ssid = "wifi1";
23 network_properties.security = onc::wifi::kWEP_PSK;
24 network_properties.signal_strength = 40;
25 networks_.push_back(network_properties);
28 NetworkProperties network_properties;
29 network_properties.connection_state = onc::connection_state::kNotConnected;
30 network_properties.guid = "stub_wifi2_guid";
31 network_properties.name = "wifi2_PSK";
32 network_properties.type = onc::network_type::kWiFi;
33 network_properties.frequency = 5000;
34 network_properties.frequency_set.insert(2400);
35 network_properties.frequency_set.insert(5000);
36 network_properties.ssid = "wifi2_PSK";
37 network_properties.security = onc::wifi::kWPA_PSK;
38 network_properties.signal_strength = 80;
39 networks_.push_back(network_properties);
43 FakeWiFiService::~FakeWiFiService() {
46 void FakeWiFiService::Initialize(
47 scoped_refptr<base::SequencedTaskRunner> task_runner) {
50 void FakeWiFiService::UnInitialize() {
53 void FakeWiFiService::GetProperties(const std::string& network_guid,
54 base::DictionaryValue* properties,
55 std::string* error) {
56 NetworkList::iterator network_properties = FindNetwork(network_guid);
57 if (network_properties == networks_.end()) {
58 *error = "Error.InvalidNetworkGuid";
59 return;
61 properties->Swap(network_properties->ToValue(false).get());
64 void FakeWiFiService::GetManagedProperties(
65 const std::string& network_guid,
66 base::DictionaryValue* managed_properties,
67 std::string* error) {
68 // Not implemented
69 *error = kErrorWiFiService;
72 void FakeWiFiService::GetState(const std::string& network_guid,
73 base::DictionaryValue* properties,
74 std::string* error) {
75 NetworkList::iterator network_properties = FindNetwork(network_guid);
76 if (network_properties == networks_.end()) {
77 *error = "Error.InvalidNetworkGuid";
78 return;
80 properties->Swap(network_properties->ToValue(true).get());
83 void FakeWiFiService::SetProperties(
84 const std::string& network_guid,
85 scoped_ptr<base::DictionaryValue> properties,
86 std::string* error) {
87 NetworkList::iterator network_properties = FindNetwork(network_guid);
88 if (network_properties == networks_.end() ||
89 !network_properties->UpdateFromValue(*properties)) {
90 *error = "Error.DBusFailed";
94 void FakeWiFiService::CreateNetwork(
95 bool shared,
96 scoped_ptr<base::DictionaryValue> properties,
97 std::string* network_guid,
98 std::string* error) {
99 NetworkProperties network_properties;
100 if (network_properties.UpdateFromValue(*properties)) {
101 network_properties.guid = network_properties.ssid;
102 networks_.push_back(network_properties);
103 *network_guid = network_properties.guid;
104 } else {
105 *error = "Error.DBusFailed";
109 void FakeWiFiService::GetVisibleNetworks(const std::string& network_type,
110 base::ListValue* network_list,
111 bool include_details) {
112 for (NetworkList::const_iterator it = networks_.begin();
113 it != networks_.end();
114 ++it) {
115 if (network_type.empty() || network_type == onc::network_type::kAllTypes ||
116 it->type == network_type) {
117 scoped_ptr<base::DictionaryValue> network(it->ToValue(!include_details));
118 network_list->Append(network.release());
123 void FakeWiFiService::RequestNetworkScan() {
124 NotifyNetworkListChanged(networks_);
127 void FakeWiFiService::StartConnect(const std::string& network_guid,
128 std::string* error) {
129 NetworkList::iterator network_properties = FindNetwork(network_guid);
130 if (network_properties == networks_.end()) {
131 *error = "Error.InvalidNetworkGuid";
132 return;
134 DisconnectAllNetworksOfType(network_properties->type);
135 network_properties->connection_state = onc::connection_state::kConnected;
136 SortNetworks();
137 NotifyNetworkListChanged(networks_);
138 NotifyNetworkChanged(network_guid);
141 void FakeWiFiService::StartDisconnect(const std::string& network_guid,
142 std::string* error) {
143 NetworkList::iterator network_properties = FindNetwork(network_guid);
144 if (network_properties == networks_.end()) {
145 *error = "Error.InvalidNetworkGuid";
146 return;
148 network_properties->connection_state = onc::connection_state::kNotConnected;
149 SortNetworks();
150 NotifyNetworkListChanged(networks_);
151 NotifyNetworkChanged(network_guid);
154 void FakeWiFiService::GetKeyFromSystem(const std::string& network_guid,
155 std::string* key_data,
156 std::string* error) {
157 *error = "not-found";
160 void FakeWiFiService::SetEventObservers(
161 scoped_refptr<base::MessageLoopProxy> message_loop_proxy,
162 const NetworkGuidListCallback& networks_changed_observer,
163 const NetworkGuidListCallback& network_list_changed_observer) {
164 message_loop_proxy_.swap(message_loop_proxy);
165 networks_changed_observer_ = networks_changed_observer;
166 network_list_changed_observer_ = network_list_changed_observer;
169 void FakeWiFiService::RequestConnectedNetworkUpdate() {
172 void FakeWiFiService::GetConnectedNetworkSSID(std::string* ssid,
173 std::string* error) {
174 *ssid = "";
175 *error = "";
178 NetworkList::iterator FakeWiFiService::FindNetwork(
179 const std::string& network_guid) {
180 for (NetworkList::iterator it = networks_.begin(); it != networks_.end();
181 ++it) {
182 if (it->guid == network_guid)
183 return it;
185 return networks_.end();
188 void FakeWiFiService::DisconnectAllNetworksOfType(const std::string& type) {
189 for (NetworkList::iterator it = networks_.begin(); it != networks_.end();
190 ++it) {
191 if (it->type == type)
192 it->connection_state = onc::connection_state::kNotConnected;
196 void FakeWiFiService::SortNetworks() {
197 // Sort networks, so connected/connecting is up front, then by type:
198 // Ethernet, WiFi, Cellular, VPN
199 networks_.sort(NetworkProperties::OrderByType);
202 void FakeWiFiService::NotifyNetworkListChanged(const NetworkList& networks) {
203 WiFiService::NetworkGuidList current_networks;
204 for (NetworkList::const_iterator it = networks.begin(); it != networks.end();
205 ++it) {
206 current_networks.push_back(it->guid);
209 message_loop_proxy_->PostTask(
210 FROM_HERE, base::Bind(network_list_changed_observer_, current_networks));
213 void FakeWiFiService::NotifyNetworkChanged(const std::string& network_guid) {
214 WiFiService::NetworkGuidList changed_networks(1, network_guid);
215 message_loop_proxy_->PostTask(
216 FROM_HERE, base::Bind(networks_changed_observer_, changed_networks));
219 } // namespace wifi