OLD | NEW |
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 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
120 class WindowsNdisApi : public WifiDataProviderCommon::WlanApiInterface { | 120 class WindowsNdisApi : public WifiDataProviderCommon::WlanApiInterface { |
121 public: | 121 public: |
122 virtual ~WindowsNdisApi(); | 122 virtual ~WindowsNdisApi(); |
123 static WindowsNdisApi* Create(); | 123 static WindowsNdisApi* Create(); |
124 | 124 |
125 // WlanApiInterface | 125 // WlanApiInterface |
126 virtual bool GetAccessPointData(WifiData::AccessPointDataSet* data); | 126 virtual bool GetAccessPointData(WifiData::AccessPointDataSet* data); |
127 | 127 |
128 private: | 128 private: |
129 static bool GetInterfacesNDIS( | 129 static bool GetInterfacesNDIS( |
130 std::vector<string16>* interface_service_names_out); | 130 std::vector<base::string16>* interface_service_names_out); |
131 | 131 |
132 // Swaps in content of the vector passed | 132 // Swaps in content of the vector passed |
133 explicit WindowsNdisApi(std::vector<string16>* interface_service_names); | 133 explicit WindowsNdisApi(std::vector<base::string16>* interface_service_names); |
134 | 134 |
135 bool GetInterfaceDataNDIS(HANDLE adapter_handle, | 135 bool GetInterfaceDataNDIS(HANDLE adapter_handle, |
136 WifiData::AccessPointDataSet* data); | 136 WifiData::AccessPointDataSet* data); |
137 // NDIS variables. | 137 // NDIS variables. |
138 std::vector<string16> interface_service_names_; | 138 std::vector<base::string16> interface_service_names_; |
139 | 139 |
140 // Remembers scan result buffer size across calls. | 140 // Remembers scan result buffer size across calls. |
141 int oid_buffer_size_; | 141 int oid_buffer_size_; |
142 }; | 142 }; |
143 | 143 |
144 // Extracts data for an access point and converts to Gears format. | 144 // Extracts data for an access point and converts to Gears format. |
145 bool GetNetworkData(const WLAN_BSS_ENTRY& bss_entry, | 145 bool GetNetworkData(const WLAN_BSS_ENTRY& bss_entry, |
146 AccessPointData* access_point_data); | 146 AccessPointData* access_point_data); |
147 bool UndefineDosDevice(const string16& device_name); | 147 bool UndefineDosDevice(const base::string16& device_name); |
148 bool DefineDosDeviceIfNotExists(const string16& device_name); | 148 bool DefineDosDeviceIfNotExists(const base::string16& device_name); |
149 HANDLE GetFileHandle(const string16& device_name); | 149 HANDLE GetFileHandle(const base::string16& device_name); |
150 // Makes the OID query and returns a Win32 error code. | 150 // Makes the OID query and returns a Win32 error code. |
151 int PerformQuery(HANDLE adapter_handle, | 151 int PerformQuery(HANDLE adapter_handle, |
152 BYTE* buffer, | 152 BYTE* buffer, |
153 DWORD buffer_size, | 153 DWORD buffer_size, |
154 DWORD* bytes_out); | 154 DWORD* bytes_out); |
155 bool ResizeBuffer(int requested_size, scoped_ptr_malloc<BYTE>* buffer); | 155 bool ResizeBuffer(int requested_size, scoped_ptr_malloc<BYTE>* buffer); |
156 // Gets the system directory and appends a trailing slash if not already | 156 // Gets the system directory and appends a trailing slash if not already |
157 // present. | 157 // present. |
158 bool GetSystemDirectory(string16* path); | 158 bool GetSystemDirectory(base::string16* path); |
159 } // namespace | 159 } // namespace |
160 | 160 |
161 WifiDataProviderImplBase* WifiDataProvider::DefaultFactoryFunction() { | 161 WifiDataProviderImplBase* WifiDataProvider::DefaultFactoryFunction() { |
162 return new Win32WifiDataProvider(); | 162 return new Win32WifiDataProvider(); |
163 } | 163 } |
164 | 164 |
165 Win32WifiDataProvider::Win32WifiDataProvider() { | 165 Win32WifiDataProvider::Win32WifiDataProvider() { |
166 } | 166 } |
167 | 167 |
168 Win32WifiDataProvider::~Win32WifiDataProvider() { | 168 Win32WifiDataProvider::~Win32WifiDataProvider() { |
(...skipping 26 matching lines...) Expand all Loading... |
195 } | 195 } |
196 | 196 |
197 WindowsWlanApi::~WindowsWlanApi() { | 197 WindowsWlanApi::~WindowsWlanApi() { |
198 FreeLibrary(library_); | 198 FreeLibrary(library_); |
199 } | 199 } |
200 | 200 |
201 WindowsWlanApi* WindowsWlanApi::Create() { | 201 WindowsWlanApi* WindowsWlanApi::Create() { |
202 if (base::win::GetVersion() < base::win::VERSION_VISTA) | 202 if (base::win::GetVersion() < base::win::VERSION_VISTA) |
203 return NULL; | 203 return NULL; |
204 // We use an absolute path to load the DLL to avoid DLL preloading attacks. | 204 // We use an absolute path to load the DLL to avoid DLL preloading attacks. |
205 string16 system_directory; | 205 base::string16 system_directory; |
206 if (!GetSystemDirectory(&system_directory)) { | 206 if (!GetSystemDirectory(&system_directory)) { |
207 return NULL; | 207 return NULL; |
208 } | 208 } |
209 DCHECK(!system_directory.empty()); | 209 DCHECK(!system_directory.empty()); |
210 string16 dll_path = system_directory + L"wlanapi.dll"; | 210 base::string16 dll_path = system_directory + L"wlanapi.dll"; |
211 HINSTANCE library = LoadLibraryEx(dll_path.c_str(), | 211 HINSTANCE library = LoadLibraryEx(dll_path.c_str(), |
212 NULL, | 212 NULL, |
213 LOAD_WITH_ALTERED_SEARCH_PATH); | 213 LOAD_WITH_ALTERED_SEARCH_PATH); |
214 if (!library) { | 214 if (!library) { |
215 return NULL; | 215 return NULL; |
216 } | 216 } |
217 return new WindowsWlanApi(library); | 217 return new WindowsWlanApi(library); |
218 } | 218 } |
219 | 219 |
220 void WindowsWlanApi::GetWLANFunctions(HINSTANCE wlan_library) { | 220 void WindowsWlanApi::GetWLANFunctions(HINSTANCE wlan_library) { |
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
352 } | 352 } |
353 } | 353 } |
354 | 354 |
355 (*WlanFreeMemory_function_)(bss_list); | 355 (*WlanFreeMemory_function_)(bss_list); |
356 | 356 |
357 return found; | 357 return found; |
358 } | 358 } |
359 | 359 |
360 // WindowsNdisApi | 360 // WindowsNdisApi |
361 WindowsNdisApi::WindowsNdisApi( | 361 WindowsNdisApi::WindowsNdisApi( |
362 std::vector<string16>* interface_service_names) | 362 std::vector<base::string16>* interface_service_names) |
363 : oid_buffer_size_(kInitialBufferSize) { | 363 : oid_buffer_size_(kInitialBufferSize) { |
364 DCHECK(!interface_service_names->empty()); | 364 DCHECK(!interface_service_names->empty()); |
365 interface_service_names_.swap(*interface_service_names); | 365 interface_service_names_.swap(*interface_service_names); |
366 } | 366 } |
367 | 367 |
368 WindowsNdisApi::~WindowsNdisApi() { | 368 WindowsNdisApi::~WindowsNdisApi() { |
369 } | 369 } |
370 | 370 |
371 WindowsNdisApi* WindowsNdisApi::Create() { | 371 WindowsNdisApi* WindowsNdisApi::Create() { |
372 std::vector<string16> interface_service_names; | 372 std::vector<base::string16> interface_service_names; |
373 if (GetInterfacesNDIS(&interface_service_names)) { | 373 if (GetInterfacesNDIS(&interface_service_names)) { |
374 return new WindowsNdisApi(&interface_service_names); | 374 return new WindowsNdisApi(&interface_service_names); |
375 } | 375 } |
376 return NULL; | 376 return NULL; |
377 } | 377 } |
378 | 378 |
379 bool WindowsNdisApi::GetAccessPointData(WifiData::AccessPointDataSet* data) { | 379 bool WindowsNdisApi::GetAccessPointData(WifiData::AccessPointDataSet* data) { |
380 DCHECK(data); | 380 DCHECK(data); |
381 int interfaces_failed = 0; | 381 int interfaces_failed = 0; |
382 int interfaces_succeeded = 0; | 382 int interfaces_succeeded = 0; |
(...skipping 22 matching lines...) Expand all Loading... |
405 CloseHandle(adapter_handle); | 405 CloseHandle(adapter_handle); |
406 UndefineDosDevice(interface_service_names_[i]); | 406 UndefineDosDevice(interface_service_names_[i]); |
407 } | 407 } |
408 | 408 |
409 // Return true if at least one interface succeeded, or at the very least none | 409 // Return true if at least one interface succeeded, or at the very least none |
410 // failed. | 410 // failed. |
411 return interfaces_succeeded > 0 || interfaces_failed == 0; | 411 return interfaces_succeeded > 0 || interfaces_failed == 0; |
412 } | 412 } |
413 | 413 |
414 bool WindowsNdisApi::GetInterfacesNDIS( | 414 bool WindowsNdisApi::GetInterfacesNDIS( |
415 std::vector<string16>* interface_service_names_out) { | 415 std::vector<base::string16>* interface_service_names_out) { |
416 HKEY network_cards_key = NULL; | 416 HKEY network_cards_key = NULL; |
417 if (RegOpenKeyEx( | 417 if (RegOpenKeyEx( |
418 HKEY_LOCAL_MACHINE, | 418 HKEY_LOCAL_MACHINE, |
419 L"Software\\Microsoft\\Windows NT\\CurrentVersion\\NetworkCards", | 419 L"Software\\Microsoft\\Windows NT\\CurrentVersion\\NetworkCards", |
420 0, | 420 0, |
421 KEY_READ, | 421 KEY_READ, |
422 &network_cards_key) != ERROR_SUCCESS) { | 422 &network_cards_key) != ERROR_SUCCESS) { |
423 return false; | 423 return false; |
424 } | 424 } |
425 DCHECK(network_cards_key); | 425 DCHECK(network_cards_key); |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
523 UTF8ToUTF16(reinterpret_cast<const char*>(bss_entry.dot11Ssid.ucSSID), | 523 UTF8ToUTF16(reinterpret_cast<const char*>(bss_entry.dot11Ssid.ucSSID), |
524 static_cast<ULONG>(bss_entry.dot11Ssid.uSSIDLength), | 524 static_cast<ULONG>(bss_entry.dot11Ssid.uSSIDLength), |
525 &access_point_data->ssid); | 525 &access_point_data->ssid); |
526 // TODO(steveblock): Is it possible to get the following? | 526 // TODO(steveblock): Is it possible to get the following? |
527 // access_point_data->signal_to_noise | 527 // access_point_data->signal_to_noise |
528 // access_point_data->age | 528 // access_point_data->age |
529 // access_point_data->channel | 529 // access_point_data->channel |
530 return true; | 530 return true; |
531 } | 531 } |
532 | 532 |
533 bool UndefineDosDevice(const string16& device_name) { | 533 bool UndefineDosDevice(const base::string16& device_name) { |
534 // We remove only the mapping we use, that is \Device\<device_name>. | 534 // We remove only the mapping we use, that is \Device\<device_name>. |
535 string16 target_path = L"\\Device\\" + device_name; | 535 base::string16 target_path = L"\\Device\\" + device_name; |
536 return DefineDosDevice( | 536 return DefineDosDevice( |
537 DDD_RAW_TARGET_PATH | DDD_REMOVE_DEFINITION | DDD_EXACT_MATCH_ON_REMOVE, | 537 DDD_RAW_TARGET_PATH | DDD_REMOVE_DEFINITION | DDD_EXACT_MATCH_ON_REMOVE, |
538 device_name.c_str(), | 538 device_name.c_str(), |
539 target_path.c_str()) == TRUE; | 539 target_path.c_str()) == TRUE; |
540 } | 540 } |
541 | 541 |
542 bool DefineDosDeviceIfNotExists(const string16& device_name) { | 542 bool DefineDosDeviceIfNotExists(const base::string16& device_name) { |
543 // We create a DOS device name for the device at \Device\<device_name>. | 543 // We create a DOS device name for the device at \Device\<device_name>. |
544 string16 target_path = L"\\Device\\" + device_name; | 544 base::string16 target_path = L"\\Device\\" + device_name; |
545 | 545 |
546 TCHAR target[kStringLength]; | 546 TCHAR target[kStringLength]; |
547 if (QueryDosDevice(device_name.c_str(), target, kStringLength) > 0 && | 547 if (QueryDosDevice(device_name.c_str(), target, kStringLength) > 0 && |
548 target_path.compare(target) == 0) { | 548 target_path.compare(target) == 0) { |
549 // Device already exists. | 549 // Device already exists. |
550 return true; | 550 return true; |
551 } | 551 } |
552 | 552 |
553 if (GetLastError() != ERROR_FILE_NOT_FOUND) { | 553 if (GetLastError() != ERROR_FILE_NOT_FOUND) { |
554 return false; | 554 return false; |
555 } | 555 } |
556 | 556 |
557 if (!DefineDosDevice(DDD_RAW_TARGET_PATH, | 557 if (!DefineDosDevice(DDD_RAW_TARGET_PATH, |
558 device_name.c_str(), | 558 device_name.c_str(), |
559 target_path.c_str())) { | 559 target_path.c_str())) { |
560 return false; | 560 return false; |
561 } | 561 } |
562 | 562 |
563 // Check that the device is really there. | 563 // Check that the device is really there. |
564 return QueryDosDevice(device_name.c_str(), target, kStringLength) > 0 && | 564 return QueryDosDevice(device_name.c_str(), target, kStringLength) > 0 && |
565 target_path.compare(target) == 0; | 565 target_path.compare(target) == 0; |
566 } | 566 } |
567 | 567 |
568 HANDLE GetFileHandle(const string16& device_name) { | 568 HANDLE GetFileHandle(const base::string16& device_name) { |
569 // We access a device with DOS path \Device\<device_name> at | 569 // We access a device with DOS path \Device\<device_name> at |
570 // \\.\<device_name>. | 570 // \\.\<device_name>. |
571 string16 formatted_device_name = L"\\\\.\\" + device_name; | 571 base::string16 formatted_device_name = L"\\\\.\\" + device_name; |
572 | 572 |
573 return CreateFile(formatted_device_name.c_str(), | 573 return CreateFile(formatted_device_name.c_str(), |
574 GENERIC_READ, | 574 GENERIC_READ, |
575 FILE_SHARE_READ | FILE_SHARE_WRITE, // share mode | 575 FILE_SHARE_READ | FILE_SHARE_WRITE, // share mode |
576 0, // security attributes | 576 0, // security attributes |
577 OPEN_EXISTING, | 577 OPEN_EXISTING, |
578 0, // flags and attributes | 578 0, // flags and attributes |
579 INVALID_HANDLE_VALUE); | 579 INVALID_HANDLE_VALUE); |
580 } | 580 } |
581 | 581 |
(...skipping 21 matching lines...) Expand all Loading... |
603 if (requested_size > kMaximumBufferSize) { | 603 if (requested_size > kMaximumBufferSize) { |
604 buffer->reset(); | 604 buffer->reset(); |
605 return false; | 605 return false; |
606 } | 606 } |
607 | 607 |
608 buffer->reset(reinterpret_cast<BYTE*>( | 608 buffer->reset(reinterpret_cast<BYTE*>( |
609 realloc(buffer->release(), requested_size))); | 609 realloc(buffer->release(), requested_size))); |
610 return buffer != NULL; | 610 return buffer != NULL; |
611 } | 611 } |
612 | 612 |
613 bool GetSystemDirectory(string16* path) { | 613 bool GetSystemDirectory(base::string16* path) { |
614 DCHECK(path); | 614 DCHECK(path); |
615 // Return value includes terminating NULL. | 615 // Return value includes terminating NULL. |
616 int buffer_size = ::GetSystemDirectory(NULL, 0); | 616 int buffer_size = ::GetSystemDirectory(NULL, 0); |
617 if (buffer_size == 0) { | 617 if (buffer_size == 0) { |
618 return false; | 618 return false; |
619 } | 619 } |
620 scoped_ptr<char16[]> buffer(new char16[buffer_size]); | 620 scoped_ptr<char16[]> buffer(new char16[buffer_size]); |
621 | 621 |
622 // Return value excludes terminating NULL. | 622 // Return value excludes terminating NULL. |
623 int characters_written = ::GetSystemDirectory(buffer.get(), buffer_size); | 623 int characters_written = ::GetSystemDirectory(buffer.get(), buffer_size); |
624 if (characters_written == 0) { | 624 if (characters_written == 0) { |
625 return false; | 625 return false; |
626 } | 626 } |
627 DCHECK_EQ(buffer_size - 1, characters_written); | 627 DCHECK_EQ(buffer_size - 1, characters_written); |
628 | 628 |
629 path->assign(buffer.get(), characters_written); | 629 path->assign(buffer.get(), characters_written); |
630 | 630 |
631 if (*path->rbegin() != L'\\') { | 631 if (*path->rbegin() != L'\\') { |
632 path->append(L"\\"); | 632 path->append(L"\\"); |
633 } | 633 } |
634 DCHECK_EQ(L'\\', *path->rbegin()); | 634 DCHECK_EQ(L'\\', *path->rbegin()); |
635 return true; | 635 return true; |
636 } | 636 } |
637 } // namespace | 637 } // namespace |
638 | 638 |
639 } // namespace content | 639 } // namespace content |
OLD | NEW |