OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "components/wifi/fake_wifi_service.h" | 5 #include "components/wifi/fake_wifi_service.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/json/json_reader.h" | 8 #include "base/json/json_reader.h" |
9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
10 #include "components/onc/onc_constants.h" | 10 #include "components/onc/onc_constants.h" |
11 | 11 |
12 namespace wifi { | 12 namespace wifi { |
13 | 13 |
14 FakeWiFiService::FakeWiFiService() { | 14 FakeWiFiService::FakeWiFiService() { |
15 // Populate data expected by unit test. | 15 // Populate data expected by unit test. |
16 { | 16 { |
17 WiFiService::NetworkProperties network_properties; | 17 NetworkProperties network_properties; |
18 network_properties.connection_state = onc::connection_state::kConnected; | 18 network_properties.connection_state = onc::connection_state::kConnected; |
19 network_properties.guid = "stub_wifi1"; | 19 network_properties.guid = "stub_wifi1"; |
20 network_properties.name = "wifi1"; | 20 network_properties.name = "wifi1"; |
21 network_properties.type = onc::network_type::kWiFi; | 21 network_properties.type = onc::network_type::kWiFi; |
22 network_properties.frequency = 0; | 22 network_properties.frequency = 0; |
23 network_properties.ssid = "wifi1"; | 23 network_properties.ssid = "wifi1"; |
24 network_properties.security = onc::wifi::kWEP_PSK; | 24 network_properties.security = onc::wifi::kWEP_PSK; |
25 network_properties.signal_strength = 40; | 25 network_properties.signal_strength = 40; |
26 network_properties.json_extra = | 26 network_properties.json_extra = |
27 "{" | 27 "{" |
28 " \"MacAddress\": \"00:11:22:AA:BB:CC\"," | 28 " \"MacAddress\": \"00:11:22:AA:BB:CC\"," |
29 " \"IPConfigs\": [{" | 29 " \"IPConfigs\": [{" |
30 " \"Gateway\": \"0.0.0.1\"," | 30 " \"Gateway\": \"0.0.0.1\"," |
31 " \"IPAddress\": \"0.0.0.0\"," | 31 " \"IPAddress\": \"0.0.0.0\"," |
32 " \"RoutingPrefix\": 0," | 32 " \"RoutingPrefix\": 0," |
33 " \"Type\": \"IPv4\"" | 33 " \"Type\": \"IPv4\"" |
34 " }]," | 34 " }]," |
35 " \"WiFi\": {" | 35 " \"WiFi\": {" |
36 " \"Frequency\": 2400," | 36 " \"Frequency\": 2400," |
37 " \"FrequencyList\": [2400]" | 37 " \"FrequencyList\": [2400]" |
38 " }" | 38 " }" |
39 "}"; | 39 "}"; |
40 networks_.push_back(network_properties); | 40 networks_.push_back(network_properties); |
41 } | 41 } |
42 { | 42 { |
43 WiFiService::NetworkProperties network_properties; | 43 NetworkProperties network_properties; |
44 network_properties.connection_state = onc::connection_state::kNotConnected; | 44 network_properties.connection_state = onc::connection_state::kNotConnected; |
45 network_properties.guid = "stub_wifi2"; | 45 network_properties.guid = "stub_wifi2"; |
46 network_properties.name = "wifi2_PSK"; | 46 network_properties.name = "wifi2_PSK"; |
47 network_properties.type = onc::network_type::kWiFi; | 47 network_properties.type = onc::network_type::kWiFi; |
48 network_properties.frequency = 5000; | 48 network_properties.frequency = 5000; |
49 network_properties.frequency_set.insert(2400); | 49 network_properties.frequency_set.insert(2400); |
50 network_properties.frequency_set.insert(5000); | 50 network_properties.frequency_set.insert(5000); |
51 network_properties.ssid = "wifi2_PSK"; | 51 network_properties.ssid = "wifi2_PSK"; |
52 network_properties.security = onc::wifi::kWPA_PSK; | 52 network_properties.security = onc::wifi::kWPA_PSK; |
53 network_properties.signal_strength = 80; | 53 network_properties.signal_strength = 80; |
54 networks_.push_back(network_properties); | 54 networks_.push_back(network_properties); |
55 } | 55 } |
56 } | 56 } |
57 | 57 |
58 FakeWiFiService::~FakeWiFiService() { | 58 FakeWiFiService::~FakeWiFiService() { |
59 } | 59 } |
60 | 60 |
61 void FakeWiFiService::Initialize( | 61 void FakeWiFiService::Initialize( |
62 scoped_refptr<base::SequencedTaskRunner> task_runner) { | 62 scoped_refptr<base::SequencedTaskRunner> task_runner) { |
63 } | 63 } |
64 | 64 |
65 void FakeWiFiService::UnInitialize() { | 65 void FakeWiFiService::UnInitialize() { |
66 } | 66 } |
67 | 67 |
68 void FakeWiFiService::GetProperties(const std::string& network_guid, | 68 void FakeWiFiService::GetProperties(const std::string& network_guid, |
69 base::DictionaryValue* properties, | 69 base::DictionaryValue* properties, |
70 std::string* error) { | 70 std::string* error) { |
71 WiFiService::NetworkList::iterator network_properties = | 71 NetworkList::iterator network_properties = FindNetwork(network_guid); |
72 FindNetwork(network_guid); | |
73 if (network_properties == networks_.end()) { | 72 if (network_properties == networks_.end()) { |
74 *error = "Error.InvalidNetworkGuid"; | 73 *error = "Error.InvalidNetworkGuid"; |
75 return; | 74 return; |
76 } | 75 } |
77 properties->Swap(network_properties->ToValue(false).get()); | 76 properties->Swap(network_properties->ToValue(false).get()); |
78 } | 77 } |
79 | 78 |
80 void FakeWiFiService::GetManagedProperties( | 79 void FakeWiFiService::GetManagedProperties( |
81 const std::string& network_guid, | 80 const std::string& network_guid, |
82 base::DictionaryValue* managed_properties, | 81 base::DictionaryValue* managed_properties, |
83 std::string* error) { | 82 std::string* error) { |
84 // Not implemented | 83 // Not implemented |
85 *error = kErrorWiFiService; | 84 *error = kErrorWiFiService; |
86 } | 85 } |
87 | 86 |
88 void FakeWiFiService::GetState(const std::string& network_guid, | 87 void FakeWiFiService::GetState(const std::string& network_guid, |
89 base::DictionaryValue* properties, | 88 base::DictionaryValue* properties, |
90 std::string* error) { | 89 std::string* error) { |
91 WiFiService::NetworkList::iterator network_properties = | 90 NetworkList::iterator network_properties = FindNetwork(network_guid); |
92 FindNetwork(network_guid); | |
93 if (network_properties == networks_.end()) { | 91 if (network_properties == networks_.end()) { |
94 *error = "Error.InvalidNetworkGuid"; | 92 *error = "Error.InvalidNetworkGuid"; |
95 return; | 93 return; |
96 } | 94 } |
97 properties->Swap(network_properties->ToValue(true).get()); | 95 properties->Swap(network_properties->ToValue(true).get()); |
98 } | 96 } |
99 | 97 |
100 void FakeWiFiService::SetProperties( | 98 void FakeWiFiService::SetProperties( |
101 const std::string& network_guid, | 99 const std::string& network_guid, |
102 scoped_ptr<base::DictionaryValue> properties, | 100 scoped_ptr<base::DictionaryValue> properties, |
103 std::string* error) { | 101 std::string* error) { |
104 WiFiService::NetworkList::iterator network_properties = | 102 NetworkList::iterator network_properties = FindNetwork(network_guid); |
105 FindNetwork(network_guid); | |
106 if (network_properties == networks_.end() || | 103 if (network_properties == networks_.end() || |
107 !network_properties->UpdateFromValue(*properties)) { | 104 !network_properties->UpdateFromValue(*properties)) { |
108 *error = "Error.DBusFailed"; | 105 *error = "Error.DBusFailed"; |
109 } | 106 } |
110 } | 107 } |
111 | 108 |
112 void FakeWiFiService::CreateNetwork( | 109 void FakeWiFiService::CreateNetwork( |
113 bool shared, | 110 bool shared, |
114 scoped_ptr<base::DictionaryValue> properties, | 111 scoped_ptr<base::DictionaryValue> properties, |
115 std::string* network_guid, | 112 std::string* network_guid, |
116 std::string* error) { | 113 std::string* error) { |
117 WiFiService::NetworkProperties network_properties; | 114 NetworkProperties network_properties; |
118 if (network_properties.UpdateFromValue(*properties)) { | 115 if (network_properties.UpdateFromValue(*properties)) { |
119 network_properties.guid = network_properties.ssid; | 116 network_properties.guid = network_properties.ssid; |
120 networks_.push_back(network_properties); | 117 networks_.push_back(network_properties); |
121 *network_guid = network_properties.guid; | 118 *network_guid = network_properties.guid; |
122 } else { | 119 } else { |
123 *error = "Error.DBusFailed"; | 120 *error = "Error.DBusFailed"; |
124 } | 121 } |
125 } | 122 } |
126 | 123 |
127 void FakeWiFiService::GetVisibleNetworks(const std::string& network_type, | 124 void FakeWiFiService::GetVisibleNetworks(const std::string& network_type, |
128 base::ListValue* network_list) { | 125 base::ListValue* network_list, |
129 for (WiFiService::NetworkList::const_iterator it = networks_.begin(); | 126 bool include_details) { |
| 127 for (NetworkList::const_iterator it = networks_.begin(); |
130 it != networks_.end(); | 128 it != networks_.end(); |
131 ++it) { | 129 ++it) { |
132 if (network_type.empty() || network_type == onc::network_type::kAllTypes || | 130 if (network_type.empty() || network_type == onc::network_type::kAllTypes || |
133 it->type == network_type) { | 131 it->type == network_type) { |
134 scoped_ptr<base::DictionaryValue> network(it->ToValue(true)); | 132 scoped_ptr<base::DictionaryValue> network(it->ToValue(!include_details)); |
135 network_list->Append(network.release()); | 133 network_list->Append(network.release()); |
136 } | 134 } |
137 } | 135 } |
138 } | 136 } |
139 | 137 |
140 void FakeWiFiService::RequestNetworkScan() { | 138 void FakeWiFiService::RequestNetworkScan() { |
141 NotifyNetworkListChanged(networks_); | 139 NotifyNetworkListChanged(networks_); |
142 } | 140 } |
143 | 141 |
144 void FakeWiFiService::StartConnect(const std::string& network_guid, | 142 void FakeWiFiService::StartConnect(const std::string& network_guid, |
145 std::string* error) { | 143 std::string* error) { |
146 NetworkList::iterator network_properties = FindNetwork(network_guid); | 144 NetworkList::iterator network_properties = FindNetwork(network_guid); |
147 if (network_properties == networks_.end()) { | 145 if (network_properties == networks_.end()) { |
148 *error = "Error.InvalidNetworkGuid"; | 146 *error = "Error.InvalidNetworkGuid"; |
149 return; | 147 return; |
150 } | 148 } |
151 DisconnectAllNetworksOfType(network_properties->type); | 149 DisconnectAllNetworksOfType(network_properties->type); |
152 network_properties->connection_state = onc::connection_state::kConnected; | 150 network_properties->connection_state = onc::connection_state::kConnected; |
153 SortNetworks(); | 151 SortNetworks(); |
154 NotifyNetworkListChanged(networks_); | 152 NotifyNetworkListChanged(networks_); |
155 NotifyNetworkChanged(network_guid); | 153 NotifyNetworkChanged(network_guid); |
156 } | 154 } |
157 | 155 |
158 void FakeWiFiService::StartDisconnect(const std::string& network_guid, | 156 void FakeWiFiService::StartDisconnect(const std::string& network_guid, |
159 std::string* error) { | 157 std::string* error) { |
160 WiFiService::NetworkList::iterator network_properties = | 158 NetworkList::iterator network_properties = FindNetwork(network_guid); |
161 FindNetwork(network_guid); | |
162 if (network_properties == networks_.end()) { | 159 if (network_properties == networks_.end()) { |
163 *error = "Error.InvalidNetworkGuid"; | 160 *error = "Error.InvalidNetworkGuid"; |
164 return; | 161 return; |
165 } | 162 } |
166 network_properties->connection_state = onc::connection_state::kNotConnected; | 163 network_properties->connection_state = onc::connection_state::kNotConnected; |
167 SortNetworks(); | 164 SortNetworks(); |
168 NotifyNetworkListChanged(networks_); | 165 NotifyNetworkListChanged(networks_); |
169 NotifyNetworkChanged(network_guid); | 166 NotifyNetworkChanged(network_guid); |
170 } | 167 } |
171 | 168 |
172 void FakeWiFiService::GetKeyFromSystem(const std::string& network_guid, | 169 void FakeWiFiService::GetKeyFromSystem(const std::string& network_guid, |
173 std::string* key_data, | 170 std::string* key_data, |
174 std::string* error) { | 171 std::string* error) { |
175 *error = "not-found"; | 172 *error = "not-found"; |
176 } | 173 } |
177 | 174 |
178 void FakeWiFiService::SetEventObservers( | 175 void FakeWiFiService::SetEventObservers( |
179 scoped_refptr<base::MessageLoopProxy> message_loop_proxy, | 176 scoped_refptr<base::MessageLoopProxy> message_loop_proxy, |
180 const NetworkGuidListCallback& networks_changed_observer, | 177 const NetworkGuidListCallback& networks_changed_observer, |
181 const NetworkGuidListCallback& network_list_changed_observer) { | 178 const NetworkGuidListCallback& network_list_changed_observer) { |
182 message_loop_proxy_.swap(message_loop_proxy); | 179 message_loop_proxy_.swap(message_loop_proxy); |
183 networks_changed_observer_ = networks_changed_observer; | 180 networks_changed_observer_ = networks_changed_observer; |
184 network_list_changed_observer_ = network_list_changed_observer; | 181 network_list_changed_observer_ = network_list_changed_observer; |
185 } | 182 } |
186 | 183 |
187 void FakeWiFiService::RequestConnectedNetworkUpdate() { | 184 void FakeWiFiService::RequestConnectedNetworkUpdate() { |
188 } | 185 } |
189 | 186 |
190 WiFiService::NetworkList::iterator FakeWiFiService::FindNetwork( | 187 NetworkList::iterator FakeWiFiService::FindNetwork( |
191 const std::string& network_guid) { | 188 const std::string& network_guid) { |
192 for (WiFiService::NetworkList::iterator it = networks_.begin(); | 189 for (NetworkList::iterator it = networks_.begin(); it != networks_.end(); |
193 it != networks_.end(); | |
194 ++it) { | 190 ++it) { |
195 if (it->guid == network_guid) | 191 if (it->guid == network_guid) |
196 return it; | 192 return it; |
197 } | 193 } |
198 return networks_.end(); | 194 return networks_.end(); |
199 } | 195 } |
200 | 196 |
201 void FakeWiFiService::DisconnectAllNetworksOfType(const std::string& type) { | 197 void FakeWiFiService::DisconnectAllNetworksOfType(const std::string& type) { |
202 for (WiFiService::NetworkList::iterator it = networks_.begin(); | 198 for (NetworkList::iterator it = networks_.begin(); it != networks_.end(); |
203 it != networks_.end(); | |
204 ++it) { | 199 ++it) { |
205 if (it->type == type) | 200 if (it->type == type) |
206 it->connection_state = onc::connection_state::kNotConnected; | 201 it->connection_state = onc::connection_state::kNotConnected; |
207 } | 202 } |
208 } | 203 } |
209 | 204 |
210 void FakeWiFiService::SortNetworks() { | 205 void FakeWiFiService::SortNetworks() { |
211 // Sort networks, so connected/connecting is up front, then by type: | 206 // Sort networks, so connected/connecting is up front, then by type: |
212 // Ethernet, WiFi, Cellular, VPN | 207 // Ethernet, WiFi, Cellular, VPN |
213 networks_.sort(WiFiService::NetworkProperties::OrderByType); | 208 networks_.sort(NetworkProperties::OrderByType); |
214 } | 209 } |
215 | 210 |
216 void FakeWiFiService::NotifyNetworkListChanged( | 211 void FakeWiFiService::NotifyNetworkListChanged(const NetworkList& networks) { |
217 const WiFiService::NetworkList& networks) { | |
218 WiFiService::NetworkGuidList current_networks; | 212 WiFiService::NetworkGuidList current_networks; |
219 for (WiFiService::NetworkList::const_iterator it = networks.begin(); | 213 for (NetworkList::const_iterator it = networks.begin(); it != networks.end(); |
220 it != networks.end(); | |
221 ++it) { | 214 ++it) { |
222 current_networks.push_back(it->guid); | 215 current_networks.push_back(it->guid); |
223 } | 216 } |
224 | 217 |
225 message_loop_proxy_->PostTask( | 218 message_loop_proxy_->PostTask( |
226 FROM_HERE, base::Bind(network_list_changed_observer_, current_networks)); | 219 FROM_HERE, base::Bind(network_list_changed_observer_, current_networks)); |
227 } | 220 } |
228 | 221 |
229 void FakeWiFiService::NotifyNetworkChanged(const std::string& network_guid) { | 222 void FakeWiFiService::NotifyNetworkChanged(const std::string& network_guid) { |
230 WiFiService::NetworkGuidList changed_networks(1, network_guid); | 223 WiFiService::NetworkGuidList changed_networks(1, network_guid); |
231 message_loop_proxy_->PostTask( | 224 message_loop_proxy_->PostTask( |
232 FROM_HERE, base::Bind(networks_changed_observer_, changed_networks)); | 225 FROM_HERE, base::Bind(networks_changed_observer_, changed_networks)); |
233 } | 226 } |
234 | 227 |
235 } // namespace wifi | 228 } // namespace wifi |
OLD | NEW |