Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1318)

Unified Diff: chrome/utility/wifi/wifi_service_win.cc

Issue 22295002: Base infrastructure for Networking Private API on Windows and Mac. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Reset DHCP after WiFi Connect to speed up the connection after factory reset. Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/utility/wifi/wifi_service_win.cc
diff --git a/chrome/utility/wifi/wifi_service_win.cc b/chrome/utility/wifi/wifi_service_win.cc
new file mode 100644
index 0000000000000000000000000000000000000000..315b3705aaa4aa2f939809de2b7cdaf26e8194fa
--- /dev/null
+++ b/chrome/utility/wifi/wifi_service_win.cc
@@ -0,0 +1,598 @@
+// 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 "chrome/utility/wifi/wifi_service.h"
+
+#include <iphlpapi.h>
+#include <objbase.h>
+#include <wlanapi.h>
+
+#include "base/bind.h"
+#include "base/memory/ref_counted.h"
+#include "base/message_loop/message_loop.h"
+#include "base/strings/string16.h"
+#include "base/strings/string_util.h"
+#include "base/strings/utf_string_conversions.h"
+
+// TODO(mef): Verify that wlanapi.dll is delay loaded.
+#pragma comment(lib, "Wlanapi.lib")
+
+class WiFiServiceImpl : public WiFiService {
+ public:
+ WiFiServiceImpl() : client_(NULL) {
+ OpenClientHandle();
+ }
+
+ virtual ~WiFiServiceImpl() { CloseClientHandle(); }
+
+ virtual void GetProperties(const std::string& network_guid,
+ const NetworkPropertiesCallback& callback,
+ const ErrorCallback& error_callback) {
+ DWORD error = CheckClientHandle();
+
+ if (error == ERROR_SUCCESS) {
+ NetworkList network_list;
+ error = GetVisibleNetworkList(&network_list);
+ if (error == ERROR_SUCCESS && !network_list.empty()) {
+ NetworkList::iterator it = FindNetwork(network_list, network_guid);
+ if (it != network_list.end())
+ callback.Run(network_guid, *it);
+ else
+ error = ERROR_NOT_FOUND;
+ }
+ }
+
+ CheckError(error_callback, "Error.DBusFailed", error);
+ }
+
+ virtual void GetState(const std::string& network_guid,
+ const NetworkPropertiesCallback& callback,
+ const ErrorCallback& error_callback) OVERRIDE {}
+
+ virtual void GetManagedProperties(
+ const std::string& network_guid,
+ const DictionaryResultCallback& callback,
+ const ErrorCallback& error_callback) OVERRIDE {}
+
+ virtual void SetProperties(const std::string& network_guid,
+ const base::DictionaryValue& properties,
+ const StringResultCallback& callback,
+ const ErrorCallback& error_callback) OVERRIDE {}
+
+ virtual void GetVisibleNetworks(
+ const NetworkListCallback& callback,
+ const ErrorCallback& error_callback) OVERRIDE {
+ DWORD error = CheckClientHandle();
+
+ if (error == ERROR_SUCCESS) {
+ NetworkList network_list;
+ error = GetVisibleNetworkList(&network_list);
+ if (error == ERROR_SUCCESS && !network_list.empty()) {
+ SortNetworks(network_list);
+ callback.Run(network_list);
+ }
+ }
+
+ CheckError(error_callback, "Error.DBusFailed", error);
+ }
+
+ virtual void RequestNetworkScan() OVERRIDE {
+ DWORD error = CheckClientHandle();
+ if (error == ERROR_SUCCESS) {
+ WlanScan(client_, &interface_guid_, NULL, NULL, NULL);
+ }
+ }
+
+ virtual void StartConnect(const std::string& network_guid,
+ const StringResultCallback& callback,
+ const ErrorCallback& error_callback) OVERRIDE {
+ DWORD error = CheckClientHandle();
+ if (error == ERROR_SUCCESS) {
+ std::string connected_network_guid;
+ error = SaveCurrentConnectedNetwork(&connected_network_guid);
+ if (error == ERROR_SUCCESS) {
+ error = Connect(network_guid);
+ if (error == ERROR_SUCCESS) {
+ callback.Run(network_guid);
+ // Notify that previously connected network has changed.
+ NotifyNetworkChanged(connected_network_guid);
+ // Start waiting for network connection state change.
+ if (!networks_changed_observer_.is_null())
+ WaitForNetworkConnect(network_guid, 0);
+ }
+ }
+ }
+ CheckError(error_callback, "Error.DBusFailed", error);
+ }
+
+ virtual void StartDisconnect(const std::string& network_guid,
+ const StringResultCallback& callback,
+ const ErrorCallback& error_callback) OVERRIDE {
+ DWORD error = CheckClientHandle();
+
+ if (error == ERROR_SUCCESS) {
+ std::string connected_network_guid;
+ error = SaveCurrentConnectedNetwork(&connected_network_guid);
+ if (error == ERROR_SUCCESS && network_guid == connected_network_guid) {
+ error = Disconnect();
+ if (error == ERROR_SUCCESS) {
+ callback.Run(network_guid);
+ }
+ }
+ }
+ CheckError(error_callback, "Error.DBusFailed", error);
+ }
+
+ virtual void SetNetworksChangedObserver(
+ const NetworkGuidListCallback& observer) OVERRIDE {
+ networks_changed_observer_ = observer;
+ }
+
+ virtual void SetNetworkListChangedObserver(
+ const NetworkGuidListCallback& observer) OVERRIDE {
+ network_list_changed_observer_ = observer;
+ }
+
+ private:
+ static void __stdcall OnWlanNotificationCallback(
+ PWLAN_NOTIFICATION_DATA wlan_notification_data,
+ PVOID context) {
+ WiFiServiceImpl* service = reinterpret_cast<WiFiServiceImpl*>(context);
+ service->OnWlanNotification(wlan_notification_data);
+ }
+
+ void OnWlanNotification(PWLAN_NOTIFICATION_DATA wlan_notification_data) {
+ PWLAN_CONNECTION_NOTIFICATION_DATA wlan_connection_data =
+ reinterpret_cast<PWLAN_CONNECTION_NOTIFICATION_DATA>(
+ wlan_notification_data->pData);
+
+ switch (wlan_notification_data->NotificationCode) {
+ case wlan_notification_acm_disconnected:
+ case wlan_notification_acm_connection_complete:
+ // TODO(mef): Figure out how to send notifications correctly from here.
+ // NotifyNetworkChanged(GuidFromSsid(wlan_connection_data->dot11Ssid));
+ break;
+ case wlan_notification_acm_connection_attempt_fail:
+ break;
+ case wlan_notification_acm_scan_complete:
+ break;
+ }
+ }
+
+ void WaitForNetworkConnect(const std::string& network_guid, int attempt) {
+ if (attempt > kMaxAttempts)
+ return;
+
+ std::string connected_network_guid;
+ DWORD error = FindConnectedNetwork(&connected_network_guid);
+ if (network_guid == connected_network_guid) {
+ // Reset DHCP to speed up the connection after Chromekey factory reset.
+ error = ResetDHCP();
+ NotifyNetworkChanged(network_guid);
+ } else {
+ // Continue waiting for network connection state change.
+ base::MessageLoop::current()->PostDelayedTask(
+ FROM_HERE,
+ base::Bind(&WiFiServiceImpl::WaitForNetworkConnect,
+ base::Unretained(this),
+ network_guid,
+ ++attempt),
+ base::TimeDelta::FromMilliseconds(kAttemptDelayMs));
+ }
+ }
+
+ bool CheckError(const ErrorCallback& error_callback,
+ const std::string& error_name,
+ DWORD error_code) {
+ if (error_code != ERROR_SUCCESS) {
+ scoped_ptr<base::DictionaryValue> error_data(new base::DictionaryValue);
+ error_data->SetInteger("Win32ErrorCode", error_code);
+ error_callback.Run(error_name, error_data.Pass());
+ return true;
+ }
+ return false;
+ }
+
+ NetworkList::iterator FindNetwork(NetworkList& networks,
+ 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();
+ }
+
+ DWORD SaveCurrentConnectedNetwork(std::string* connected_network_guid) {
+ // Find currently connected network.
+ DWORD error = FindConnectedNetwork(connected_network_guid);
+ if (error == ERROR_SUCCESS && !connected_network_guid->empty()) {
+ if (error == ERROR_SUCCESS) {
+ SaveTempProfile(*connected_network_guid);
+ std::string profile_xml;
+ error = GetProfile(*connected_network_guid, &profile_xml);
+ if (error == ERROR_SUCCESS) {
+ saved_profiles_xml_[*connected_network_guid] = profile_xml;
+ }
+ }
+ }
+ return error;
+ }
+
+ void SortNetworks(NetworkList& networks) {
+ // Sort networks, so connected/connecting is up front, then by type:
+ // Ethernet, WiFi, Cellular, VPN
+ networks.sort(WiFiService::NetworkProperties::OrderByType);
+ }
+
+ // open a WLAN client handle
+ DWORD OpenClientHandle() {
+ CloseClientHandle();
+
+ DWORD error = ERROR_SUCCESS;
+ DWORD service_version = 0;
+
+ // open a handle to the service
+ error = WlanOpenHandle(WLAN_API_VERSION, NULL, &service_version, &client_);
+
+ PWLAN_INTERFACE_INFO_LIST pIntfList = NULL;
+ UINT i = 0;
+
+ if (error == ERROR_SUCCESS) {
+ // enumerate wireless interfaces
+ error = WlanEnumInterfaces(client_, NULL, &pIntfList);
+ if (error == ERROR_SUCCESS) {
+ if (pIntfList != NULL && pIntfList->dwNumberOfItems != 0) {
+ // Use first interface.
+ interface_guid_ = pIntfList->InterfaceInfo[0].InterfaceGuid;
+ WlanRegisterNotification(client_,
+ WLAN_NOTIFICATION_SOURCE_ALL,
+ FALSE,
+ OnWlanNotificationCallback,
+ this,
+ NULL,
+ NULL);
+ } else {
+ error = ERROR_NOINTERFACE;
+ }
+ }
+ // clean up
+ if (pIntfList != NULL)
+ WlanFreeMemory(pIntfList);
+ }
+ return error;
+ }
+
+ DWORD ResetDHCP() {
+ IP_ADAPTER_INDEX_MAP adapter_index_map = {0};
+ DWORD error = FindAdapterIndexMapByGuid(interface_guid_,
+ &adapter_index_map);
+ if (error == ERROR_SUCCESS) {
+ error = IpReleaseAddress(&adapter_index_map);
+ if (error == ERROR_SUCCESS) {
+ error = IpRenewAddress(&adapter_index_map);
+ }
+ }
+ return error;
+ }
+
+ DWORD FindAdapterIndexMapByGuid(const GUID& interface_guid,
+ IP_ADAPTER_INDEX_MAP* adapter_index_map) {
+ string16 guid_string;
+ const int kGUIDSize = 39;
+ ::StringFromGUID2(interface_guid, WriteInto(&guid_string, kGUIDSize),
+ kGUIDSize);
+
+ ULONG buffer_length = 0;
+ DWORD error = GetInterfaceInfo(NULL, &buffer_length);
+ if (error == ERROR_INSUFFICIENT_BUFFER) {
+ scoped_ptr<unsigned char[]> buffer(new unsigned char[buffer_length]);
+ IP_INTERFACE_INFO* interface_info =
+ reinterpret_cast<IP_INTERFACE_INFO*>(buffer.get());
+ error = GetInterfaceInfo(interface_info, &buffer_length);
+ if (error == ERROR_SUCCESS) {
+ for (long adapter = 0; adapter < interface_info->NumAdapters;
+ ++adapter) {
+ if (EndsWith(interface_info->Adapter[adapter].Name, guid_string,
+ false)) {
+ *adapter_index_map = interface_info->Adapter[adapter];
+ break;
+ }
+ }
+ }
+ }
+
+ return error;
+ }
+
+
+ DWORD CheckClientHandle() {
+ if (client_ != NULL)
+ return ERROR_SUCCESS;
+ return OpenClientHandle();
+ }
+
+ DWORD CloseClientHandle() {
+ DWORD error = ERROR_SUCCESS;
+ if (client_ != NULL) {
+ WlanCloseHandle(client_, NULL);
+ client_ = NULL;
+ }
+ return error;
+ }
+
+ base::string16 ProfileNameFromGuid(const std::string& network_guid) const {
+ return base::UTF8ToUTF16(network_guid);
+ }
+
+ DOT11_SSID SsidFromGuid(const std::string& network_guid) const {
+ DOT11_SSID ssid = {0};
+ if (network_guid.length() <= DOT11_SSID_MAX_LENGTH) {
+ ssid.uSSIDLength = network_guid.length();
+ strncpy(reinterpret_cast<char*>(ssid.ucSSID),
+ network_guid.c_str(),
+ ssid.uSSIDLength);
+ }
+ return ssid;
+ }
+
+ std::string GuidFromSsid(const DOT11_SSID& dot11Ssid) {
+ return std::string(reinterpret_cast<const char*>(dot11Ssid.ucSSID),
+ dot11Ssid.uSSIDLength);
+ }
+
+ std::string SsidFromWlan(const WLAN_AVAILABLE_NETWORK& wlan) {
+ return GuidFromSsid(wlan.dot11Ssid);
+ }
+
+
+ std::string GuidFromWlan(const WLAN_AVAILABLE_NETWORK& wlan) {
+ return SsidFromWlan(wlan);
+ }
+
+ WiFiService::Security SecurityFromDot11AuthAlg(DOT11_AUTH_ALGORITHM alg) {
+ // TODO(mef): Figure out correct mapping.
+ switch (alg) {
+ case DOT11_AUTH_ALGO_RSNA:
+ return kSecurityWPA;
+ case DOT11_AUTH_ALGO_RSNA_PSK:
+ return kSecurityWPA_PSK;
+ case DOT11_AUTH_ALGO_80211_SHARED_KEY:
+ return kSecurityWEP_PSK;
+ case DOT11_AUTH_ALGO_80211_OPEN:
+ return kSecurityNone;
+ default:
+ return kSecurityUnknown;
+ }
+ return kSecurityUnknown;
+ }
+
+ void NetworkPropertiesFromAvailableNetwork(const WLAN_AVAILABLE_NETWORK& wlan,
+ const WLAN_BSS_LIST& wlan_bss_list,
+ NetworkProperties* properties) {
+ if (wlan.dwFlags & WLAN_AVAILABLE_NETWORK_CONNECTED) {
+ properties->connection_state = WiFiService::kConnectionStateConnected;
+ } else {
+ properties->connection_state = WiFiService::kConnectionStateNotConnected;
+ }
+
+ properties->ssid = SsidFromWlan(wlan);
+ properties->name = properties->ssid;
+ properties->guid = GuidFromWlan(wlan);
+ properties->type = WiFiService::kNetworkTypeWiFi;
+
+ for (size_t bss = 0; bss < wlan_bss_list.dwNumberOfItems; ++bss) {
+ const WLAN_BSS_ENTRY& bss_entry(wlan_bss_list.wlanBssEntries[bss]);
+ if (bss_entry.dot11Ssid.uSSIDLength == wlan.dot11Ssid.uSSIDLength &&
+ 0 == memcmp(bss_entry.dot11Ssid.ucSSID,
+ wlan.dot11Ssid.ucSSID,
+ bss_entry.dot11Ssid.uSSIDLength)) {
+ if (bss_entry.ulChCenterFrequency < 3000000)
+ properties->frequency = kFrequency2400;
+ else
+ properties->frequency = kFrequency5000;
+ properties->frequency_list.push_back(properties->frequency);
+ properties->bssid = WiFiService::NetworkProperties::MacAddressAsString(
+ bss_entry.dot11Bssid);
+ }
+ }
+ properties->frequency_list.sort();
+ properties->frequency_list.unique();
+
+ properties->security =
+ SecurityFromDot11AuthAlg(wlan.dot11DefaultAuthAlgorithm);
+
+ properties->signal_strength = wlan.wlanSignalQuality;
+ }
+
+ // get the list of visible wireless networks
+ DWORD GetVisibleNetworkList(NetworkList* network_list) {
+ DWORD error = ERROR_SUCCESS;
+
+ if (client_ == NULL) {
+ return ERROR_NOINTERFACE;
+ }
+
+ PWLAN_AVAILABLE_NETWORK_LIST pVList = NULL;
+ PWLAN_BSS_LIST pWlanBssList = NULL;
+
+ error = WlanGetAvailableNetworkList(
+ client_, &interface_guid_, 0, NULL, &pVList);
+
+ if (error == ERROR_SUCCESS && NULL != pVList) {
+ error = WlanGetNetworkBssList(client_,
+ &interface_guid_,
+ NULL,
+ dot11_BSS_type_any,
+ FALSE,
+ NULL,
+ &pWlanBssList);
+ if (error == ERROR_SUCCESS && NULL != pWlanBssList) {
+ for (DWORD i = 0; i < pVList->dwNumberOfItems; ++i) {
+ network_list->push_back(NetworkProperties());
+ NetworkPropertiesFromAvailableNetwork(
+ pVList->Network[i], *pWlanBssList, &network_list->back());
+ }
+ }
+ }
+
+ // clean up
+ if (pVList != NULL) {
+ WlanFreeMemory(pVList);
+ }
+ if (pWlanBssList != NULL) {
+ WlanFreeMemory(pWlanBssList);
+ }
+ return error;
+ }
+
+ // Find currently connected network.
+ DWORD FindConnectedNetwork(std::string* connected_network_guid) {
+ DWORD error = ERROR_SUCCESS;
+
+ if (client_ == NULL) {
+ return ERROR_NOINTERFACE;
+ }
+
+ PWLAN_AVAILABLE_NETWORK_LIST pVList = NULL;
+
+ error = WlanGetAvailableNetworkList(
+ client_, &interface_guid_, 0, NULL, &pVList);
+
+ if (error == ERROR_SUCCESS && NULL != pVList) {
+ for (DWORD i = 0; i < pVList->dwNumberOfItems; ++i) {
+ const WLAN_AVAILABLE_NETWORK& wlan = pVList->Network[i];
+ if (wlan.dwFlags & WLAN_AVAILABLE_NETWORK_CONNECTED) {
+ *connected_network_guid = GuidFromWlan(wlan);
+ break;
+ }
+ }
+ }
+
+ // clean up
+ if (pVList != NULL) {
+ WlanFreeMemory(pVList);
+ }
+
+ return error;
+ }
+
+ DWORD Connect(const std::string& network_guid) {
+ DWORD error = ERROR_SUCCESS;
+
+ if (client_ == NULL) {
+ return ERROR_NOINTERFACE;
+ }
+
+ base::string16 profile_name = ProfileNameFromGuid(network_guid);
+
+ if (HaveProfile(network_guid)) {
+ WLAN_CONNECTION_PARAMETERS wlan_params = {
+ wlan_connection_mode_profile, profile_name.c_str(), NULL,
+ NULL, dot11_BSS_type_any, 0};
+ error = ::WlanConnect(client_, &interface_guid_, &wlan_params, NULL);
+ } else {
+ DOT11_SSID ssid = SsidFromGuid(network_guid);
+ WLAN_CONNECTION_PARAMETERS wlan_params = {
+ wlan_connection_mode_discovery_unsecure, NULL, &ssid, NULL,
+ dot11_BSS_type_infrastructure, 0};
+ error = ::WlanConnect(client_, &interface_guid_, &wlan_params, NULL);
+ }
+
+ return error;
+ }
+
+ DWORD Disconnect() {
+ DWORD error = ERROR_SUCCESS;
+
+ if (client_ == NULL) {
+ return ERROR_NOINTERFACE;
+ }
+
+ error = ::WlanDisconnect(client_, &interface_guid_, NULL);
+ return error;
+ }
+
+ DWORD SaveTempProfile(const std::string& network_guid) {
+ DWORD error = ERROR_SUCCESS;
+
+ if (client_ == NULL) {
+ return ERROR_NOINTERFACE;
+ }
+
+ base::string16 profile_name = ProfileNameFromGuid(network_guid);
+
+ error = ::WlanSaveTemporaryProfile(
+ client_, &interface_guid_, profile_name.c_str(), NULL, 0, true, NULL);
+ return error;
+ }
+
+ DWORD GetProfile(const std::string& network_guid, std::string* profile_xml) {
+ DWORD error = ERROR_SUCCESS;
+
+ if (client_ == NULL) {
+ return ERROR_NOINTERFACE;
+ }
+
+ base::string16 profile_name = ProfileNameFromGuid(network_guid);
+ LPWSTR str_profile_xml = NULL;
+ error = ::WlanGetProfile(client_,
+ &interface_guid_,
+ profile_name.c_str(),
+ NULL,
+ &str_profile_xml,
+ NULL,
+ NULL);
+
+ if (error == ERROR_SUCCESS && str_profile_xml != NULL) {
+ *profile_xml = base::UTF16ToUTF8(str_profile_xml);
+ }
+ // clean up
+ if (str_profile_xml != NULL) {
+ WlanFreeMemory(str_profile_xml);
+ }
+
+ return error;
+ }
+
+ bool HaveProfile(const std::string& network_guid) {
+ DWORD error = ERROR_SUCCESS;
+ std::string profile_xml;
+ return GetProfile(network_guid, &profile_xml) == ERROR_SUCCESS;
+ }
+
+ void NotifyNetworkListChanged(const NetworkList& networks) {
+ if (network_list_changed_observer_.is_null())
+ return;
+
+ WiFiService::NetworkGuidList current_networks;
+ for (WiFiService::NetworkList::const_iterator it = networks.begin();
+ it != networks.end();
+ ++it) {
+ current_networks.push_back(it->guid);
+ }
+ network_list_changed_observer_.Run(current_networks);
+ }
+
+ void NotifyNetworkChanged(const std::string& network_guid) {
+ WiFiService::NetworkGuidList changed_networks(1, network_guid);
+ if (!networks_changed_observer_.is_null())
+ networks_changed_observer_.Run(changed_networks);
+ }
+
+ // Wlan Service Handle.
+ HANDLE client_;
+ // Wlan Interface Guid.
+ GUID interface_guid_;
+ // Preserved Wlan Profile Xml.
+ std::map<std::string, std::string> saved_profiles_xml_;
+
+ NetworkGuidListCallback networks_changed_observer_;
+ NetworkGuidListCallback network_list_changed_observer_;
+
+ static const int kMaxAttempts = 100;
+ static const int kAttemptDelayMs = 100;
+};
+
+WiFiService* WiFiService::CreateService() { return new WiFiServiceImpl(); }

Powered by Google App Engine
This is Rietveld 408576698