diff options
author | mef@chromium.org <mef@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-11-22 12:44:41 +0000 |
---|---|---|
committer | mef@chromium.org <mef@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-11-22 12:44:41 +0000 |
commit | 1b6888ad29c5bc31fa3faff3aaa27008af1e7c4a (patch) | |
tree | 686154ebd3f55ad1a39fdcabfa342942b8d6c5c2 /components/wifi/fake_wifi_service.cc | |
parent | 1385954a60937c198519acc45c04242adb4123e5 (diff) | |
download | chromium_src-1b6888ad29c5bc31fa3faff3aaa27008af1e7c4a.zip chromium_src-1b6888ad29c5bc31fa3faff3aaa27008af1e7c4a.tar.gz chromium_src-1b6888ad29c5bc31fa3faff3aaa27008af1e7c4a.tar.bz2 |
Base infrastructure for Networking Private API on Windows and Mac.
Based on https://codereview.chromium.org/22295002/,
but moves WiFiService to components/ and runs it in browser process instead of utility process.
Windows implementation is in http://crrev.com/68503019.
BUG=267667
Review URL: https://codereview.chromium.org/54323003
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@236752 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'components/wifi/fake_wifi_service.cc')
-rw-r--r-- | components/wifi/fake_wifi_service.cc | 208 |
1 files changed, 208 insertions, 0 deletions
diff --git a/components/wifi/fake_wifi_service.cc b/components/wifi/fake_wifi_service.cc new file mode 100644 index 0000000..90902ed --- /dev/null +++ b/components/wifi/fake_wifi_service.cc @@ -0,0 +1,208 @@ +// Copyright 2013 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "components/wifi/wifi_service.h" + +#include "base/bind.h" +#include "base/message_loop/message_loop.h" +#include "components/onc/onc_constants.h" + +namespace wifi { + +// Fake implementation of WiFiService used to satisfy expectations of +// networkingPrivateApi browser test. +class FakeWiFiService : public WiFiService { + public: + FakeWiFiService() { + // Populate data expected by unit test. + { + WiFiService::NetworkProperties network_properties; + network_properties.connection_state = onc::connection_state::kConnected; + network_properties.guid = "stub_ethernet"; + network_properties.name = "eth0"; + network_properties.type = onc::network_type::kEthernet; + network_properties.json_extra = + " {" + " \"Authentication\": \"None\"" + " }"; + networks_.push_back(network_properties); + } + { + WiFiService::NetworkProperties network_properties; + network_properties.connection_state = onc::connection_state::kConnected; + network_properties.guid = "stub_wifi1"; + network_properties.name = "wifi1"; + network_properties.type = onc::network_type::kWiFi; + network_properties.frequency = 0; + network_properties.ssid = "stub_wifi1"; + network_properties.security = onc::wifi::kWEP_PSK; + network_properties.signal_strength = 0; + networks_.push_back(network_properties); + } + { + WiFiService::NetworkProperties network_properties; + network_properties.connection_state = onc::connection_state::kConnected; + network_properties.guid = "stub_vpn1"; + network_properties.name = "vpn1"; + network_properties.type = onc::network_type::kVPN; + networks_.push_back(network_properties); + } + { + WiFiService::NetworkProperties network_properties; + network_properties.connection_state = + onc::connection_state::kNotConnected; + network_properties.guid = "stub_wifi2"; + network_properties.name = "wifi2_PSK"; + network_properties.type = onc::network_type::kWiFi; + network_properties.frequency = 5000; + network_properties.frequency_list.push_back(2400); + network_properties.frequency_list.push_back(5000); + network_properties.ssid = "wifi2_PSK"; + network_properties.security = onc::wifi::kWPA_PSK; + network_properties.signal_strength = 80; + networks_.push_back(network_properties); + } + { + WiFiService::NetworkProperties network_properties; + network_properties.connection_state = + onc::connection_state::kNotConnected; + network_properties.guid = "stub_cellular1"; + network_properties.name = "cellular1"; + network_properties.type = onc::network_type::kCellular; + network_properties.json_extra = + " {" + " \"ActivateOverNonCellularNetwork\": false," + " \"ActivationState\": \"not-activated\"," + " \"NetworkTechnology\": \"GSM\"," + " \"RoamingState\": \"home\"" + " }"; + networks_.push_back(network_properties); + } + } + + virtual void GetProperties(const std::string& network_guid, + DictionaryValue* properties, + std::string* error) OVERRIDE { + NetworkList::iterator network_properties = FindNetwork(network_guid); + if (network_properties != networks_.end()) { + properties->Swap(network_properties->ToValue(false).get()); + } else { + *error = "Error.DBusFailed"; + } + } + + virtual void SetProperties(const std::string& network_guid, + scoped_ptr<base::DictionaryValue> properties, + std::string* error) OVERRIDE { + NetworkList::iterator network_properties = FindNetwork(network_guid); + if (network_properties == networks_.end() || + !network_properties->UpdateFromValue(*properties)) { + *error = "Error.DBusFailed"; + } + } + + virtual void GetVisibleNetworks(ListValue* network_list) OVERRIDE { + for (WiFiService::NetworkList::const_iterator it = networks_.begin(); + it != networks_.end(); + ++it) { + scoped_ptr<DictionaryValue> network(it->ToValue(true)); + network_list->Append(network.release()); + } + } + + virtual void RequestNetworkScan() OVERRIDE { + NotifyNetworkListChanged(networks_); + } + + virtual void StartConnect(const std::string& network_guid, + std::string* error) OVERRIDE { + NetworkList::iterator network_properties = FindNetwork(network_guid); + if (network_properties != networks_.end()) { + DisconnectAllNetworksOfType(network_properties->type); + network_properties->connection_state = onc::connection_state::kConnected; + SortNetworks(); + NotifyNetworkListChanged(networks_); + NotifyNetworkChanged(network_guid); + } else { + *error = "configure-failed"; + } + } + + virtual void StartDisconnect(const std::string& network_guid, + std::string* error) OVERRIDE { + NetworkList::iterator network_properties = FindNetwork(network_guid); + if (network_properties != networks_.end()) { + network_properties->connection_state = + onc::connection_state::kNotConnected; + SortNetworks(); + NotifyNetworkListChanged(networks_); + NotifyNetworkChanged(network_guid); + } else { + *error = "not-found"; + } + } + + virtual void SetEventObservers( + scoped_refptr<base::MessageLoopProxy> message_loop_proxy, + const NetworkGuidListCallback& networks_changed_observer, + const NetworkGuidListCallback& network_list_changed_observer) OVERRIDE { + message_loop_proxy_.swap(message_loop_proxy); + networks_changed_observer_ = networks_changed_observer; + network_list_changed_observer_ = network_list_changed_observer; + } + + private: + NetworkList::iterator FindNetwork(const std::string& network_guid) { + for (NetworkList::iterator it = networks_.begin(); it != networks_.end(); + ++it) { + if (it->guid == network_guid) + return it; + } + return networks_.end(); + } + + void DisconnectAllNetworksOfType(const std::string& type) { + for (NetworkList::iterator it = networks_.begin(); it != networks_.end(); + ++it) { + if (it->type == type) + it->connection_state = onc::connection_state::kNotConnected; + } + } + + void SortNetworks() { + // Sort networks, so connected/connecting is up front, then by type: + // Ethernet, WiFi, Cellular, VPN + networks_.sort(WiFiService::NetworkProperties::OrderByType); + } + + void NotifyNetworkListChanged(const NetworkList& networks) { + WiFiService::NetworkGuidList current_networks; + for (WiFiService::NetworkList::const_iterator it = networks.begin(); + it != networks.end(); + ++it) { + current_networks.push_back(it->guid); + } + + message_loop_proxy_->PostTask( + FROM_HERE, + base::Bind(network_list_changed_observer_, current_networks)); + } + + void NotifyNetworkChanged(const std::string& network_guid) { + WiFiService::NetworkGuidList changed_networks(1, network_guid); + message_loop_proxy_->PostTask( + FROM_HERE, + base::Bind(networks_changed_observer_, changed_networks)); + } + + NetworkList networks_; + scoped_refptr<base::MessageLoopProxy> message_loop_proxy_; + NetworkGuidListCallback networks_changed_observer_; + NetworkGuidListCallback network_list_changed_observer_; +}; + +WiFiService* WiFiService::CreateForTest() { return new FakeWiFiService(); } +WiFiService* WiFiService::Create() { return new FakeWiFiService(); } + +} // namespace wifi |