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 | |
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')
-rw-r--r-- | components/OWNERS | 2 | ||||
-rw-r--r-- | components/components.gyp | 1 | ||||
-rw-r--r-- | components/wifi.gypi | 28 | ||||
-rw-r--r-- | components/wifi/DEPS | 3 | ||||
-rw-r--r-- | components/wifi/OWNERS | 1 | ||||
-rw-r--r-- | components/wifi/fake_wifi_service.cc | 208 | ||||
-rw-r--r-- | components/wifi/wifi_export.h | 29 | ||||
-rw-r--r-- | components/wifi/wifi_service.cc | 105 | ||||
-rw-r--r-- | components/wifi/wifi_service.h | 119 |
9 files changed, 496 insertions, 0 deletions
diff --git a/components/OWNERS b/components/OWNERS index 356fe7b..63c9c4d 100644 --- a/components/OWNERS +++ b/components/OWNERS @@ -67,5 +67,7 @@ per-file variations.gypi=asvitkine@chromium.org per-file variations.gypi=jwd@chromium.org per-file variations.gypi=stevet@chromium.org +per-file wifi.gypi=mef@chromium.org + per-file *.isolate=csharp@chromium.org per-file *.isolate=maruel@chromium.org diff --git a/components/components.gyp b/components/components.gyp index d9b31c4..053852b 100644 --- a/components/components.gyp +++ b/components/components.gyp @@ -31,5 +31,6 @@ 'webdata.gypi', 'web_contents_delegate_android.gypi', 'web_modal.gypi', + 'wifi.gypi', ], } diff --git a/components/wifi.gypi b/components/wifi.gypi new file mode 100644 index 0000000..f65bdaa --- /dev/null +++ b/components/wifi.gypi @@ -0,0 +1,28 @@ +# 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. + +{ + 'targets': [ + { + 'target_name': 'wifi_component', + 'type': '<(component)', + 'dependencies': [ + '../base/base.gyp:base', + '../components/components.gyp:onc_component', + ], + 'include_dirs': [ + '..', + ], + 'defines': [ + 'WIFI_IMPLEMENTATION', + ], + 'sources': [ + 'wifi/wifi_export.h', + 'wifi/wifi_service.cc', + 'wifi/wifi_service.h', + 'wifi/fake_wifi_service.cc', + ], + }, + ], +} diff --git a/components/wifi/DEPS b/components/wifi/DEPS new file mode 100644 index 0000000..273d26b --- /dev/null +++ b/components/wifi/DEPS @@ -0,0 +1,3 @@ +include_rules = [ + "+components/onc", +] diff --git a/components/wifi/OWNERS b/components/wifi/OWNERS new file mode 100644 index 0000000..db8bbdc --- /dev/null +++ b/components/wifi/OWNERS @@ -0,0 +1 @@ +mef@chromium.org 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 diff --git a/components/wifi/wifi_export.h b/components/wifi/wifi_export.h new file mode 100644 index 0000000..50c8676 --- /dev/null +++ b/components/wifi/wifi_export.h @@ -0,0 +1,29 @@ +// 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. + +#ifndef COMPONENTS_WIFI_WIFI_EXPORT_H_ +#define COMPONENTS_WIFI_WIFI_EXPORT_H_ + +#if defined(COMPONENT_BUILD) +#if defined(WIN32) + +#if defined(WIFI_IMPLEMENTATION) +#define WIFI_EXPORT __declspec(dllexport) +#else +#define WIFI_EXPORT __declspec(dllimport) +#endif // defined(WIFI_IMPLEMENTATION) + +#else // defined(WIN32) +#if defined(WIFI_IMPLEMENTATION) +#define WIFI_EXPORT __attribute__((visibility("default"))) +#else +#define WIFI_EXPORT +#endif +#endif + +#else // defined(COMPONENT_BUILD) +#define WIFI_EXPORT +#endif + +#endif // COMPONENTS_WIFI_WIFI_EXPORT_H_ diff --git a/components/wifi/wifi_service.cc b/components/wifi/wifi_service.cc new file mode 100644 index 0000000..2ca87c3 --- /dev/null +++ b/components/wifi/wifi_service.cc @@ -0,0 +1,105 @@ +// 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/json/json_reader.h" +#include "base/message_loop/message_loop.h" +#include "base/strings/stringprintf.h" +#include "components/onc/onc_constants.h" + +namespace wifi { + +WiFiService::NetworkProperties::NetworkProperties() + : connection_state(onc::connection_state::kNotConnected), + security(onc::wifi::kNone), + signal_strength(0), + auto_connect(false), + frequency(WiFiService::kFrequencyUnknown) {} + +WiFiService::NetworkProperties::~NetworkProperties() {} + +scoped_ptr<base::DictionaryValue> WiFiService::NetworkProperties::ToValue( + bool network_list) const { + scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue()); + + value->SetString(onc::network_config::kGUID, guid); + value->SetString(onc::network_config::kName, name); + value->SetString(onc::network_config::kConnectionState, connection_state); + value->SetString(onc::network_config::kType, type); + + if (type == onc::network_type::kWiFi) { + scoped_ptr<base::DictionaryValue> wifi(new base::DictionaryValue()); + wifi->SetString(onc::wifi::kSecurity, security); + wifi->SetInteger(onc::wifi::kSignalStrength, signal_strength); + + // Network list expects subset of data. + if (!network_list) { + if (frequency != WiFiService::kFrequencyUnknown) + wifi->SetInteger(onc::wifi::kFrequency, frequency); + scoped_ptr<base::ListValue> frequency_list(new base::ListValue()); + for (FrequencyList::const_iterator it = this->frequency_list.begin(); + it != this->frequency_list.end(); + ++it) { + frequency_list->AppendInteger(*it); + } + if (!frequency_list->empty()) + wifi->Set(onc::wifi::kFrequencyList, frequency_list.release()); + if (!bssid.empty()) + wifi->SetString(onc::wifi::kBSSID, bssid); + wifi->SetString(onc::wifi::kSSID, ssid); + } + value->Set(onc::network_type::kWiFi, wifi.release()); + } else { + // Add properites from json extra if present. + if (!json_extra.empty()) { + Value* value_extra = base::JSONReader::Read(json_extra); + value->Set(type, value_extra); + } + } + return value.Pass(); +} + +bool WiFiService::NetworkProperties::UpdateFromValue( + const base::DictionaryValue& value) { + const base::DictionaryValue* wifi = NULL; + std::string wifi_security; + if (value.GetDictionary(onc::network_type::kWiFi, &wifi) && + wifi->GetString(onc::wifi::kSecurity, &wifi_security)) { + security = wifi_security; + return true; + } + return false; +} + +std::string WiFiService::NetworkProperties::MacAddressAsString( + const uint8 mac_as_int[6]) { + // mac_as_int is big-endian. Write in byte chunks. + // Format is XX:XX:XX:XX:XX:XX. + static const char* const kMacFormatString = "%02x:%02x:%02x:%02x:%02x:%02x"; + return base::StringPrintf(kMacFormatString, + mac_as_int[0], + mac_as_int[1], + mac_as_int[2], + mac_as_int[3], + mac_as_int[4], + mac_as_int[5]); +} + +bool WiFiService::NetworkProperties::OrderByType(const NetworkProperties& l, + const NetworkProperties& r) { + if (l.connection_state != r.connection_state) + return l.connection_state < r.connection_state; + // This sorting order is needed only for browser_tests, which expect this + // network type sort order: ethernet < wifi < vpn < cellular. + if (l.type == r.type) + return l.guid < r.guid; + if (l.type == onc::network_type::kEthernet) + return true; + if (r.type == onc::network_type::kEthernet) + return false; + return l.type > r.type; +} + +} // namespace wifi diff --git a/components/wifi/wifi_service.h b/components/wifi/wifi_service.h new file mode 100644 index 0000000..e29d936 --- /dev/null +++ b/components/wifi/wifi_service.h @@ -0,0 +1,119 @@ +// 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. + +#ifndef CHROME_UTILITY_WIFI_WIFI_SERVICE_H_ +#define CHROME_UTILITY_WIFI_WIFI_SERVICE_H_ + +#include <list> +#include <string> +#include <vector> + +#include "base/callback.h" +#include "base/memory/ref_counted.h" +#include "base/message_loop/message_loop_proxy.h" +#include "base/values.h" +#include "components/wifi/wifi_export.h" + +namespace wifi { + +// WiFiService interface used by implementation of chrome.networkingPrivate +// JavaScript extension API. All methods should be called on worker thread. +// It could be created on any (including UI) thread, so nothing expensive should +// be done in the constructor. +class WIFI_EXPORT WiFiService { + public: + typedef std::vector<std::string> NetworkGuidList; + typedef base::Callback< + void(const NetworkGuidList& network_guid_list)> NetworkGuidListCallback; + + virtual ~WiFiService() {} + + // Create instance of |WiFiService| for normal use. + static WiFiService* Create(); + // Create instance of |WiFiService| for unit test use. + static WiFiService* CreateForTest(); + + // Get Properties of network identified by |network_guid|. Populates + // |properties| on success, |error| on failure. + virtual void GetProperties(const std::string& network_guid, + DictionaryValue* properties, + std::string* error) = 0; + + // Set Properties of network identified by |network_guid|. Populates |error| + // on failure. + virtual void SetProperties(const std::string& network_guid, + scoped_ptr<base::DictionaryValue> properties, + std::string* error) = 0; + + // Get list of visible networks. Populates |network_list| on success. + virtual void GetVisibleNetworks(ListValue* network_list) = 0; + + // Request network scan. Send |NetworkListChanged| event on completion. + virtual void RequestNetworkScan() = 0; + + // Start connect to network identified by |network_guid|. Populates |error| + // on failure. + virtual void StartConnect(const std::string& network_guid, + std::string* error) = 0; + + // Start disconnect from network identified by |network_guid|. Populates + // |error| on failure. + virtual void StartDisconnect(const std::string& network_guid, + std::string* error) = 0; + + // Set observers to run when |NetworksChanged| and |NetworksListChanged| + // events needs to be sent. Notifications are posted on |message_loop_proxy|. + virtual void SetEventObservers( + scoped_refptr<base::MessageLoopProxy> message_loop_proxy, + const NetworkGuidListCallback& networks_changed_observer, + const NetworkGuidListCallback& network_list_changed_observer) = 0; + + protected: + WiFiService() {} + + typedef int32 Frequency; + enum FrequencyEnum { + kFrequencyUnknown = 0, + kFrequency2400 = 2400, + kFrequency5000 = 5000 + }; + + typedef std::list<Frequency> FrequencyList; + // Network Properties, used as result of |GetProperties| and + // |GetVisibleNetworks|. + struct WIFI_EXPORT NetworkProperties { + NetworkProperties(); + ~NetworkProperties(); + + std::string connection_state; + std::string guid; + std::string name; + std::string ssid; + std::string bssid; + std::string type; + std::string security; + // WiFi Signal Strength. 0..100 + uint32 signal_strength; + bool auto_connect; + Frequency frequency; + FrequencyList frequency_list; + + std::string json_extra; // Extra JSON properties for unit tests + + scoped_ptr<base::DictionaryValue> ToValue(bool network_list) const; + bool UpdateFromValue(const base::DictionaryValue& value); + static std::string MacAddressAsString(const uint8 mac_as_int[6]); + static bool OrderByType(const NetworkProperties& l, + const NetworkProperties& r); + }; + + typedef std::list<NetworkProperties> NetworkList; + + private: + DISALLOW_COPY_AND_ASSIGN(WiFiService); +}; + +} // namespace wifi + +#endif // CHROME_UTILITY_WIFI_WIFI_SERVICE_H_ |