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"
|
|
|
|
#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
|
|
|
|
|
|
|
|
NS_IMPL_THREADSAFE_ISUPPORTS3(nsWifiMonitor,
|
|
|
|
nsIRunnable,
|
|
|
|
nsIObserver,
|
|
|
|
nsIWifiMonitor)
|
|
|
|
|
|
|
|
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,
|
|
|
|
const PRUnichar *data)
|
|
|
|
{
|
|
|
|
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
|
|
|
|
|
|
|
mListeners.AppendElement(nsWifiListener(aListener));
|
|
|
|
|
|
|
|
// 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
|
|
|
|
2009-03-17 03:30:58 +03:00
|
|
|
for (PRUint32 i = 0; i < mListeners.Length(); i++) {
|
|
|
|
|
|
|
|
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();
|
|
|
|
mThread = nsnull;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-06-06 07:18:25 +04:00
|
|
|
class nsPassErrorToWifiListeners MOZ_FINAL : public nsIRunnable
|
2011-07-27 20:26:20 +04:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSIRUNNABLE
|
|
|
|
|
|
|
|
nsPassErrorToWifiListeners(nsAutoPtr<nsCOMArray<nsIWifiListener> > aListeners,
|
|
|
|
nsresult aResult)
|
|
|
|
: mListeners(aListeners),
|
|
|
|
mResult(aResult)
|
|
|
|
{}
|
|
|
|
|
|
|
|
private:
|
|
|
|
nsAutoPtr<nsCOMArray<nsIWifiListener> > mListeners;
|
|
|
|
nsresult mResult;
|
|
|
|
};
|
|
|
|
|
|
|
|
NS_IMPL_THREADSAFE_ISUPPORTS1(nsPassErrorToWifiListeners,
|
|
|
|
nsIRunnable)
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsPassErrorToWifiListeners::Run()
|
|
|
|
{
|
|
|
|
LOG(("About to send error to the wifi listeners\n"));
|
|
|
|
for (PRInt32 i = 0; i < mListeners->Count(); 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)) {
|
2011-07-27 20:26:20 +04:00
|
|
|
nsAutoPtr<nsCOMArray<nsIWifiListener> > currentListeners(
|
|
|
|
new nsCOMArray<nsIWifiListener>(mListeners.Length()));
|
|
|
|
if (!currentListeners)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2009-03-17 03:30:58 +03:00
|
|
|
|
2011-07-27 20:26:20 +04:00
|
|
|
for (PRUint32 i = 0; i < mListeners.Length(); i++)
|
|
|
|
currentListeners->AppendObject(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:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSIRUNNABLE
|
|
|
|
|
|
|
|
nsCallWifiListeners(nsAutoPtr<nsCOMArray<nsIWifiListener> > aListeners,
|
|
|
|
nsAutoPtr<nsTArray<nsIWifiAccessPoint*> > aAccessPoints)
|
|
|
|
: mListeners(aListeners),
|
|
|
|
mAccessPoints(aAccessPoints)
|
|
|
|
{}
|
|
|
|
|
|
|
|
private:
|
|
|
|
nsAutoPtr<nsCOMArray<nsIWifiListener> > mListeners;
|
|
|
|
nsAutoPtr<nsTArray<nsIWifiAccessPoint*> > mAccessPoints;
|
|
|
|
};
|
|
|
|
|
|
|
|
NS_IMPL_THREADSAFE_ISUPPORTS1(nsCallWifiListeners,
|
|
|
|
nsIRunnable)
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsCallWifiListeners::Run()
|
|
|
|
{
|
|
|
|
LOG(("About to send data to the wifi listeners\n"));
|
|
|
|
for (PRInt32 i = 0; i < mListeners->Count(); 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
|
|
|
{
|
|
|
|
nsAutoPtr<nsCOMArray<nsIWifiListener> > currentListeners(
|
|
|
|
new nsCOMArray<nsIWifiListener>(mListeners.Length()));
|
|
|
|
if (!currentListeners)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
{
|
|
|
|
ReentrantMonitorAutoEnter mon(mReentrantMonitor);
|
|
|
|
|
|
|
|
for (PRUint32 i = 0; i < mListeners.Length(); i++) {
|
|
|
|
if (!mListeners[i].mHasSentData || aAccessPointsChanged) {
|
2011-10-17 18:59:28 +04:00
|
|
|
mListeners[i].mHasSentData = true;
|
2011-07-27 20:26:20 +04:00
|
|
|
currentListeners->AppendObject(mListeners[i].mListener);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (currentListeners->Count() > 0)
|
|
|
|
{
|
|
|
|
PRUint32 resultCount = aAccessPoints.Count();
|
|
|
|
nsAutoPtr<nsTArray<nsIWifiAccessPoint*> > accessPoints(
|
|
|
|
new nsTArray<nsIWifiAccessPoint *>(resultCount));
|
|
|
|
if (!accessPoints)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
for (PRUint32 i = 0; i < resultCount; i++)
|
|
|
|
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;
|
|
|
|
}
|