Bug 995405: Don't pass around references to the observer service off the main thread. r=michal
☠☠ backed out by e2cf132023be ☠ ☠
authorKyle Huey <khuey@kylehuey.com>
Mon, 14 Apr 2014 12:04:26 -0700
changeset 196895 9d4032c2f2372b2d9af180404f0d26fdfa3f6d36
parent 196894 ddbac34527fefbc05afbdf735141184cca07ea10
child 196896 3797f7c91ed2f88712b6a847380b9a75dbbfa7cf
push id3624
push userasasaki@mozilla.com
push dateMon, 09 Jun 2014 21:49:01 +0000
treeherdermozilla-beta@b1a5da15899a [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmichal
bugs995405
milestone31.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 995405: Don't pass around references to the observer service off the main thread. r=michal
netwerk/base/src/NetworkActivityMonitor.cpp
netwerk/base/src/NetworkActivityMonitor.h
--- a/netwerk/base/src/NetworkActivityMonitor.cpp
+++ b/netwerk/base/src/NetworkActivityMonitor.cpp
@@ -146,47 +146,54 @@ nsNetMon_AcceptRead(PRFileDesc *listenSo
   if (ret > 0)
     NetworkActivityMonitor::DataInOut(NetworkActivityMonitor::kDownload);
   return ret;
 }
 
 
 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:
   nsCOMPtr<nsIObserverService>      mObs;
   NetworkActivityMonitor::Direction mDirection;
 };
 
 NetworkActivityMonitor * NetworkActivityMonitor::gInstance = nullptr;
 
 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;
 }
 
 nsresult
 NetworkActivityMonitor::Init(int32_t blipInterval)
 {
   nsresult rv;
 
@@ -224,20 +231,16 @@ NetworkActivityMonitor::Init_Internal(in
   mLayerMethods.write      = nsNetMon_Write;
   mLayerMethods.writev     = nsNetMon_Writev;
   mLayerMethods.recv       = nsNetMon_Recv;
   mLayerMethods.send       = nsNetMon_Send;
   mLayerMethods.recvfrom   = nsNetMon_RecvFrom;
   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.
   mLastNotificationTime[kUpload] = PR_IntervalNow() - mBlipInterval;
   mLastNotificationTime[kDownload] = mLastNotificationTime[kUpload];
 
   return NS_OK;
 }
@@ -282,12 +285,11 @@ NetworkActivityMonitor::DataInOut(Direct
   }
 
   return NS_OK;
 }
 
 void
 NetworkActivityMonitor::PostNotification(Direction direction)
 {
-  nsRefPtr<nsIRunnable> ev = new NotifyNetworkActivity(mObserverService,
-                                                       direction);
+  nsRefPtr<nsIRunnable> ev = new NotifyNetworkActivity(direction);
   NS_DispatchToMainThread(ev);
 }
--- a/netwerk/base/src/NetworkActivityMonitor.h
+++ b/netwerk/base/src/NetworkActivityMonitor.h
@@ -2,22 +2,19 @@
  *
  * 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/. */
 
 #ifndef NetworkActivityMonitor_h___
 #define NetworkActivityMonitor_h___
 
-#include "nsCOMPtr.h"
 #include "prio.h"
 #include "prinrval.h"
 
-class nsIObserverService;
-
 namespace mozilla { namespace net {
 
 class NetworkActivityMonitor
 {
 public:
   enum Direction {
     kUpload   = 0,
     kDownload = 1
@@ -36,14 +33,13 @@ private:
   nsresult Init_Internal(int32_t blipInterval);
   void PostNotification(Direction direction);
 
   static NetworkActivityMonitor * gInstance;
   PRDescIdentity                  mLayerIdentity;
   PRIOMethods                     mLayerMethods;
   PRIntervalTime                  mBlipInterval;
   PRIntervalTime                  mLastNotificationTime[2];
-  nsCOMPtr<nsIObserverService>    mObserverService;
 };
 
 }} // namespace mozilla::net
 
 #endif /* NetworkActivityMonitor_h___ */