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"
|
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;
|
|
|
|
|
2009-03-17 03:30:58 +03:00
|
|
|
#if defined(PR_LOGGING)
|
|
|
|
PRLogModuleInfo *gWifiMonitorLog;
|
|
|
|
#endif
|
|
|
|
|
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)
|
2011-04-29 23:21:57 +04:00
|
|
|
, mReentrantMonitor("nsWifiMonitor.mReentrantMonitor")
|
2009-03-17 03:30:58 +03:00
|
|
|
{
|
|
|
|
#if defined(PR_LOGGING)
|
|
|
|
gWifiMonitorLog = PR_NewLogModule("WifiMonitor");
|
|
|
|
#endif
|
|
|
|
|
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();
|
2009-03-17 03:30:58 +03:00
|
|
|
if (obsSvc)
|
2011-10-17 18:59:28 +04:00
|
|
|
obsSvc->AddObserver(this, "xpcom-shutdown", false);
|
2009-03-17 03:30:58 +03:00
|
|
|
|
|
|
|
LOG(("@@@@@ wifimonitor created\n"));
|
|
|
|
}
|
|
|
|
|
|
|
|
nsWifiMonitor::~nsWifiMonitor()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsWifiMonitor::Observe(nsISupports *subject, const char *topic,
|
2014-01-04 19:02:17 +04:00
|
|
|
const char16_t *data)
|
2009-03-17 03:30:58 +03:00
|
|
|
{
|
|
|
|
if (!strcmp(topic, "xpcom-shutdown")) {
|
|
|
|
LOG(("Shutting down\n"));
|
2011-10-17 18:59:28 +04:00
|
|
|
mKeepGoing = false;
|
2009-03-17 03:30:58 +03:00
|
|
|
|
2011-04-29 23:21:57 +04:00
|
|
|
ReentrantMonitorAutoEnter mon(mReentrantMonitor);
|
2009-03-17 03:30:58 +03:00
|
|
|
mon.Notify();
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsWifiMonitor::StartWatching(nsIWifiListener *aListener)
|
|
|
|
{
|
|
|
|
if (!aListener)
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
if (!mThread) {
|
|
|
|
rv = NS_NewThread(getter_AddRefs(mThread), this);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2011-04-29 23:21:57 +04:00
|
|
|
ReentrantMonitorAutoEnter mon(mReentrantMonitor);
|
2010-03-02 08:56:06 +03:00
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
mKeepGoing = true;
|
2009-03-17 03:30:58 +03:00
|
|
|
|
2013-03-16 03:02:01 +04:00
|
|
|
mListeners.AppendElement(nsWifiListener(new nsMainThreadPtrHolder<nsIWifiListener>(aListener)));
|
2009-03-17 03:30:58 +03:00
|
|
|
|
|
|
|
// tell ourselves that we have a new watcher.
|
|
|
|
mon.Notify();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsWifiMonitor::StopWatching(nsIWifiListener *aListener)
|
|
|
|
{
|
|
|
|
if (!aListener)
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
|
|
|
|
LOG(("removing listener\n"));
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mListeners.Length() == 0) {
|
2011-10-17 18:59:28 +04:00
|
|
|
mKeepGoing = false;
|
2009-03-17 03:30:58 +03:00
|
|
|
mon.Notify();
|
2012-07-30 18:20:58 +04:00
|
|
|
mThread = nullptr;
|
2009-03-17 03:30:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-03-16 03:02:01 +04:00
|
|
|
typedef nsTArray<nsMainThreadPtrHandle<nsIWifiListener> > WifiListenerArray;
|
|
|
|
|
2012-06-06 07:18:25 +04:00
|
|
|
class nsPassErrorToWifiListeners MOZ_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:
|
2014-06-24 20:36:44 +04:00
|
|
|
~nsPassErrorToWifiListeners() {}
|
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"));
|
|
|
|
|
2012-06-12 21:06:20 +04:00
|
|
|
PR_SetCurrentThreadName("Wifi Monitor");
|
|
|
|
|
2009-03-17 03:30:58 +03:00
|
|
|
nsresult rv = DoScan();
|
|
|
|
|
|
|
|
if (mKeepGoing && NS_FAILED(rv)) {
|
2013-03-16 03:02:01 +04:00
|
|
|
nsAutoPtr<WifiListenerArray> currentListeners(
|
|
|
|
new WifiListenerArray(mListeners.Length()));
|
2011-07-27 20:26:20 +04:00
|
|
|
if (!currentListeners)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2009-03-17 03:30:58 +03:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t i = 0; i < mListeners.Length(); i++)
|
2013-03-16 03:02:01 +04:00
|
|
|
currentListeners->AppendElement(mListeners[i].mListener);
|
2009-05-06 21:21:44 +04:00
|
|
|
|
2011-07-27 20:26:20 +04:00
|
|
|
nsCOMPtr<nsIThread> thread = do_GetMainThread();
|
|
|
|
if (!thread)
|
|
|
|
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
|
|
|
|
2011-07-27 20:26:20 +04:00
|
|
|
thread->Dispatch(runnable, NS_DISPATCH_SYNC);
|
2009-03-17 03:30:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2011-07-27 20:26:20 +04:00
|
|
|
|
2012-06-06 07:18:25 +04:00
|
|
|
class nsCallWifiListeners MOZ_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
|
|
|
nsCallWifiListeners(nsAutoPtr<WifiListenerArray> aListeners,
|
2011-07-27 20:26:20 +04:00
|
|
|
nsAutoPtr<nsTArray<nsIWifiAccessPoint*> > aAccessPoints)
|
|
|
|
: mListeners(aListeners),
|
|
|
|
mAccessPoints(aAccessPoints)
|
|
|
|
{}
|
|
|
|
|
|
|
|
private:
|
2014-06-24 20:36:44 +04:00
|
|
|
~nsCallWifiListeners() {}
|
2013-03-16 03:02:01 +04:00
|
|
|
nsAutoPtr<WifiListenerArray> mListeners;
|
2011-07-27 20:26:20 +04:00
|
|
|
nsAutoPtr<nsTArray<nsIWifiAccessPoint*> > mAccessPoints;
|
|
|
|
};
|
|
|
|
|
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"));
|
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]->OnChange(mAccessPoints->Elements(), mAccessPoints->Length());
|
2011-07-27 20:26:20 +04:00
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsWifiMonitor::CallWifiListeners(const nsCOMArray<nsWifiAccessPoint> &aAccessPoints,
|
2011-09-29 10:19:26 +04:00
|
|
|
bool aAccessPointsChanged)
|
2011-07-27 20:26:20 +04:00
|
|
|
{
|
2013-03-16 03:02:01 +04:00
|
|
|
nsAutoPtr<WifiListenerArray> currentListeners(
|
|
|
|
new WifiListenerArray(mListeners.Length()));
|
2011-07-27 20:26:20 +04:00
|
|
|
if (!currentListeners)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
{
|
|
|
|
ReentrantMonitorAutoEnter mon(mReentrantMonitor);
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t i = 0; i < mListeners.Length(); i++) {
|
2011-07-27 20:26:20 +04:00
|
|
|
if (!mListeners[i].mHasSentData || aAccessPointsChanged) {
|
2011-10-17 18:59:28 +04:00
|
|
|
mListeners[i].mHasSentData = true;
|
2013-03-16 03:02:01 +04:00
|
|
|
currentListeners->AppendElement(mListeners[i].mListener);
|
2011-07-27 20:26:20 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-16 03:02:01 +04:00
|
|
|
if (currentListeners->Length() > 0)
|
2011-07-27 20:26:20 +04:00
|
|
|
{
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t resultCount = aAccessPoints.Count();
|
2011-07-27 20:26:20 +04:00
|
|
|
nsAutoPtr<nsTArray<nsIWifiAccessPoint*> > accessPoints(
|
|
|
|
new nsTArray<nsIWifiAccessPoint *>(resultCount));
|
|
|
|
if (!accessPoints)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t i = 0; i < resultCount; i++)
|
2011-07-27 20:26:20 +04:00
|
|
|
accessPoints->AppendElement(aAccessPoints[i]);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIThread> thread = do_GetMainThread();
|
|
|
|
if (!thread)
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIRunnable> runnable(
|
|
|
|
new nsCallWifiListeners(currentListeners, accessPoints));
|
|
|
|
if (!runnable)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
thread->Dispatch(runnable, NS_DISPATCH_SYNC);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|