Chromium Code Reviews| 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 #include <dlfcn.h> | |
| 7 #import <netinet/in.h> | 8 #import <netinet/in.h> |
| 8 #import <CoreWLAN/CoreWLAN.h> | 9 #import <CoreWLAN/CoreWLAN.h> |
| 9 #import <SystemConfiguration/SystemConfiguration.h> | 10 #import <SystemConfiguration/SystemConfiguration.h> |
| 10 | 11 |
| 11 #include "base/bind.h" | 12 #include "base/bind.h" |
| 12 #include "base/mac/foundation_util.h" | 13 #include "base/mac/foundation_util.h" |
| 13 #include "base/mac/scoped_cftyperef.h" | 14 #include "base/mac/scoped_cftyperef.h" |
| 14 #include "base/mac/scoped_nsobject.h" | 15 #include "base/mac/scoped_nsobject.h" |
| 15 #include "base/mac/sdk_forward_declarations.h" | 16 #include "base/mac/sdk_forward_declarations.h" |
| 16 #include "base/message_loop/message_loop.h" | 17 #include "base/message_loop/message_loop.h" |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 93 return base::SysNSStringToUTF8(ssid); | 94 return base::SysNSStringToUTF8(ssid); |
| 94 } | 95 } |
| 95 | 96 |
| 96 // Populates |properties| from |network|. | 97 // Populates |properties| from |network|. |
| 97 void NetworkPropertiesFromCWNetwork(const CWNetwork* network, | 98 void NetworkPropertiesFromCWNetwork(const CWNetwork* network, |
| 98 NetworkProperties* properties) const; | 99 NetworkProperties* properties) const; |
| 99 | 100 |
| 100 // Converts |CWSecurityMode| into onc::wifi::k{WPA|WEP}* security constant. | 101 // Converts |CWSecurityMode| into onc::wifi::k{WPA|WEP}* security constant. |
| 101 std::string SecurityFromCWSecurityMode(CWSecurityMode security) const; | 102 std::string SecurityFromCWSecurityMode(CWSecurityMode security) const; |
| 102 | 103 |
| 104 // Returns onc::wifi::k{WPA|WEP}* security constant supported by the | |
| 105 // |CWNetwork|. | |
| 106 std::string SecurityFromCWNetwork(const CWNetwork* network) const; | |
| 107 | |
| 103 // Converts |CWChannelBand| into Frequency constant. | 108 // Converts |CWChannelBand| into Frequency constant. |
| 104 Frequency FrequencyFromCWChannelBand(CWChannelBand band) const; | 109 Frequency FrequencyFromCWChannelBand(CWChannelBand band) const; |
| 105 | 110 |
| 106 // Gets current |onc::connection_state| for given |network_guid|. | 111 // Gets current |onc::connection_state| for given |network_guid|. |
| 107 std::string GetNetworkConnectionState(const std::string& network_guid) const; | 112 std::string GetNetworkConnectionState(const std::string& network_guid) const; |
| 108 | 113 |
| 109 // Updates |networks_| with the list of visible wireless networks. | 114 // Updates |networks_| with the list of visible wireless networks. |
| 110 void UpdateNetworks(); | 115 void UpdateNetworks(); |
| 111 | 116 |
| 112 // Find network by |network_guid| and return iterator to its entry in | 117 // Find network by |network_guid| and return iterator to its entry in |
| (...skipping 280 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 393 if (!networks_changed_observer_.is_null()) { | 398 if (!networks_changed_observer_.is_null()) { |
| 394 void (^ns_observer) (NSNotification* notification) = | 399 void (^ns_observer) (NSNotification* notification) = |
| 395 ^(NSNotification* notification) { | 400 ^(NSNotification* notification) { |
| 396 DVLOG(1) << "Received CWSSIDDidChangeNotification"; | 401 DVLOG(1) << "Received CWSSIDDidChangeNotification"; |
| 397 task_runner_->PostTask( | 402 task_runner_->PostTask( |
| 398 FROM_HERE, | 403 FROM_HERE, |
| 399 base::Bind(&WiFiServiceMac::OnWlanObserverNotification, | 404 base::Bind(&WiFiServiceMac::OnWlanObserverNotification, |
| 400 base::Unretained(this))); | 405 base::Unretained(this))); |
| 401 }; | 406 }; |
| 402 | 407 |
| 408 NSBundle* bundle = [NSBundle | |
|
mef
2014/09/05 17:56:22
If anything I'd move this out into separate method
| |
| 409 bundleWithPath:@"/System/Library/Frameworks/CoreWLAN.framework"]; | |
| 410 const char* path = [[bundle executablePath] fileSystemRepresentation]; | |
| 411 CHECK(path); | |
| 412 void* library_handle = dlopen(path, RTLD_LAZY | RTLD_LOCAL); | |
| 413 CHECK(library_handle) << dlerror(); | |
| 414 | |
| 415 // Try out the symbol name for 10.7+ first, if not found use the one from | |
| 416 // 10.6 SDK. | |
| 417 void* symbol = dlsym(library_handle, "CWSSIDDidChangeNotification"); | |
| 418 if (!symbol) | |
| 419 symbol = dlsym(library_handle, "kCWSSIDDidChangeNotification"); | |
|
mef
2014/09/05 17:56:23
Is kCWSSIDDidChangeNotification actually causing i
| |
| 420 CHECK(symbol) << dlerror(); | |
| 421 NSString* notification_name = *reinterpret_cast<NSString**>(symbol); | |
| 422 | |
| 403 wlan_observer_ = [[NSNotificationCenter defaultCenter] | 423 wlan_observer_ = [[NSNotificationCenter defaultCenter] |
| 404 addObserverForName:kCWSSIDDidChangeNotification | 424 addObserverForName:notification_name |
| 405 object:nil | 425 object:nil |
| 406 queue:nil | 426 queue:nil |
| 407 usingBlock:ns_observer]; | 427 usingBlock:ns_observer]; |
| 408 } | 428 } |
| 409 } | 429 } |
| 410 | 430 |
| 411 void WiFiServiceMac::RequestConnectedNetworkUpdate() { | 431 void WiFiServiceMac::RequestConnectedNetworkUpdate() { |
| 412 OnWlanObserverNotification(); | 432 OnWlanObserverNotification(); |
| 413 } | 433 } |
| 414 | 434 |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 498 properties->connection_state = GetNetworkConnectionState(network_guid); | 518 properties->connection_state = GetNetworkConnectionState(network_guid); |
| 499 properties->ssid = base::SysNSStringToUTF8([network ssid]); | 519 properties->ssid = base::SysNSStringToUTF8([network ssid]); |
| 500 properties->name = properties->ssid; | 520 properties->name = properties->ssid; |
| 501 properties->guid = network_guid; | 521 properties->guid = network_guid; |
| 502 properties->type = onc::network_type::kWiFi; | 522 properties->type = onc::network_type::kWiFi; |
| 503 | 523 |
| 504 properties->bssid = base::SysNSStringToUTF8([network bssid]); | 524 properties->bssid = base::SysNSStringToUTF8([network bssid]); |
| 505 properties->frequency = FrequencyFromCWChannelBand( | 525 properties->frequency = FrequencyFromCWChannelBand( |
| 506 static_cast<CWChannelBand>([[network wlanChannel] channelBand])); | 526 static_cast<CWChannelBand>([[network wlanChannel] channelBand])); |
| 507 properties->frequency_set.insert(properties->frequency); | 527 properties->frequency_set.insert(properties->frequency); |
| 508 properties->security = SecurityFromCWSecurityMode( | |
| 509 static_cast<CWSecurityMode>([[network securityMode] intValue])); | |
| 510 | 528 |
| 511 properties->signal_strength = [[network rssi] intValue]; | 529 if ([network respondsToSelector:@selector(supportsSecurity:)]) { |
| 530 properties->security = SecurityFromCWNetwork(network); | |
| 531 } else { | |
| 532 properties->security = SecurityFromCWSecurityMode( | |
| 533 static_cast<CWSecurityMode>([[network securityMode] intValue])); | |
| 534 } | |
| 535 | |
| 536 if ([network respondsToSelector:@selector(rssiValue:)]) | |
| 537 properties->signal_strength = [network rssiValue]; | |
| 538 else | |
| 539 properties->signal_strength = [[network rssi] intValue]; | |
| 512 } | 540 } |
| 513 | 541 |
| 514 std::string WiFiServiceMac::SecurityFromCWSecurityMode( | 542 std::string WiFiServiceMac::SecurityFromCWSecurityMode( |
| 515 CWSecurityMode security) const { | 543 CWSecurityMode security) const { |
| 516 switch (security) { | 544 switch (security) { |
| 517 case kCWSecurityModeWPA_Enterprise: | 545 case kCWSecurityModeWPA_Enterprise: |
| 518 case kCWSecurityModeWPA2_Enterprise: | 546 case kCWSecurityModeWPA2_Enterprise: |
| 519 return onc::wifi::kWPA_EAP; | 547 return onc::wifi::kWPA_EAP; |
| 520 case kCWSecurityModeWPA_PSK: | 548 case kCWSecurityModeWPA_PSK: |
| 521 case kCWSecurityModeWPA2_PSK: | 549 case kCWSecurityModeWPA2_PSK: |
| 522 return onc::wifi::kWPA_PSK; | 550 return onc::wifi::kWPA_PSK; |
| 523 case kCWSecurityModeWEP: | 551 case kCWSecurityModeWEP: |
| 524 return onc::wifi::kWEP_PSK; | 552 return onc::wifi::kWEP_PSK; |
| 525 case kCWSecurityModeOpen: | 553 case kCWSecurityModeOpen: |
| 526 return onc::wifi::kSecurityNone; | 554 return onc::wifi::kSecurityNone; |
| 527 // TODO(mef): Figure out correct mapping. | 555 // TODO(mef): Figure out correct mapping. |
| 528 case kCWSecurityModeWPS: | 556 case kCWSecurityModeWPS: |
| 529 case kCWSecurityModeDynamicWEP: | 557 case kCWSecurityModeDynamicWEP: |
| 530 return onc::wifi::kWPA_EAP; | 558 return onc::wifi::kWPA_EAP; |
| 531 } | 559 } |
| 532 return onc::wifi::kWPA_EAP; | 560 return onc::wifi::kWPA_EAP; |
| 533 } | 561 } |
| 534 | 562 |
| 563 std::string WiFiServiceMac::SecurityFromCWNetwork( | |
| 564 const CWNetwork* network) const { | |
| 565 if ([network supportsSecurity:kCWSecurityWPAEnterprise] || | |
| 566 [network supportsSecurity:kCWSecurityWPA2Enterprise]) { | |
| 567 return onc::wifi::kWPA_EAP; | |
| 568 } | |
| 569 | |
| 570 if ([network supportsSecurity:kCWSecurityWPAPersonal] || | |
| 571 [network supportsSecurity:kCWSecurityWPA2Personal]) { | |
| 572 return onc::wifi::kWPA_PSK; | |
| 573 } | |
| 574 | |
| 575 if ([network supportsSecurity:kCWSecurityWEP]) | |
| 576 return onc::wifi::kWEP_PSK; | |
| 577 | |
| 578 if ([network supportsSecurity:kCWSecurityNone]) | |
| 579 return onc::wifi::kSecurityNone; | |
| 580 | |
| 581 // TODO(mef): Figure out correct mapping. | |
| 582 if ([network supportsSecurity:kCWSecurityDynamicWEP]) | |
| 583 return onc::wifi::kWPA_EAP; | |
| 584 | |
| 585 return onc::wifi::kWPA_EAP; | |
| 586 } | |
| 587 | |
| 535 Frequency WiFiServiceMac::FrequencyFromCWChannelBand(CWChannelBand band) const { | 588 Frequency WiFiServiceMac::FrequencyFromCWChannelBand(CWChannelBand band) const { |
| 536 return band == kCWChannelBand2GHz ? kFrequency2400 : kFrequency5000; | 589 return band == kCWChannelBand2GHz ? kFrequency2400 : kFrequency5000; |
| 537 } | 590 } |
| 538 | 591 |
| 539 NetworkList::iterator WiFiServiceMac::FindNetwork( | 592 NetworkList::iterator WiFiServiceMac::FindNetwork( |
| 540 const std::string& network_guid) { | 593 const std::string& network_guid) { |
| 541 for (NetworkList::iterator it = networks_.begin(); | 594 for (NetworkList::iterator it = networks_.begin(); |
| 542 it != networks_.end(); | 595 it != networks_.end(); |
| 543 ++it) { | 596 ++it) { |
| 544 if (it->guid == network_guid) | 597 if (it->guid == network_guid) |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 600 NetworkGuidList changed_networks(1, network_guid); | 653 NetworkGuidList changed_networks(1, network_guid); |
| 601 message_loop_proxy_->PostTask( | 654 message_loop_proxy_->PostTask( |
| 602 FROM_HERE, | 655 FROM_HERE, |
| 603 base::Bind(networks_changed_observer_, changed_networks)); | 656 base::Bind(networks_changed_observer_, changed_networks)); |
| 604 } | 657 } |
| 605 | 658 |
| 606 // static | 659 // static |
| 607 WiFiService* WiFiService::Create() { return new WiFiServiceMac(); } | 660 WiFiService* WiFiService::Create() { return new WiFiServiceMac(); } |
| 608 | 661 |
| 609 } // namespace wifi | 662 } // namespace wifi |
| OLD | NEW |