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"
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"
20 #include "mozilla/Attributes.h"
21 #include "mozilla/Monitor.h"
22 #include "WifiScanner.h"
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"
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)
41 struct WifiListenerHolder
{
42 RefPtr
<nsIWifiListener
> mListener
;
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
{
53 NS_DECL_THREADSAFE_ISUPPORTS
54 NS_DECL_NSIWIFIMONITOR
57 explicit nsWifiMonitor(
58 mozilla::UniquePtr
<mozilla::WifiScanner
>&& aScanner
= nullptr);
61 friend class mozilla::TestWifiMonitor
;
65 nsresult
DispatchScanToBackgroundThread(uint64_t aPollingId
= 0,
66 uint32_t aWaitMs
= 0);
68 void Scan(uint64_t aPollingId
);
71 nsresult
CallWifiListeners(
72 nsTArray
<RefPtr
<nsIWifiAccessPoint
>>&& aAccessPoints
,
73 bool aAccessPointsChanged
);
75 nsresult
PassErrorToWifiListeners(nsresult rv
);
79 bool IsBackgroundThread();
82 MOZ_ASSERT(!IsBackgroundThread());
83 return (mShouldPollForCurrentNetwork
&& !mListeners
.IsEmpty()) ||
84 mNumPollingListeners
> 0;
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
91 bool IsPolling() { return mThread
&& mPollingId
; }
95 nsCOMPtr
<nsIThread
> mThread
;
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
115 uint32_t mNumPollingListeners
= 0;
117 // True if the current network type is one that requires polling
118 // (i.e. a "mobile" network type).
120 bool mShouldPollForCurrentNetwork
= false;