Bug 995405: Don't pass around references to the observer service off the main thread. r=michal

--HG--
extra : rebase_source : 14baedc2f8e0d9f63b30be9c3c9eb373ba432ec5
This commit is contained in:
Kyle Huey 2014-04-14 12:04:26 -07:00
parent c8e649664b
commit 0b268f46dc
2 changed files with 19 additions and 19 deletions

View File

@ -151,18 +151,22 @@ nsNetMon_AcceptRead(PRFileDesc *listenSock,
class NotifyNetworkActivity : public nsRunnable {
public:
NotifyNetworkActivity(nsIObserverService* aObs,
NetworkActivityMonitor::Direction aDirection)
: mObs(aObs)
, mDirection(aDirection)
NotifyNetworkActivity(NetworkActivityMonitor::Direction aDirection)
: mDirection(aDirection)
{}
NS_IMETHOD Run()
{
mObs->NotifyObservers(nullptr,
mDirection == NetworkActivityMonitor::kUpload
? NS_NETWORK_ACTIVITY_BLIP_UPLOAD_TOPIC
: NS_NETWORK_ACTIVITY_BLIP_DOWNLOAD_TOPIC,
nullptr);
MOZ_ASSERT(NS_IsMainThread());
nsCOMPtr<nsIObserverService> obs = mozilla::services::GetObserverService();
if (!obs)
return NS_ERROR_FAILURE;
obs->NotifyObservers(nullptr,
mDirection == NetworkActivityMonitor::kUpload
? NS_NETWORK_ACTIVITY_BLIP_UPLOAD_TOPIC
: NS_NETWORK_ACTIVITY_BLIP_DOWNLOAD_TOPIC,
nullptr);
return NS_OK;
}
private:
@ -176,12 +180,15 @@ NetworkActivityMonitor::NetworkActivityMonitor()
: mLayerIdentity(PR_INVALID_IO_LAYER)
, mBlipInterval(PR_INTERVAL_NO_TIMEOUT)
{
MOZ_COUNT_CTOR(NetworkActivityMonitor);
NS_ASSERTION(gInstance==nullptr,
"multiple NetworkActivityMonitor instances!");
}
NetworkActivityMonitor::~NetworkActivityMonitor()
{
MOZ_COUNT_DTOR(NetworkActivityMonitor);
gInstance = nullptr;
}
@ -229,10 +236,6 @@ NetworkActivityMonitor::Init_Internal(int32_t blipInterval)
mLayerMethods.sendto = nsNetMon_SendTo;
mLayerMethods.acceptread = nsNetMon_AcceptRead;
mObserverService = mozilla::services::GetObserverService();
if (!mObserverService)
return NS_ERROR_FAILURE;
mBlipInterval = PR_MillisecondsToInterval(blipInterval);
// Set the last notification times to time that has just expired, so any
// activity even right now will trigger notification.
@ -287,7 +290,6 @@ NetworkActivityMonitor::DataInOut(Direction direction)
void
NetworkActivityMonitor::PostNotification(Direction direction)
{
nsRefPtr<nsIRunnable> ev = new NotifyNetworkActivity(mObserverService,
direction);
nsRefPtr<nsIRunnable> ev = new NotifyNetworkActivity(direction);
NS_DispatchToMainThread(ev);
}

View File

@ -7,12 +7,11 @@
#ifndef NetworkActivityMonitor_h___
#define NetworkActivityMonitor_h___
#include "nsCOMPtr.h"
#include <stdint.h>
#include "nscore.h"
#include "prio.h"
#include "prinrval.h"
class nsIObserverService;
namespace mozilla { namespace net {
class NetworkActivityMonitor
@ -41,7 +40,6 @@ private:
PRIOMethods mLayerMethods;
PRIntervalTime mBlipInterval;
PRIntervalTime mLastNotificationTime[2];
nsCOMPtr<nsIObserverService> mObserverService;
};
}} // namespace mozilla::net