Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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. | |
| 4 | |
| 5 #include "components/wifi/wifi_service.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/message_loop/message_loop.h" | |
| 9 #include "components/onc/onc_constants.h" | |
| 10 | |
| 11 namespace wifi { | |
| 12 | |
| 13 class WiFiServiceMock : public WiFiService { | |
|
tbarzic
2013/11/15 22:56:30
This isn't actually a mock.. I think TestWifiServi
mef
2013/11/17 20:05:18
Done.
| |
| 14 public: | |
| 15 WiFiServiceMock() { | |
| 16 // Populate mock data expected by unit test. | |
| 17 { | |
| 18 WiFiService::NetworkProperties network_properties; | |
| 19 network_properties.connection_state = onc::connection_state::kConnected; | |
| 20 network_properties.guid = "stub_ethernet"; | |
| 21 network_properties.name = "eth0"; | |
| 22 network_properties.type = onc::network_type::kEthernet; | |
| 23 network_properties.json_extra = | |
| 24 " {" | |
| 25 " \"Authentication\": \"None\"" | |
| 26 " }"; | |
| 27 networks_.push_back(network_properties); | |
| 28 } | |
| 29 { | |
| 30 WiFiService::NetworkProperties network_properties; | |
| 31 network_properties.connection_state = onc::connection_state::kConnected; | |
| 32 network_properties.guid = "stub_wifi1"; | |
| 33 network_properties.name = "wifi1"; | |
| 34 network_properties.type = onc::network_type::kWiFi; | |
| 35 network_properties.frequency = 0; | |
| 36 network_properties.ssid = "stub_wifi1"; | |
| 37 network_properties.security = onc::wifi::kWEP_PSK; | |
| 38 network_properties.signal_strength = 0; | |
| 39 networks_.push_back(network_properties); | |
| 40 } | |
| 41 { | |
| 42 WiFiService::NetworkProperties network_properties; | |
| 43 network_properties.connection_state = onc::connection_state::kConnected; | |
| 44 network_properties.guid = "stub_vpn1"; | |
| 45 network_properties.name = "vpn1"; | |
| 46 network_properties.type = onc::network_type::kVPN; | |
| 47 networks_.push_back(network_properties); | |
| 48 } | |
| 49 { | |
| 50 WiFiService::NetworkProperties network_properties; | |
| 51 network_properties.connection_state = | |
| 52 onc::connection_state::kNotConnected; | |
| 53 network_properties.guid = "stub_wifi2"; | |
| 54 network_properties.name = "wifi2_PSK"; | |
| 55 network_properties.type = onc::network_type::kWiFi; | |
| 56 network_properties.frequency = 5000; | |
| 57 network_properties.frequency_list.push_back(2400); | |
| 58 network_properties.frequency_list.push_back(5000); | |
| 59 network_properties.ssid = "wifi2_PSK"; | |
| 60 network_properties.security = onc::wifi::kWPA_PSK; | |
| 61 network_properties.signal_strength = 80; | |
| 62 networks_.push_back(network_properties); | |
| 63 } | |
| 64 { | |
| 65 WiFiService::NetworkProperties network_properties; | |
| 66 network_properties.connection_state = | |
| 67 onc::connection_state::kNotConnected; | |
| 68 network_properties.guid = "stub_cellular1"; | |
| 69 network_properties.name = "cellular1"; | |
| 70 network_properties.type = onc::network_type::kCellular; | |
| 71 network_properties.json_extra = | |
| 72 " {" | |
| 73 " \"ActivateOverNonCellularNetwork\": false," | |
| 74 " \"ActivationState\": \"not-activated\"," | |
| 75 " \"NetworkTechnology\": \"GSM\"," | |
| 76 " \"RoamingState\": \"home\"" | |
| 77 " }"; | |
| 78 networks_.push_back(network_properties); | |
| 79 } | |
| 80 } | |
| 81 | |
| 82 virtual void GetProperties(const std::string& network_guid, | |
| 83 DictionaryValue* properties, | |
| 84 std::string* error) OVERRIDE { | |
| 85 NetworkList::iterator network_properties = FindNetwork(network_guid); | |
| 86 if (network_properties != networks_.end()) { | |
| 87 properties->Swap(network_properties->ToValue(false).get()); | |
| 88 } else { | |
| 89 *error = "Error.DBusFailed"; | |
| 90 } | |
| 91 } | |
| 92 | |
| 93 virtual void SetProperties(const std::string& network_guid, | |
| 94 scoped_ptr<base::DictionaryValue> properties, | |
| 95 std::string* error) OVERRIDE { | |
| 96 NetworkList::iterator network_properties = FindNetwork(network_guid); | |
| 97 if (network_properties == networks_.end() || | |
| 98 !network_properties->UpdateFromValue(*properties)) { | |
| 99 *error = "Error.DBusFailed"; | |
| 100 } | |
| 101 } | |
| 102 | |
| 103 virtual void GetVisibleNetworks(ListValue* network_list) OVERRIDE { | |
| 104 for (WiFiService::NetworkList::const_iterator it = networks_.begin(); | |
| 105 it != networks_.end(); | |
| 106 ++it) { | |
| 107 scoped_ptr<DictionaryValue> network(it->ToValue(true)); | |
| 108 network_list->Append(network.release()); | |
| 109 } | |
| 110 } | |
| 111 | |
| 112 virtual void RequestNetworkScan() OVERRIDE { | |
| 113 NotifyNetworkListChanged(networks_); | |
| 114 } | |
| 115 | |
| 116 virtual void StartConnect(const std::string& network_guid, | |
| 117 std::string* error) OVERRIDE { | |
| 118 NetworkList::iterator network_properties = FindNetwork(network_guid); | |
| 119 if (network_properties != networks_.end()) { | |
| 120 DisconnectAllNetworksOfType(network_properties->type); | |
| 121 network_properties->connection_state = onc::connection_state::kConnected; | |
| 122 SortNetworks(); | |
| 123 NotifyNetworkListChanged(networks_); | |
| 124 NotifyNetworkChanged(network_guid); | |
| 125 } else { | |
| 126 *error = "configure-failed"; | |
| 127 } | |
| 128 } | |
| 129 | |
| 130 virtual void StartDisconnect(const std::string& network_guid, | |
| 131 std::string* error) OVERRIDE { | |
| 132 NetworkList::iterator network_properties = FindNetwork(network_guid); | |
| 133 if (network_properties != networks_.end()) { | |
| 134 network_properties->connection_state = | |
| 135 onc::connection_state::kNotConnected; | |
| 136 SortNetworks(); | |
| 137 NotifyNetworkListChanged(networks_); | |
| 138 NotifyNetworkChanged(network_guid); | |
| 139 } else { | |
| 140 *error = "not-found"; | |
| 141 } | |
| 142 } | |
| 143 | |
| 144 virtual void SetEventObservers( | |
| 145 scoped_refptr<base::MessageLoopProxy> message_loop_proxy, | |
| 146 const NetworkGuidListCallback& networks_changed_observer, | |
| 147 const NetworkGuidListCallback& network_list_changed_observer) OVERRIDE { | |
| 148 message_loop_proxy_.swap(message_loop_proxy); | |
| 149 networks_changed_observer_ = networks_changed_observer; | |
| 150 network_list_changed_observer_ = network_list_changed_observer; | |
| 151 } | |
| 152 | |
| 153 private: | |
| 154 NetworkList::iterator FindNetwork(const std::string& network_guid) { | |
| 155 for (NetworkList::iterator it = networks_.begin(); it != networks_.end(); | |
| 156 ++it) { | |
| 157 if (it->guid == network_guid) | |
| 158 return it; | |
| 159 } | |
| 160 return networks_.end(); | |
| 161 } | |
| 162 | |
| 163 void DisconnectAllNetworksOfType(const std::string& type) { | |
| 164 for (NetworkList::iterator it = networks_.begin(); it != networks_.end(); | |
| 165 ++it) { | |
| 166 if (it->type == type) | |
| 167 it->connection_state = onc::connection_state::kNotConnected; | |
| 168 } | |
| 169 } | |
| 170 | |
| 171 void SortNetworks() { | |
| 172 // Sort networks, so connected/connecting is up front, then by type: | |
| 173 // Ethernet, WiFi, Cellular, VPN | |
| 174 networks_.sort(WiFiService::NetworkProperties::OrderByType); | |
| 175 } | |
| 176 | |
| 177 void NotifyNetworkListChanged(const NetworkList& networks) { | |
| 178 WiFiService::NetworkGuidList current_networks; | |
| 179 for (WiFiService::NetworkList::const_iterator it = networks.begin(); | |
| 180 it != networks.end(); | |
| 181 ++it) { | |
| 182 current_networks.push_back(it->guid); | |
| 183 } | |
| 184 | |
| 185 message_loop_proxy_->PostTask( | |
| 186 FROM_HERE, | |
| 187 base::Bind(network_list_changed_observer_, current_networks)); | |
| 188 } | |
| 189 | |
| 190 void NotifyNetworkChanged(const std::string& network_guid) { | |
| 191 WiFiService::NetworkGuidList changed_networks(1, network_guid); | |
| 192 message_loop_proxy_->PostTask( | |
| 193 FROM_HERE, | |
| 194 base::Bind(networks_changed_observer_, changed_networks)); | |
| 195 } | |
| 196 | |
| 197 NetworkList networks_; | |
| 198 scoped_refptr<base::MessageLoopProxy> message_loop_proxy_; | |
| 199 NetworkGuidListCallback networks_changed_observer_; | |
| 200 NetworkGuidListCallback network_list_changed_observer_; | |
| 201 }; | |
| 202 | |
| 203 WiFiService* WiFiService::CreateServiceMock() { return new WiFiServiceMock(); } | |
| 204 WiFiService* WiFiService::CreateService() { return new WiFiServiceMock(); } | |
| 205 | |
| 206 } // namespace wifi | |
| OLD | NEW |