Chromium Code Reviews| 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..03d6de425c20522104bb0a8777e07f6be057e653 |
| --- /dev/null |
| +++ b/chrome/utility/wifi/wifi_service_win.cc |
| @@ -0,0 +1,858 @@ |
| +// 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" |
| +#include "components/onc/onc_constants.h" |
| + |
| +namespace { |
| +const char kWiFiServiceError[] = "Error.WiFiService"; |
| +} // namespace |
| + |
| +namespace wifi { |
| + |
| +// Implementation of WiFiService for Windows. |
| +class WiFiServiceImpl : public WiFiService, base::NonThreadSafe { |
| + public: |
| + WiFiServiceImpl(); |
| + virtual ~WiFiServiceImpl(); |
| + |
| + // WiFiService interface implementation. |
| + |
| + virtual void GetProperties(const std::string& network_guid, |
| + const NetworkPropertiesCallback& callback, |
| + const ErrorCallback& error_callback) OVERRIDE; |
| + |
| + 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; |
| + |
| + virtual void RequestNetworkScan() OVERRIDE; |
| + |
| + virtual void StartConnect(const std::string& network_guid, |
| + const StringResultCallback& callback, |
| + const ErrorCallback& error_callback) OVERRIDE; |
| + |
| + virtual void StartDisconnect(const std::string& network_guid, |
| + const StringResultCallback& callback, |
| + const ErrorCallback& error_callback) OVERRIDE; |
| + |
| + virtual void SetNetworksChangedObserver( |
| + const NetworkGuidListCallback& observer) OVERRIDE; |
| + |
| + virtual void SetNetworkListChangedObserver( |
| + const NetworkGuidListCallback& observer) OVERRIDE; |
| + |
| + private: |
| + // Static callback for Windows WLAN_NOTIFICATION. Calls OnWlanNotification |
| + // on WiFiServiceImpl passed back as |context|. |
| + static void __stdcall OnWlanNotificationCallback( |
| + PWLAN_NOTIFICATION_DATA wlan_notification_data, |
| + PVOID context); |
| + |
| + // Callback for Windows WLAN_NOTIFICATION. Called on random thread from |
| + // OnWlanNotificationCallback. Handles network connectivity and scan complete |
| + // notification and posts tasks to main thread. |
| + void OnWlanNotification(PWLAN_NOTIFICATION_DATA wlan_notification_data); |
| + |
| + // Handles NetworkScanComplete notification on main thread. Sends |
| + // |NetworkListChanged| event with new list of visible networks. |
| + void OnNetworkScanCompleteOnMainThread(); |
| + |
| + // Wait up to |kMaxAttempts| with |kAttemptDelayMs| delay for connection |
| + // to network with |network_guid|. Reset DHCP and Notify that |NetworkChanged| |
| + // upon success. |
| + void WaitForNetworkConnect(const std::string& network_guid, int attempt); |
| + |
| + // Check |error_code| and if is not |ERROR_SUCCESS|, then run |error_callback| |
| + // with |error_name|. |
| + bool CheckError(const ErrorCallback& error_callback, |
| + const std::string& error_name, |
| + DWORD error_code) const; |
| + |
| + // Return |iterator| to network identified by |network_guid| in |networks| |
| + // list. |
| + NetworkList::const_iterator FindNetwork( |
| + const NetworkList& networks, |
| + const std::string& network_guid) const; |
| + |
| + // Save currently connected network profile and return its |
| + // |connected_network_guid|, so it can be re-connected later. |
| + DWORD SaveCurrentConnectedNetwork(std::string* connected_network_guid); |
| + |
| + // Sort networks, so connected/connecting is up front, then by type: |
| + // Ethernet, WiFi, Cellular, VPN |
| + static void SortNetworks(NetworkList* networks); |
| + |
| + // Open a WLAN client handle, register for WLAN notifications. |
| + DWORD OpenClientHandle(); |
| + |
| + // Reset DHCP on wireless network to work around an issue when Windows |
| + // takes forever to connect to the network, e.g. after Chromecast |
| + // device reset. |
| + DWORD ResetDHCP(); |
| + |
| + // Find |adapter_index_map| by |interface_guid| for DHCP reset. |
| + DWORD FindAdapterIndexMapByGUID(const GUID& interface_guid, |
| + IP_ADAPTER_INDEX_MAP* adapter_index_map); |
| + |
| + // Ensure that |client_| handle is initialized. |
| + DWORD EnsureInitialized(); |
| + |
| + // Close |client_| handle if it is open. |
| + DWORD CloseClientHandle(); |
| + |
| + // Get |profile_name| from unique |network_guid|. |
| + base::string16 ProfileNameFromGUID(const std::string& network_guid) const { |
| + return base::UTF8ToUTF16(network_guid); |
| + } |
| + |
| + // Get |dot11_ssid| from unique |network_guid|. |
| + DOT11_SSID SSIDFromGUID(const std::string& network_guid) const; |
| + |
| + // Get unique |network_guid| string based on |dot11_ssid|. |
| + std::string GUIDFromSSID(const DOT11_SSID& dot11_ssid) const { |
| + return std::string(reinterpret_cast<const char*>(dot11_ssid.ucSSID), |
| + dot11_ssid.uSSIDLength); |
| + } |
| + |
| + // Get network |ssid| string based on |wlan|. |
| + std::string SSIDFromWLAN(const WLAN_AVAILABLE_NETWORK& wlan) const { |
| + return GUIDFromSSID(wlan.dot11Ssid); |
| + } |
| + |
| + // Get unique |network_guid| string based on |wlan|. |
| + std::string GUIDFromWLAN(const WLAN_AVAILABLE_NETWORK& wlan) const { |
| + return SSIDFromWLAN(wlan); |
| + } |
| + |
| + // Deduce |onc::wifi| security from |alg|. |
| + std::string SecurityFromDot11AuthAlg(DOT11_AUTH_ALGORITHM alg) const; |
| + |
| + // Populate |properties| based on |wlan| and its corresponding bss info from |
| + // |wlan_bss_list|. |
| + void NetworkPropertiesFromAvailableNetwork(const WLAN_AVAILABLE_NETWORK& wlan, |
| + const WLAN_BSS_LIST& wlan_bss_list, |
| + NetworkProperties* properties); |
| + |
| + // Get the list of visible wireless networks. |
| + DWORD GetVisibleNetworkList(NetworkList* network_list); |
| + |
| + // Find currently connected network if any. Populate |connected_network_guid| |
| + // on success. |
| + DWORD FindConnectedNetwork(std::string* connected_network_guid); |
| + |
| + // Connect to network |network_guid| using previosly stored profile if exists, |
| + // or just network sid. |
| + DWORD Connect(const std::string& network_guid); |
| + |
| + // Disconnect from currently connected network if any. |
| + DWORD Disconnect(); |
| + |
| + // Save temporary wireless profile for |network_guid|. |
| + DWORD SaveTempProfile(const std::string& network_guid); |
| + |
| + // Get previously stored |profile_xml| for |network_guid|. |
| + DWORD GetProfile(const std::string& network_guid, std::string* profile_xml); |
| + |
| + // Return true if there is previously stored profile xml for |network_guid|. |
| + bool HaveProfile(const std::string& network_guid); |
| + |
| + // Notify |network_list_changed_observer_| that list of visible networks has |
| + // changed to |networks|. |
| + void NotifyNetworkListChanged(const NetworkList& networks); |
| + |
| + // Notify |networks_changed_observer_| that network |network_guid| status has |
| + // changed. |
| + void NotifyNetworkChanged(const std::string& network_guid); |
| + |
| + // WLAN service handle. |
| + HANDLE client_; |
| + // GUID of the currently connected interface, if any, otherwise the GUID of |
| + // one of the WLAN interfaces. |
| + GUID interface_guid_; |
| + // Preserved WLAN profile xml. |
| + std::map<std::string, std::string> saved_profiles_xml_; |
| + // Observer to get notified when network(s) have changed (e.g. connect). |
| + NetworkGuidListCallback networks_changed_observer_; |
| + // Observer to get notified when network list has changed (scan complete). |
| + NetworkGuidListCallback network_list_changed_observer_; |
| + // Task runner to post tasks on main thread. |
| + scoped_refptr<base::TaskRunner> task_runner_; |
| + // If |false|, then |networks_changed_observer_| is not notified. |
| + bool enable_notify_network_changed_; |
| + // Number of attempts to check that network has connected successfully. |
| + static const int kMaxAttempts = 100; |
| + // Delay between attempts to check that network has connected successfully. |
| + static const int kAttemptDelayMs = 100; |
| + // Delay after DHCP Renew to allow IP address to be acquired. |
| + static const int kDhcpRenewDelayMs = 5000; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(WiFiServiceImpl); |
| +}; |
| + |
| +WiFiServiceImpl::WiFiServiceImpl() |
| + : client_(NULL), enable_notify_network_changed_(true) {} |
| + |
| +WiFiServiceImpl::~WiFiServiceImpl() { CloseClientHandle(); } |
| + |
| +void WiFiServiceImpl::GetProperties(const std::string& network_guid, |
| + const NetworkPropertiesCallback& callback, |
| + const ErrorCallback& error_callback) { |
| + DWORD error = EnsureInitialized(); |
| + if (error == ERROR_SUCCESS) { |
| + NetworkList network_list; |
| + error = GetVisibleNetworkList(&network_list); |
| + if (error == ERROR_SUCCESS && !network_list.empty()) { |
| + NetworkList::const_iterator it = FindNetwork(network_list, network_guid); |
| + if (it != network_list.end()) { |
| + DLOG(INFO) << "Get Properties: " << network_guid << ":" |
| + << it->connection_state; |
| + callback.Run(network_guid, *it); |
| + return; |
| + } else { |
| + error = ERROR_NOT_FOUND; |
| + } |
| + } |
| + } |
| + |
| + CheckError(error_callback, kWiFiServiceError, error); |
| +} |
| + |
| +void WiFiServiceImpl::GetState(const std::string& network_guid, |
| + const NetworkPropertiesCallback& callback, |
| + const ErrorCallback& error_callback) { |
| + // This method is not implemented in first version as it is not used by |
| + // Google Cast extension. |
| + CheckError(error_callback, kWiFiServiceError, ERROR_CALL_NOT_IMPLEMENTED); |
| +} |
| + |
| +void WiFiServiceImpl::GetManagedProperties( |
| + const std::string& network_guid, |
| + const DictionaryResultCallback& callback, |
| + const ErrorCallback& error_callback) { |
| + // This method is not implemented in first version as it is not used by |
| + // Google Cast extension. |
| + CheckError(error_callback, kWiFiServiceError, ERROR_CALL_NOT_IMPLEMENTED); |
| +} |
| + |
| +void WiFiServiceImpl::SetProperties(const std::string& network_guid, |
| + const base::DictionaryValue& properties, |
| + const StringResultCallback& callback, |
| + const ErrorCallback& error_callback) { |
| + // This method is not implemented in first version as it is not used by |
| + // Google Cast extension. |
| + CheckError(error_callback, kWiFiServiceError, ERROR_CALL_NOT_IMPLEMENTED); |
| +} |
| + |
| +void WiFiServiceImpl::GetVisibleNetworks(const NetworkListCallback& callback, |
| + const ErrorCallback& error_callback) { |
| + DWORD error = EnsureInitialized(); |
| + |
| + 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); |
| + return; |
| + } |
| + } |
| + |
| + CheckError(error_callback, kWiFiServiceError, error); |
| +} |
| + |
| +void WiFiServiceImpl::RequestNetworkScan() { |
| + DWORD error = EnsureInitialized(); |
| + if (error == ERROR_SUCCESS) { |
| + WlanScan(client_, &interface_guid_, NULL, NULL, NULL); |
| + } |
| +} |
| + |
| +void WiFiServiceImpl::StartConnect(const std::string& network_guid, |
| + const StringResultCallback& callback, |
| + const ErrorCallback& error_callback) { |
| + DLOG(INFO) << "Start Connect: " << network_guid; |
| + DWORD error = EnsureInitialized(); |
| + if (error == ERROR_SUCCESS) { |
| + std::string connected_network_guid; |
| + error = SaveCurrentConnectedNetwork(&connected_network_guid); |
| + if (error == ERROR_SUCCESS) { |
| + // Connect only if network |network_guid| is not connected already. |
| + if (network_guid != connected_network_guid) |
| + 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()) { |
| + // Disable automatic network change notifications as they get fired |
| + // when network is just connected, but not yet accessible (doesn't |
| + // have valid IP address). |
| + enable_notify_network_changed_ = false; |
| + WaitForNetworkConnect(network_guid, 0); |
| + return; |
| + } |
| + } |
| + } |
| + } |
| + CheckError(error_callback, kWiFiServiceError, error); |
| +} |
| + |
| +void WiFiServiceImpl::StartDisconnect(const std::string& network_guid, |
| + const StringResultCallback& callback, |
| + const ErrorCallback& error_callback) { |
| + DLOG(INFO) << "Start Disconnect: " << network_guid; |
| + DWORD error = EnsureInitialized(); |
| + 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); |
| + NotifyNetworkChanged(network_guid); |
| + return; |
| + } |
| + } |
| + } |
| + CheckError(error_callback, kWiFiServiceError, error); |
| +} |
| + |
| +void WiFiServiceImpl::SetNetworksChangedObserver( |
| + const NetworkGuidListCallback& observer) { |
| + networks_changed_observer_ = observer; |
| +} |
| + |
| +void WiFiServiceImpl::SetNetworkListChangedObserver( |
| + const NetworkGuidListCallback& observer) { |
| + network_list_changed_observer_ = observer; |
| +} |
| + |
| +void WiFiServiceImpl::OnWlanNotificationCallback( |
| + PWLAN_NOTIFICATION_DATA wlan_notification_data, |
| + PVOID context) { |
| + WiFiServiceImpl* service = reinterpret_cast<WiFiServiceImpl*>(context); |
| + service->OnWlanNotification(wlan_notification_data); |
| +} |
| + |
| +void WiFiServiceImpl::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: |
| + case wlan_notification_acm_connection_attempt_fail: |
| + task_runner_->PostTask( |
| + FROM_HERE, |
| + base::Bind(&WiFiServiceImpl::NotifyNetworkChanged, |
| + base::Unretained(this), |
| + GUIDFromSSID(wlan_connection_data->dot11Ssid))); |
| + break; |
| + case wlan_notification_acm_scan_complete: |
| + task_runner_->PostTask( |
| + FROM_HERE, |
| + base::Bind(&WiFiServiceImpl::OnNetworkScanCompleteOnMainThread, |
| + base::Unretained(this))); |
| + break; |
| + } |
| +} |
| + |
| +void WiFiServiceImpl::OnNetworkScanCompleteOnMainThread() { |
| + NetworkList networks; |
| + // Get current list of visible networks and notify that network list has |
| + // changed. |
| + DWORD error = GetVisibleNetworkList(&networks); |
| + if (error == ERROR_SUCCESS) |
| + NotifyNetworkListChanged(networks); |
| +} |
| + |
| +void WiFiServiceImpl::WaitForNetworkConnect(const std::string& network_guid, |
| + int attempt) { |
| + // If network didn't get connected in |kMaxAttempts|, then restore automatic |
| + // network change notifications and stop waiting. |
| + if (attempt > kMaxAttempts) { |
| + enable_notify_network_changed_ = true; |
| + return; |
| + } |
| + std::string connected_network_guid; |
| + DWORD error = FindConnectedNetwork(&connected_network_guid); |
| + if (network_guid == connected_network_guid) { |
| + DLOG(INFO) << "WiFi Connected, Reset DHCP: " << network_guid; |
| + enable_notify_network_changed_ = true; |
| + // Even though wireless network is now connected, it may still be unusable, |
| + // e.g. after Chromecast device reset. Reset DHCP on wireless network to |
| + // work around this issue. |
| + error = ResetDHCP(); |
| + if (error == ERROR_SUCCESS) { |
| + // ResetDHCP has initiated IP address renewal, but may not have gotten |
| + // the address yet, so post network changed notification after |
| + // |kDhcpRenewDelayMs| delay. |
| + base::MessageLoop::current()->PostDelayedTask( |
| + FROM_HERE, |
| + base::Bind(&WiFiServiceImpl::NotifyNetworkChanged, |
| + base::Unretained(this), |
| + network_guid), |
| + base::TimeDelta::FromMilliseconds(kDhcpRenewDelayMs)); |
| + } else { |
| + 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 WiFiServiceImpl::CheckError(const ErrorCallback& error_callback, |
| + const std::string& error_name, |
| + DWORD error_code) const { |
| + if (error_code != ERROR_SUCCESS) { |
| + DLOG(ERROR) << "WiFiService Error " << error_code << ": " << error_name; |
| + 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; |
| +} |
| + |
| +WiFiService::NetworkList::const_iterator WiFiServiceImpl::FindNetwork( |
| + const NetworkList& networks, |
| + const std::string& network_guid) const { |
| + for (NetworkList::const_iterator it = networks.begin(); it != networks.end(); |
| + ++it) { |
| + if (it->guid == network_guid) |
| + return it; |
| + } |
| + return networks.end(); |
| +} |
| + |
| +DWORD WiFiServiceImpl::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 WiFiServiceImpl::SortNetworks(NetworkList* networks) { |
| + networks->sort(NetworkProperties::OrderByType); |
| +} |
| + |
| +DWORD WiFiServiceImpl::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 interface_list = NULL; |
| + if (error == ERROR_SUCCESS) { |
| + // Enumerate wireless interfaces. |
| + error = ::WlanEnumInterfaces(client_, NULL, &interface_list); |
| + if (error == ERROR_SUCCESS) { |
| + if (interface_list != NULL && interface_list->dwNumberOfItems != 0) { |
| + // Remember first interface just in case if none are connected. |
|
Jói
2013/10/21 16:44:03
nit: in case if none -> in case none
|
| + interface_guid_ = interface_list->InterfaceInfo[0].InterfaceGuid; |
| + // Try to find a connected interface. |
| + for (DWORD itf = 0; itf < interface_list->dwNumberOfItems; ++itf) { |
| + if (interface_list->InterfaceInfo[itf].isState == |
| + wlan_interface_state_connected) { |
| + // Found connected interface, remember it! |
| + interface_guid_ = interface_list->InterfaceInfo[itf].InterfaceGuid; |
| + break; |
| + } |
| + } |
| + ::WlanRegisterNotification(client_, |
| + WLAN_NOTIFICATION_SOURCE_ALL, |
| + FALSE, |
| + OnWlanNotificationCallback, |
| + this, |
| + NULL, |
| + NULL); |
| + } else { |
| + error = ERROR_NOINTERFACE; |
| + } |
| + } |
| + // Clean up. |
| + if (interface_list != NULL) |
| + ::WlanFreeMemory(interface_list); |
| + } |
| + return error; |
| +} |
| + |
| +DWORD WiFiServiceImpl::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 WiFiServiceImpl::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 (int 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 WiFiServiceImpl::EnsureInitialized() { |
| + DCHECK(CalledOnValidThread()); |
| + if (client_ != NULL) |
| + return ERROR_SUCCESS; |
| + if (task_runner_.get() == NULL) |
| + task_runner_ = base::MessageLoopProxy::current(); |
| + |
| + return OpenClientHandle(); |
| +} |
| + |
| +DWORD WiFiServiceImpl::CloseClientHandle() { |
| + DWORD error = ERROR_SUCCESS; |
| + if (client_ != NULL) { |
| + error = ::WlanCloseHandle(client_, NULL); |
| + client_ = NULL; |
| + } |
| + return error; |
| +} |
| + |
| +DOT11_SSID WiFiServiceImpl::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); |
| + } else { |
| + NOTREACHED(); |
| + } |
| + return ssid; |
| +} |
| + |
| +std::string WiFiServiceImpl::SecurityFromDot11AuthAlg( |
| + DOT11_AUTH_ALGORITHM alg) const { |
| + switch (alg) { |
| + case DOT11_AUTH_ALGO_RSNA: |
| + return onc::wifi::kWPA_EAP; |
| + case DOT11_AUTH_ALGO_RSNA_PSK: |
| + return onc::wifi::kWPA_PSK; |
| + case DOT11_AUTH_ALGO_80211_SHARED_KEY: |
| + return onc::wifi::kWEP_PSK; |
| + case DOT11_AUTH_ALGO_80211_OPEN: |
| + return onc::wifi::kNone; |
| + default: |
| + return onc::wifi::kWPA_EAP; |
| + } |
| +} |
| + |
| +void WiFiServiceImpl::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 = onc::connection_state::kConnected; |
| + } else { |
| + properties->connection_state = onc::connection_state::kNotConnected; |
| + } |
| + |
| + properties->ssid = SSIDFromWLAN(wlan); |
| + properties->name = properties->ssid; |
| + properties->guid = GUIDFromWLAN(wlan); |
| + properties->type = onc::network_type::kWiFi; |
| + |
| + 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 = 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 WiFiServiceImpl::GetVisibleNetworkList(NetworkList* network_list) { |
| + if (client_ == NULL) { |
| + NOTREACHED(); |
| + return ERROR_NOINTERFACE; |
| + } |
| + |
| + DWORD error = ERROR_SUCCESS; |
| + PWLAN_AVAILABLE_NETWORK_LIST available_network_list = NULL; |
| + PWLAN_BSS_LIST bss_list = NULL; |
| + |
| + error = ::WlanGetAvailableNetworkList( |
| + client_, &interface_guid_, 0, NULL, &available_network_list); |
| + |
| + if (error == ERROR_SUCCESS && NULL != available_network_list) { |
| + // TODO(mef): WlanGetNetworkBssList is not available on XP. If XP support is |
| + // needed, then different method of getting BSS (e.g. OID query) will have |
| + // to be used. |
| + error = ::WlanGetNetworkBssList(client_, |
| + &interface_guid_, |
| + NULL, |
| + dot11_BSS_type_any, |
| + FALSE, |
| + NULL, |
| + &bss_list); |
| + if (error == ERROR_SUCCESS && NULL != bss_list) { |
| + for (DWORD i = 0; i < available_network_list->dwNumberOfItems; ++i) { |
| + network_list->push_back(NetworkProperties()); |
| + NetworkPropertiesFromAvailableNetwork( |
| + available_network_list->Network[i], |
| + *bss_list, |
| + &network_list->back()); |
| + } |
| + } |
| + } |
| + |
| + // clean up |
| + if (available_network_list != NULL) { |
| + ::WlanFreeMemory(available_network_list); |
| + } |
| + if (bss_list != NULL) { |
| + ::WlanFreeMemory(bss_list); |
| + } |
| + return error; |
| +} |
| + |
| +// Find currently connected network. |
| +DWORD WiFiServiceImpl::FindConnectedNetwork( |
| + std::string* connected_network_guid) { |
| + if (client_ == NULL) { |
| + NOTREACHED(); |
| + return ERROR_NOINTERFACE; |
| + } |
| + |
| + DWORD error = ERROR_SUCCESS; |
| + PWLAN_AVAILABLE_NETWORK_LIST available_network_list = NULL; |
| + error = ::WlanGetAvailableNetworkList( |
| + client_, &interface_guid_, 0, NULL, &available_network_list); |
| + |
| + if (error == ERROR_SUCCESS && NULL != available_network_list) { |
| + for (DWORD i = 0; i < available_network_list->dwNumberOfItems; ++i) { |
| + const WLAN_AVAILABLE_NETWORK& wlan = available_network_list->Network[i]; |
| + if (wlan.dwFlags & WLAN_AVAILABLE_NETWORK_CONNECTED) { |
| + *connected_network_guid = GUIDFromWLAN(wlan); |
| + break; |
| + } |
| + } |
| + } |
| + |
| + // clean up |
| + if (available_network_list != NULL) { |
| + ::WlanFreeMemory(available_network_list); |
| + } |
| + |
| + return error; |
| +} |
| + |
| +DWORD WiFiServiceImpl::Connect(const std::string& network_guid) { |
| + if (client_ == NULL) { |
| + NOTREACHED(); |
| + return ERROR_NOINTERFACE; |
| + } |
| + |
| + DWORD error = ERROR_SUCCESS; |
| + 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 { |
| + // TODO(mef): wlan_connection_mode_discovery_unsecure is not available on |
| + // XP. If XP support is needed, then temporary profile will have to be |
| + // created. |
| + 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 WiFiServiceImpl::Disconnect() { |
| + if (client_ == NULL) { |
| + NOTREACHED(); |
| + return ERROR_NOINTERFACE; |
| + } |
| + |
| + DWORD error = ERROR_SUCCESS; |
| + error = ::WlanDisconnect(client_, &interface_guid_, NULL); |
| + return error; |
| +} |
| + |
| +DWORD WiFiServiceImpl::SaveTempProfile(const std::string& network_guid) { |
| + if (client_ == NULL) { |
| + NOTREACHED(); |
| + return ERROR_NOINTERFACE; |
| + } |
| + |
| + DWORD error = ERROR_SUCCESS; |
| + base::string16 profile_name = ProfileNameFromGUID(network_guid); |
| + // TODO(mef): WlanSaveTemporaryProfile is not available on XP. If XP support |
| + // is needed, then different method of saving network profile will have to be |
| + // used. |
| + error = ::WlanSaveTemporaryProfile(client_, |
| + &interface_guid_, |
| + profile_name.c_str(), |
| + NULL, |
| + WLAN_PROFILE_USER, |
| + true, |
| + NULL); |
| + return error; |
| +} |
| + |
| +DWORD WiFiServiceImpl::GetProfile(const std::string& network_guid, |
| + std::string* profile_xml) { |
| + if (client_ == NULL) { |
| + NOTREACHED(); |
| + return ERROR_NOINTERFACE; |
| + } |
| + |
| + DWORD error = ERROR_SUCCESS; |
| + 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 WiFiServiceImpl::HaveProfile(const std::string& network_guid) { |
| + DWORD error = ERROR_SUCCESS; |
| + std::string profile_xml; |
| + return GetProfile(network_guid, &profile_xml) == ERROR_SUCCESS; |
| +} |
| + |
| +void WiFiServiceImpl::NotifyNetworkListChanged(const NetworkList& networks) { |
| + if (network_list_changed_observer_.is_null()) |
| + return; |
| + |
| + NetworkGuidList current_networks; |
| + for (NetworkList::const_iterator it = networks.begin(); |
| + it != networks.end(); |
| + ++it) { |
| + current_networks.push_back(it->guid); |
| + } |
| + network_list_changed_observer_.Run(current_networks); |
| +} |
| + |
| +void WiFiServiceImpl::NotifyNetworkChanged(const std::string& network_guid) { |
| + if (enable_notify_network_changed_ && !networks_changed_observer_.is_null()) { |
| + DLOG(INFO) << "NotifyNetworkChanged: " << network_guid; |
| + NetworkGuidList changed_networks(1, network_guid); |
| + networks_changed_observer_.Run(changed_networks); |
| + } |
| +} |
| + |
| +WiFiService* WiFiService::CreateService() { return new WiFiServiceImpl(); } |
| + |
| +} // namespace wifi |