2012-05-21 04:12:37 -07: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-16 17:30:58 -07:00
|
|
|
|
|
|
|
#include "nsCOMPtr.h"
|
2013-03-15 16:02:01 -07:00
|
|
|
#include "nsProxyRelease.h"
|
2009-03-16 17:30:58 -07: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 09:26:20 -07:00
|
|
|
#include "nsWifiAccessPoint.h"
|
2009-03-16 17:30:58 -07: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 09:59:13 -07:00
|
|
|
#include "mozilla/Services.h"
|
2009-03-16 17:30:58 -07: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-03-31 21:29:02 -07:00
|
|
|
using namespace mozilla;
|
|
|
|
|
2009-03-16 17:30:58 -07:00
|
|
|
#if defined(PR_LOGGING)
|
|
|
|
PRLogModuleInfo *gWifiMonitorLog;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
NS_IMPL_THREADSAFE_ISUPPORTS3(nsWifiMonitor,
|
|
|
|
nsIRunnable,
|
|
|
|
nsIObserver,
|
|
|
|
nsIWifiMonitor)
|
|
|
|
|
|
|
|
nsWifiMonitor::nsWifiMonitor()
|
2011-10-17 07:59:28 -07:00
|
|
|
: mKeepGoing(true)
|
2011-04-29 12:21:57 -07:00
|
|
|
, mReentrantMonitor("nsWifiMonitor.mReentrantMonitor")
|
2009-03-16 17:30:58 -07: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 09:59:13 -07:00
|
|
|
nsCOMPtr<nsIObserverService> obsSvc = mozilla::services::GetObserverService();
|
2009-03-16 17:30:58 -07:00
|
|
|
if (obsSvc)
|
2011-10-17 07:59:28 -07:00
|
|
|
obsSvc->AddObserver(this, "xpcom-shutdown", false);
|
2009-03-16 17:30:58 -07:00
|
|
|
|
|
|
|
LOG(("@@@@@ wifimonitor created\n"));
|
|
|
|
}
|
|
|
|
|
|
|
|
nsWifiMonitor::~nsWifiMonitor()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsWifiMonitor::Observe(nsISupports *subject, const char *topic,
|
|
|
|
const PRUnichar *data)
|
|
|
|
{
|
|
|
|
if (!strcmp(topic, "xpcom-shutdown")) {
|
|
|
|
LOG(("Shutting down\n"));
|
2011-10-17 07:59:28 -07:00
|
|
|
mKeepGoing = false;
|
2009-03-16 17:30:58 -07:00
|
|
|
|
2011-04-29 12:21:57 -07:00
|
|
|
ReentrantMonitorAutoEnter mon(mReentrantMonitor);
|
2009-03-16 17:30:58 -07: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 12:21:57 -07:00
|
|
|
ReentrantMonitorAutoEnter mon(mReentrantMonitor);
|
2010-03-01 21:56:06 -08:00
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
mKeepGoing = true;
|
2009-03-16 17:30:58 -07:00
|
|
|
|
2013-03-15 16:02:01 -07:00
|
|
|
mListeners.AppendElement(nsWifiListener(new nsMainThreadPtrHolder<nsIWifiListener>(aListener)));
|
2009-03-16 17:30:58 -07: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 12:21:57 -07:00
|
|
|
ReentrantMonitorAutoEnter mon(mReentrantMonitor);
|
2010-03-01 21:56:06 -08:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t i = 0; i < mListeners.Length(); i++) {
|
2009-03-16 17:30:58 -07:00
|
|
|
|
|
|
|
if (mListeners[i].mListener == aListener) {
|
|
|
|
mListeners.RemoveElementAt(i);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mListeners.Length() == 0) {
|
2011-10-17 07:59:28 -07:00
|
|
|
mKeepGoing = false;
|
2009-03-16 17:30:58 -07:00
|
|
|
mon.Notify();
|
2012-07-30 07:20:58 -07:00
|
|
|
mThread = nullptr;
|
2009-03-16 17:30:58 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-03-15 16:02:01 -07:00
|
|
|
typedef nsTArray<nsMainThreadPtrHandle<nsIWifiListener> > WifiListenerArray;
|
|
|
|
|
2012-06-05 20:18:25 -07:00
|
|
|
class nsPassErrorToWifiListeners MOZ_FINAL : public nsIRunnable
|
2011-07-27 09:26:20 -07:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSIRUNNABLE
|
|
|
|
|
2013-03-15 16:02:01 -07:00
|
|
|
nsPassErrorToWifiListeners(nsAutoPtr<WifiListenerArray> aListeners,
|
2011-07-27 09:26:20 -07:00
|
|
|
nsresult aResult)
|
|
|
|
: mListeners(aListeners),
|
|
|
|
mResult(aResult)
|
|
|
|
{}
|
|
|
|
|
|
|
|
private:
|
2013-03-15 16:02:01 -07:00
|
|
|
nsAutoPtr<WifiListenerArray> mListeners;
|
2011-07-27 09:26:20 -07:00
|
|
|
nsresult mResult;
|
|
|
|
};
|
|
|
|
|
|
|
|
NS_IMPL_THREADSAFE_ISUPPORTS1(nsPassErrorToWifiListeners,
|
|
|
|
nsIRunnable)
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsPassErrorToWifiListeners::Run()
|
|
|
|
{
|
|
|
|
LOG(("About to send error to the wifi listeners\n"));
|
2013-03-15 16:02:01 -07:00
|
|
|
for (size_t i = 0; i < mListeners->Length(); i++) {
|
2011-08-14 10:39:20 -07:00
|
|
|
(*mListeners)[i]->OnError(mResult);
|
2011-07-27 09:26:20 -07:00
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2009-03-16 17:30:58 -07:00
|
|
|
|
|
|
|
NS_IMETHODIMP nsWifiMonitor::Run()
|
|
|
|
{
|
|
|
|
LOG(("@@@@@ wifi monitor run called\n"));
|
|
|
|
|
2012-06-12 10:06:20 -07:00
|
|
|
PR_SetCurrentThreadName("Wifi Monitor");
|
|
|
|
|
2009-03-16 17:30:58 -07:00
|
|
|
nsresult rv = DoScan();
|
|
|
|
|
|
|
|
if (mKeepGoing && NS_FAILED(rv)) {
|
2013-03-15 16:02:01 -07:00
|
|
|
nsAutoPtr<WifiListenerArray> currentListeners(
|
|
|
|
new WifiListenerArray(mListeners.Length()));
|
2011-07-27 09:26:20 -07:00
|
|
|
if (!currentListeners)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2009-03-16 17:30:58 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t i = 0; i < mListeners.Length(); i++)
|
2013-03-15 16:02:01 -07:00
|
|
|
currentListeners->AppendElement(mListeners[i].mListener);
|
2009-05-06 10:21:44 -07:00
|
|
|
|
2011-07-27 09:26:20 -07:00
|
|
|
nsCOMPtr<nsIThread> thread = do_GetMainThread();
|
|
|
|
if (!thread)
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
2009-05-06 10:21:44 -07:00
|
|
|
|
2011-07-27 09:26:20 -07:00
|
|
|
nsCOMPtr<nsIRunnable> runnable(new nsPassErrorToWifiListeners(currentListeners, rv));
|
|
|
|
if (!runnable)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2009-05-06 10:21:44 -07:00
|
|
|
|
2011-07-27 09:26:20 -07:00
|
|
|
thread->Dispatch(runnable, NS_DISPATCH_SYNC);
|
2009-03-16 17:30:58 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2011-07-27 09:26:20 -07:00
|
|
|
|
2012-06-05 20:18:25 -07:00
|
|
|
class nsCallWifiListeners MOZ_FINAL : public nsIRunnable
|
2011-07-27 09:26:20 -07:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSIRUNNABLE
|
|
|
|
|
2013-03-15 16:02:01 -07:00
|
|
|
nsCallWifiListeners(nsAutoPtr<WifiListenerArray> aListeners,
|
2011-07-27 09:26:20 -07:00
|
|
|
nsAutoPtr<nsTArray<nsIWifiAccessPoint*> > aAccessPoints)
|
|
|
|
: mListeners(aListeners),
|
|
|
|
mAccessPoints(aAccessPoints)
|
|
|
|
{}
|
|
|
|
|
|
|
|
private:
|
2013-03-15 16:02:01 -07:00
|
|
|
nsAutoPtr<WifiListenerArray> mListeners;
|
2011-07-27 09:26:20 -07:00
|
|
|
nsAutoPtr<nsTArray<nsIWifiAccessPoint*> > mAccessPoints;
|
|
|
|
};
|
|
|
|
|
|
|
|
NS_IMPL_THREADSAFE_ISUPPORTS1(nsCallWifiListeners,
|
|
|
|
nsIRunnable)
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsCallWifiListeners::Run()
|
|
|
|
{
|
|
|
|
LOG(("About to send data to the wifi listeners\n"));
|
2013-03-15 16:02:01 -07:00
|
|
|
for (size_t i = 0; i < mListeners->Length(); i++) {
|
2011-08-14 10:39:20 -07:00
|
|
|
(*mListeners)[i]->OnChange(mAccessPoints->Elements(), mAccessPoints->Length());
|
2011-07-27 09:26:20 -07:00
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsWifiMonitor::CallWifiListeners(const nsCOMArray<nsWifiAccessPoint> &aAccessPoints,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool aAccessPointsChanged)
|
2011-07-27 09:26:20 -07:00
|
|
|
{
|
2013-03-15 16:02:01 -07:00
|
|
|
nsAutoPtr<WifiListenerArray> currentListeners(
|
|
|
|
new WifiListenerArray(mListeners.Length()));
|
2011-07-27 09:26:20 -07:00
|
|
|
if (!currentListeners)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
{
|
|
|
|
ReentrantMonitorAutoEnter mon(mReentrantMonitor);
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t i = 0; i < mListeners.Length(); i++) {
|
2011-07-27 09:26:20 -07:00
|
|
|
if (!mListeners[i].mHasSentData || aAccessPointsChanged) {
|
2011-10-17 07:59:28 -07:00
|
|
|
mListeners[i].mHasSentData = true;
|
2013-03-15 16:02:01 -07:00
|
|
|
currentListeners->AppendElement(mListeners[i].mListener);
|
2011-07-27 09:26:20 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-15 16:02:01 -07:00
|
|
|
if (currentListeners->Length() > 0)
|
2011-07-27 09:26:20 -07:00
|
|
|
{
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t resultCount = aAccessPoints.Count();
|
2011-07-27 09:26:20 -07:00
|
|
|
nsAutoPtr<nsTArray<nsIWifiAccessPoint*> > accessPoints(
|
|
|
|
new nsTArray<nsIWifiAccessPoint *>(resultCount));
|
|
|
|
if (!accessPoints)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t i = 0; i < resultCount; i++)
|
2011-07-27 09:26:20 -07: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;
|
|
|
|
}
|