Bug 1876541 - Remove an assert that doesn't hold for svg roots.
[gecko.git] / netwerk / wifi / nsWifiMonitor.h
blob5a37cc75be05fc5bafdf0baaf0d1414359e78496
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/. */
5 #ifndef __nsWifiMonitor__
6 #define __nsWifiMonitor__
8 #include "nsIWifiMonitor.h"
9 #include "nsCOMPtr.h"
10 #include "nsProxyRelease.h"
11 #include "nsIThread.h"
12 #include "nsIRunnable.h"
13 #include "nsCOMArray.h"
14 #include "nsIWifiListener.h"
15 #include "mozilla/Atomics.h"
16 #include "mozilla/ReentrantMonitor.h"
17 #include "mozilla/Logging.h"
18 #include "nsIObserver.h"
19 #include "nsTArray.h"
20 #include "mozilla/Attributes.h"
21 #include "mozilla/Monitor.h"
22 #include "WifiScanner.h"
24 namespace mozilla {
25 class TestWifiMonitor;
28 extern mozilla::LazyLogModule gWifiMonitorLog;
30 class nsWifiAccessPoint;
32 // Period between scans when on mobile network.
33 #define WIFI_SCAN_INTERVAL_MS_PREF "network.wifi.scanning_period"
35 #ifdef XP_MACOSX
36 // Use a larger stack size for the wifi monitor thread of macOS, to
37 // accommodate Core WLAN making large stack allocations.
38 # define kMacOSWifiMonitorStackSize (512 * 1024)
39 #endif
41 struct WifiListenerHolder {
42 RefPtr<nsIWifiListener> mListener;
43 bool mShouldPoll;
44 bool mHasSentData = false;
46 explicit WifiListenerHolder(nsIWifiListener* aListener,
47 bool aShouldPoll = false)
48 : mListener(aListener), mShouldPoll(aShouldPoll) {}
51 class nsWifiMonitor final : public nsIWifiMonitor, public nsIObserver {
52 public:
53 NS_DECL_THREADSAFE_ISUPPORTS
54 NS_DECL_NSIWIFIMONITOR
55 NS_DECL_NSIOBSERVER
57 explicit nsWifiMonitor(
58 mozilla::UniquePtr<mozilla::WifiScanner>&& aScanner = nullptr);
60 private:
61 friend class mozilla::TestWifiMonitor;
63 ~nsWifiMonitor();
65 nsresult DispatchScanToBackgroundThread(uint64_t aPollingId = 0,
66 uint32_t aWaitMs = 0);
68 void Scan(uint64_t aPollingId);
69 nsresult DoScan();
71 nsresult CallWifiListeners(
72 nsTArray<RefPtr<nsIWifiAccessPoint>>&& aAccessPoints,
73 bool aAccessPointsChanged);
75 nsresult PassErrorToWifiListeners(nsresult rv);
77 void Close();
79 bool IsBackgroundThread();
81 bool ShouldPoll() {
82 MOZ_ASSERT(!IsBackgroundThread());
83 return (mShouldPollForCurrentNetwork && !mListeners.IsEmpty()) ||
84 mNumPollingListeners > 0;
87 #ifdef ENABLE_TESTS
88 // Test-only function that confirms we "should" be polling. May be wrong
89 // if somehow the polling tasks are not set to run on the background
90 // thread.
91 bool IsPolling() { return mThread && mPollingId; }
92 #endif
94 // Main thread only.
95 nsCOMPtr<nsIThread> mThread;
97 // Main thread only.
98 nsTArray<WifiListenerHolder> mListeners;
100 // Background thread only.
101 mozilla::UniquePtr<mozilla::WifiScanner> mWifiScanner;
103 // Background thread only. Sorted.
104 nsTArray<RefPtr<nsIWifiAccessPoint>> mLastAccessPoints;
106 // Wifi-scanning requests may poll, meaning they will run repeatedly on
107 // a scheduled time period. If this value is 0 then polling is not running,
108 // otherwise, it indicates the "ID" of the polling that is running. if some
109 // other polling (with different ID) is running, it will stop, not iterate.
110 mozilla::Atomic<uint64_t> mPollingId;
112 // Number of current listeners that requested that the wifi scan poll
113 // periodically.
114 // Main thread only.
115 uint32_t mNumPollingListeners = 0;
117 // True if the current network type is one that requires polling
118 // (i.e. a "mobile" network type).
119 // Main thread only.
120 bool mShouldPollForCurrentNetwork = false;
123 #endif