Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(753)

Side by Side Diff: device/geolocation/wifi_data_provider_mac.mm

Issue 2472463002: [Mac] Use CoreWLAN directly now that macOS 10.9 is the minimum version. (Closed)
Patch Set: Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « device/geolocation/wifi_data_provider_mac.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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 // Implements a WLAN API binding for CoreWLAN, as available on OSX 10.6
6
7 #include "device/geolocation/wifi_data_provider_mac.h" 5 #include "device/geolocation/wifi_data_provider_mac.h"
8 6
9 #include <dlfcn.h> 7 #import <CoreWLAN/CoreWLAN.h>
10 #import <Foundation/Foundation.h> 8 #import <Foundation/Foundation.h>
11 #include <stdint.h>
12 9
13 #include "base/mac/scoped_nsautorelease_pool.h" 10 #include "base/mac/scoped_nsautorelease_pool.h"
14 #include "base/mac/scoped_nsobject.h" 11 #include "base/mac/scoped_nsobject.h"
15 #include "base/macros.h" 12 #include "base/macros.h"
16 #include "base/metrics/histogram_macros.h" 13 #include "base/metrics/histogram_macros.h"
17 #include "base/strings/sys_string_conversions.h" 14 #include "base/strings/sys_string_conversions.h"
15 #include "device/geolocation/wifi_data_provider_common.h"
16 #include "device/geolocation/wifi_data_provider_manager.h"
18 17
19 // Define a subset of the CoreWLAN interfaces we require. We can't depend on 18 extern "C" NSString* const kCWScanKeyMerge;
20 // CoreWLAN.h existing as we need to build on 10.5 SDKs. We can't just send
21 // messages to an untyped id due to the build treating warnings as errors,
22 // hence the reason we need class definitions.
23 // TODO(joth): When we build all 10.6 code exclusively 10.6 SDK (or later)
24 // tidy this up to use the framework directly. See http://crbug.com/37703
25 19
26 @interface CWInterface : NSObject 20 @interface CWInterface (Private)
27 + (CWInterface*)interface; 21 - (NSArray*)scanForNetworksWithParameters:(NSDictionary*)params
28 + (CWInterface*)interfaceWithName:(NSString*)name;
29 + (NSArray*)supportedInterfaces;
30 - (NSArray*)scanForNetworksWithParameters:(NSDictionary*)parameters
31 error:(NSError**)error; 22 error:(NSError**)error;
32 @end 23 @end
33 24
34 @interface CWNetwork : NSObject <NSCopying, NSCoding> 25 namespace device {
35 @property (nonatomic, readonly) NSString* ssid;
36 @property (nonatomic, readonly) NSString* bssid;
37 @property (nonatomic, readonly) NSData* bssidData;
38 @property (nonatomic, readonly) NSNumber* securityMode;
39 @property (nonatomic, readonly) NSNumber* phyMode;
40 @property (nonatomic, readonly) NSNumber* channel;
41 @property (nonatomic, readonly) NSNumber* rssi;
42 @property (nonatomic, readonly) NSInteger rssiValue;
43 @property (nonatomic, readonly) NSNumber* noise;
44 @property (nonatomic, readonly) NSData* ieData;
45 @property (nonatomic, readonly) BOOL isIBSS;
46 - (BOOL)isEqualToNetwork:(CWNetwork*)network;
47 @end
48 26
49 namespace device { 27 namespace {
50 28
51 class CoreWlanApi : public WifiDataProviderCommon::WlanApiInterface { 29 class CoreWlanApi : public WifiDataProviderCommon::WlanApiInterface {
52 public: 30 public:
53 CoreWlanApi() {} 31 CoreWlanApi() {}
54 32
55 // Must be called before any other interface method. Will return false if the 33 // WlanApiInterface:
56 // CoreWLAN framework cannot be initialized (e.g. running on pre-10.6 OSX),
57 // in which case no other method may be called.
58 bool Init();
59
60 // WlanApiInterface
61 bool GetAccessPointData(WifiData::AccessPointDataSet* data) override; 34 bool GetAccessPointData(WifiData::AccessPointDataSet* data) override;
62 35
63 private: 36 private:
64 base::scoped_nsobject<NSBundle> bundle_;
65 base::scoped_nsobject<NSString> merge_key_;
66
67 DISALLOW_COPY_AND_ASSIGN(CoreWlanApi); 37 DISALLOW_COPY_AND_ASSIGN(CoreWlanApi);
68 }; 38 };
69 39
70 bool CoreWlanApi::Init() {
71 // As the WLAN api binding runs on its own thread, we need to provide our own
72 // auto release pool. It's simplest to do this as an automatic variable in
73 // each method that needs it, to ensure the scoping is correct and does not
74 // interfere with any other code using autorelease pools on the thread.
75 base::mac::ScopedNSAutoreleasePool auto_pool;
76 bundle_.reset([[NSBundle alloc]
77 initWithPath:@"/System/Library/Frameworks/CoreWLAN.framework"]);
78 if (!bundle_) {
79 DVLOG(1) << "Failed to load the CoreWLAN framework bundle";
80 return false;
81 }
82
83 // Dynamically look up the value of the kCWScanKeyMerge (i.e. without build
84 // time dependency on the 10.6 specific library).
85 void* dl_handle = dlopen([[bundle_ executablePath] fileSystemRepresentation],
86 RTLD_LAZY | RTLD_LOCAL);
87 if (dl_handle) {
88 NSString* key = *reinterpret_cast<NSString**>(dlsym(dl_handle,
89 "kCWScanKeyMerge"));
90 if (key)
91 merge_key_.reset([key copy]);
92 }
93 // "Leak" dl_handle rather than dlclose it, to ensure |merge_key_|
94 // remains valid.
95 if (!merge_key_) {
96 // Fall back to a known-working value should the lookup fail (if
97 // this value is itself wrong it's not the end of the world, we might just
98 // get very slightly lower quality location fixes due to SSID merges).
99 DLOG(WARNING) << "Could not dynamically load the CoreWLAN merge key";
100 merge_key_.reset([@"SCAN_MERGE" retain]);
101 }
102
103 return true;
104 }
105
106 bool CoreWlanApi::GetAccessPointData(WifiData::AccessPointDataSet* data) { 40 bool CoreWlanApi::GetAccessPointData(WifiData::AccessPointDataSet* data) {
107 base::mac::ScopedNSAutoreleasePool auto_pool; 41 base::mac::ScopedNSAutoreleasePool auto_pool;
108 // Initialize the scan parameters with scan key merging disabled, so we get 42 // Initialize the scan parameters with scan key merging disabled, so we get
109 // every AP listed in the scan without any SSID de-duping logic. 43 // every AP listed in the scan without any SSID de-duping logic.
110 NSDictionary* params = 44 NSDictionary* params = @{ kCWScanKeyMerge : @NO };
111 [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:NO]
112 forKey:merge_key_.get()];
113 45
114 Class cw_interface_class = [bundle_ classNamed:@"CWInterface"]; 46 NSSet* supported_interfaces = [CWInterface interfaceNames];
115 NSArray* supported_interfaces = [cw_interface_class supportedInterfaces]; 47 NSUInteger interface_error_count = 0;
116 uint interface_error_count = 0;
117 for (NSString* interface_name in supported_interfaces) { 48 for (NSString* interface_name in supported_interfaces) {
118 CWInterface* corewlan_interface = 49 CWInterface* corewlan_interface =
119 [cw_interface_class interfaceWithName:interface_name]; 50 [CWInterface interfaceWithName:interface_name];
120 if (!corewlan_interface) { 51 if (!corewlan_interface) {
121 DLOG(WARNING) << interface_name << ": initWithName failed"; 52 DLOG(WARNING) << interface_name << ": initWithName failed";
122 ++interface_error_count; 53 ++interface_error_count;
123 continue; 54 continue;
124 } 55 }
125 56
126 const base::TimeTicks start_time = base::TimeTicks::Now(); 57 const base::TimeTicks start_time = base::TimeTicks::Now();
127 58
128 NSError* err = nil; 59 NSError* err = nil;
129 NSArray* scan = [corewlan_interface scanForNetworksWithParameters:params 60 NSArray* scan =
130 error:&err]; 61 [corewlan_interface scanForNetworksWithParameters:params error:&err];
131 const int error_code = [err code]; 62 const int error_code = [err code];
132 const int count = [scan count]; 63 const int count = [scan count];
133 // We could get an error code but count != 0 if the scan was interrupted, 64 // We could get an error code but count != 0 if the scan was interrupted,
134 // for example. For our purposes this is not fatal, so process as normal. 65 // for example. For our purposes this is not fatal, so process as normal.
135 if (error_code && count == 0) { 66 if (error_code && count == 0) {
136 DLOG(WARNING) << interface_name << ": CoreWLAN scan failed with error " 67 DLOG(WARNING) << interface_name << ": CoreWLAN scan failed with error "
137 << error_code; 68 << error_code;
138 ++interface_error_count; 69 ++interface_error_count;
139 continue; 70 continue;
140 } 71 }
141 72
142 const base::TimeDelta duration = base::TimeTicks::Now() - start_time; 73 const base::TimeDelta duration = base::TimeTicks::Now() - start_time;
143 74
144 UMA_HISTOGRAM_CUSTOM_TIMES( 75 UMA_HISTOGRAM_CUSTOM_TIMES("Net.Wifi.ScanLatency", duration,
145 "Net.Wifi.ScanLatency", 76 base::TimeDelta::FromMilliseconds(1),
146 duration, 77 base::TimeDelta::FromMinutes(1), 100);
147 base::TimeDelta::FromMilliseconds(1),
148 base::TimeDelta::FromMinutes(1),
149 100);
150 78
151 DVLOG(1) << interface_name << ": found " << count << " wifi APs"; 79 DVLOG(1) << interface_name << ": found " << count << " wifi APs";
152 80
153 for (CWNetwork* network in scan) { 81 for (CWNetwork* network in scan) {
154 DCHECK(network); 82 DCHECK(network);
155 AccessPointData access_point_data; 83 AccessPointData access_point_data;
156 NSData* mac = [network bssidData]; 84 // -[CWNetwork bssid] uses colons to separate the components of the MAC
157 DCHECK([mac length] == 6); 85 // address, but AccessPointData requires they be separated with a dash.
158 if (![mac bytes]) 86 access_point_data.mac_address = base::SysNSStringToUTF16([[network bssid]
159 continue; // crbug.com/545501 87 stringByReplacingOccurrencesOfString:@":"
160 access_point_data.mac_address = 88 withString:@"-"]);
161 MacAddressAsString16(static_cast<const uint8_t*>([mac bytes]));
162 access_point_data.radio_signal_strength = [network rssiValue]; 89 access_point_data.radio_signal_strength = [network rssiValue];
163 access_point_data.channel = [[network channel] intValue]; 90 access_point_data.channel = [[network wlanChannel] channelNumber];
164 access_point_data.signal_to_noise = 91 access_point_data.signal_to_noise =
165 access_point_data.radio_signal_strength - [[network noise] intValue]; 92 access_point_data.radio_signal_strength - [network noiseMeasurement];
166 access_point_data.ssid = base::SysNSStringToUTF16([network ssid]); 93 access_point_data.ssid = base::SysNSStringToUTF16([network ssid]);
167 data->insert(access_point_data); 94 data->insert(access_point_data);
168 } 95 }
169 } 96 }
170 97
171 UMA_HISTOGRAM_CUSTOM_COUNTS( 98 UMA_HISTOGRAM_CUSTOM_COUNTS(
172 "Net.Wifi.InterfaceCount", 99 "Net.Wifi.InterfaceCount",
173 [supported_interfaces count] - interface_error_count, 100 [supported_interfaces count] - interface_error_count, 1, 5, 6);
174 1,
175 5,
176 6);
177 101
178 // Return true even if some interfaces failed to scan, so long as at least 102 // Return true even if some interfaces failed to scan, so long as at least
179 // one interface did not fail. 103 // one interface did not fail.
180 return interface_error_count == 0 || 104 return interface_error_count == 0 ||
181 [supported_interfaces count] > interface_error_count; 105 [supported_interfaces count] > interface_error_count;
106 };
107
108 // The time periods, in milliseconds, between successive polls of the wifi data.
109 const int kDefaultPollingInterval = 120000; // 2 mins
mcasas 2016/11/04 21:45:52 micro-nit here and in the following lines: kDefaul
Robert Sesek 2016/11/04 22:05:13 These were from the old file (https://codereview.c
110 const int kNoChangePollingInterval = 300000; // 5 mins
111 const int kTwoNoChangePollingInterval = 600000; // 10 mins
112 const int kNoWifiPollingIntervalMilliseconds = 20 * 1000; // 20s
113
114 } // namespace
115
116 // static
117 WifiDataProvider* WifiDataProviderManager::DefaultFactoryFunction() {
118 return new WifiDataProviderMac();
182 } 119 }
183 120
184 WifiDataProviderCommon::WlanApiInterface* NewCoreWlanApi() { 121 WifiDataProviderMac::WifiDataProviderMac() {}
185 std::unique_ptr<CoreWlanApi> self(new CoreWlanApi);
186 if (self->Init())
187 return self.release();
188 122
189 return NULL; 123 WifiDataProviderMac::~WifiDataProviderMac() {}
124
125 WifiDataProviderMac::WlanApiInterface* WifiDataProviderMac::NewWlanApi() {
126 return new CoreWlanApi();
127 }
128
129 WifiPollingPolicy* WifiDataProviderMac::NewPollingPolicy() {
130 return new GenericWifiPollingPolicy<
131 kDefaultPollingInterval, kNoChangePollingInterval,
132 kTwoNoChangePollingInterval, kNoWifiPollingIntervalMilliseconds>;
190 } 133 }
191 134
192 } // namespace device 135 } // namespace device
OLDNEW
« no previous file with comments | « device/geolocation/wifi_data_provider_mac.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698