Bug 1810421: Refactor wifi scanning to consolidate generic code r=necko-reviewers,kershaw,valentin a=reland CLOSED TREE

Reorganizes the wifi-scanning code and makes more of it platform-generic
to ease the transition from polling the wifi to usually scanning only on
network changes.  This is essentially just moving files/code around and
promoting nsWifiMonitor::DoScan to be platform-independent.

Differential Revision: https://phabricator.services.mozilla.com/D176199
This commit is contained in:
David Parks 2023-05-13 00:36:00 +00:00
Родитель 23e08c0b13
Коммит 61ef08e50a
21 изменённых файлов: 370 добавлений и 341 удалений

Просмотреть файл

@ -0,0 +1,33 @@
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#pragma once
#include "nsTArray.h"
#include "mozilla/RefPtr.h"
class nsIWifiAccessPoint;
namespace mozilla {
class WifiScanner {
public:
/**
* GetAccessPointsFromWLAN
*
* Scans the available wireless interfaces for nearby access points and
* populates the supplied collection with them
*
* @param accessPoints The collection to populate with available APs
* @return NS_OK on success, failure codes on failure
*/
virtual nsresult GetAccessPointsFromWLAN(
nsTArray<RefPtr<nsIWifiAccessPoint>>& accessPoints) = 0;
virtual ~WifiScanner() = default;
};
} // namespace mozilla

Просмотреть файл

@ -2,17 +2,13 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "nsWifiScannerDBus.h"
#include "DbusWifiScanner.h"
#include "mozilla/DBusHelpers.h"
#include "nsWifiAccessPoint.h"
namespace mozilla {
nsWifiScannerDBus::nsWifiScannerDBus(
nsCOMArray<nsWifiAccessPoint>* aAccessPoints)
: mAccessPoints(aAccessPoints) {
MOZ_ASSERT(mAccessPoints);
WifiScannerImpl::WifiScannerImpl() {
mConnection =
already_AddRefed<DBusConnection>(dbus_bus_get(DBUS_BUS_SYSTEM, nullptr));
@ -21,23 +17,24 @@ nsWifiScannerDBus::nsWifiScannerDBus(
dbus_connection_set_exit_on_disconnect(mConnection, false);
}
MOZ_COUNT_CTOR(nsWifiScannerDBus);
MOZ_COUNT_CTOR(WifiScannerImpl);
}
nsWifiScannerDBus::~nsWifiScannerDBus() { MOZ_COUNT_DTOR(nsWifiScannerDBus); }
WifiScannerImpl::~WifiScannerImpl() { MOZ_COUNT_DTOR(WifiScannerImpl); }
nsresult nsWifiScannerDBus::Scan() {
nsresult WifiScannerImpl::GetAccessPointsFromWLAN(
AccessPointArray& accessPoints) {
if (!mConnection) {
return NS_ERROR_NOT_AVAILABLE;
}
return SendGetDevices();
return SendGetDevices(accessPoints);
}
// http://dbus.freedesktop.org/doc/api/html/group__DBusConnection.html
// Refer to function dbus_connection_send_with_reply_and_block.
static const uint32_t DBUS_DEFAULT_TIMEOUT = -1;
nsresult nsWifiScannerDBus::SendGetDevices() {
nsresult WifiScannerImpl::SendGetDevices(AccessPointArray& accessPoints) {
RefPtr<DBusMessage> msg =
already_AddRefed<DBusMessage>(dbus_message_new_method_call(
"org.freedesktop.NetworkManager", "/org/freedesktop/NetworkManager",
@ -57,10 +54,11 @@ nsresult nsWifiScannerDBus::SendGetDevices() {
return NS_ERROR_FAILURE;
}
return IdentifyDevices(reply);
return IdentifyDevices(reply, accessPoints);
}
nsresult nsWifiScannerDBus::SendGetDeviceType(const char* aPath) {
nsresult WifiScannerImpl::SendGetDeviceType(const char* aPath,
AccessPointArray& accessPoints) {
RefPtr<DBusMessage> msg = already_AddRefed<DBusMessage>(
dbus_message_new_method_call("org.freedesktop.NetworkManager", aPath,
"org.freedesktop.DBus.Properties", "Get"));
@ -94,10 +92,11 @@ nsresult nsWifiScannerDBus::SendGetDeviceType(const char* aPath) {
return NS_ERROR_FAILURE;
}
return IdentifyDeviceType(reply, aPath);
return IdentifyDeviceType(reply, aPath, accessPoints);
}
nsresult nsWifiScannerDBus::SendGetAccessPoints(const char* aPath) {
nsresult WifiScannerImpl::SendGetAccessPoints(const char* aPath,
AccessPointArray& accessPoints) {
RefPtr<DBusMessage> msg =
already_AddRefed<DBusMessage>(dbus_message_new_method_call(
"org.freedesktop.NetworkManager", aPath,
@ -119,10 +118,11 @@ nsresult nsWifiScannerDBus::SendGetAccessPoints(const char* aPath) {
return NS_OK;
}
return IdentifyAccessPoints(reply);
return IdentifyAccessPoints(reply, accessPoints);
}
nsresult nsWifiScannerDBus::SendGetAPProperties(const char* aPath) {
nsresult WifiScannerImpl::SendGetAPProperties(const char* aPath,
AccessPointArray& accessPoints) {
RefPtr<DBusMessage> msg =
already_AddRefed<DBusMessage>(dbus_message_new_method_call(
"org.freedesktop.NetworkManager", aPath,
@ -150,10 +150,11 @@ nsresult nsWifiScannerDBus::SendGetAPProperties(const char* aPath) {
return NS_ERROR_FAILURE;
}
return IdentifyAPProperties(reply);
return IdentifyAPProperties(reply, accessPoints);
}
nsresult nsWifiScannerDBus::IdentifyDevices(DBusMessage* aMsg) {
nsresult WifiScannerImpl::IdentifyDevices(DBusMessage* aMsg,
AccessPointArray& accessPoints) {
DBusMessageIter iter;
nsresult rv = GetDBusIterator(aMsg, &iter);
NS_ENSURE_SUCCESS(rv, rv);
@ -169,15 +170,16 @@ nsresult nsWifiScannerDBus::IdentifyDevices(DBusMessage* aMsg) {
return NS_ERROR_FAILURE;
}
rv = SendGetDeviceType(devicePath);
rv = SendGetDeviceType(devicePath, accessPoints);
NS_ENSURE_SUCCESS(rv, rv);
} while (dbus_message_iter_next(&iter));
return NS_OK;
}
nsresult nsWifiScannerDBus::IdentifyDeviceType(DBusMessage* aMsg,
const char* aDevicePath) {
nsresult WifiScannerImpl::IdentifyDeviceType(DBusMessage* aMsg,
const char* aDevicePath,
AccessPointArray& accessPoints) {
DBusMessageIter args;
if (!dbus_message_iter_init(aMsg, &args)) {
return NS_ERROR_FAILURE;
@ -201,13 +203,14 @@ nsresult nsWifiScannerDBus::IdentifyDeviceType(DBusMessage* aMsg,
const uint32_t NM_DEVICE_TYPE_WIFI = 2;
nsresult rv = NS_OK;
if (deviceType == NM_DEVICE_TYPE_WIFI) {
rv = SendGetAccessPoints(aDevicePath);
rv = SendGetAccessPoints(aDevicePath, accessPoints);
}
return rv;
}
nsresult nsWifiScannerDBus::IdentifyAccessPoints(DBusMessage* aMsg) {
nsresult WifiScannerImpl::IdentifyAccessPoints(DBusMessage* aMsg,
AccessPointArray& accessPoints) {
DBusMessageIter iter;
nsresult rv = GetDBusIterator(aMsg, &iter);
NS_ENSURE_SUCCESS(rv, rv);
@ -222,14 +225,15 @@ nsresult nsWifiScannerDBus::IdentifyAccessPoints(DBusMessage* aMsg) {
return NS_ERROR_FAILURE;
}
rv = SendGetAPProperties(path);
rv = SendGetAPProperties(path, accessPoints);
NS_ENSURE_SUCCESS(rv, rv);
} while (dbus_message_iter_next(&iter));
return NS_OK;
}
nsresult nsWifiScannerDBus::IdentifyAPProperties(DBusMessage* aMsg) {
nsresult WifiScannerImpl::IdentifyAPProperties(DBusMessage* aMsg,
AccessPointArray& accessPoints) {
DBusMessageIter arr;
nsresult rv = GetDBusIterator(aMsg, &arr);
NS_ENSURE_SUCCESS(rv, rv);
@ -275,12 +279,12 @@ nsresult nsWifiScannerDBus::IdentifyAPProperties(DBusMessage* aMsg) {
} while (dbus_message_iter_next(&dict));
} while (dbus_message_iter_next(&arr));
mAccessPoints->AppendObject(ap);
accessPoints.AppendElement(ap);
return NS_OK;
}
nsresult nsWifiScannerDBus::StoreSsid(DBusMessageIter* aVariant,
nsWifiAccessPoint* aAp) {
nsresult WifiScannerImpl::StoreSsid(DBusMessageIter* aVariant,
nsWifiAccessPoint* aAp) {
if (dbus_message_iter_get_arg_type(aVariant) != DBUS_TYPE_ARRAY) {
return NS_ERROR_FAILURE;
}
@ -305,8 +309,8 @@ nsresult nsWifiScannerDBus::StoreSsid(DBusMessageIter* aVariant,
return NS_OK;
}
nsresult nsWifiScannerDBus::SetMac(DBusMessageIter* aVariant,
nsWifiAccessPoint* aAp) {
nsresult WifiScannerImpl::SetMac(DBusMessageIter* aVariant,
nsWifiAccessPoint* aAp) {
if (dbus_message_iter_get_arg_type(aVariant) != DBUS_TYPE_STRING) {
return NS_ERROR_FAILURE;
}
@ -333,8 +337,8 @@ nsresult nsWifiScannerDBus::SetMac(DBusMessageIter* aVariant,
return NS_OK;
}
nsresult nsWifiScannerDBus::GetDBusIterator(DBusMessage* aMsg,
DBusMessageIter* aIterArray) {
nsresult WifiScannerImpl::GetDBusIterator(DBusMessage* aMsg,
DBusMessageIter* aIterArray) {
DBusMessageIter iter;
if (!dbus_message_iter_init(aMsg, &iter)) {
return NS_ERROR_FAILURE;
@ -349,27 +353,3 @@ nsresult nsWifiScannerDBus::GetDBusIterator(DBusMessage* aMsg,
}
} // namespace mozilla
nsresult nsWifiMonitor::DoScan() {
nsCOMArray<nsWifiAccessPoint> accessPoints;
mozilla::nsWifiScannerDBus wifiScanner(&accessPoints);
nsCOMArray<nsWifiAccessPoint> lastAccessPoints;
while (mKeepGoing) {
accessPoints.Clear();
nsresult rv = wifiScanner.Scan();
NS_ENSURE_SUCCESS(rv, rv);
bool accessPointsChanged =
!AccessPointsEqual(accessPoints, lastAccessPoints);
ReplaceArray(lastAccessPoints, accessPoints);
rv = CallWifiListeners(lastAccessPoints, accessPointsChanged);
NS_ENSURE_SUCCESS(rv, rv);
LOG(("waiting on monitor\n"));
mozilla::ReentrantMonitorAutoEnter mon(mReentrantMonitor);
mon.Wait(PR_SecondsToInterval(kDefaultWifiScanInterval));
}
return NS_OK;
}

Просмотреть файл

@ -0,0 +1,60 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#ifndef NSWIFIAPSCANNERDBUS_H_
#define NSWIFIAPSCANNERDBUS_H_
#define DBUS_API_SUBJECT_TO_CHANGE
#include <dbus/dbus.h>
#include <dbus/dbus-glib-lowlevel.h>
#include "WifiScanner.h"
class nsIWifiAccessPoint;
class nsWifiAccessPoint;
namespace mozilla {
using AccessPointArray = nsTArray<RefPtr<nsIWifiAccessPoint>>;
class WifiScannerImpl final : public WifiScanner {
public:
explicit WifiScannerImpl();
~WifiScannerImpl();
/**
* GetAccessPointsFromWLAN
*
* Scans the available wireless interfaces for nearby access points and
* populates the supplied collection with them
*
* @param accessPoints The collection to populate with available APs
* @return NS_OK on success, failure codes on failure
*/
nsresult GetAccessPointsFromWLAN(AccessPointArray& accessPoints);
private:
nsresult SendGetDevices(AccessPointArray& accessPoints);
nsresult SendGetDeviceType(const char* aPath, AccessPointArray& accessPoints);
nsresult SendGetAccessPoints(const char* aPath,
AccessPointArray& accessPoints);
nsresult SendGetAPProperties(const char* aPath,
AccessPointArray& accessPoints);
nsresult IdentifyDevices(DBusMessage* aMsg, AccessPointArray& accessPoints);
nsresult IdentifyDeviceType(DBusMessage* aMsg, const char* aDevicePath,
AccessPointArray& accessPoints);
nsresult IdentifyAccessPoints(DBusMessage* aMsg,
AccessPointArray& accessPoints);
nsresult IdentifyAPProperties(DBusMessage* aMsg,
AccessPointArray& accessPoints);
nsresult StoreSsid(DBusMessageIter* aVariant, nsWifiAccessPoint* aAp);
nsresult SetMac(DBusMessageIter* aVariant, nsWifiAccessPoint* aAp);
nsresult GetDBusIterator(DBusMessage* aMsg, DBusMessageIter* aIterArray);
RefPtr<DBusConnection> mConnection;
};
} // namespace mozilla
#endif // NSWIFIAPSCANNERDBUS_H_

Просмотреть файл

@ -23,12 +23,13 @@
#include <string.h>
#include <unistd.h>
#include "FreeBsdWifiScanner.h"
#include "nsWifiAccessPoint.h"
using namespace mozilla;
static nsresult FreeBSDGetAccessPointData(
nsCOMArray<nsWifiAccessPoint>& accessPoints) {
nsresult WifiScannerImpl::GetAccessPointsFromWLAN(
nsTArray<RefPtr<nsIWifiAccessPoint>>& accessPoints) {
// get list of interfaces
struct ifaddrs* ifal;
if (getifaddrs(&ifal) < 0) {
@ -117,7 +118,7 @@ static nsresult FreeBSDGetAccessPointData(
ap->setSSID(ssid, strlen(ssid));
ap->setMac(isr->isr_bssid);
ap->setSignal(isr->isr_rssi);
accessPoints.AppendObject(ap);
accessPoints.AppendElement(ap);
rv = NS_OK;
// log the data
@ -139,30 +140,3 @@ static nsresult FreeBSDGetAccessPointData(
return rv;
}
nsresult nsWifiMonitor::DoScan() {
// Regularly get the access point data.
nsCOMArray<nsWifiAccessPoint> lastAccessPoints;
nsCOMArray<nsWifiAccessPoint> accessPoints;
do {
nsresult rv = FreeBSDGetAccessPointData(accessPoints);
if (NS_FAILED(rv)) return rv;
bool accessPointsChanged =
!AccessPointsEqual(accessPoints, lastAccessPoints);
ReplaceArray(lastAccessPoints, accessPoints);
rv = CallWifiListeners(lastAccessPoints, accessPointsChanged);
NS_ENSURE_SUCCESS(rv, rv);
// wait for some reasonable amount of time. pref?
LOG(("waiting on monitor\n"));
ReentrantMonitorAutoEnter mon(mReentrantMonitor);
mon.Wait(PR_SecondsToInterval(kDefaultWifiScanInterval));
} while (mKeepGoing);
return NS_OK;
}

Просмотреть файл

@ -0,0 +1,31 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#pragma once
#include "WifiScanner.h"
class nsIWifiAccessPoint;
namespace mozilla {
class WifiScannerImpl final : public WifiScanner {
public:
WifiScannerImpl();
~WifiScannerImpl();
/**
* GetAccessPointsFromWLAN
*
* Scans the available wireless interfaces for nearby access points and
* populates the supplied collection with them
*
* @param accessPoints The collection to populate with available APs
* @return NS_OK on success, failure codes on failure
*/
nsresult GetAccessPointsFromWLAN(
nsTArray<RefPtr<nsIWifiAccessPoint>>& accessPoints);
};
} // namespace mozilla

Просмотреть файл

@ -0,0 +1,28 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#pragma once
#include "WifiScanner.h"
class nsWifiAccessPoint;
namespace mozilla {
class WifiScannerImpl final : public WifiScanner {
public:
/**
* GetAccessPointsFromWLAN
*
* Scans the available wireless interfaces for nearby access points and
* populates the supplied collection with them
*
* @param accessPoints The collection to populate with available APs
* @return NS_OK on success, failure codes on failure
*/
nsresult GetAccessPointsFromWLAN(
nsTArray<RefPtr<nsIWifiAccessPoint>>& accessPoints);
};
} // namespace mozilla

Просмотреть файл

@ -15,8 +15,12 @@
#include "nsCOMArray.h"
#include "nsWifiMonitor.h"
#include "nsWifiAccessPoint.h"
#include "MacWifiScanner.h"
nsresult GetAccessPointsFromWLAN(nsCOMArray<nsWifiAccessPoint>& accessPoints) {
namespace mozilla {
nsresult WifiScannerImpl::GetAccessPointsFromWLAN(
nsTArray<RefPtr<nsIWifiAccessPoint>>& accessPoints) {
NS_OBJC_BEGIN_TRY_BLOCK_RETURN;
accessPoints.Clear();
@ -85,7 +89,7 @@ nsresult GetAccessPointsFromWLAN(nsCOMArray<nsWifiAccessPoint>& accessPoints) {
ap->setSignal(signal);
ap->setSSID([[anObject ssid] UTF8String], 32);
accessPoints.AppendObject(ap);
accessPoints.AppendElement(ap);
}
} @catch (NSException*) {
[pool release];
@ -98,3 +102,5 @@ nsresult GetAccessPointsFromWLAN(nsCOMArray<nsWifiAccessPoint>& accessPoints) {
NS_OBJC_END_TRY_BLOCK_RETURN(NS_ERROR_NOT_AVAILABLE);
}
} // namespace mozilla

Просмотреть файл

Просмотреть файл

@ -18,34 +18,44 @@ UNIFIED_SOURCES += [
]
if CONFIG["OS_ARCH"] == "Darwin":
UNIFIED_SOURCES += [
"nsWifiScannerMac.cpp",
]
SOURCES += [
"osx_corewlan.mm",
"mac/MacWifiScanner.mm",
]
LOCAL_INCLUDES += [
"mac",
]
elif CONFIG["OS_ARCH"] in ("DragonFly", "FreeBSD"):
UNIFIED_SOURCES += [
"nsWifiScannerFreeBSD.cpp",
"freebsd/FreeBsdWifiScanner.cpp",
]
LOCAL_INCLUDES += [
"freebsd",
]
elif CONFIG["OS_ARCH"] == "WINNT":
UNIFIED_SOURCES += [
"nsWifiScannerWin.cpp",
"win_wifiScanner.cpp",
"win_wlanLibrary.cpp",
"win/WinWifiScanner.cpp",
"win/WlanLibrary.cpp",
]
LOCAL_INCLUDES += [
"win",
]
elif CONFIG["OS_ARCH"] == "SunOS":
CXXFLAGS += CONFIG["GLIB_CFLAGS"]
UNIFIED_SOURCES += [
"nsWifiScannerSolaris.cpp",
"solaris/SolarisWifiScanner.cpp",
]
if CONFIG["NECKO_WIFI_DBUS"]:
LOCAL_INCLUDES += [
"solaris",
]
elif CONFIG["NECKO_WIFI_DBUS"]:
UNIFIED_SOURCES += [
"nsWifiScannerDBus.cpp",
"dbus/DbusWifiScanner.cpp",
]
LOCAL_INCLUDES += [
"dbus",
]
if CONFIG["NECKO_WIFI_DBUS"]:
CXXFLAGS += CONFIG["MOZ_DBUS_GLIB_CFLAGS"]
FINAL_LIBRARY = "xul"
include("/ipc/chromium/chromium-config.mozbuild")

Просмотреть файл

@ -19,8 +19,25 @@
#include "mozilla/IntegerPrintfMacros.h"
#include "mozilla/Services.h"
#ifdef XP_MACOSX
#if defined(XP_WIN)
# include "WinWifiScanner.h"
#endif
#if defined(XP_MACOSX)
# include "nsCocoaFeatures.h"
# include "MacWifiScanner.h"
#endif
#if defined(__DragonFly__) || defined(__FreeBSD__)
# include "FreeBsdWifiScanner.h"
#endif
#if defined(XP_SOLARIS)
# include "SolarisWifiScanner.h"
#endif
#if defined(NECKO_WIFI_DBUS)
# include "DbusWifiScanner.h"
#endif
using namespace mozilla;
@ -29,10 +46,14 @@ LazyLogModule gWifiMonitorLog("WifiMonitor");
NS_IMPL_ISUPPORTS(nsWifiMonitor, nsIRunnable, nsIObserver, nsIWifiMonitor)
nsWifiMonitor::nsWifiMonitor()
nsWifiMonitor::nsWifiMonitor(UniquePtr<mozilla::WifiScanner>&& aScanner)
: mKeepGoing(true),
mThreadComplete(false),
mWifiScanner(std::move(aScanner)),
mReentrantMonitor("nsWifiMonitor.mReentrantMonitor") {
LOG(("Creating nsWifiMonitor"));
MOZ_ASSERT(NS_IsMainThread());
nsCOMPtr<nsIObserverService> obsSvc = mozilla::services::GetObserverService();
if (obsSvc) obsSvc->AddObserver(this, "xpcom-shutdown", false);
@ -194,6 +215,51 @@ NS_IMETHODIMP nsWifiMonitor::Run() {
return NS_OK;
}
nsresult nsWifiMonitor::DoScan() {
if (!mWifiScanner) {
mWifiScanner = MakeUnique<mozilla::WifiScannerImpl>();
if (!mWifiScanner) {
// TODO: Probably return OOM error
return NS_ERROR_FAILURE;
}
}
// Regularly get the access point data.
nsCOMArray<nsWifiAccessPoint> lastAccessPoints;
nsTArray<RefPtr<nsIWifiAccessPoint>> accessPointsArray;
do {
accessPointsArray.Clear();
nsresult rv = mWifiScanner->GetAccessPointsFromWLAN(accessPointsArray);
if (NS_FAILED(rv)) {
return rv;
}
nsCOMArray<nsWifiAccessPoint> accessPoints;
for (auto& ap : accessPointsArray) {
accessPoints.AppendObject(static_cast<nsWifiAccessPoint*>(ap.get()));
}
bool accessPointsChanged =
!AccessPointsEqual(accessPoints, lastAccessPoints);
ReplaceArray(lastAccessPoints, accessPoints);
rv = CallWifiListeners(lastAccessPoints, accessPointsChanged);
NS_ENSURE_SUCCESS(rv, rv);
// wait for some reasonable amount of time. pref?
LOG(("waiting on monitor\n"));
ReentrantMonitorAutoEnter mon(mReentrantMonitor);
if (mKeepGoing) {
mon.Wait(PR_SecondsToInterval(kDefaultWifiScanInterval));
}
} while (mKeepGoing);
return NS_OK;
}
class nsCallWifiListeners final : public nsIRunnable {
public:
NS_DECL_THREADSAFE_ISUPPORTS

Просмотреть файл

@ -18,10 +18,7 @@
#include "nsIObserver.h"
#include "nsTArray.h"
#include "mozilla/Attributes.h"
#ifdef XP_WIN
# include "win_wifiScanner.h"
#endif
#include "WifiScanner.h"
extern mozilla::LazyLogModule gWifiMonitorLog;
#define LOG(args) MOZ_LOG(gWifiMonitorLog, mozilla::LogLevel::Debug, args)
@ -55,7 +52,8 @@ class nsWifiMonitor final : nsIRunnable, nsIWifiMonitor, nsIObserver {
NS_DECL_NSIRUNNABLE
NS_DECL_NSIOBSERVER
nsWifiMonitor();
explicit nsWifiMonitor(
mozilla::UniquePtr<mozilla::WifiScanner>&& aScanner = nullptr);
private:
~nsWifiMonitor() = default;
@ -71,13 +69,12 @@ class nsWifiMonitor final : nsIRunnable, nsIWifiMonitor, nsIObserver {
mozilla::Atomic<bool> mThreadComplete;
nsCOMPtr<nsIThread> mThread; // only accessed on MainThread
// Background thread only (except in test).
mozilla::UniquePtr<mozilla::WifiScanner> mWifiScanner;
nsTArray<nsWifiListener> mListeners MOZ_GUARDED_BY(mReentrantMonitor);
mozilla::ReentrantMonitor mReentrantMonitor;
#ifdef XP_WIN
mozilla::UniquePtr<WinWifiScanner> mWinWifiScanner;
#endif
};
#endif

Просмотреть файл

@ -1,44 +0,0 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#ifndef NSWIFIAPSCANNERDBUS_H_
#define NSWIFIAPSCANNERDBUS_H_
#include "nsCOMArray.h"
#define DBUS_API_SUBJECT_TO_CHANGE
#include <dbus/dbus.h>
#include <dbus/dbus-glib-lowlevel.h>
class nsWifiAccessPoint;
namespace mozilla {
class nsWifiScannerDBus final {
public:
explicit nsWifiScannerDBus(nsCOMArray<nsWifiAccessPoint>* aAccessPoints);
~nsWifiScannerDBus();
nsresult Scan();
private:
nsresult SendGetDevices();
nsresult SendGetDeviceType(const char* aPath);
nsresult SendGetAccessPoints(const char* aPath);
nsresult SendGetAPProperties(const char* aPath);
nsresult IdentifyDevices(DBusMessage* aMsg);
nsresult IdentifyDeviceType(DBusMessage* aMsg, const char* aDevicePath);
nsresult IdentifyAccessPoints(DBusMessage* aMsg);
nsresult IdentifyAPProperties(DBusMessage* aMsg);
nsresult StoreSsid(DBusMessageIter* aVariant, nsWifiAccessPoint* aAp);
nsresult SetMac(DBusMessageIter* aVariant, nsWifiAccessPoint* aAp);
nsresult GetDBusIterator(DBusMessage* aMsg, DBusMessageIter* aIterArray);
RefPtr<DBusConnection> mConnection;
nsCOMArray<nsWifiAccessPoint>* mAccessPoints;
};
} // namespace mozilla
#endif // NSWIFIAPSCANNERDBUS_H_

Просмотреть файл

@ -1,51 +0,0 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include <mach-o/dyld.h>
#include <dlfcn.h>
#include <unistd.h>
#include "osx_wifi.h"
#include "nsCOMArray.h"
#include "nsWifiMonitor.h"
#include "nsWifiAccessPoint.h"
#include "nsServiceManagerUtils.h"
#include "nsComponentManagerUtils.h"
using namespace mozilla;
// defined in osx_corewlan.mm
// basically replaces accesspoints in the passed reference
// it lives in a separate file so that we can use objective c.
extern nsresult GetAccessPointsFromWLAN(
nsCOMArray<nsWifiAccessPoint>& accessPoints);
nsresult nsWifiMonitor::DoScan() {
// Regularly get the access point data.
nsCOMArray<nsWifiAccessPoint> lastAccessPoints;
nsCOMArray<nsWifiAccessPoint> accessPoints;
do {
nsresult rv = GetAccessPointsFromWLAN(accessPoints);
if (NS_FAILED(rv)) return rv;
bool accessPointsChanged =
!AccessPointsEqual(accessPoints, lastAccessPoints);
ReplaceArray(lastAccessPoints, accessPoints);
rv = CallWifiListeners(lastAccessPoints, accessPointsChanged);
NS_ENSURE_SUCCESS(rv, rv);
// wait for some reasonable amount of time. pref?
LOG(("waiting on monitor\n"));
ReentrantMonitorAutoEnter mon(mReentrantMonitor);
mon.Wait(PR_SecondsToInterval(kDefaultWifiScanInterval));
} while (mKeepGoing);
return NS_OK;
}

Просмотреть файл

@ -1,64 +0,0 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "nsWifiMonitor.h"
// moz headers (alphabetical)
#include "nsCOMArray.h"
#include "nsComponentManagerUtils.h"
#include "nsServiceManagerUtils.h"
#include "nsWifiAccessPoint.h"
#include "win_wifiScanner.h"
using namespace mozilla;
/**
* `nsWifiMonitor` is declared in the cross-platform nsWifiMonitor.h and
* is mostly defined in the cross-platform nsWifiMonitor.cpp. This function
* is implemented in various platform-specific files but the implementation
* is almost identical in each file. We relegate the Windows-specific
* work to the `WinWifiScanner` class and deal with non-Windows-specific
* issues like calling listeners here. Hopefully this file can be merged
* with the other implementations of `nsWifiMonitor::DoScan` since a lot
* of the code is identical
*/
nsresult nsWifiMonitor::DoScan() {
if (!mWinWifiScanner) {
mWinWifiScanner = MakeUnique<WinWifiScanner>();
if (!mWinWifiScanner) {
// TODO: Probably return OOM error
return NS_ERROR_FAILURE;
}
}
// Regularly get the access point data.
nsCOMArray<nsWifiAccessPoint> lastAccessPoints;
nsCOMArray<nsWifiAccessPoint> accessPoints;
do {
accessPoints.Clear();
nsresult rv = mWinWifiScanner->GetAccessPointsFromWLAN(accessPoints);
if (NS_FAILED(rv)) {
return rv;
}
bool accessPointsChanged =
!AccessPointsEqual(accessPoints, lastAccessPoints);
ReplaceArray(lastAccessPoints, accessPoints);
rv = CallWifiListeners(lastAccessPoints, accessPointsChanged);
NS_ENSURE_SUCCESS(rv, rv);
// wait for some reasonable amount of time. pref?
LOG(("waiting on monitor\n"));
ReentrantMonitorAutoEnter mon(mReentrantMonitor);
if (mKeepGoing) {
mon.Wait(PR_SecondsToInterval(kDefaultWifiScanInterval));
}
} while (mKeepGoing);
return NS_OK;
}

Просмотреть файл

@ -9,6 +9,8 @@
#include "nsServiceManagerUtils.h"
#include "nsComponentManagerUtils.h"
#include "SolarisWifiScanner.h"
#include <glib.h>
#define DLADM_STRSIZE 256
@ -53,7 +55,8 @@ static nsWifiAccessPoint* do_parse_str(char* bssid_str, char* essid_str,
return ap;
}
static void do_dladm(nsCOMArray<nsWifiAccessPoint>& accessPoints) {
nsresult WifiScannerImpl::GetAccessPointsFromWLAN(
nsTArray<RefPtr<nsIWifiAccessPoint>>& accessPoints) {
GError* err = nullptr;
char* sout = nullptr;
char* serr = nullptr;
@ -99,7 +102,7 @@ static void do_dladm(nsCOMArray<nsWifiAccessPoint>& accessPoints) {
if (section == DLADM_SECTIONS - 1) {
ap = do_parse_str(wlan[0], wlan[1], wlan[2]);
if (ap) {
accessPoints.AppendObject(ap);
accessPoints.AppendElement(ap);
}
}
section = 0;
@ -116,29 +119,3 @@ static void do_dladm(nsCOMArray<nsWifiAccessPoint>& accessPoints) {
g_free(sout);
g_free(serr);
}
nsresult nsWifiMonitor::DoScan() {
// Regularly get the access point data.
nsCOMArray<nsWifiAccessPoint> lastAccessPoints;
nsCOMArray<nsWifiAccessPoint> accessPoints;
while (mKeepGoing) {
accessPoints.Clear();
do_dladm(accessPoints);
bool accessPointsChanged =
!AccessPointsEqual(accessPoints, lastAccessPoints);
ReplaceArray(lastAccessPoints, accessPoints);
nsresult rv = CallWifiListeners(lastAccessPoints, accessPointsChanged);
NS_ENSURE_SUCCESS(rv, rv);
LOG(("waiting on monitor\n"));
ReentrantMonitorAutoEnter mon(mReentrantMonitor);
mon.Wait(PR_SecondsToInterval(kDefaultWifiScanInterval));
}
return NS_OK;
}

Просмотреть файл

@ -0,0 +1,31 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#pragma once
#include "WifiScanner.h"
class nsIWifiAccessPoint;
namespace mozilla {
class WifiScannerImpl final : public WifiScanner {
public:
WifiScannerImpl();
~WifiScannerImpl();
/**
* GetAccessPointsFromWLAN
*
* Scans the available wireless interfaces for nearby access points and
* populates the supplied collection with them
*
* @param accessPoints The collection to populate with available APs
* @return NS_OK on success, failure codes on failure
*/
nsresult GetAccessPointsFromWLAN(
nsTArray<RefPtr<nsIWifiAccessPoint>>& accessPoints);
};
} // namespace mozilla

Просмотреть файл

@ -3,13 +3,12 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "nsWifiAccessPoint.h"
#include "win_wifiScanner.h"
// Moz headers (alphabetical)
#include "win_wlanLibrary.h"
#include "WinWifiScanner.h"
#define DOT11_BSS_TYPE_UNUSED static_cast<DOT11_BSS_TYPE>(0)
namespace mozilla {
class InterfaceScanCallbackData {
public:
explicit InterfaceScanCallbackData(uint32_t numInterfaces)
@ -57,7 +56,7 @@ static void WINAPI OnScanComplete(PWLAN_NOTIFICATION_DATA data, PVOID context) {
cbData->OnInterfaceScanComplete();
}
WinWifiScanner::WinWifiScanner() {
WifiScannerImpl::WifiScannerImpl() {
// NOTE: We assume that, if we were unable to load the WLAN library when
// we initially tried, we will not be able to load it in the future.
// Technically, on Windows XP SP2, a user could install the redistributable
@ -70,14 +69,14 @@ WinWifiScanner::WinWifiScanner() {
}
}
WinWifiScanner::~WinWifiScanner() {}
WifiScannerImpl::~WifiScannerImpl() {}
nsresult WinWifiScanner::GetAccessPointsFromWLAN(
nsCOMArray<nsWifiAccessPoint>& accessPoints) {
nsresult WifiScannerImpl::GetAccessPointsFromWLAN(
nsTArray<RefPtr<nsIWifiAccessPoint>>& accessPoints) {
accessPoints.Clear();
// NOTE: We do not try to load the WLAN library if we previously failed
// to load it. See the note in WinWifiScanner constructor
// to load it. See the note in WifiScannerImpl constructor
if (!mWlanLibrary) {
return NS_ERROR_NOT_AVAILABLE;
}
@ -161,9 +160,11 @@ nsresult WinWifiScanner::GetAccessPointsFromWLAN(
ap->setSSID(reinterpret_cast<char const*>(bss_entry.dot11Ssid.ucSSID),
bss_entry.dot11Ssid.uSSIDLength);
accessPoints.AppendObject(ap);
accessPoints.AppendElement(ap);
}
}
return NS_OK;
}
} // namespace mozilla

Просмотреть файл

@ -4,17 +4,18 @@
#pragma once
// Moz headers (alphabetical)
#include "mozilla/UniquePtr.h"
#include "nsCOMArray.h"
#include "win_wlanLibrary.h"
#include "WlanLibrary.h"
#include "WifiScanner.h"
class nsWifiAccessPoint;
class nsIWifiAccessPoint;
class WinWifiScanner final {
namespace mozilla {
class WifiScannerImpl final : public WifiScanner {
public:
WinWifiScanner();
~WinWifiScanner();
WifiScannerImpl();
~WifiScannerImpl();
/**
* GetAccessPointsFromWLAN
@ -25,8 +26,11 @@ class WinWifiScanner final {
* @param accessPoints The collection to populate with available APs
* @return NS_OK on success, failure codes on failure
*/
nsresult GetAccessPointsFromWLAN(nsCOMArray<nsWifiAccessPoint>& accessPoints);
nsresult GetAccessPointsFromWLAN(
nsTArray<RefPtr<nsIWifiAccessPoint>>& accessPoints);
private:
mozilla::UniquePtr<WinWLANLibrary> mWlanLibrary;
};
} // namespace mozilla

Просмотреть файл

@ -2,7 +2,7 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "win_wlanLibrary.h"
#include "WlanLibrary.h"
// Moz headers (alphabetical)
@ -20,17 +20,6 @@ WinWLANLibrary* WinWLANLibrary::Load() {
return ret;
}
WinWLANLibrary::WinWLANLibrary()
: mWlanLibrary(nullptr),
mWlanHandle(nullptr),
mWlanEnumInterfacesPtr(nullptr),
mWlanGetNetworkBssListPtr(nullptr),
mWlanFreeMemoryPtr(nullptr),
mWlanCloseHandlePtr(nullptr),
mWlanOpenHandlePtr(nullptr),
mWlanRegisterNotificationPtr(nullptr),
mWlanScanPtr(nullptr) {}
HANDLE
WinWLANLibrary::GetWLANHandle() const { return mWlanHandle; }

Просмотреть файл

@ -27,18 +27,18 @@ class WinWLANLibrary {
decltype(::WlanScan)* GetWlanScanPtr() const;
private:
WinWLANLibrary();
WinWLANLibrary() = default;
bool Initialize();
HMODULE mWlanLibrary;
HANDLE mWlanHandle;
decltype(::WlanEnumInterfaces)* mWlanEnumInterfacesPtr;
decltype(::WlanGetNetworkBssList)* mWlanGetNetworkBssListPtr;
decltype(::WlanFreeMemory)* mWlanFreeMemoryPtr;
decltype(::WlanCloseHandle)* mWlanCloseHandlePtr;
decltype(::WlanOpenHandle)* mWlanOpenHandlePtr;
decltype(::WlanRegisterNotification)* mWlanRegisterNotificationPtr;
decltype(::WlanScan)* mWlanScanPtr;
HMODULE mWlanLibrary = nullptr;
HANDLE mWlanHandle = nullptr;
decltype(::WlanEnumInterfaces)* mWlanEnumInterfacesPtr = nullptr;
decltype(::WlanGetNetworkBssList)* mWlanGetNetworkBssListPtr = nullptr;
decltype(::WlanFreeMemory)* mWlanFreeMemoryPtr = nullptr;
decltype(::WlanCloseHandle)* mWlanCloseHandlePtr = nullptr;
decltype(::WlanOpenHandle)* mWlanOpenHandlePtr = nullptr;
decltype(::WlanRegisterNotification)* mWlanRegisterNotificationPtr = nullptr;
decltype(::WlanScan)* mWlanScanPtr = nullptr;
};
class ScopedWLANObject {

Просмотреть файл

@ -2987,6 +2987,7 @@ def necko_wifi_dbus(necko_wifi, dbus):
set_config("NECKO_WIFI_DBUS", True, when=necko_wifi_dbus)
set_define("NECKO_WIFI_DBUS", True, when=necko_wifi_dbus)
# Frontend JS debug mode