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

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

Issue 604019: Refactor the state-machine & threading out of the windows wifi provider into ... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 10 years, 10 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
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. 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 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 // Windows Vista uses the Native Wifi (WLAN) API for accessing WiFi cards. See 5 // Windows Vista uses the Native Wifi (WLAN) API for accessing WiFi cards. See
6 // http://msdn.microsoft.com/en-us/library/ms705945(VS.85).aspx. Windows XP 6 // http://msdn.microsoft.com/en-us/library/ms705945(VS.85).aspx. Windows XP
7 // Service Pack 3 (and Windows XP Service Pack 2, if upgraded with a hot fix) 7 // Service Pack 3 (and Windows XP Service Pack 2, if upgraded with a hot fix)
8 // also support a limited version of the WLAN API. See 8 // also support a limited version of the WLAN API. See
9 // http://msdn.microsoft.com/en-us/library/bb204766.aspx. The WLAN API uses 9 // http://msdn.microsoft.com/en-us/library/bb204766.aspx. The WLAN API uses
10 // wlanapi.h, which is not part of the SDK used by Gears, so is replicated 10 // wlanapi.h, which is not part of the SDK used by Gears, so is replicated
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
72 72
73 // WlanFreeMemory 73 // WlanFreeMemory
74 typedef VOID (WINAPI *WlanFreeMemoryFunction)(PVOID pMemory); 74 typedef VOID (WINAPI *WlanFreeMemoryFunction)(PVOID pMemory);
75 75
76 // WlanCloseHandle 76 // WlanCloseHandle
77 typedef DWORD (WINAPI *WlanCloseHandleFunction)(HANDLE hClientHandle, 77 typedef DWORD (WINAPI *WlanCloseHandleFunction)(HANDLE hClientHandle,
78 PVOID pReserved); 78 PVOID pReserved);
79 79
80 80
81 // Local classes and functions 81 // Local classes and functions
82 class WindowsWlanApi : public Win32WifiDataProvider::WlanApiInterface { 82 class WindowsWlanApi : public WifiDataProviderCommon::WlanApiInterface {
83 public: 83 public:
84 ~WindowsWlanApi(); 84 ~WindowsWlanApi();
85 // Factory function. Will return NULL if this API is unavailable. 85 // Factory function. Will return NULL if this API is unavailable.
86 static WindowsWlanApi* Create(); 86 static WindowsWlanApi* Create();
87 87
88 // WlanApiInterface 88 // WlanApiInterface
89 virtual bool GetAccessPointData(WifiData::AccessPointDataSet *data); 89 virtual bool GetAccessPointData(WifiData::AccessPointDataSet *data);
90 90
91 private: 91 private:
92 // Takes ownership of the library handle. 92 // Takes ownership of the library handle.
93 explicit WindowsWlanApi(HINSTANCE library); 93 explicit WindowsWlanApi(HINSTANCE library);
94 94
95 // Loads the required functions from the DLL. 95 // Loads the required functions from the DLL.
96 void GetWLANFunctions(HINSTANCE wlan_library); 96 void GetWLANFunctions(HINSTANCE wlan_library);
97 int GetInterfaceDataWLAN(HANDLE wlan_handle, 97 int GetInterfaceDataWLAN(HANDLE wlan_handle,
98 const GUID &interface_id, 98 const GUID &interface_id,
99 WifiData::AccessPointDataSet *data); 99 WifiData::AccessPointDataSet *data);
100 100
101 // Handle to the wlanapi.dll library. 101 // Handle to the wlanapi.dll library.
102 HINSTANCE library_; 102 HINSTANCE library_;
103 103
104 // Function pointers for WLAN 104 // Function pointers for WLAN
105 WlanOpenHandleFunction WlanOpenHandle_function_; 105 WlanOpenHandleFunction WlanOpenHandle_function_;
106 WlanEnumInterfacesFunction WlanEnumInterfaces_function_; 106 WlanEnumInterfacesFunction WlanEnumInterfaces_function_;
107 WlanGetNetworkBssListFunction WlanGetNetworkBssList_function_; 107 WlanGetNetworkBssListFunction WlanGetNetworkBssList_function_;
108 WlanFreeMemoryFunction WlanFreeMemory_function_; 108 WlanFreeMemoryFunction WlanFreeMemory_function_;
109 WlanCloseHandleFunction WlanCloseHandle_function_; 109 WlanCloseHandleFunction WlanCloseHandle_function_;
110 }; 110 };
111 111
112 class WindowsNdisApi : public Win32WifiDataProvider::WlanApiInterface { 112 class WindowsNdisApi : public WifiDataProviderCommon::WlanApiInterface {
113 public: 113 public:
114 ~WindowsNdisApi(); 114 ~WindowsNdisApi();
115 static WindowsNdisApi* Create(); 115 static WindowsNdisApi* Create();
116 116
117 // WlanApiInterface 117 // WlanApiInterface
118 virtual bool GetAccessPointData(WifiData::AccessPointDataSet *data); 118 virtual bool GetAccessPointData(WifiData::AccessPointDataSet *data);
119 119
120 private: 120 private:
121 static bool GetInterfacesNDIS( 121 static bool GetInterfacesNDIS(
122 std::vector<string16>* interface_service_names_out); 122 std::vector<string16>* interface_service_names_out);
(...skipping 29 matching lines...) Expand all
152 // Gets the system directory and appends a trailing slash if not already 152 // Gets the system directory and appends a trailing slash if not already
153 // present. 153 // present.
154 bool GetSystemDirectory(string16 *path); 154 bool GetSystemDirectory(string16 *path);
155 } // namespace 155 } // namespace
156 156
157 template<> 157 template<>
158 WifiDataProviderImplBase* WifiDataProvider::DefaultFactoryFunction() { 158 WifiDataProviderImplBase* WifiDataProvider::DefaultFactoryFunction() {
159 return new Win32WifiDataProvider(); 159 return new Win32WifiDataProvider();
160 } 160 }
161 161
162 Win32WifiDataProvider::Win32WifiDataProvider() 162 Win32WifiDataProvider::Win32WifiDataProvider() {
163 : Thread(__FILE__),
164 is_first_scan_complete_(false),
165 ALLOW_THIS_IN_INITIALIZER_LIST(task_factory_(this)) {
166 } 163 }
167 164
168 Win32WifiDataProvider::~Win32WifiDataProvider() { 165 Win32WifiDataProvider::~Win32WifiDataProvider() {
169 // Thread must be stopped before entering destructor chain to avoid race
170 // conditions; see comment in DeviceDataProvider::Unregister.
171 DCHECK(!IsRunning()) << "Must call StopDataProvider before destroying me";
172 } 166 }
173 167
174 void Win32WifiDataProvider::inject_mock_wlan_api(WlanApiInterface* wlan_api) { 168 WifiDataProviderCommon::WlanApiInterface* Win32WifiDataProvider::NewWlanApi() {
175 DCHECK(wlan_api_ == NULL); 169 // Use the WLAN interface if we're on Vista and if it's available. Otherwise,
176 DCHECK(wlan_api); 170 // use NDIS.
177 wlan_api_.reset(wlan_api); 171 WlanApiInterface* api = WindowsWlanApi::Create();
172 if (api) {
173 return api;
174 }
175 return WindowsNdisApi::Create();
178 } 176 }
179 177
180 void Win32WifiDataProvider::inject_mock_polling_policy( 178 PollingPolicyInterface* Win32WifiDataProvider::NewPolicyPolicy() {
181 PollingPolicyInterface* policy) { 179 return new GenericPollingPolicy<kDefaultPollingInterval,
182 DCHECK(polling_policy_ == NULL); 180 kNoChangePollingInterval,
183 DCHECK(policy); 181 kTwoNoChangePollingInterval>;
184 polling_policy_.reset(policy);
185 }
186
187 bool Win32WifiDataProvider::StartDataProvider() {
188 DCHECK(CalledOnClientThread());
189 return Start();
190 }
191
192 void Win32WifiDataProvider::StopDataProvider() {
193 DCHECK(CalledOnClientThread());
194 Stop();
195 }
196
197 bool Win32WifiDataProvider::GetData(WifiData *data) {
198 DCHECK(CalledOnClientThread());
199 DCHECK(data);
200 AutoLock lock(data_mutex_);
201 *data = wifi_data_;
202 // If we've successfully completed a scan, indicate that we have all of the
203 // data we can get.
204 return is_first_scan_complete_;
205 }
206
207 // Thread implementation
208 void Win32WifiDataProvider::Init() {
209 // Use the WLAN interface if we're on Vista and if it's available. Otherwise,
210 // use NDIS.
211 if (wlan_api_ == NULL) {
212 wlan_api_.reset(WindowsWlanApi::Create());
213 }
214 if (wlan_api_ == NULL) {
215 wlan_api_.reset(WindowsNdisApi::Create());
216 }
217 if (wlan_api_ == NULL) {
218 // Error! Can't do scans, so don't try and schedule one.
219 is_first_scan_complete_ = true;
220 return;
221 }
222
223 if (polling_policy_ == NULL) {
224 polling_policy_.reset(
225 new GenericPollingPolicy<kDefaultPollingInterval,
226 kNoChangePollingInterval,
227 kTwoNoChangePollingInterval>);
228 }
229 DCHECK(polling_policy_ != NULL);
230
231 ScheduleNextScan();
232 }
233
234 void Win32WifiDataProvider::CleanUp() {
235 // Destroy the wlan api instance in the thread in which it was created.
236 wlan_api_.reset();
237 }
238
239 void Win32WifiDataProvider::DoWifiScanTask() {
240 // TODO(joth): Almost all of this is shareable across platforms.
241 WifiData new_data;
242 if (wlan_api_->GetAccessPointData(&new_data.access_point_data)) {
243 bool update_available;
244 data_mutex_.Acquire();
245 update_available = wifi_data_.DiffersSignificantly(new_data);
246 wifi_data_ = new_data;
247 data_mutex_.Release();
248 polling_policy_->UpdatePollingInterval(update_available);
249 if (update_available || !is_first_scan_complete_) {
250 is_first_scan_complete_ = true;
251 NotifyListeners();
252 }
253 }
254 ScheduleNextScan();
255 }
256
257 void Win32WifiDataProvider::ScheduleNextScan() {
258 message_loop()->PostDelayedTask(FROM_HERE,
259 task_factory_.NewRunnableMethod(&Win32WifiDataProvider::DoWifiScanTask),
260 polling_policy_->PollingInterval());
261 } 182 }
262 183
263 // Local classes and functions 184 // Local classes and functions
264 namespace { 185 namespace {
265 186
266 // WindowsWlanApi 187 // WindowsWlanApi
267 WindowsWlanApi::WindowsWlanApi(HINSTANCE library) 188 WindowsWlanApi::WindowsWlanApi(HINSTANCE library)
268 : library_(library) { 189 : library_(library) {
269 GetWLANFunctions(library_); 190 GetWLANFunctions(library_);
270 } 191 }
(...skipping 361 matching lines...) Expand 10 before | Expand all | Expand 10 after
632 buffer, 553 buffer,
633 buffer_size, 554 buffer_size,
634 bytes_out, 555 bytes_out,
635 NULL)) { 556 NULL)) {
636 return GetLastError(); 557 return GetLastError();
637 } 558 }
638 return ERROR_SUCCESS; 559 return ERROR_SUCCESS;
639 } 560 }
640 561
641 bool ResizeBuffer(int requested_size, scoped_ptr_malloc<BYTE>* buffer) { 562 bool ResizeBuffer(int requested_size, scoped_ptr_malloc<BYTE>* buffer) {
642 DCHECK(requested_size > 0); 563 DCHECK_GT(requested_size, 0);
643 DCHECK(buffer); 564 DCHECK(buffer);
644 if (requested_size > kMaximumBufferSize) { 565 if (requested_size > kMaximumBufferSize) {
645 buffer->reset(); 566 buffer->reset();
646 return false; 567 return false;
647 } 568 }
648 569
649 buffer->reset(reinterpret_cast<BYTE*>( 570 buffer->reset(reinterpret_cast<BYTE*>(
650 realloc(buffer->release(), requested_size))); 571 realloc(buffer->release(), requested_size)));
651 return buffer != NULL; 572 return buffer != NULL;
652 } 573 }
(...skipping 16 matching lines...) Expand all
669 590
670 path->assign(buffer); 591 path->assign(buffer);
671 delete[] buffer; 592 delete[] buffer;
672 593
673 if (path->at(path->length() - 1) != L'\\') { 594 if (path->at(path->length() - 1) != L'\\') {
674 path->append(L"\\"); 595 path->append(L"\\");
675 } 596 }
676 return true; 597 return true;
677 } 598 }
678 } // namespace 599 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698