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