gecko-dev/netwerk/wifi/nsWifiMonitor.h
Sandor Molnar eaa5676b40 Backed out 6 changesets (bug 1900225) for causing build bustages @ GeolocationSystemWin.cpp CLOSED TREE
Backed out changeset 2c0d4dc4c52e (bug 1900225)
Backed out changeset 092cece48dcd (bug 1900225)
Backed out changeset d9debcb16932 (bug 1900225)
Backed out changeset b389e47cd649 (bug 1900225)
Backed out changeset 0bdd05d6a2f0 (bug 1900225)
Backed out changeset 35a1d931e407 (bug 1900225)
2024-08-09 02:18:32 +03:00

124 lines
3.5 KiB
C++

/* 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 __nsWifiMonitor__
#define __nsWifiMonitor__
#include "nsIWifiMonitor.h"
#include "nsCOMPtr.h"
#include "nsProxyRelease.h"
#include "nsIThread.h"
#include "nsIRunnable.h"
#include "nsCOMArray.h"
#include "nsIWifiListener.h"
#include "mozilla/Atomics.h"
#include "mozilla/ReentrantMonitor.h"
#include "mozilla/Logging.h"
#include "nsIObserver.h"
#include "nsTArray.h"
#include "mozilla/Attributes.h"
#include "mozilla/Monitor.h"
#include "WifiScanner.h"
namespace mozilla {
class TestWifiMonitor;
}
extern mozilla::LazyLogModule gWifiMonitorLog;
class nsWifiAccessPoint;
// Period between scans when on mobile network.
#define WIFI_SCAN_INTERVAL_MS_PREF "network.wifi.scanning_period"
#ifdef XP_MACOSX
// Use a larger stack size for the wifi monitor thread of macOS, to
// accommodate Core WLAN making large stack allocations.
# define kMacOSWifiMonitorStackSize (512 * 1024)
#endif
struct WifiListenerHolder {
RefPtr<nsIWifiListener> mListener;
bool mShouldPoll;
bool mHasSentData = false;
explicit WifiListenerHolder(nsIWifiListener* aListener,
bool aShouldPoll = false)
: mListener(aListener), mShouldPoll(aShouldPoll) {}
};
class nsWifiMonitor final : public nsIWifiMonitor, public nsIObserver {
public:
NS_DECL_THREADSAFE_ISUPPORTS
NS_DECL_NSIWIFIMONITOR
NS_DECL_NSIOBSERVER
explicit nsWifiMonitor(
mozilla::UniquePtr<mozilla::WifiScanner>&& aScanner = nullptr);
private:
friend class mozilla::TestWifiMonitor;
~nsWifiMonitor();
nsresult DispatchScanToBackgroundThread(uint64_t aPollingId = 0,
uint32_t aWaitMs = 0);
void Scan(uint64_t aPollingId);
nsresult DoScan();
nsresult CallWifiListeners(
const nsTArray<RefPtr<nsIWifiAccessPoint>>& aAccessPoints,
bool aAccessPointsChanged);
nsresult PassErrorToWifiListeners(nsresult rv);
void Close();
bool IsBackgroundThread();
bool ShouldPoll() {
MOZ_ASSERT(!IsBackgroundThread());
return (mShouldPollForCurrentNetwork && !mListeners.IsEmpty()) ||
mNumPollingListeners > 0;
};
#ifdef ENABLE_TESTS
// Test-only function that confirms we "should" be polling. May be wrong
// if somehow the polling tasks are not set to run on the background
// thread.
bool IsPolling() { return mThread && mPollingId; }
#endif
// Main thread only.
nsCOMPtr<nsIThread> mThread;
// Main thread only.
nsTArray<WifiListenerHolder> mListeners;
// Background thread only.
mozilla::UniquePtr<mozilla::WifiScanner> mWifiScanner;
// Background thread only. Sorted.
nsTArray<RefPtr<nsIWifiAccessPoint>> mLastAccessPoints;
// Wifi-scanning requests may poll, meaning they will run repeatedly on
// a scheduled time period. If this value is 0 then polling is not running,
// otherwise, it indicates the "ID" of the polling that is running. if some
// other polling (with different ID) is running, it will stop, not iterate.
mozilla::Atomic<uint64_t> mPollingId;
// Number of current listeners that requested that the wifi scan poll
// periodically.
// Main thread only.
uint32_t mNumPollingListeners = 0;
// True if the current network type is one that requires polling
// (i.e. a "mobile" network type).
// Main thread only.
bool mShouldPollForCurrentNetwork = false;
};
#endif