| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "components/wifi/wifi_service.h" | 5 #include "components/wifi/wifi_service.h" |
| 6 | 6 |
| 7 #include <iphlpapi.h> | 7 #include <iphlpapi.h> |
| 8 #include <objbase.h> | 8 #include <objbase.h> |
| 9 #include <wlanapi.h> | 9 #include <wlanapi.h> |
| 10 | 10 |
| 11 #include <set> | 11 #include <set> |
| 12 | 12 |
| 13 #include "base/base_paths_win.h" | 13 #include "base/base_paths_win.h" |
| 14 #include "base/bind.h" | 14 #include "base/bind.h" |
| 15 #include "base/files/file_path.h" | 15 #include "base/files/file_path.h" |
| 16 #include "base/memory/ref_counted.h" | 16 #include "base/memory/ref_counted.h" |
| 17 #include "base/message_loop/message_loop.h" | 17 #include "base/message_loop/message_loop.h" |
| 18 #include "base/path_service.h" | 18 #include "base/path_service.h" |
| 19 #include "base/strings/string16.h" | 19 #include "base/strings/string16.h" |
| 20 #include "base/strings/string_util.h" | 20 #include "base/strings/string_util.h" |
| 21 #include "base/strings/utf_string_conversions.h" | 21 #include "base/strings/utf_string_conversions.h" |
| 22 #include "base/win/registry.h" | 22 #include "base/win/registry.h" |
| 23 #include "components/onc/onc_constants.h" | 23 #include "components/onc/onc_constants.h" |
| 24 #include "third_party/libxml/chromium/libxml_utils.h" | 24 #include "third_party/libxml/chromium/libxml_utils.h" |
| 25 | 25 |
| 26 namespace { | 26 namespace { |
| 27 const char kWiFiServiceError[] = "Error.WiFiService"; | 27 const char kWiFiServiceError[] = "Error.WiFiService"; |
| 28 const char kWiFiServiceErrorNotImplemented[] = |
| 29 "Error.WiFiService.NotImplemented"; |
| 28 const wchar_t kNwCategoryWizardRegKey[] = | 30 const wchar_t kNwCategoryWizardRegKey[] = |
| 29 L"Software\\Microsoft\\Windows NT\\CurrentVersion\\Network\\" | 31 L"Software\\Microsoft\\Windows NT\\CurrentVersion\\Network\\" |
| 30 L"NwCategoryWizard"; | 32 L"NwCategoryWizard"; |
| 31 const wchar_t kNwCategoryWizardRegValue[] = L"Show"; | 33 const wchar_t kNwCategoryWizardRegValue[] = L"Show"; |
| 32 const wchar_t kNwCategoryWizardSavedRegValue[] = L"ShowSaved"; | 34 const wchar_t kNwCategoryWizardSavedRegValue[] = L"ShowSaved"; |
| 33 const wchar_t kNwCategoryWizardDeleteRegValue[] = L"ShowDelete"; | 35 const wchar_t kNwCategoryWizardDeleteRegValue[] = L"ShowDelete"; |
| 34 const wchar_t kWlanApiDll[] = L"wlanapi.dll"; | 36 const wchar_t kWlanApiDll[] = L"wlanapi.dll"; |
| 35 | 37 |
| 36 // WlanApi function names | 38 // WlanApi function names |
| 37 const char kWlanConnect[] = "WlanConnect"; | 39 const char kWlanConnect[] = "WlanConnect"; |
| (...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 201 base::ListValue* network_list) OVERRIDE; | 203 base::ListValue* network_list) OVERRIDE; |
| 202 | 204 |
| 203 virtual void RequestNetworkScan() OVERRIDE; | 205 virtual void RequestNetworkScan() OVERRIDE; |
| 204 | 206 |
| 205 virtual void StartConnect(const std::string& network_guid, | 207 virtual void StartConnect(const std::string& network_guid, |
| 206 std::string* error) OVERRIDE; | 208 std::string* error) OVERRIDE; |
| 207 | 209 |
| 208 virtual void StartDisconnect(const std::string& network_guid, | 210 virtual void StartDisconnect(const std::string& network_guid, |
| 209 std::string* error) OVERRIDE; | 211 std::string* error) OVERRIDE; |
| 210 | 212 |
| 213 virtual void GetPassphraseFromSystem(const std::string& network_guid, |
| 214 std::string* passphrase, |
| 215 std::string* error) OVERRIDE; |
| 216 |
| 211 virtual void SetEventObservers( | 217 virtual void SetEventObservers( |
| 212 scoped_refptr<base::MessageLoopProxy> message_loop_proxy, | 218 scoped_refptr<base::MessageLoopProxy> message_loop_proxy, |
| 213 const NetworkGuidListCallback& networks_changed_observer, | 219 const NetworkGuidListCallback& networks_changed_observer, |
| 214 const NetworkGuidListCallback& network_list_changed_observer) OVERRIDE; | 220 const NetworkGuidListCallback& network_list_changed_observer) OVERRIDE; |
| 215 | 221 |
| 216 virtual void RequestConnectedNetworkUpdate() OVERRIDE {} | 222 virtual void RequestConnectedNetworkUpdate() OVERRIDE {} |
| 217 | 223 |
| 218 private: | 224 private: |
| 219 // Static callback for Windows WLAN_NOTIFICATION. Calls OnWlanNotification | 225 // Static callback for Windows WLAN_NOTIFICATION. Calls OnWlanNotification |
| 220 // on WiFiServiceImpl passed back as |context|. | 226 // on WiFiServiceImpl passed back as |context|. |
| (...skipping 409 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 630 error_code = Disconnect(); | 636 error_code = Disconnect(); |
| 631 if (error_code == ERROR_SUCCESS) { | 637 if (error_code == ERROR_SUCCESS) { |
| 632 NotifyNetworkChanged(network_guid); | 638 NotifyNetworkChanged(network_guid); |
| 633 return; | 639 return; |
| 634 } | 640 } |
| 635 } | 641 } |
| 636 } | 642 } |
| 637 CheckError(error_code, kWiFiServiceError, error); | 643 CheckError(error_code, kWiFiServiceError, error); |
| 638 } | 644 } |
| 639 | 645 |
| 646 void WiFiServiceImpl::GetPassphraseFromSystem(const std::string& network_guid, |
| 647 std::string* passphrase, |
| 648 std::string* error) { |
| 649 DWORD error_code = EnsureInitialized(); |
| 650 if (CheckError(error_code, kWiFiServiceError, error)) |
| 651 return; |
| 652 |
| 653 std::string profile_xml; |
| 654 error_code = GetProfile(network_guid, &profile_xml); |
| 655 if (CheckError(error_code, kWiFiServiceError, error)) |
| 656 return; |
| 657 |
| 658 // Quick check to verify presence of <sharedKey> element. |
| 659 if (profile_xml.find("<sharedKey>") == std::string::npos) { |
| 660 *error = kWiFiServiceError; |
| 661 return; |
| 662 } |
| 663 |
| 664 XmlReader reader; |
| 665 if (reader.Load(profile_xml)) { |
| 666 while(reader.Read()) { |
| 667 reader.SkipToElement(); |
| 668 if (reader.NodeName() == "keyMaterial") { |
| 669 reader.ReadElementContent(passphrase); |
| 670 return; |
| 671 } |
| 672 } |
| 673 } |
| 674 // Did not find passphrase in the profile. |
| 675 *error = kWiFiServiceError; |
| 676 } |
| 677 |
| 640 void WiFiServiceImpl::SetEventObservers( | 678 void WiFiServiceImpl::SetEventObservers( |
| 641 scoped_refptr<base::MessageLoopProxy> message_loop_proxy, | 679 scoped_refptr<base::MessageLoopProxy> message_loop_proxy, |
| 642 const NetworkGuidListCallback& networks_changed_observer, | 680 const NetworkGuidListCallback& networks_changed_observer, |
| 643 const NetworkGuidListCallback& network_list_changed_observer) { | 681 const NetworkGuidListCallback& network_list_changed_observer) { |
| 644 message_loop_proxy_.swap(message_loop_proxy); | 682 message_loop_proxy_.swap(message_loop_proxy); |
| 645 networks_changed_observer_ = networks_changed_observer; | 683 networks_changed_observer_ = networks_changed_observer; |
| 646 network_list_changed_observer_ = network_list_changed_observer; | 684 network_list_changed_observer_ = network_list_changed_observer; |
| 647 } | 685 } |
| 648 | 686 |
| 649 void WiFiServiceImpl::OnWlanNotificationCallback( | 687 void WiFiServiceImpl::OnWlanNotificationCallback( |
| (...skipping 793 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1443 | 1481 |
| 1444 DWORD WiFiServiceImpl::GetProfile(const std::string& network_guid, | 1482 DWORD WiFiServiceImpl::GetProfile(const std::string& network_guid, |
| 1445 std::string* profile_xml) { | 1483 std::string* profile_xml) { |
| 1446 if (client_ == NULL) { | 1484 if (client_ == NULL) { |
| 1447 NOTREACHED(); | 1485 NOTREACHED(); |
| 1448 return ERROR_NOINTERFACE; | 1486 return ERROR_NOINTERFACE; |
| 1449 } | 1487 } |
| 1450 | 1488 |
| 1451 DWORD error = ERROR_SUCCESS; | 1489 DWORD error = ERROR_SUCCESS; |
| 1452 base::string16 profile_name = ProfileNameFromGUID(network_guid); | 1490 base::string16 profile_name = ProfileNameFromGUID(network_guid); |
| 1491 DWORD flags = WLAN_PROFILE_GET_PLAINTEXT_KEY; |
| 1453 LPWSTR str_profile_xml = NULL; | 1492 LPWSTR str_profile_xml = NULL; |
| 1454 error = WlanGetProfile_function_(client_, | 1493 error = WlanGetProfile_function_(client_, |
| 1455 &interface_guid_, | 1494 &interface_guid_, |
| 1456 profile_name.c_str(), | 1495 profile_name.c_str(), |
| 1457 NULL, | 1496 NULL, |
| 1458 &str_profile_xml, | 1497 &str_profile_xml, |
| 1459 NULL, | 1498 &flags, |
| 1460 NULL); | 1499 NULL); |
| 1461 | 1500 |
| 1462 if (error == ERROR_SUCCESS && str_profile_xml != NULL) { | 1501 if (error == ERROR_SUCCESS && str_profile_xml != NULL) { |
| 1463 *profile_xml = base::UTF16ToUTF8(str_profile_xml); | 1502 *profile_xml = base::UTF16ToUTF8(str_profile_xml); |
| 1464 } | 1503 } |
| 1465 // Clean up. | 1504 // Clean up. |
| 1466 if (str_profile_xml != NULL) { | 1505 if (str_profile_xml != NULL) { |
| 1467 WlanFreeMemory_function_(str_profile_xml); | 1506 WlanFreeMemory_function_(str_profile_xml); |
| 1468 } | 1507 } |
| 1469 | 1508 |
| (...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1580 NetworkGuidList changed_networks(1, network_guid); | 1619 NetworkGuidList changed_networks(1, network_guid); |
| 1581 message_loop_proxy_->PostTask( | 1620 message_loop_proxy_->PostTask( |
| 1582 FROM_HERE, | 1621 FROM_HERE, |
| 1583 base::Bind(networks_changed_observer_, changed_networks)); | 1622 base::Bind(networks_changed_observer_, changed_networks)); |
| 1584 } | 1623 } |
| 1585 } | 1624 } |
| 1586 | 1625 |
| 1587 WiFiService* WiFiService::Create() { return new WiFiServiceImpl(); } | 1626 WiFiService* WiFiService::Create() { return new WiFiServiceImpl(); } |
| 1588 | 1627 |
| 1589 } // namespace wifi | 1628 } // namespace wifi |
| OLD | NEW |