OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 #import <netinet/in.h> | 7 #import <netinet/in.h> |
8 #import <CoreWLAN/CoreWLAN.h> | 8 #import <CoreWLAN/CoreWLAN.h> |
9 #import <SystemConfiguration/SystemConfiguration.h> | 9 #import <SystemConfiguration/SystemConfiguration.h> |
10 | 10 |
11 #include "base/bind.h" | 11 #include "base/bind.h" |
| 12 #include "base/mac/foundation_util.h" |
12 #include "base/mac/scoped_cftyperef.h" | 13 #include "base/mac/scoped_cftyperef.h" |
13 #include "base/mac/scoped_nsobject.h" | 14 #include "base/mac/scoped_nsobject.h" |
14 #include "base/message_loop/message_loop.h" | 15 #include "base/message_loop/message_loop.h" |
15 #include "base/strings/sys_string_conversions.h" | 16 #include "base/strings/sys_string_conversions.h" |
16 #include "components/onc/onc_constants.h" | 17 #include "components/onc/onc_constants.h" |
17 | 18 |
18 #if !defined(MAC_OS_X_VERSION_10_7) || \ | 19 #if !defined(MAC_OS_X_VERSION_10_7) || \ |
19 MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_7 | 20 MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_7 |
20 | 21 |
21 // Local definitions of API added in Mac OS X 10.7 | 22 // Local definitions of API added in Mac OS X 10.7 |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
90 base::ListValue* network_list) OVERRIDE; | 91 base::ListValue* network_list) OVERRIDE; |
91 | 92 |
92 virtual void RequestNetworkScan() OVERRIDE; | 93 virtual void RequestNetworkScan() OVERRIDE; |
93 | 94 |
94 virtual void StartConnect(const std::string& network_guid, | 95 virtual void StartConnect(const std::string& network_guid, |
95 std::string* error) OVERRIDE; | 96 std::string* error) OVERRIDE; |
96 | 97 |
97 virtual void StartDisconnect(const std::string& network_guid, | 98 virtual void StartDisconnect(const std::string& network_guid, |
98 std::string* error) OVERRIDE; | 99 std::string* error) OVERRIDE; |
99 | 100 |
| 101 virtual void GetKeyFromSystem(const std::string& network_guid, |
| 102 bool get_plaintext_key, |
| 103 std::string* key_data, |
| 104 std::string* error) OVERRIDE; |
| 105 |
100 virtual void SetEventObservers( | 106 virtual void SetEventObservers( |
101 scoped_refptr<base::MessageLoopProxy> message_loop_proxy, | 107 scoped_refptr<base::MessageLoopProxy> message_loop_proxy, |
102 const NetworkGuidListCallback& networks_changed_observer, | 108 const NetworkGuidListCallback& networks_changed_observer, |
103 const NetworkGuidListCallback& network_list_changed_observer) OVERRIDE; | 109 const NetworkGuidListCallback& network_list_changed_observer) OVERRIDE; |
104 | 110 |
105 virtual void RequestConnectedNetworkUpdate() OVERRIDE; | 111 virtual void RequestConnectedNetworkUpdate() OVERRIDE; |
106 | 112 |
107 private: | 113 private: |
108 // Checks |ns_error| and if is not |nil|, then stores |error_name| | 114 // Checks |ns_error| and if is not |nil|, then stores |error_name| |
109 // into |error|. | 115 // into |error|. |
(...skipping 250 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
360 NSError* ns_error = nil; | 366 NSError* ns_error = nil; |
361 [interface_ setPower:NO error:&ns_error]; | 367 [interface_ setPower:NO error:&ns_error]; |
362 CheckError(ns_error, kErrorAssociateToNetwork, error); | 368 CheckError(ns_error, kErrorAssociateToNetwork, error); |
363 [interface_ setPower:YES error:&ns_error]; | 369 [interface_ setPower:YES error:&ns_error]; |
364 CheckError(ns_error, kErrorAssociateToNetwork, error); | 370 CheckError(ns_error, kErrorAssociateToNetwork, error); |
365 } else { | 371 } else { |
366 *error = kErrorNotConnected; | 372 *error = kErrorNotConnected; |
367 } | 373 } |
368 } | 374 } |
369 | 375 |
| 376 void WiFiServiceMac::GetKeyFromSystem(const std::string& network_guid, |
| 377 bool get_plaintext_key, |
| 378 std::string* key_data, |
| 379 std::string* error) { |
| 380 static const char kAirPortServiceName[] = "AirPort"; |
| 381 |
| 382 UInt32 password_length = 0; |
| 383 void *password_data = NULL; |
| 384 OSStatus status = noErr; |
| 385 if (get_plaintext_key) { |
| 386 status = SecKeychainFindGenericPassword(NULL, |
| 387 strlen(kAirPortServiceName), |
| 388 kAirPortServiceName, |
| 389 network_guid.length(), |
| 390 network_guid.c_str(), |
| 391 &password_length, |
| 392 &password_data, |
| 393 NULL); |
| 394 } else { |
| 395 NSDictionary *query = [NSDictionary dictionaryWithObjectsAndKeys: |
| 396 (id)kSecClassGenericPassword, kSecClass, |
| 397 SSIDFromGUID(network_guid), kSecAttrAccount, |
| 398 kCFBooleanTrue, kSecReturnRef, |
| 399 nil]; |
| 400 |
| 401 CFDataRef data = NULL; |
| 402 status = SecItemCopyMatching(base::mac::NSToCFCast(query), |
| 403 reinterpret_cast<CFTypeRef *>(&data)); |
| 404 base::ScopedCFTypeRef<CFDataRef> scoped_data(data); |
| 405 } |
| 406 |
| 407 if (status != errSecSuccess) { |
| 408 *error = kErrorNotFound; |
| 409 return; |
| 410 } |
| 411 |
| 412 if (password_data) { |
| 413 *key_data = std::string(reinterpret_cast<char*>(password_data), |
| 414 password_length); |
| 415 SecKeychainItemFreeContent(NULL, password_data); |
| 416 } |
| 417 } |
| 418 |
370 void WiFiServiceMac::SetEventObservers( | 419 void WiFiServiceMac::SetEventObservers( |
371 scoped_refptr<base::MessageLoopProxy> message_loop_proxy, | 420 scoped_refptr<base::MessageLoopProxy> message_loop_proxy, |
372 const NetworkGuidListCallback& networks_changed_observer, | 421 const NetworkGuidListCallback& networks_changed_observer, |
373 const NetworkGuidListCallback& network_list_changed_observer) { | 422 const NetworkGuidListCallback& network_list_changed_observer) { |
374 message_loop_proxy_.swap(message_loop_proxy); | 423 message_loop_proxy_.swap(message_loop_proxy); |
375 networks_changed_observer_ = networks_changed_observer; | 424 networks_changed_observer_ = networks_changed_observer; |
376 network_list_changed_observer_ = network_list_changed_observer; | 425 network_list_changed_observer_ = network_list_changed_observer; |
377 | 426 |
378 // Remove previous OS notifications observer. | 427 // Remove previous OS notifications observer. |
379 if (wlan_observer_) { | 428 if (wlan_observer_) { |
(...skipping 214 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
594 NetworkGuidList changed_networks(1, network_guid); | 643 NetworkGuidList changed_networks(1, network_guid); |
595 message_loop_proxy_->PostTask( | 644 message_loop_proxy_->PostTask( |
596 FROM_HERE, | 645 FROM_HERE, |
597 base::Bind(networks_changed_observer_, changed_networks)); | 646 base::Bind(networks_changed_observer_, changed_networks)); |
598 } | 647 } |
599 | 648 |
600 // static | 649 // static |
601 WiFiService* WiFiService::Create() { return new WiFiServiceMac(); } | 650 WiFiService* WiFiService::Create() { return new WiFiServiceMac(); } |
602 | 651 |
603 } // namespace wifi | 652 } // namespace wifi |
OLD | NEW |