1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 // Implements a WLAN API binding for CoreWLAN, as available on OSX 10.6
7 #include "content/browser/geolocation/wifi_data_provider_mac.h"
10 #import <Foundation/Foundation.h>
12 #include "base/mac/scoped_nsautorelease_pool.h"
13 #include "base/mac/scoped_nsobject.h"
14 #include "base/metrics/histogram.h"
15 #include "base/strings/sys_string_conversions.h"
17 // Define a subset of the CoreWLAN interfaces we require. We can't depend on
18 // CoreWLAN.h existing as we need to build on 10.5 SDKs. We can't just send
19 // messages to an untyped id due to the build treating warnings as errors,
20 // hence the reason we need class definitions.
21 // TODO(joth): When we build all 10.6 code exclusively 10.6 SDK (or later)
22 // tidy this up to use the framework directly. See http://crbug.com/37703
24 @interface CWInterface : NSObject
25 + (CWInterface*)interface;
26 + (CWInterface*)interfaceWithName:(NSString*)name;
27 + (NSArray*)supportedInterfaces;
28 - (NSArray*)scanForNetworksWithParameters:(NSDictionary*)parameters
29 error:(NSError**)error;
32 @interface CWNetwork : NSObject <NSCopying, NSCoding>
33 @property (nonatomic, readonly) NSString* ssid;
34 @property (nonatomic, readonly) NSString* bssid;
35 @property (nonatomic, readonly) NSData* bssidData;
36 @property (nonatomic, readonly) NSNumber* securityMode;
37 @property (nonatomic, readonly) NSNumber* phyMode;
38 @property (nonatomic, readonly) NSNumber* channel;
39 @property (nonatomic, readonly) NSNumber* rssi;
40 @property (nonatomic, readonly) NSNumber* noise;
41 @property (nonatomic, readonly) NSData* ieData;
42 @property (nonatomic, readonly) BOOL isIBSS;
43 - (BOOL)isEqualToNetwork:(CWNetwork*)network;
48 class CoreWlanApi : public WifiDataProviderCommon::WlanApiInterface {
52 // Must be called before any other interface method. Will return false if the
53 // CoreWLAN framework cannot be initialized (e.g. running on pre-10.6 OSX),
54 // in which case no other method may be called.
58 virtual bool GetAccessPointData(WifiData::AccessPointDataSet* data) OVERRIDE;
61 base::scoped_nsobject<NSBundle> bundle_;
62 base::scoped_nsobject<NSString> merge_key_;
64 DISALLOW_COPY_AND_ASSIGN(CoreWlanApi);
67 bool CoreWlanApi::Init() {
68 // As the WLAN api binding runs on its own thread, we need to provide our own
69 // auto release pool. It's simplest to do this as an automatic variable in
70 // each method that needs it, to ensure the scoping is correct and does not
71 // interfere with any other code using autorelease pools on the thread.
72 base::mac::ScopedNSAutoreleasePool auto_pool;
73 bundle_.reset([[NSBundle alloc]
74 initWithPath:@"/System/Library/Frameworks/CoreWLAN.framework"]);
76 DVLOG(1) << "Failed to load the CoreWLAN framework bundle";
80 // Dynamically look up the value of the kCWScanKeyMerge (i.e. without build
81 // time dependency on the 10.6 specific library).
82 void* dl_handle = dlopen([[bundle_ executablePath] fileSystemRepresentation],
83 RTLD_LAZY | RTLD_LOCAL);
85 NSString* key = *reinterpret_cast<NSString**>(dlsym(dl_handle,
88 merge_key_.reset([key copy]);
90 // "Leak" dl_handle rather than dlclose it, to ensure |merge_key_|
93 // Fall back to a known-working value should the lookup fail (if
94 // this value is itself wrong it's not the end of the world, we might just
95 // get very slightly lower quality location fixes due to SSID merges).
96 DLOG(WARNING) << "Could not dynamically load the CoreWLAN merge key";
97 merge_key_.reset([@"SCAN_MERGE" retain]);
103 bool CoreWlanApi::GetAccessPointData(WifiData::AccessPointDataSet* data) {
104 base::mac::ScopedNSAutoreleasePool auto_pool;
105 // Initialize the scan parameters with scan key merging disabled, so we get
106 // every AP listed in the scan without any SSID de-duping logic.
107 NSDictionary* params =
108 [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:NO]
109 forKey:merge_key_.get()];
111 Class cw_interface_class = [bundle_ classNamed:@"CWInterface"];
112 NSArray* supported_interfaces = [cw_interface_class supportedInterfaces];
113 uint interface_error_count = 0;
114 for (NSString* interface_name in supported_interfaces) {
115 CWInterface* corewlan_interface =
116 [cw_interface_class interfaceWithName:interface_name];
117 if (!corewlan_interface) {
118 DLOG(WARNING) << interface_name << ": initWithName failed";
119 ++interface_error_count;
123 const base::TimeTicks start_time = base::TimeTicks::Now();
126 NSArray* scan = [corewlan_interface scanForNetworksWithParameters:params
128 const int error_code = [err code];
129 const int count = [scan count];
130 // We could get an error code but count != 0 if the scan was interrupted,
131 // for example. For our purposes this is not fatal, so process as normal.
132 if (error_code && count == 0) {
133 DLOG(WARNING) << interface_name << ": CoreWLAN scan failed with error "
135 ++interface_error_count;
139 const base::TimeDelta duration = base::TimeTicks::Now() - start_time;
141 UMA_HISTOGRAM_CUSTOM_TIMES(
142 "Net.Wifi.ScanLatency",
144 base::TimeDelta::FromMilliseconds(1),
145 base::TimeDelta::FromMinutes(1),
148 DVLOG(1) << interface_name << ": found " << count << " wifi APs";
150 for (CWNetwork* network in scan) {
152 AccessPointData access_point_data;
153 NSData* mac = [network bssidData];
154 DCHECK([mac length] == 6);
155 access_point_data.mac_address = MacAddressAsString16(
156 static_cast<const uint8*>([mac bytes]));
157 access_point_data.radio_signal_strength = [[network rssi] intValue];
158 access_point_data.channel = [[network channel] intValue];
159 access_point_data.signal_to_noise =
160 access_point_data.radio_signal_strength - [[network noise] intValue];
161 access_point_data.ssid = base::SysNSStringToUTF16([network ssid]);
162 data->insert(access_point_data);
166 UMA_HISTOGRAM_CUSTOM_COUNTS(
167 "Net.Wifi.InterfaceCount",
168 [supported_interfaces count] - interface_error_count,
173 // Return true even if some interfaces failed to scan, so long as at least
174 // one interface did not fail.
175 return interface_error_count == 0 ||
176 [supported_interfaces count] > interface_error_count;
179 WifiDataProviderCommon::WlanApiInterface* NewCoreWlanApi() {
180 scoped_ptr<CoreWlanApi> self(new CoreWlanApi);
182 return self.release();
187 } // namespace content