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 private: | 222 private: |
217 // Static callback for Windows WLAN_NOTIFICATION. Calls OnWlanNotification | 223 // Static callback for Windows WLAN_NOTIFICATION. Calls OnWlanNotification |
218 // on WiFiServiceImpl passed back as |context|. | 224 // on WiFiServiceImpl passed back as |context|. |
219 static void __stdcall OnWlanNotificationCallback( | 225 static void __stdcall OnWlanNotificationCallback( |
220 PWLAN_NOTIFICATION_DATA wlan_notification_data, | 226 PWLAN_NOTIFICATION_DATA wlan_notification_data, |
(...skipping 407 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
628 error_code = Disconnect(); | 634 error_code = Disconnect(); |
629 if (error_code == ERROR_SUCCESS) { | 635 if (error_code == ERROR_SUCCESS) { |
630 NotifyNetworkChanged(network_guid); | 636 NotifyNetworkChanged(network_guid); |
631 return; | 637 return; |
632 } | 638 } |
633 } | 639 } |
634 } | 640 } |
635 CheckError(error_code, kWiFiServiceError, error); | 641 CheckError(error_code, kWiFiServiceError, error); |
636 } | 642 } |
637 | 643 |
| 644 void WiFiServiceImpl::GetPassphraseFromSystem(const std::string& network_guid, |
| 645 std::string* passphrase, |
| 646 std::string* error) { |
| 647 DWORD error_code = EnsureInitialized(); |
| 648 if (CheckError(error_code, kWiFiServiceError, error)) |
| 649 return; |
| 650 |
| 651 std::string profile_xml; |
| 652 error_code = GetProfile(network_guid, &profile_xml); |
| 653 if (CheckError(error_code, kWiFiServiceError, error)) |
| 654 return; |
| 655 |
| 656 // Quick check to verify presence of <sharedKey> element. |
| 657 if (profile_xml.find("<sharedKey>") == std::string::npos) { |
| 658 *error = kWiFiServiceError; |
| 659 return; |
| 660 } |
| 661 |
| 662 XmlReader reader; |
| 663 if (reader.Load(profile_xml)) { |
| 664 while(reader.Read()) { |
| 665 reader.SkipToElement(); |
| 666 if (reader.NodeName() == "keyMaterial") { |
| 667 reader.ReadElementContent(passphrase); |
| 668 return; |
| 669 } |
| 670 } |
| 671 } |
| 672 // Did not find passphrase in the profile. |
| 673 *error = kWiFiServiceError; |
| 674 } |
| 675 |
638 void WiFiServiceImpl::SetEventObservers( | 676 void WiFiServiceImpl::SetEventObservers( |
639 scoped_refptr<base::MessageLoopProxy> message_loop_proxy, | 677 scoped_refptr<base::MessageLoopProxy> message_loop_proxy, |
640 const NetworkGuidListCallback& networks_changed_observer, | 678 const NetworkGuidListCallback& networks_changed_observer, |
641 const NetworkGuidListCallback& network_list_changed_observer) { | 679 const NetworkGuidListCallback& network_list_changed_observer) { |
642 message_loop_proxy_.swap(message_loop_proxy); | 680 message_loop_proxy_.swap(message_loop_proxy); |
643 networks_changed_observer_ = networks_changed_observer; | 681 networks_changed_observer_ = networks_changed_observer; |
644 network_list_changed_observer_ = network_list_changed_observer; | 682 network_list_changed_observer_ = network_list_changed_observer; |
645 } | 683 } |
646 | 684 |
647 void WiFiServiceImpl::OnWlanNotificationCallback( | 685 void WiFiServiceImpl::OnWlanNotificationCallback( |
(...skipping 795 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 |