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

Side by Side Diff: chrome/browser/geolocation/wifi_data_provider_mac.cc

Issue 6591034: Move core pieces of geolocation from chrome to content.... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: fix Linux build Created 9 years, 9 months 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 | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2010 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 // For OSX 10.5 we use the system API function WirelessScanSplit. This function
6 // is not documented or included in the SDK, so we use a reverse-engineered
7 // header, osx_wifi_.h. This file is taken from the iStumbler project
8 // (http://www.istumbler.net).
9
10 #include "chrome/browser/geolocation/wifi_data_provider_mac.h"
11
12 #include <dlfcn.h>
13 #include <stdio.h>
14 #include "base/utf_string_conversions.h"
15 #include "chrome/browser/geolocation/osx_wifi.h"
16 #include "chrome/browser/geolocation/wifi_data_provider_common.h"
17
18 namespace {
19 // The time periods, in milliseconds, between successive polls of the wifi data.
20 const int kDefaultPollingInterval = 120000; // 2 mins
21 const int kNoChangePollingInterval = 300000; // 5 mins
22 const int kTwoNoChangePollingInterval = 600000; // 10 mins
23 const int kNoWifiPollingIntervalMilliseconds = 20 * 1000; // 20s
24
25 // Provides the wifi API binding for use when running on OSX 10.5 machines using
26 // the Apple80211 framework.
27 class Apple80211Api : public WifiDataProviderCommon::WlanApiInterface {
28 public:
29 Apple80211Api();
30 virtual ~Apple80211Api();
31
32 // Must be called before any other interface method. Will return false if the
33 // Apple80211 framework cannot be initialized (e.g. running on post-10.5 OSX),
34 // in which case no other method may be called.
35 bool Init();
36
37 // WlanApiInterface
38 virtual bool GetAccessPointData(WifiData::AccessPointDataSet* data);
39
40 private:
41 // Handle, context and function pointers for Apple80211 library.
42 void* apple_80211_library_;
43 WirelessContext* wifi_context_;
44 WirelessAttachFunction WirelessAttach_function_;
45 WirelessScanSplitFunction WirelessScanSplit_function_;
46 WirelessDetachFunction WirelessDetach_function_;
47
48 WifiData wifi_data_;
49
50 DISALLOW_COPY_AND_ASSIGN(Apple80211Api);
51 };
52
53 Apple80211Api::Apple80211Api()
54 : apple_80211_library_(NULL), wifi_context_(NULL),
55 WirelessAttach_function_(NULL), WirelessScanSplit_function_(NULL),
56 WirelessDetach_function_(NULL) {
57 }
58
59 Apple80211Api::~Apple80211Api() {
60 if (WirelessDetach_function_)
61 (*WirelessDetach_function_)(wifi_context_);
62 dlclose(apple_80211_library_);
63 }
64
65 bool Apple80211Api::Init() {
66 DVLOG(1) << "Apple80211Api::Init";
67 apple_80211_library_ = dlopen(
68 "/System/Library/PrivateFrameworks/Apple80211.framework/Apple80211",
69 RTLD_LAZY);
70 if (!apple_80211_library_) {
71 DLOG(WARNING) << "Could not open Apple80211 library";
72 return false;
73 }
74 WirelessAttach_function_ = reinterpret_cast<WirelessAttachFunction>(
75 dlsym(apple_80211_library_, "WirelessAttach"));
76 WirelessScanSplit_function_ = reinterpret_cast<WirelessScanSplitFunction>(
77 dlsym(apple_80211_library_, "WirelessScanSplit"));
78 WirelessDetach_function_ = reinterpret_cast<WirelessDetachFunction>(
79 dlsym(apple_80211_library_, "WirelessDetach"));
80 DCHECK(WirelessAttach_function_);
81 DCHECK(WirelessScanSplit_function_);
82 DCHECK(WirelessDetach_function_);
83
84 if (!WirelessAttach_function_ || !WirelessScanSplit_function_ ||
85 !WirelessDetach_function_) {
86 DLOG(WARNING) << "Symbol error. Attach: " << !!WirelessAttach_function_
87 << " Split: " << !!WirelessScanSplit_function_
88 << " Detach: " << !!WirelessDetach_function_;
89 return false;
90 }
91
92 WIErr err = (*WirelessAttach_function_)(&wifi_context_, 0);
93 if (err != noErr) {
94 DLOG(WARNING) << "Error attaching: " << err;
95 return false;
96 }
97 return true;
98 }
99
100 bool Apple80211Api::GetAccessPointData(WifiData::AccessPointDataSet* data) {
101 DVLOG(1) << "Apple80211Api::GetAccessPointData";
102 DCHECK(data);
103 DCHECK(WirelessScanSplit_function_);
104 CFArrayRef managed_access_points = NULL;
105 CFArrayRef adhoc_access_points = NULL;
106 // Arrays returned here are owned by the caller.
107 WIErr err = (*WirelessScanSplit_function_)(wifi_context_,
108 &managed_access_points,
109 &adhoc_access_points,
110 0);
111 if (err != noErr) {
112 DLOG(WARNING) << "Error spliting scan: " << err;
113 return false;
114 }
115
116 if (managed_access_points == NULL) {
117 DLOG(WARNING) << "managed_access_points == NULL";
118 return false;
119 }
120
121 int num_access_points = CFArrayGetCount(managed_access_points);
122 DVLOG(1) << "Found " << num_access_points << " managed access points";
123 for (int i = 0; i < num_access_points; ++i) {
124 const WirelessNetworkInfo* access_point_info =
125 reinterpret_cast<const WirelessNetworkInfo*>(
126 CFDataGetBytePtr(
127 reinterpret_cast<const CFDataRef>(
128 CFArrayGetValueAtIndex(managed_access_points, i))));
129
130 // Currently we get only MAC address, signal strength, channel
131 // signal-to-noise and SSID
132 AccessPointData access_point_data;
133 access_point_data.mac_address =
134 MacAddressAsString16(access_point_info->macAddress);
135 // WirelessNetworkInfo::signal appears to be signal strength in dBm.
136 access_point_data.radio_signal_strength = access_point_info->signal;
137 access_point_data.channel = access_point_info->channel;
138 // WirelessNetworkInfo::noise appears to be noise floor in dBm.
139 access_point_data.signal_to_noise = access_point_info->signal -
140 access_point_info->noise;
141 if (!UTF8ToUTF16(reinterpret_cast<const char*>(access_point_info->name),
142 access_point_info->nameLen,
143 &access_point_data.ssid)) {
144 access_point_data.ssid.clear();
145 }
146 data->insert(access_point_data);
147 }
148
149 if (managed_access_points)
150 CFRelease(managed_access_points);
151 if (adhoc_access_points)
152 CFRelease(adhoc_access_points);
153
154 return true;
155 }
156 } // namespace
157
158 // static
159 template<>
160 WifiDataProviderImplBase* WifiDataProvider::DefaultFactoryFunction() {
161 return new MacWifiDataProvider();
162 }
163
164 MacWifiDataProvider::MacWifiDataProvider() {
165 }
166
167 MacWifiDataProvider::~MacWifiDataProvider() {
168 }
169
170 MacWifiDataProvider::WlanApiInterface* MacWifiDataProvider::NewWlanApi() {
171 // Try and find a API binding that works: first try the officially supported
172 // CoreWLAN API, and if this fails (e.g. on OSX 10.5) fall back to the reverse
173 // engineered Apple80211 API.
174 MacWifiDataProvider::WlanApiInterface* core_wlan_api = NewCoreWlanApi();
175 if (core_wlan_api)
176 return core_wlan_api;
177
178 scoped_ptr<Apple80211Api> wlan_api(new Apple80211Api);
179 if (wlan_api->Init())
180 return wlan_api.release();
181
182 DVLOG(1) << "MacWifiDataProvider : failed to initialize any wlan api";
183 return NULL;
184 }
185
186 PollingPolicyInterface* MacWifiDataProvider::NewPollingPolicy() {
187 return new GenericPollingPolicy<kDefaultPollingInterval,
188 kNoChangePollingInterval,
189 kTwoNoChangePollingInterval,
190 kNoWifiPollingIntervalMilliseconds>;
191 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698