Bug 713728 - 'Workers: Need a shutdown observer topic'. r=mrbkap.
authorBen Turner <bent.mozilla@gmail.com>
Thu, 29 Dec 2011 13:28:09 -0500
changeset 83608 af2659d377d40e24af4a9a550ae13cad47b6453c
parent 83607 ca99dd8313ce46018751a7e44686f5ce24382faa
child 83609 a895e9432ea4eebaf577c2e6937c7de15385bebc
push id21773
push userphilringnalda@gmail.com
push dateSun, 01 Jan 2012 02:50:39 +0000
treeherdermozilla-central@da6c33eb4b16 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmrbkap
bugs713728
milestone12.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 713728 - 'Workers: Need a shutdown observer topic'. r=mrbkap.
dom/workers/RuntimeService.cpp
dom/workers/Workers.h
--- a/dom/workers/RuntimeService.cpp
+++ b/dom/workers/RuntimeService.cpp
@@ -861,22 +861,21 @@ RuntimeService::Init()
   NS_ENSURE_STATE(mIdleThreadTimer);
 
   bool ok = mDomainMap.Init();
   NS_ENSURE_STATE(ok);
 
   ok = mWindowMap.Init();
   NS_ENSURE_STATE(ok);
 
-  nsresult rv;
-  nsCOMPtr<nsIObserverService> obs =
-    do_GetService(NS_OBSERVERSERVICE_CONTRACTID, &rv);
-  NS_ENSURE_SUCCESS(rv, rv);
+  nsCOMPtr<nsIObserverService> obs = services::GetObserverService();
+  NS_ENSURE_TRUE(obs, NS_ERROR_FAILURE);
 
-  rv = obs->AddObserver(this, NS_XPCOM_SHUTDOWN_THREADS_OBSERVER_ID, false);
+  nsresult rv =
+    obs->AddObserver(this, NS_XPCOM_SHUTDOWN_THREADS_OBSERVER_ID, false);
   NS_ENSURE_SUCCESS(rv, rv);
 
   mObserved = true;
 
   for (PRUint32 index = 0; index < ArrayLength(gPrefsToWatch); index++) {
     if (NS_FAILED(Preferences::RegisterCallback(PrefCallback,
                                                 gPrefsToWatch[index], this))) {
       NS_WARNING("Failed to register pref callback?!");
@@ -911,16 +910,26 @@ RuntimeService::Init()
 
 // This spins the event loop until all workers are finished and their threads
 // have been joined.
 void
 RuntimeService::Cleanup()
 {
   AssertIsOnMainThread();
 
+  nsCOMPtr<nsIObserverService> obs = services::GetObserverService();
+  NS_WARN_IF_FALSE(obs, "Failed to get observer service?!");
+
+  // Tell anyone that cares that they're about to lose worker support.
+  if (obs && NS_FAILED(obs->NotifyObservers(nsnull, WORKERS_SHUTDOWN_TOPIC,
+                                            nsnull))) {
+    NS_WARNING("NotifyObservers failed!");
+  }
+
+  // That's it, no more workers.
   mShuttingDown = true;
 
   if (mIdleThreadTimer) {
     if (NS_FAILED(mIdleThreadTimer->Cancel())) {
       NS_WARNING("Failed to cancel idle timer!");
     }
     mIdleThreadTimer = nsnull;
   }
@@ -990,23 +999,20 @@ RuntimeService::Cleanup()
     NS_ASSERTION(!mWindowMap.Count(), "All windows should have been released!");
   }
 
   if (mObserved) {
     for (PRUint32 index = 0; index < ArrayLength(gPrefsToWatch); index++) {
       Preferences::UnregisterCallback(PrefCallback, gPrefsToWatch[index], this);
     }
 
-    nsCOMPtr<nsIObserverService> obs = services::GetObserverService();
-    NS_WARN_IF_FALSE(obs, "Failed to get observer service?!");
-
     if (obs) {
       nsresult rv =
         obs->RemoveObserver(this, NS_XPCOM_SHUTDOWN_THREADS_OBSERVER_ID);
-      mObserved = !NS_SUCCEEDED(rv);
+      mObserved = NS_FAILED(rv);
     }
   }
 }
 
 // static
 PLDHashOperator
 RuntimeService::AddAllTopLevelWorkersToArray(const nsACString& aKey,
                                              WorkerDomainInfo* aData,
--- a/dom/workers/Workers.h
+++ b/dom/workers/Workers.h
@@ -46,16 +46,18 @@
 
 #define BEGIN_WORKERS_NAMESPACE \
   namespace mozilla { namespace dom { namespace workers {
 #define END_WORKERS_NAMESPACE \
   } /* namespace workers */ } /* namespace dom */ } /* namespace mozilla */
 #define USING_WORKERS_NAMESPACE \
   using namespace mozilla::dom::workers;
 
+#define WORKERS_SHUTDOWN_TOPIC "web-workers-shutdown"
+
 class nsPIDOMWindow;
 
 BEGIN_WORKERS_NAMESPACE
 
 class WorkerPrivate;
 
 struct PrivatizableBase
 { };