| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 5 // Implements a WLAN API binding for CoreWLAN, as available on OSX 10.6 | |
| 6 | |
| 7 #include "content/browser/geolocation/wifi_data_provider_mac.h" | |
| 8 | |
| 9 #include <dlfcn.h> | |
| 10 #import <Foundation/Foundation.h> | |
| 11 #include <stdint.h> | |
| 12 | |
| 13 #include "base/mac/scoped_nsautorelease_pool.h" | |
| 14 #include "base/mac/scoped_nsobject.h" | |
| 15 #include "base/macros.h" | |
| 16 #include "base/metrics/histogram.h" | |
| 17 #include "base/strings/sys_string_conversions.h" | |
| 18 | |
| 19 // Define a subset of the CoreWLAN interfaces we require. We can't depend on | |
| 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 | |
| 26 @interface CWInterface : NSObject | |
| 27 + (CWInterface*)interface; | |
| 28 + (CWInterface*)interfaceWithName:(NSString*)name; | |
| 29 + (NSArray*)supportedInterfaces; | |
| 30 - (NSArray*)scanForNetworksWithParameters:(NSDictionary*)parameters | |
| 31 error:(NSError**)error; | |
| 32 @end | |
| 33 | |
| 34 @interface CWNetwork : NSObject <NSCopying, NSCoding> | |
| 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 | |
| 49 namespace content { | |
| 50 | |
| 51 class CoreWlanApi : public WifiDataProviderCommon::WlanApiInterface { | |
| 52 public: | |
| 53 CoreWlanApi() {} | |
| 54 | |
| 55 // Must be called before any other interface method. Will return false if the | |
| 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; | |
| 62 | |
| 63 private: | |
| 64 base::scoped_nsobject<NSBundle> bundle_; | |
| 65 base::scoped_nsobject<NSString> merge_key_; | |
| 66 | |
| 67 DISALLOW_COPY_AND_ASSIGN(CoreWlanApi); | |
| 68 }; | |
| 69 | |
| 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) { | |
| 107 base::mac::ScopedNSAutoreleasePool auto_pool; | |
| 108 // 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. | |
| 110 NSDictionary* params = | |
| 111 [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:NO] | |
| 112 forKey:merge_key_.get()]; | |
| 113 | |
| 114 Class cw_interface_class = [bundle_ classNamed:@"CWInterface"]; | |
| 115 NSArray* supported_interfaces = [cw_interface_class supportedInterfaces]; | |
| 116 uint interface_error_count = 0; | |
| 117 for (NSString* interface_name in supported_interfaces) { | |
| 118 CWInterface* corewlan_interface = | |
| 119 [cw_interface_class interfaceWithName:interface_name]; | |
| 120 if (!corewlan_interface) { | |
| 121 DLOG(WARNING) << interface_name << ": initWithName failed"; | |
| 122 ++interface_error_count; | |
| 123 continue; | |
| 124 } | |
| 125 | |
| 126 const base::TimeTicks start_time = base::TimeTicks::Now(); | |
| 127 | |
| 128 NSError* err = nil; | |
| 129 NSArray* scan = [corewlan_interface scanForNetworksWithParameters:params | |
| 130 error:&err]; | |
| 131 const int error_code = [err code]; | |
| 132 const int count = [scan count]; | |
| 133 // 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. | |
| 135 if (error_code && count == 0) { | |
| 136 DLOG(WARNING) << interface_name << ": CoreWLAN scan failed with error " | |
| 137 << error_code; | |
| 138 ++interface_error_count; | |
| 139 continue; | |
| 140 } | |
| 141 | |
| 142 const base::TimeDelta duration = base::TimeTicks::Now() - start_time; | |
| 143 | |
| 144 UMA_HISTOGRAM_CUSTOM_TIMES( | |
| 145 "Net.Wifi.ScanLatency", | |
| 146 duration, | |
| 147 base::TimeDelta::FromMilliseconds(1), | |
| 148 base::TimeDelta::FromMinutes(1), | |
| 149 100); | |
| 150 | |
| 151 DVLOG(1) << interface_name << ": found " << count << " wifi APs"; | |
| 152 | |
| 153 for (CWNetwork* network in scan) { | |
| 154 DCHECK(network); | |
| 155 AccessPointData access_point_data; | |
| 156 NSData* mac = [network bssidData]; | |
| 157 DCHECK([mac length] == 6); | |
| 158 if (![mac bytes]) | |
| 159 continue; // crbug.com/545501 | |
| 160 access_point_data.mac_address = | |
| 161 MacAddressAsString16(static_cast<const uint8_t*>([mac bytes])); | |
| 162 access_point_data.radio_signal_strength = [network rssiValue]; | |
| 163 access_point_data.channel = [[network channel] intValue]; | |
| 164 access_point_data.signal_to_noise = | |
| 165 access_point_data.radio_signal_strength - [[network noise] intValue]; | |
| 166 access_point_data.ssid = base::SysNSStringToUTF16([network ssid]); | |
| 167 data->insert(access_point_data); | |
| 168 } | |
| 169 } | |
| 170 | |
| 171 UMA_HISTOGRAM_CUSTOM_COUNTS( | |
| 172 "Net.Wifi.InterfaceCount", | |
| 173 [supported_interfaces count] - interface_error_count, | |
| 174 1, | |
| 175 5, | |
| 176 6); | |
| 177 | |
| 178 // Return true even if some interfaces failed to scan, so long as at least | |
| 179 // one interface did not fail. | |
| 180 return interface_error_count == 0 || | |
| 181 [supported_interfaces count] > interface_error_count; | |
| 182 } | |
| 183 | |
| 184 WifiDataProviderCommon::WlanApiInterface* NewCoreWlanApi() { | |
| 185 std::unique_ptr<CoreWlanApi> self(new CoreWlanApi); | |
| 186 if (self->Init()) | |
| 187 return self.release(); | |
| 188 | |
| 189 return NULL; | |
| 190 } | |
| 191 | |
| 192 } // namespace content | |
| OLD | NEW |