2012-05-21 15:12:37 +04:00
|
|
|
/* 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/. */
|
2009-03-17 03:30:58 +03:00
|
|
|
|
|
|
|
#include "nsCOMPtr.h"
|
2013-03-16 03:02:01 +04:00
|
|
|
#include "nsProxyRelease.h"
|
2009-03-17 03:30:58 +03:00
|
|
|
#include "nsComponentManagerUtils.h"
|
|
|
|
#include "nsServiceManagerUtils.h"
|
|
|
|
#include "nsThreadUtils.h"
|
|
|
|
#include "nsXPCOM.h"
|
|
|
|
#include "nsXPCOMCID.h"
|
|
|
|
#include "nsIObserver.h"
|
|
|
|
#include "nsIObserverService.h"
|
|
|
|
#include "nsWifiMonitor.h"
|
2011-07-27 20:26:20 +04:00
|
|
|
#include "nsWifiAccessPoint.h"
|
2009-03-17 03:30:58 +03:00
|
|
|
|
|
|
|
#include "nsServiceManagerUtils.h"
|
|
|
|
#include "nsComponentManagerUtils.h"
|
2016-12-16 06:16:31 +03:00
|
|
|
#include "mozilla/IntegerPrintfMacros.h"
|
Bug 560095 - Use mozilla::services::GetObserverService(). r=biesi,dveditz,gavin,josh,jst,mrbkap,roc,sdwilsh,shaver,sicking,smontagu,surkov
2010-04-29 20:59:13 +04:00
|
|
|
#include "mozilla/Services.h"
|
2009-03-17 03:30:58 +03:00
|
|
|
|
Rollup of bug 645263 and bug 646259: Switch to mozilla:: sync primitives. r=cjones,dbaron,doublec,ehsan src=bsmedberg
Bug 645263, part 0: Count sync primitive ctor/dtors. r=dbaron
Bug 645263, part 1: Migrate content/media to mozilla:: sync primitives. r=doublec
Bug 645263, part 2: Migrate modules/plugin to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 3: Migrate nsComponentManagerImpl to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 4: Migrate everything else to mozilla:: sync primitives. r=dbaron
Bug 645263, part 5: Remove nsAutoLock.*. sr=bsmedberg
Bug 645263, part 6: Make editor test be nicer to deadlock detector. r=ehsan
Bug 645263, part 7: Disable tracemalloc backtraces for xpcshell tests. r=dbaron
Bug 646259: Fix nsCacheService to use a CondVar for notifying. r=cjones
2011-04-01 08:29:02 +04:00
|
|
|
using namespace mozilla;
|
|
|
|
|
2015-11-03 07:35:29 +03:00
|
|
|
LazyLogModule gWifiMonitorLog("WifiMonitor");
|
2009-03-17 03:30:58 +03:00
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(nsWifiMonitor, nsIRunnable, nsIObserver, nsIWifiMonitor)
|
2009-03-17 03:30:58 +03:00
|
|
|
|
|
|
|
nsWifiMonitor::nsWifiMonitor()
|
2011-10-17 18:59:28 +04:00
|
|
|
: mKeepGoing(true),
|
2015-04-02 20:58:14 +03:00
|
|
|
mThreadComplete(false),
|
2011-04-29 23:21:57 +04:00
|
|
|
mReentrantMonitor("nsWifiMonitor.mReentrantMonitor") {
|
Bug 560095 - Use mozilla::services::GetObserverService(). r=biesi,dveditz,gavin,josh,jst,mrbkap,roc,sdwilsh,shaver,sicking,smontagu,surkov
2010-04-29 20:59:13 +04:00
|
|
|
nsCOMPtr<nsIObserverService> obsSvc = mozilla::services::GetObserverService();
|
2011-10-17 18:59:28 +04:00
|
|
|
if (obsSvc) obsSvc->AddObserver(this, "xpcom-shutdown", false);
|
2009-03-17 03:30:58 +03:00
|
|
|
|
|
|
|
LOG(("@@@@@ wifimonitor created\n"));
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2019-05-01 11:47:10 +03:00
|
|
|
nsWifiMonitor::Observe(nsISupports* subject, const char* topic,
|
|
|
|
const char16_t* data) {
|
2009-03-17 03:30:58 +03:00
|
|
|
if (!strcmp(topic, "xpcom-shutdown")) {
|
|
|
|
LOG(("Shutting down\n"));
|
|
|
|
|
2011-04-29 23:21:57 +04:00
|
|
|
ReentrantMonitorAutoEnter mon(mReentrantMonitor);
|
2015-04-02 20:58:14 +03:00
|
|
|
mKeepGoing = false;
|
2009-03-17 03:30:58 +03:00
|
|
|
mon.Notify();
|
2015-04-02 20:58:14 +03:00
|
|
|
mThread = nullptr;
|
2009-03-17 03:30:58 +03:00
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
NS_IMETHODIMP nsWifiMonitor::StartWatching(nsIWifiListener* aListener) {
|
2015-04-02 20:58:14 +03:00
|
|
|
LOG(("nsWifiMonitor::StartWatching %p thread %p listener %p\n", this,
|
|
|
|
mThread.get(), aListener));
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2009-03-17 03:30:58 +03:00
|
|
|
if (!aListener) return NS_ERROR_NULL_POINTER;
|
2015-04-02 20:58:14 +03:00
|
|
|
if (!mKeepGoing) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
2009-03-17 03:30:58 +03:00
|
|
|
|
|
|
|
nsresult rv = NS_OK;
|
2015-04-02 20:58:14 +03:00
|
|
|
|
|
|
|
ReentrantMonitorAutoEnter mon(mReentrantMonitor);
|
|
|
|
if (mThreadComplete) {
|
|
|
|
// generally there is just one thread for the lifetime of the service,
|
|
|
|
// but if DoScan returns with an error before shutdown (i.e. !mKeepGoing)
|
|
|
|
// then we will respawn the thread.
|
|
|
|
LOG(("nsWifiMonitor::StartWatching %p restarting thread\n", this));
|
|
|
|
mThreadComplete = false;
|
|
|
|
mThread = nullptr;
|
|
|
|
}
|
|
|
|
|
2009-03-17 03:30:58 +03:00
|
|
|
if (!mThread) {
|
2016-12-21 13:33:21 +03:00
|
|
|
rv = NS_NewNamedThread("Wifi Monitor", getter_AddRefs(mThread), this);
|
2009-03-17 03:30:58 +03:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
}
|
|
|
|
|
2017-06-14 04:27:17 +03:00
|
|
|
mListeners.AppendElement(
|
|
|
|
nsWifiListener(new nsMainThreadPtrHolder<nsIWifiListener>(
|
|
|
|
"nsIWifiListener", aListener)));
|
2009-03-17 03:30:58 +03:00
|
|
|
|
|
|
|
// tell ourselves that we have a new watcher.
|
|
|
|
mon.Notify();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
NS_IMETHODIMP nsWifiMonitor::StopWatching(nsIWifiListener* aListener) {
|
2015-04-02 20:58:14 +03:00
|
|
|
LOG(("nsWifiMonitor::StopWatching %p thread %p listener %p\n", this,
|
|
|
|
mThread.get(), aListener));
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2009-03-17 03:30:58 +03:00
|
|
|
if (!aListener) return NS_ERROR_NULL_POINTER;
|
|
|
|
|
2011-04-29 23:21:57 +04:00
|
|
|
ReentrantMonitorAutoEnter mon(mReentrantMonitor);
|
2010-03-02 08:56:06 +03:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t i = 0; i < mListeners.Length(); i++) {
|
2009-03-17 03:30:58 +03:00
|
|
|
if (mListeners[i].mListener == aListener) {
|
|
|
|
mListeners.RemoveElementAt(i);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2019-06-11 21:59:50 +03:00
|
|
|
typedef nsTArray<nsMainThreadPtrHandle<nsIWifiListener>> WifiListenerArray;
|
2013-03-16 03:02:01 +04:00
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
class nsPassErrorToWifiListeners final : public nsIRunnable {
|
2011-07-27 20:26:20 +04:00
|
|
|
public:
|
2013-07-19 06:24:13 +04:00
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
2011-07-27 20:26:20 +04:00
|
|
|
NS_DECL_NSIRUNNABLE
|
|
|
|
|
2013-03-16 03:02:01 +04:00
|
|
|
nsPassErrorToWifiListeners(nsAutoPtr<WifiListenerArray> aListeners,
|
2011-07-27 20:26:20 +04:00
|
|
|
nsresult aResult)
|
|
|
|
: mListeners(aListeners), mResult(aResult) {}
|
|
|
|
|
|
|
|
private:
|
2018-04-30 19:46:04 +03:00
|
|
|
~nsPassErrorToWifiListeners() = default;
|
2013-03-16 03:02:01 +04:00
|
|
|
nsAutoPtr<WifiListenerArray> mListeners;
|
2011-07-27 20:26:20 +04:00
|
|
|
nsresult mResult;
|
|
|
|
};
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(nsPassErrorToWifiListeners, nsIRunnable)
|
2011-07-27 20:26:20 +04:00
|
|
|
|
|
|
|
NS_IMETHODIMP nsPassErrorToWifiListeners::Run() {
|
|
|
|
LOG(("About to send error to the wifi listeners\n"));
|
2013-03-16 03:02:01 +04:00
|
|
|
for (size_t i = 0; i < mListeners->Length(); i++) {
|
2011-08-14 21:39:20 +04:00
|
|
|
(*mListeners)[i]->OnError(mResult);
|
2011-07-27 20:26:20 +04:00
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2009-03-17 03:30:58 +03:00
|
|
|
|
|
|
|
NS_IMETHODIMP nsWifiMonitor::Run() {
|
|
|
|
LOG(("@@@@@ wifi monitor run called\n"));
|
|
|
|
|
|
|
|
nsresult rv = DoScan();
|
2016-12-16 06:16:31 +03:00
|
|
|
LOG(("@@@@@ wifi monitor run::doscan complete %" PRIx32 "\n",
|
|
|
|
static_cast<uint32_t>(rv)));
|
2009-03-17 03:30:58 +03:00
|
|
|
|
2015-04-02 20:58:14 +03:00
|
|
|
nsAutoPtr<WifiListenerArray> currentListeners;
|
|
|
|
bool doError = false;
|
2009-03-17 03:30:58 +03:00
|
|
|
|
2015-04-02 20:58:14 +03:00
|
|
|
{
|
|
|
|
ReentrantMonitorAutoEnter mon(mReentrantMonitor);
|
|
|
|
if (mKeepGoing && NS_FAILED(rv)) {
|
|
|
|
doError = true;
|
|
|
|
currentListeners = new WifiListenerArray(mListeners.Length());
|
|
|
|
for (uint32_t i = 0; i < mListeners.Length(); i++)
|
|
|
|
currentListeners->AppendElement(mListeners[i].mListener);
|
|
|
|
}
|
|
|
|
mThreadComplete = true;
|
|
|
|
}
|
2009-05-06 21:21:44 +04:00
|
|
|
|
2015-04-02 20:58:14 +03:00
|
|
|
if (doError) {
|
2017-06-01 23:43:44 +03:00
|
|
|
nsCOMPtr<nsIEventTarget> target = GetMainThreadEventTarget();
|
2011-07-27 20:26:20 +04:00
|
|
|
if (!target) return NS_ERROR_UNEXPECTED;
|
2009-05-06 21:21:44 +04:00
|
|
|
|
2011-07-27 20:26:20 +04:00
|
|
|
nsCOMPtr<nsIRunnable> runnable(
|
|
|
|
new nsPassErrorToWifiListeners(currentListeners, rv));
|
|
|
|
if (!runnable) return NS_ERROR_OUT_OF_MEMORY;
|
2009-05-06 21:21:44 +04:00
|
|
|
|
2017-06-01 23:43:44 +03:00
|
|
|
target->Dispatch(runnable, NS_DISPATCH_SYNC);
|
2009-03-17 03:30:58 +03:00
|
|
|
}
|
|
|
|
|
2015-04-02 20:58:14 +03:00
|
|
|
LOG(("@@@@@ wifi monitor run complete\n"));
|
2009-03-17 03:30:58 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2011-07-27 20:26:20 +04:00
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
class nsCallWifiListeners final : public nsIRunnable {
|
2011-07-27 20:26:20 +04:00
|
|
|
public:
|
2013-07-19 06:24:13 +04:00
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
2011-07-27 20:26:20 +04:00
|
|
|
NS_DECL_NSIRUNNABLE
|
|
|
|
|
2019-06-11 21:59:50 +03:00
|
|
|
nsCallWifiListeners(WifiListenerArray&& aListeners,
|
|
|
|
nsTArray<RefPtr<nsIWifiAccessPoint>>&& aAccessPoints)
|
|
|
|
: mListeners(std::move(aListeners)),
|
|
|
|
mAccessPoints(std::move(aAccessPoints)) {}
|
2011-07-27 20:26:20 +04:00
|
|
|
|
|
|
|
private:
|
2018-04-30 19:46:04 +03:00
|
|
|
~nsCallWifiListeners() = default;
|
2019-06-11 21:59:50 +03:00
|
|
|
const WifiListenerArray mListeners;
|
|
|
|
const nsTArray<RefPtr<nsIWifiAccessPoint>> mAccessPoints;
|
2011-07-27 20:26:20 +04:00
|
|
|
};
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(nsCallWifiListeners, nsIRunnable)
|
2011-07-27 20:26:20 +04:00
|
|
|
|
|
|
|
NS_IMETHODIMP nsCallWifiListeners::Run() {
|
|
|
|
LOG(("About to send data to the wifi listeners\n"));
|
2019-06-11 21:59:50 +03:00
|
|
|
for (auto& listener : mListeners) {
|
|
|
|
listener->OnChange(mAccessPoints);
|
2011-07-27 20:26:20 +04:00
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult nsWifiMonitor::CallWifiListeners(
|
2019-05-01 11:47:10 +03:00
|
|
|
const nsCOMArray<nsWifiAccessPoint>& aAccessPoints,
|
2011-09-29 10:19:26 +04:00
|
|
|
bool aAccessPointsChanged) {
|
2019-06-11 21:59:50 +03:00
|
|
|
WifiListenerArray currentListeners;
|
2011-07-27 20:26:20 +04:00
|
|
|
{
|
|
|
|
ReentrantMonitorAutoEnter mon(mReentrantMonitor);
|
|
|
|
|
2019-06-11 21:59:50 +03:00
|
|
|
currentListeners.SetCapacity(mListeners.Length());
|
2015-04-02 20:58:14 +03:00
|
|
|
|
2019-06-11 21:59:50 +03:00
|
|
|
for (auto& listener : mListeners) {
|
|
|
|
if (!listener.mHasSentData || aAccessPointsChanged) {
|
|
|
|
listener.mHasSentData = true;
|
|
|
|
currentListeners.AppendElement(listener.mListener);
|
2011-07-27 20:26:20 +04:00
|
|
|
}
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2011-07-27 20:26:20 +04:00
|
|
|
|
2019-06-11 21:59:50 +03:00
|
|
|
if (!currentListeners.IsEmpty()) {
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t resultCount = aAccessPoints.Count();
|
2019-06-11 21:59:50 +03:00
|
|
|
nsTArray<RefPtr<nsIWifiAccessPoint>> accessPoints(resultCount);
|
2011-07-27 20:26:20 +04:00
|
|
|
|
2019-06-11 21:59:50 +03:00
|
|
|
for (uint32_t i = 0; i < resultCount; i++) {
|
|
|
|
accessPoints.AppendElement(aAccessPoints[i]);
|
|
|
|
}
|
2011-07-27 20:26:20 +04:00
|
|
|
|
|
|
|
nsCOMPtr<nsIThread> thread = do_GetMainThread();
|
|
|
|
if (!thread) return NS_ERROR_UNEXPECTED;
|
|
|
|
|
2019-06-11 21:59:50 +03:00
|
|
|
nsCOMPtr<nsIRunnable> runnable(new nsCallWifiListeners(
|
|
|
|
std::move(currentListeners), std::move(accessPoints)));
|
2011-07-27 20:26:20 +04:00
|
|
|
if (!runnable) return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
thread->Dispatch(runnable, NS_DISPATCH_SYNC);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|