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