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 #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/foundation_util.h" |
| 13 #include "base/mac/scoped_cftyperef.h" | 13 #include "base/mac/scoped_cftyperef.h" |
| 14 #include "base/mac/scoped_nsobject.h" | 14 #include "base/mac/scoped_nsobject.h" |
| 15 #include "base/mac/sdk_forward_declarations.h" | 15 #include "base/mac/sdk_forward_declarations.h" |
| 16 #include "base/message_loop/message_loop.h" | 16 #include "base/message_loop/message_loop.h" |
| 17 #include "base/strings/sys_string_conversions.h" | 17 #include "base/strings/sys_string_conversions.h" |
| 18 #include "components/onc/onc_constants.h" | 18 #include "components/onc/onc_constants.h" |
| 19 #include "components/wifi/network_properties.h" | 19 #include "components/wifi/network_properties.h" |
| 20 | 20 |
| 21 // 10.6 SDK don't have CWSecurity while 10.9 SDK don't have CWSecurityMode, to | |
| 22 // build with SDKs from 10.6 to 10.9 we need to forward declare both and do | |
| 23 // runtime checks to ensure we use correct methods on different OS X versions. | |
| 24 #if !defined(MAC_OS_X_VERSION_10_9) || \ | |
|
Robert Sesek
2014/09/04 15:13:30
Move these to base/mac/sdk_forward_declarations.h.
Jiang Jiang
2014/09/04 15:17:49
Done.
| |
| 25 MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_9 | |
| 26 | |
| 27 enum { | |
| 28 kCWSecurityNone = 0, | |
| 29 kCWSecurityWEP = 1, | |
| 30 kCWSecurityWPAPersonal = 2, | |
| 31 kCWSecurityWPAPersonalMixed = 3, | |
| 32 kCWSecurityWPA2Personal = 4, | |
| 33 kCWSecurityPersonal = 5, | |
| 34 kCWSecurityDynamicWEP = 6, | |
| 35 kCWSecurityWPAEnterprise = 7, | |
| 36 kCWSecurityWPAEnterpriseMixed = 8, | |
| 37 kCWSecurityWPA2Enterprise = 9, | |
| 38 kCWSecurityEnterprise = 10, | |
| 39 kCWSecurityUnknown = NSIntegerMax, | |
| 40 }; | |
| 41 | |
| 42 typedef NSInteger CWSecurity; | |
| 43 | |
| 44 @interface CWNetwork (ForwardDeclarations) | |
| 45 @property(readonly) NSInteger rssiValue; | |
| 46 -(BOOL)supportsSecurity:(CWSecurity)security; | |
|
Robert Sesek
2014/09/04 15:13:30
nit: space before (
Jiang Jiang
2014/09/04 15:17:49
Done.
| |
| 47 @end | |
| 48 | |
| 49 #else | |
| 50 | |
| 51 typedef enum { | |
| 52 kCWSecurityModeOpen = 0, | |
| 53 kCWSecurityModeWEP, | |
| 54 kCWSecurityModeWPA_PSK, | |
| 55 kCWSecurityModeWPA2_PSK, | |
| 56 kCWSecurityModeWPA_Enterprise, | |
| 57 kCWSecurityModeWPA2_Enterprise, | |
| 58 kCWSecurityModeWPS, | |
| 59 kCWSecurityModeDynamicWEP | |
| 60 } CWSecurityMode; | |
| 61 | |
| 62 @interface CWNetwork (ForwardDeclarations) | |
| 63 @property(readonly) NSNumber* rssi; | |
| 64 @property(readonly) NSNumber* securityMode; | |
| 65 @end | |
| 66 | |
| 67 #endif | |
| 68 | |
| 21 namespace wifi { | 69 namespace wifi { |
| 22 | 70 |
| 23 // Implementation of WiFiService for Mac OS X. | 71 // Implementation of WiFiService for Mac OS X. |
| 24 class WiFiServiceMac : public WiFiService { | 72 class WiFiServiceMac : public WiFiService { |
| 25 public: | 73 public: |
| 26 WiFiServiceMac(); | 74 WiFiServiceMac(); |
| 27 virtual ~WiFiServiceMac(); | 75 virtual ~WiFiServiceMac(); |
| 28 | 76 |
| 29 // WiFiService interface implementation. | 77 // WiFiService interface implementation. |
| 30 virtual void Initialize( | 78 virtual void Initialize( |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 93 return base::SysNSStringToUTF8(ssid); | 141 return base::SysNSStringToUTF8(ssid); |
| 94 } | 142 } |
| 95 | 143 |
| 96 // Populates |properties| from |network|. | 144 // Populates |properties| from |network|. |
| 97 void NetworkPropertiesFromCWNetwork(const CWNetwork* network, | 145 void NetworkPropertiesFromCWNetwork(const CWNetwork* network, |
| 98 NetworkProperties* properties) const; | 146 NetworkProperties* properties) const; |
| 99 | 147 |
| 100 // Converts |CWSecurityMode| into onc::wifi::k{WPA|WEP}* security constant. | 148 // Converts |CWSecurityMode| into onc::wifi::k{WPA|WEP}* security constant. |
| 101 std::string SecurityFromCWSecurityMode(CWSecurityMode security) const; | 149 std::string SecurityFromCWSecurityMode(CWSecurityMode security) const; |
| 102 | 150 |
| 151 // Converts |CWSecurity| into onc::wifi::k{WPA|WEP}* security constant. | |
| 152 std::string SecurityFromCWNetwork(const CWNetwork* network) const; | |
| 153 | |
| 103 // Converts |CWChannelBand| into Frequency constant. | 154 // Converts |CWChannelBand| into Frequency constant. |
| 104 Frequency FrequencyFromCWChannelBand(CWChannelBand band) const; | 155 Frequency FrequencyFromCWChannelBand(CWChannelBand band) const; |
| 105 | 156 |
| 106 // Gets current |onc::connection_state| for given |network_guid|. | 157 // Gets current |onc::connection_state| for given |network_guid|. |
| 107 std::string GetNetworkConnectionState(const std::string& network_guid) const; | 158 std::string GetNetworkConnectionState(const std::string& network_guid) const; |
| 108 | 159 |
| 109 // Updates |networks_| with the list of visible wireless networks. | 160 // Updates |networks_| with the list of visible wireless networks. |
| 110 void UpdateNetworks(); | 161 void UpdateNetworks(); |
| 111 | 162 |
| 112 // Find network by |network_guid| and return iterator to its entry in | 163 // Find network by |network_guid| and return iterator to its entry in |
| (...skipping 281 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 394 void (^ns_observer) (NSNotification* notification) = | 445 void (^ns_observer) (NSNotification* notification) = |
| 395 ^(NSNotification* notification) { | 446 ^(NSNotification* notification) { |
| 396 DVLOG(1) << "Received CWSSIDDidChangeNotification"; | 447 DVLOG(1) << "Received CWSSIDDidChangeNotification"; |
| 397 task_runner_->PostTask( | 448 task_runner_->PostTask( |
| 398 FROM_HERE, | 449 FROM_HERE, |
| 399 base::Bind(&WiFiServiceMac::OnWlanObserverNotification, | 450 base::Bind(&WiFiServiceMac::OnWlanObserverNotification, |
| 400 base::Unretained(this))); | 451 base::Unretained(this))); |
| 401 }; | 452 }; |
| 402 | 453 |
| 403 wlan_observer_ = [[NSNotificationCenter defaultCenter] | 454 wlan_observer_ = [[NSNotificationCenter defaultCenter] |
| 404 addObserverForName:kCWSSIDDidChangeNotification | 455 addObserverForName:CWSSIDDidChangeNotification |
| 405 object:nil | 456 object:nil |
| 406 queue:nil | 457 queue:nil |
| 407 usingBlock:ns_observer]; | 458 usingBlock:ns_observer]; |
| 408 } | 459 } |
| 409 } | 460 } |
| 410 | 461 |
| 411 void WiFiServiceMac::RequestConnectedNetworkUpdate() { | 462 void WiFiServiceMac::RequestConnectedNetworkUpdate() { |
| 412 OnWlanObserverNotification(); | 463 OnWlanObserverNotification(); |
| 413 } | 464 } |
| 414 | 465 |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 498 properties->connection_state = GetNetworkConnectionState(network_guid); | 549 properties->connection_state = GetNetworkConnectionState(network_guid); |
| 499 properties->ssid = base::SysNSStringToUTF8([network ssid]); | 550 properties->ssid = base::SysNSStringToUTF8([network ssid]); |
| 500 properties->name = properties->ssid; | 551 properties->name = properties->ssid; |
| 501 properties->guid = network_guid; | 552 properties->guid = network_guid; |
| 502 properties->type = onc::network_type::kWiFi; | 553 properties->type = onc::network_type::kWiFi; |
| 503 | 554 |
| 504 properties->bssid = base::SysNSStringToUTF8([network bssid]); | 555 properties->bssid = base::SysNSStringToUTF8([network bssid]); |
| 505 properties->frequency = FrequencyFromCWChannelBand( | 556 properties->frequency = FrequencyFromCWChannelBand( |
| 506 static_cast<CWChannelBand>([[network wlanChannel] channelBand])); | 557 static_cast<CWChannelBand>([[network wlanChannel] channelBand])); |
| 507 properties->frequency_set.insert(properties->frequency); | 558 properties->frequency_set.insert(properties->frequency); |
| 508 properties->security = SecurityFromCWSecurityMode( | |
| 509 static_cast<CWSecurityMode>([[network securityMode] intValue])); | |
| 510 | 559 |
| 511 properties->signal_strength = [[network rssi] intValue]; | 560 if ([network respondsToSelector:@selector(supportsSecurity:)]) |
|
Robert Sesek
2014/09/04 15:13:30
nit: since the else has curly braces, the if body
Jiang Jiang
2014/09/04 15:17:49
Done.
| |
| 561 properties->security = SecurityFromCWNetwork(network); | |
| 562 else { | |
| 563 properties->security = SecurityFromCWSecurityMode( | |
| 564 static_cast<CWSecurityMode>([[network securityMode] intValue])); | |
| 565 } | |
| 566 | |
| 567 if ([network respondsToSelector:@selector(rssiValue:)]) | |
| 568 properties->signal_strength = [network rssiValue]; | |
| 569 else | |
| 570 properties->signal_strength = [[network rssi] intValue]; | |
| 512 } | 571 } |
| 513 | 572 |
| 514 std::string WiFiServiceMac::SecurityFromCWSecurityMode( | 573 std::string WiFiServiceMac::SecurityFromCWSecurityMode( |
| 515 CWSecurityMode security) const { | 574 CWSecurityMode security) const { |
| 516 switch (security) { | 575 switch (security) { |
| 517 case kCWSecurityModeWPA_Enterprise: | 576 case kCWSecurityModeWPA_Enterprise: |
| 518 case kCWSecurityModeWPA2_Enterprise: | 577 case kCWSecurityModeWPA2_Enterprise: |
| 519 return onc::wifi::kWPA_EAP; | 578 return onc::wifi::kWPA_EAP; |
| 520 case kCWSecurityModeWPA_PSK: | 579 case kCWSecurityModeWPA_PSK: |
| 521 case kCWSecurityModeWPA2_PSK: | 580 case kCWSecurityModeWPA2_PSK: |
| 522 return onc::wifi::kWPA_PSK; | 581 return onc::wifi::kWPA_PSK; |
| 523 case kCWSecurityModeWEP: | 582 case kCWSecurityModeWEP: |
| 524 return onc::wifi::kWEP_PSK; | 583 return onc::wifi::kWEP_PSK; |
| 525 case kCWSecurityModeOpen: | 584 case kCWSecurityModeOpen: |
| 526 return onc::wifi::kSecurityNone; | 585 return onc::wifi::kSecurityNone; |
| 527 // TODO(mef): Figure out correct mapping. | 586 // TODO(mef): Figure out correct mapping. |
| 528 case kCWSecurityModeWPS: | 587 case kCWSecurityModeWPS: |
| 529 case kCWSecurityModeDynamicWEP: | 588 case kCWSecurityModeDynamicWEP: |
| 530 return onc::wifi::kWPA_EAP; | 589 return onc::wifi::kWPA_EAP; |
| 531 } | 590 } |
| 532 return onc::wifi::kWPA_EAP; | 591 return onc::wifi::kWPA_EAP; |
| 533 } | 592 } |
| 534 | 593 |
| 594 std::string WiFiServiceMac::SecurityFromCWNetwork( | |
| 595 const CWNetwork* network) const { | |
| 596 if ([network supportsSecurity:kCWSecurityWPAEnterprise] || | |
| 597 [network supportsSecurity:kCWSecurityWPA2Enterprise]) | |
|
Robert Sesek
2014/09/04 15:13:30
nit: braces needed since condition is multi-line,
Jiang Jiang
2014/09/04 15:17:49
Done.
| |
| 598 return onc::wifi::kWPA_EAP; | |
| 599 | |
| 600 if ([network supportsSecurity:kCWSecurityWPAPersonal] || | |
| 601 [network supportsSecurity:kCWSecurityWPA2Personal]) | |
| 602 return onc::wifi::kWPA_PSK; | |
| 603 | |
| 604 if ([network supportsSecurity:kCWSecurityWEP]) | |
| 605 return onc::wifi::kWEP_PSK; | |
| 606 | |
| 607 if ([network supportsSecurity:kCWSecurityNone]) | |
| 608 return onc::wifi::kSecurityNone; | |
| 609 | |
| 610 // TODO(mef): Figure out correct mapping. | |
| 611 if ([network supportsSecurity:kCWSecurityDynamicWEP]) | |
| 612 return onc::wifi::kWPA_EAP; | |
| 613 | |
| 614 return onc::wifi::kWPA_EAP; | |
| 615 } | |
| 616 | |
| 535 Frequency WiFiServiceMac::FrequencyFromCWChannelBand(CWChannelBand band) const { | 617 Frequency WiFiServiceMac::FrequencyFromCWChannelBand(CWChannelBand band) const { |
| 536 return band == kCWChannelBand2GHz ? kFrequency2400 : kFrequency5000; | 618 return band == kCWChannelBand2GHz ? kFrequency2400 : kFrequency5000; |
| 537 } | 619 } |
| 538 | 620 |
| 539 NetworkList::iterator WiFiServiceMac::FindNetwork( | 621 NetworkList::iterator WiFiServiceMac::FindNetwork( |
| 540 const std::string& network_guid) { | 622 const std::string& network_guid) { |
| 541 for (NetworkList::iterator it = networks_.begin(); | 623 for (NetworkList::iterator it = networks_.begin(); |
| 542 it != networks_.end(); | 624 it != networks_.end(); |
| 543 ++it) { | 625 ++it) { |
| 544 if (it->guid == network_guid) | 626 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); | 682 NetworkGuidList changed_networks(1, network_guid); |
| 601 message_loop_proxy_->PostTask( | 683 message_loop_proxy_->PostTask( |
| 602 FROM_HERE, | 684 FROM_HERE, |
| 603 base::Bind(networks_changed_observer_, changed_networks)); | 685 base::Bind(networks_changed_observer_, changed_networks)); |
| 604 } | 686 } |
| 605 | 687 |
| 606 // static | 688 // static |
| 607 WiFiService* WiFiService::Create() { return new WiFiServiceMac(); } | 689 WiFiService* WiFiService::Create() { return new WiFiServiceMac(); } |
| 608 | 690 |
| 609 } // namespace wifi | 691 } // namespace wifi |
| OLD | NEW |