Bug 1259290: Part 3 - Remove JS_ReportError and cx handling from RegisterWorker and friends. r=bz,jandem
authorKyle Huey <khuey@kylehuey.com>
Mon, 28 Mar 2016 10:28:14 -0700
changeset 290736 d317ee09ff1a1bda98797be54b7d2f7dc10b84d6
parent 290735 780e64f25e1a2bb3ef072ac83a5bc94d8c31dde2
child 290737 a4dbc5475d87762085ac168a1c701a35e16016f7
push id19656
push usergwagner@mozilla.com
push dateMon, 04 Apr 2016 13:43:23 +0000
treeherderb2g-inbound@e99061fde28a [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbz, jandem
bugs1259290
milestone48.0a1
Bug 1259290: Part 3 - Remove JS_ReportError and cx handling from RegisterWorker and friends. r=bz,jandem
dom/workers/RuntimeService.cpp
dom/workers/RuntimeService.h
dom/workers/WorkerPrivate.cpp
js/src/jsapi.cpp
js/src/jsapi.h
js/src/shell/js.cpp
--- a/dom/workers/RuntimeService.cpp
+++ b/dom/workers/RuntimeService.cpp
@@ -1382,26 +1382,25 @@ RuntimeService::GetOrCreateService()
 // static
 RuntimeService*
 RuntimeService::GetService()
 {
   return gRuntimeService;
 }
 
 bool
-RuntimeService::RegisterWorker(JSContext* aCx, WorkerPrivate* aWorkerPrivate)
+RuntimeService::RegisterWorker(WorkerPrivate* aWorkerPrivate)
 {
   aWorkerPrivate->AssertIsOnParentThread();
 
   WorkerPrivate* parent = aWorkerPrivate->GetParent();
   if (!parent) {
     AssertIsOnMainThread();
 
     if (mShuttingDown) {
-      JS_ReportError(aCx, "Cannot create worker during shutdown!");
       return false;
     }
   }
 
   const bool isServiceWorker = aWorkerPrivate->IsServiceWorker();
   const bool isSharedWorker = aWorkerPrivate->IsSharedWorker();
   if (isServiceWorker) {
     AssertIsOnMainThread();
@@ -1413,17 +1412,16 @@ RuntimeService::RegisterWorker(JSContext
     AssertIsOnMainThread();
 
     nsCOMPtr<nsIURI> scriptURI = aWorkerPrivate->GetResolvedScriptURI();
     NS_ASSERTION(scriptURI, "Null script URI!");
 
     nsresult rv = scriptURI->GetSpec(sharedWorkerScriptSpec);
     if (NS_FAILED(rv)) {
       NS_WARNING("GetSpec failed?!");
-      xpc::Throw(aCx, rv);
       return false;
     }
 
     NS_ASSERTION(!sharedWorkerScriptSpec.IsEmpty(), "Empty spec!");
   }
 
   bool exemptFromPerDomainMax = false;
   if (isServiceWorker) {
@@ -1491,30 +1489,29 @@ RuntimeService::RegisterWorker(JSContext
                              sharedWorkerName);
       domainInfo->mSharedWorkerInfos.Put(key, sharedWorkerInfo);
     }
   }
 
   // From here on out we must call UnregisterWorker if something fails!
   if (parent) {
     if (!parent->AddChildWorker(aWorkerPrivate)) {
-      UnregisterWorker(aCx, aWorkerPrivate);
+      UnregisterWorker(aWorkerPrivate);
       return false;
     }
   }
   else {
     if (!mNavigatorPropertiesLoaded) {
       Navigator::AppName(mNavigatorProperties.mAppName,
                          false /* aUsePrefOverriddenValue */);
       if (NS_FAILED(Navigator::GetAppVersion(mNavigatorProperties.mAppVersion,
                                              false /* aUsePrefOverriddenValue */)) ||
           NS_FAILED(Navigator::GetPlatform(mNavigatorProperties.mPlatform,
                                            false /* aUsePrefOverriddenValue */))) {
-        JS_ReportError(aCx, "Failed to load navigator strings!");
-        UnregisterWorker(aCx, aWorkerPrivate);
+        UnregisterWorker(aWorkerPrivate);
         return false;
       }
 
       // The navigator overridden properties should have already been read.
 
       Navigator::GetAcceptLanguages(mNavigatorProperties.mLanguages);
       mNavigatorPropertiesLoaded = true;
     }
@@ -1533,17 +1530,17 @@ RuntimeService::RegisterWorker(JSContext
       if (!windowArray->Contains(aWorkerPrivate)) {
         windowArray->AppendElement(aWorkerPrivate);
       } else {
         MOZ_ASSERT(aWorkerPrivate->IsSharedWorker());
       }
     }
   }
 
-  if (!queued && !ScheduleWorker(aCx, aWorkerPrivate)) {
+  if (!queued && !ScheduleWorker(aWorkerPrivate)) {
     return false;
   }
 
   if (isServiceWorker) {
     AssertIsOnMainThread();
     Telemetry::Accumulate(Telemetry::SERVICE_WORKER_WAS_SPAWNED, 1);
   }
   return true;
@@ -1566,17 +1563,17 @@ RuntimeService::RemoveSharedWorker(Worke
 #endif
       iter.Remove();
       break;
     }
   }
 }
 
 void
-RuntimeService::UnregisterWorker(JSContext* aCx, WorkerPrivate* aWorkerPrivate)
+RuntimeService::UnregisterWorker(WorkerPrivate* aWorkerPrivate)
 {
   aWorkerPrivate->AssertIsOnParentThread();
 
   WorkerPrivate* parent = aWorkerPrivate->GetParent();
   if (!parent) {
     AssertIsOnMainThread();
   }
 
@@ -1680,26 +1677,23 @@ RuntimeService::UnregisterWorker(JSConte
 
     MOZ_ALWAYS_TRUE(windowArray->RemoveElement(aWorkerPrivate));
 
     if (windowArray->IsEmpty()) {
       mWindowMap.Remove(window);
     }
   }
 
-  if (queuedWorker && !ScheduleWorker(aCx, queuedWorker)) {
-    UnregisterWorker(aCx, queuedWorker);
-    // There's nowhere sane to report the exception from ScheduleWorker, if any,
-    // here.
-    JS_ClearPendingException(aCx);
+  if (queuedWorker && !ScheduleWorker(queuedWorker)) {
+    UnregisterWorker(queuedWorker);
   }
 }
 
 bool
-RuntimeService::ScheduleWorker(JSContext* aCx, WorkerPrivate* aWorkerPrivate)
+RuntimeService::ScheduleWorker(WorkerPrivate* aWorkerPrivate)
 {
   if (!aWorkerPrivate->Start()) {
     // This is ok, means that we didn't need to make a thread for this worker.
     return true;
   }
 
   RefPtr<WorkerThread> thread;
   {
@@ -1711,35 +1705,35 @@ RuntimeService::ScheduleWorker(JSContext
     }
   }
 
   const WorkerThreadFriendKey friendKey;
 
   if (!thread) {
     thread = WorkerThread::Create(friendKey);
     if (!thread) {
-      UnregisterWorker(aCx, aWorkerPrivate);
-      JS_ReportError(aCx, "Could not create new thread!");
+      UnregisterWorker(aWorkerPrivate);
       return false;
     }
   }
 
   int32_t priority = aWorkerPrivate->IsChromeWorker() ?
                      nsISupportsPriority::PRIORITY_NORMAL :
                      nsISupportsPriority::PRIORITY_LOW;
 
   if (NS_FAILED(thread->SetPriority(priority))) {
     NS_WARNING("Could not set the thread's priority!");
   }
 
+  JSRuntime* rt = CycleCollectedJSRuntime::Get()->Runtime();
   nsCOMPtr<nsIRunnable> runnable =
-    new WorkerThreadPrimaryRunnable(aWorkerPrivate, thread, JS_GetParentRuntime(aCx));
+    new WorkerThreadPrimaryRunnable(aWorkerPrivate, thread,
+                                    JS_GetParentRuntime(rt));
   if (NS_FAILED(thread->DispatchPrimaryRunnable(friendKey, runnable.forget()))) {
-    UnregisterWorker(aCx, aWorkerPrivate);
-    JS_ReportError(aCx, "Could not dispatch to thread!");
+    UnregisterWorker(aWorkerPrivate);
     return false;
   }
 
   return true;
 }
 
 // static
 void
--- a/dom/workers/RuntimeService.h
+++ b/dom/workers/RuntimeService.h
@@ -119,20 +119,20 @@ public:
 
   static RuntimeService*
   GetOrCreateService();
 
   static RuntimeService*
   GetService();
 
   bool
-  RegisterWorker(JSContext* aCx, WorkerPrivate* aWorkerPrivate);
+  RegisterWorker(WorkerPrivate* aWorkerPrivate);
 
   void
-  UnregisterWorker(JSContext* aCx, WorkerPrivate* aWorkerPrivate);
+  UnregisterWorker(WorkerPrivate* aWorkerPrivate);
 
   void
   RemoveSharedWorker(WorkerDomainInfo* aDomainInfo,
                      WorkerPrivate* aWorkerPrivate);
 
   void
   CancelWorkersForWindow(nsPIDOMWindowInner* aWindow);
 
@@ -270,17 +270,17 @@ private:
   void
   AddAllTopLevelWorkersToArray(nsTArray<WorkerPrivate*>& aWorkers);
 
   void
   GetWorkersForWindow(nsPIDOMWindowInner* aWindow,
                       nsTArray<WorkerPrivate*>& aWorkers);
 
   bool
-  ScheduleWorker(JSContext* aCx, WorkerPrivate* aWorkerPrivate);
+  ScheduleWorker(WorkerPrivate* aWorkerPrivate);
 
   static void
   ShutdownIdleThreads(nsITimer* aTimer, void* aClosure);
 
   static void
   WorkerPrefChanged(const char* aPrefName, void* aClosure);
 
   static void
--- a/dom/workers/WorkerPrivate.cpp
+++ b/dom/workers/WorkerPrivate.cpp
@@ -400,17 +400,17 @@ private:
       NS_WARNING("Failed to dispatch, going to leak!");
     }
 
     RuntimeService* runtime = RuntimeService::GetService();
     NS_ASSERTION(runtime, "This should never be null!");
 
     mFinishedWorker->DisableDebugger();
 
-    runtime->UnregisterWorker(aCx, mFinishedWorker);
+    runtime->UnregisterWorker(mFinishedWorker);
 
     mFinishedWorker->ClearSelfRef();
     return true;
   }
 };
 
 class TopLevelWorkerFinishedRunnable final : public nsRunnable
 {
@@ -431,22 +431,19 @@ private:
   NS_IMETHOD
   Run() override
   {
     AssertIsOnMainThread();
 
     RuntimeService* runtime = RuntimeService::GetService();
     MOZ_ASSERT(runtime);
 
-    AutoSafeJSContext cx;
-    JSAutoRequest ar(cx);
-
     mFinishedWorker->DisableDebugger();
 
-    runtime->UnregisterWorker(cx, mFinishedWorker);
+    runtime->UnregisterWorker(mFinishedWorker);
 
     nsCOMPtr<nsILoadGroup> loadGroupToCancel;
     mFinishedWorker->ForgetOverridenLoadGroup(loadGroupToCancel);
 
     nsTArray<nsCOMPtr<nsISupports> > doomed;
     mFinishedWorker->ForgetMainThreadObjects(doomed);
 
     RefPtr<MainThreadReleaseRunnable> runnable =
@@ -4106,17 +4103,17 @@ WorkerPrivate::Constructor(JSContext* aC
   }
 
   MOZ_ASSERT(runtimeService);
 
   RefPtr<WorkerPrivate> worker =
     new WorkerPrivate(parent, aScriptURL, aIsChromeWorker,
                       aWorkerType, aWorkerName, *aLoadInfo);
 
-  if (!runtimeService->RegisterWorker(aCx, worker)) {
+  if (!runtimeService->RegisterWorker(worker)) {
     aRv.Throw(NS_ERROR_UNEXPECTED);
     return nullptr;
   }
 
   worker->EnableDebugger();
 
   RefPtr<CompileScriptRunnable> compiler =
     new CompileScriptRunnable(worker, aScriptURL);
--- a/js/src/jsapi.cpp
+++ b/js/src/jsapi.cpp
@@ -616,19 +616,18 @@ JS_SetSecondContextPrivate(JSContext* cx
 
 JS_PUBLIC_API(JSRuntime*)
 JS_GetRuntime(JSContext* cx)
 {
     return cx->runtime();
 }
 
 JS_PUBLIC_API(JSRuntime*)
-JS_GetParentRuntime(JSContext* cx)
-{
-    JSRuntime* rt = cx->runtime();
+JS_GetParentRuntime(JSRuntime* rt)
+{
     return rt->parentRuntime ? rt->parentRuntime : rt;
 }
 
 JS_PUBLIC_API(JSContext*)
 JS_ContextIterator(JSRuntime* rt, JSContext** iterp)
 {
     JSContext* cx = *iterp;
     cx = cx ? cx->getNext() : rt->contextList.getFirst();
--- a/js/src/jsapi.h
+++ b/js/src/jsapi.h
@@ -993,17 +993,17 @@ JS_GetCurrentEmbedderTime();
 
 JS_PUBLIC_API(void*)
 JS_GetRuntimePrivate(JSRuntime* rt);
 
 extern JS_PUBLIC_API(JSRuntime*)
 JS_GetRuntime(JSContext* cx);
 
 extern JS_PUBLIC_API(JSRuntime*)
-JS_GetParentRuntime(JSContext* cx);
+JS_GetParentRuntime(JSRuntime* rt);
 
 JS_PUBLIC_API(void)
 JS_SetRuntimePrivate(JSRuntime* rt, void* data);
 
 extern JS_PUBLIC_API(void)
 JS_BeginRequest(JSContext* cx);
 
 extern JS_PUBLIC_API(void)
--- a/js/src/shell/js.cpp
+++ b/js/src/shell/js.cpp
@@ -3013,17 +3013,17 @@ EvalInWorker(JSContext* cx, unsigned arg
     char16_t* chars = (char16_t*) js_malloc(str->length() * sizeof(char16_t));
     if (!chars) {
         ReportOutOfMemory(cx);
         return false;
     }
 
     CopyChars(chars, *str);
 
-    WorkerInput* input = js_new<WorkerInput>(JS_GetParentRuntime(cx), chars, str->length());
+    WorkerInput* input = js_new<WorkerInput>(JS_GetParentRuntime(JS_GetRuntime(cx)), chars, str->length());
     if (!input) {
         ReportOutOfMemory(cx);
         return false;
     }
 
     PRThread* thread = PR_CreateThread(PR_USER_THREAD, WorkerMain, input,
                                        PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD, PR_JOINABLE_THREAD,
                                        gMaxStackSize + 128 * 1024);