Chromium Code Reviews| Index: components/wifi/wifi_service_win.cc |
| diff --git a/components/wifi/wifi_service_win.cc b/components/wifi/wifi_service_win.cc |
| index 1280b4944ec666593db09375b87ffb98890820c7..f1341a7357a8a13effef14aac98e18b6c0fc1ed2 100644 |
| --- a/components/wifi/wifi_service_win.cc |
| +++ b/components/wifi/wifi_service_win.cc |
| @@ -35,6 +35,10 @@ const wchar_t kNwCategoryWizardSavedRegValue[] = L"ShowSaved"; |
| const wchar_t kNwCategoryWizardDeleteRegValue[] = L"ShowDelete"; |
| const wchar_t kWlanApiDll[] = L"wlanapi.dll"; |
| +// TKIP Profile Dictionary keys |
| +const char kProfileXmlKey[] = "profile"; |
| +const char kSharedProfileKey[] = "shared"; |
| + |
| // WlanApi function names |
| const char kWlanConnect[] = "WlanConnect"; |
| const char kWlanCloseHandle[] = "WlanCloseHandle"; |
| @@ -222,6 +226,13 @@ class WiFiServiceImpl : public WiFiService { |
| virtual void RequestConnectedNetworkUpdate() OVERRIDE {} |
| private: |
| + typedef int32 EncryptionType; |
| + enum EncryptionTypeEnum { |
| + kEncryptionTypeAny = 0, |
| + kEncryptionTypeAES = 1, |
| + kEncryptionTypeTKIP = 2 |
| + }; |
| + |
| // Static callback for Windows WLAN_NOTIFICATION. Calls OnWlanNotification |
| // on WiFiServiceImpl passed back as |context|. |
| static void __stdcall OnWlanNotificationCallback( |
| @@ -315,6 +326,7 @@ class WiFiServiceImpl : public WiFiService { |
| // Deduce WLANProfile |authEncryption| values from |onc::wifi| security. |
| bool AuthEncryptionFromSecurity(const std::string& security, |
| + EncryptionType encryption_type, |
| std::string* authentication, |
| std::string* encryption, |
| std::string* key_type) const; |
| @@ -358,8 +370,11 @@ class WiFiServiceImpl : public WiFiService { |
| // Normalizes |frequency_in_mhz| into one of |Frequency| values. |
| Frequency GetNormalizedFrequency(int frequency_in_mhz) const; |
| - // Create |profile_xml| based on |network_properties|. |
| + // Create |profile_xml| based on |network_properties|. If |encryptio_type| |
|
afontan
2014/03/17 19:56:55
typo encryptio_type
mef
2014/03/17 21:40:09
Done.
|
| + // is |kEncryptionTypeAny| applies the type most suitable for parameters in |
| + // |network_properties|. |
| bool CreateProfile(const NetworkProperties& network_properties, |
| + EncryptionType encryption_type, |
| std::string* profile_xml); |
| // Save temporary wireless profile for |network_guid|. |
| @@ -372,6 +387,10 @@ class WiFiServiceImpl : public WiFiService { |
| bool get_plaintext_key, |
| std::string* profile_xml); |
| + // Set |profile_xml| to current user or all users depending on |shared| flag. |
| + // If |overwrite| is false, then returns an error if profile exists. |
| + DWORD SetProfile(bool shared, const std::string& profile_xml, bool overwrite); |
| + |
| // Return true if there is previously stored profile xml for |network_guid|. |
| bool HaveProfile(const std::string& network_guid); |
| @@ -415,6 +434,9 @@ class WiFiServiceImpl : public WiFiService { |
| base::DictionaryValue connect_properties_; |
| // Preserved WLAN profile xml. |
| std::map<std::string, std::string> saved_profiles_xml_; |
| + // WLAN profile xml with TKIP encryption type saved by |CreateNetwork| |
| + // indexed by |network_guid|. Persist only in memory. |
| + base::DictionaryValue tkip_profiles_; |
| // 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). |
| @@ -536,28 +558,36 @@ void WiFiServiceImpl::CreateNetwork( |
| network_properties.guid = network_properties.ssid; |
| std::string profile_xml; |
| - if (!CreateProfile(network_properties, &profile_xml)) { |
| + if (!CreateProfile(network_properties, kEncryptionTypeAny, &profile_xml)) { |
| CheckError(ERROR_INVALID_DATA, kWiFiServiceError, error); |
| return; |
| } |
| - base::string16 profile_xml16(base::UTF8ToUTF16(profile_xml)); |
| - DWORD reason_code = 0u; |
| - |
| - error_code = WlanSetProfile_function_(client_, |
| - &interface_guid_, |
| - shared ? 0 : WLAN_PROFILE_USER, |
| - profile_xml16.c_str(), |
| - NULL, |
| - FALSE, |
| - NULL, |
| - &reason_code); |
| + error_code = SetProfile(shared, profile_xml, false); |
| if (CheckError(error_code, kWiFiServiceError, error)) { |
| DVLOG(0) << profile_xml; |
| - DVLOG(0) << "SetProfile Reason Code:" << reason_code; |
| return; |
| } |
| + // WAP and WAP2 networks could use either AES or TKIP encryption type. |
| + // Preserve alternative profile to use in case if connection with default |
| + // encryption type fails. |
| + std::string tkip_profile_xml; |
| + if (!CreateProfile(network_properties, |
| + kEncryptionTypeTKIP, |
| + &tkip_profile_xml)) { |
| + CheckError(ERROR_INVALID_DATA, kWiFiServiceError, error); |
| + return; |
| + } |
| + |
| + if (tkip_profile_xml != profile_xml) { |
| + scoped_ptr<base::DictionaryValue> tkip_profile(new base::DictionaryValue()); |
| + tkip_profile->SetString(kProfileXmlKey, tkip_profile_xml); |
| + tkip_profile->SetBoolean(kSharedProfileKey, shared); |
| + tkip_profiles_.SetWithoutPathExpansion(network_properties.guid, |
| + tkip_profile.release()); |
| + } |
| + |
| *network_guid = network_properties.guid; |
| } |
| @@ -784,8 +814,28 @@ void WiFiServiceImpl::WaitForNetworkConnect(const std::string& network_guid, |
| if (attempt > kMaxAttempts) { |
| DLOG(ERROR) << kMaxAttempts << " attempts exceeded waiting for connect to " |
| << network_guid; |
| - enable_notify_network_changed_ = true; |
| - RestoreNwCategoryWizard(); |
| + |
| + // Check, if there is alternative TKIP profile that should be tried. |
| + // If there is, then set it up and try to connect again. |
| + const base::DictionaryValue* tkip_profile = NULL; |
| + if (tkip_profiles_.GetDictionaryWithoutPathExpansion( |
| + network_guid, &tkip_profile)) { |
| + std::string tkip_profile_xml; |
| + bool shared = false; |
| + tkip_profile->GetString(kProfileXmlKey, &tkip_profile_xml); |
| + tkip_profile->GetBoolean(kSharedProfileKey, &shared); |
| + DWORD error_code = SetProfile(shared, tkip_profile_xml, true); |
| + if (error_code == ERROR_SUCCESS) { |
| + // Try to connect with new profile. |
| + error_code = Connect(network_guid, GetFrequencyToConnect(network_guid)); |
| + if (error_code == ERROR_SUCCESS) |
| + WaitForNetworkConnect(network_guid, 0); |
| + } |
| + tkip_profiles_.RemoveWithoutPathExpansion(network_guid, NULL); |
| + } else { |
| + enable_notify_network_changed_ = true; |
| + RestoreNwCategoryWizard(); |
| + } |
| return; |
| } |
| std::string connected_network_guid; |
| @@ -796,6 +846,8 @@ void WiFiServiceImpl::WaitForNetworkConnect(const std::string& network_guid, |
| // e.g. after Chromecast device reset. Reset DHCP on wireless network to |
| // work around this issue. |
| error = ResetDHCP(); |
| + // There is no need to keep TKIP profile as network is connected. |
| + tkip_profiles_.RemoveWithoutPathExpansion(network_guid, NULL); |
| // Restore previously suppressed notifications. |
| enable_notify_network_changed_ = true; |
| RestoreNwCategoryWizard(); |
| @@ -1552,6 +1604,25 @@ DWORD WiFiServiceImpl::GetProfile(const std::string& network_guid, |
| return error; |
| } |
| +DWORD WiFiServiceImpl::SetProfile(bool shared, |
| + const std::string& profile_xml, |
| + bool overwrite) { |
| + DWORD error_code = ERROR_SUCCESS; |
| + |
| + base::string16 profile_xml16(base::UTF8ToUTF16(profile_xml)); |
| + DWORD reason_code = 0u; |
| + |
| + error_code = WlanSetProfile_function_(client_, |
| + &interface_guid_, |
| + shared ? 0 : WLAN_PROFILE_USER, |
| + profile_xml16.c_str(), |
| + NULL, |
| + overwrite, |
| + NULL, |
| + &reason_code); |
|
afontan
2014/03/17 19:56:55
Why WlanSetProfile instead of WlanSaveTemporaryPro
mef
2014/03/17 21:40:09
Hmm, no apparently good reason. WlanSaveTemporaryP
|
| + return error_code; |
| +} |
| + |
| bool WiFiServiceImpl::HaveProfile(const std::string& network_guid) { |
| DWORD error = ERROR_SUCCESS; |
| std::string profile_xml; |
| @@ -1560,6 +1631,7 @@ bool WiFiServiceImpl::HaveProfile(const std::string& network_guid) { |
| bool WiFiServiceImpl::AuthEncryptionFromSecurity( |
| const std::string& security, |
| + EncryptionType encryption_type, |
| std::string* authentication, |
| std::string* encryption, |
| std::string* key_type) const { |
| @@ -1572,15 +1644,19 @@ bool WiFiServiceImpl::AuthEncryptionFromSecurity( |
| *key_type = kKeyTypeNetwork; |
| } else if (security == onc::wifi::kWPA_PSK) { |
| *authentication = kAuthenticationWpaPsk; |
| - // TODO(mef): WAP |encryption| could be either |AES| or |TKIP|. It has to be |
| - // determined and adjusted properly during |Connect|. |
| - *encryption = kEncryptionAES; |
| + if (encryption_type == kEncryptionTypeTKIP) { |
| + *encryption = kEncryptionTKIP; |
| + } else { |
| + *encryption = kEncryptionAES; |
| + } |
| *key_type = kKeyTypePassphrase; |
| } else if (security == onc::wifi::kWPA2_PSK) { |
| *authentication = kAuthenticationWpa2Psk; |
| - // TODO(mef): WAP |encryption| could be either |AES| or |TKIP|. It has to be |
| - // determined and adjusted properly during |Connect|. |
| - *encryption = kEncryptionAES; |
| + if (encryption_type == kEncryptionTypeTKIP) { |
| + *encryption = kEncryptionTKIP; |
| + } else { |
| + *encryption = kEncryptionAES; |
| + } |
| *key_type = kKeyTypePassphrase; |
| } else { |
| return false; |
| @@ -1590,12 +1666,14 @@ bool WiFiServiceImpl::AuthEncryptionFromSecurity( |
| bool WiFiServiceImpl::CreateProfile( |
| const NetworkProperties& network_properties, |
| + EncryptionType encryption_type, |
| std::string* profile_xml) { |
| // Get authentication and encryption values from security. |
| std::string authentication; |
| std::string encryption; |
| std::string key_type; |
| bool valid = AuthEncryptionFromSecurity(network_properties.security, |
| + encryption_type, |
| &authentication, |
| &encryption, |
| &key_type); |