Rollup of bug 786631: Get processes with non-default permissions on the prelaunch...
[gecko.git] / netwerk / wifi / nsWifiScannerWin.cpp
blob3bf7826b79087541a95041dfee94de5b6f34db81
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #include "windows.h"
7 #include "wlanapi.h"
9 #include "stdlib.h"
11 #include "nsWifiMonitor.h"
12 #include "nsWifiAccessPoint.h"
14 #include "nsServiceManagerUtils.h"
15 #include "nsComponentManagerUtils.h"
16 #include "nsIMutableArray.h"
18 using namespace mozilla;
20 nsresult
21 nsWifiMonitor::DoScan()
23 HINSTANCE wlan_library = LoadLibrary("Wlanapi.dll");
24 if (!wlan_library)
25 return NS_ERROR_NOT_AVAILABLE;
27 WlanOpenHandleFunction WlanOpenHandle = (WlanOpenHandleFunction) GetProcAddress(wlan_library, "WlanOpenHandle");
28 WlanEnumInterfacesFunction WlanEnumInterfaces = (WlanEnumInterfacesFunction) GetProcAddress(wlan_library, "WlanEnumInterfaces");
29 WlanGetNetworkBssListFunction WlanGetNetworkBssList = (WlanGetNetworkBssListFunction) GetProcAddress(wlan_library, "WlanGetNetworkBssList");
30 WlanFreeMemoryFunction WlanFreeMemory = (WlanFreeMemoryFunction) GetProcAddress(wlan_library, "WlanFreeMemory");
31 WlanCloseHandleFunction WlanCloseHandle = (WlanCloseHandleFunction) GetProcAddress(wlan_library, "WlanCloseHandle");
33 if (!WlanOpenHandle ||
34 !WlanEnumInterfaces ||
35 !WlanGetNetworkBssList ||
36 !WlanFreeMemory ||
37 !WlanCloseHandle)
38 return NS_ERROR_FAILURE;
40 // Regularly get the access point data.
42 nsCOMArray<nsWifiAccessPoint> lastAccessPoints;
43 nsCOMArray<nsWifiAccessPoint> accessPoints;
45 do {
46 accessPoints.Clear();
48 // Get the handle to the WLAN API.
49 DWORD negotiated_version;
50 HANDLE wlan_handle = NULL;
51 // We could be executing on either Windows XP or Windows Vista, so use the
52 // lower version of the client WLAN API. It seems that the negotiated version
53 // is the Vista version irrespective of what we pass!
54 static const int kXpWlanClientVersion = 1;
55 if ((*WlanOpenHandle)(kXpWlanClientVersion,
56 NULL,
57 &negotiated_version,
58 &wlan_handle) != ERROR_SUCCESS) {
59 return NS_ERROR_NOT_AVAILABLE;
62 // try again later.
63 if (!wlan_handle)
64 return NS_ERROR_FAILURE;
66 // Get the list of interfaces. WlanEnumInterfaces allocates interface_list.
67 WLAN_INTERFACE_INFO_LIST *interface_list = NULL;
68 if ((*WlanEnumInterfaces)(wlan_handle, NULL, &interface_list) != ERROR_SUCCESS) {
69 // try again later
70 (*WlanCloseHandle)(wlan_handle, NULL);
71 return NS_ERROR_FAILURE;
74 // Go through the list of interfaces and get the data for each.
75 for (int i = 0; i < static_cast<int>(interface_list->dwNumberOfItems); ++i) {
77 WLAN_BSS_LIST *bss_list;
78 HRESULT rv = (*WlanGetNetworkBssList)(wlan_handle,
79 &interface_list->InterfaceInfo[i].InterfaceGuid,
80 NULL, // Use all SSIDs.
81 DOT11_BSS_TYPE_UNUSED,
82 false, // bSecurityEnabled - unused
83 NULL, // reserved
84 &bss_list);
85 if (rv != ERROR_SUCCESS) {
86 continue;
89 for (int j = 0; j < static_cast<int>(bss_list->dwNumberOfItems); ++j) {
91 nsWifiAccessPoint* ap = new nsWifiAccessPoint();
92 if (!ap)
93 continue;
95 const WLAN_BSS_ENTRY bss_entry = bss_list->wlanBssEntries[j];
97 ap->setMac(bss_entry.dot11Bssid);
98 ap->setSignal(bss_entry.lRssi);
99 ap->setSSID((char*) bss_entry.dot11Ssid.ucSSID,
100 bss_entry.dot11Ssid.uSSIDLength);
102 accessPoints.AppendObject(ap);
104 (*WlanFreeMemory)(bss_list);
107 // Free interface_list.
108 (*WlanFreeMemory)(interface_list);
110 // Close the handle.
111 (*WlanCloseHandle)(wlan_handle, NULL);
114 bool accessPointsChanged = !AccessPointsEqual(accessPoints, lastAccessPoints);
115 ReplaceArray(lastAccessPoints, accessPoints);
117 nsresult rv = CallWifiListeners(lastAccessPoints, accessPointsChanged);
118 NS_ENSURE_SUCCESS(rv, rv);
120 // wait for some reasonable amount of time. pref?
121 LOG(("waiting on monitor\n"));
123 ReentrantMonitorAutoEnter mon(mReentrantMonitor);
124 mon.Wait(PR_SecondsToInterval(60));
126 while (mKeepGoing);
128 return NS_OK;