Bug 1469579 - WorkerRef in ScriptLoader, r=bkelly
authorAndrea Marchesini <amarchesini@mozilla.com>
Tue, 19 Jun 2018 13:08:12 -0400
changeset 423049 2b24215292039221a21f66296467eb372dbb3fdd
parent 423048 5d130a2d962c247af9316edb7506b19165e506dc
child 423050 85d2aec2f4b6ebd72a8a87af20575b678142023f
push id65342
push userapavel@mozilla.com
push dateWed, 20 Jun 2018 11:30:38 +0000
treeherderautoland@dadc58a65c2e [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbkelly
bugs1469579
milestone62.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 1469579 - WorkerRef in ScriptLoader, r=bkelly
dom/workers/ScriptLoader.cpp
--- a/dom/workers/ScriptLoader.cpp
+++ b/dom/workers/ScriptLoader.cpp
@@ -600,36 +600,32 @@ private:
   ~LoaderListener() {}
 
   RefPtr<ScriptLoaderRunnable> mRunnable;
   uint32_t mIndex;
 };
 
 NS_IMPL_ISUPPORTS(LoaderListener, nsIStreamLoaderObserver, nsIRequestObserver)
 
-class ScriptLoaderHolder;
-
 class ScriptLoaderRunnable final : public nsIRunnable,
                                    public nsINamed
 {
   friend class ScriptExecutorRunnable;
-  friend class ScriptLoaderHolder;
   friend class CachePromiseHandler;
   friend class CacheScriptLoader;
   friend class LoaderListener;
 
   WorkerPrivate* mWorkerPrivate;
   nsCOMPtr<nsIEventTarget> mSyncLoopTarget;
   nsTArray<ScriptLoadInfo> mLoadInfos;
   RefPtr<CacheCreator> mCacheCreator;
   Maybe<ClientInfo> mClientInfo;
   Maybe<ServiceWorkerDescriptor> mController;
   bool mIsMainScript;
   WorkerScriptType mWorkerScriptType;
-  bool mCanceled;
   bool mCanceledMainThread;
   ErrorResult& mRv;
 
 public:
   NS_DECL_THREADSAFE_ISUPPORTS
 
   ScriptLoaderRunnable(WorkerPrivate* aWorkerPrivate,
                        nsIEventTarget* aSyncLoopTarget,
@@ -637,25 +633,31 @@ public:
                        const Maybe<ClientInfo>& aClientInfo,
                        const Maybe<ServiceWorkerDescriptor>& aController,
                        bool aIsMainScript,
                        WorkerScriptType aWorkerScriptType,
                        ErrorResult& aRv)
   : mWorkerPrivate(aWorkerPrivate), mSyncLoopTarget(aSyncLoopTarget),
     mClientInfo(aClientInfo), mController(aController),
     mIsMainScript(aIsMainScript), mWorkerScriptType(aWorkerScriptType),
-    mCanceled(false), mCanceledMainThread(false), mRv(aRv)
+    mCanceledMainThread(false), mRv(aRv)
   {
     aWorkerPrivate->AssertIsOnWorkerThread();
     MOZ_ASSERT(aSyncLoopTarget);
     MOZ_ASSERT_IF(aIsMainScript, aLoadInfos.Length() == 1);
 
     mLoadInfos.SwapElements(aLoadInfos);
   }
 
+  void
+  CancelMainThreadWithBindingAborted()
+  {
+    CancelMainThread(NS_BINDING_ABORTED);
+  }
+
 private:
   ~ScriptLoaderRunnable()
   { }
 
   NS_IMETHOD
   Run() override
   {
     AssertIsOnMainThread();
@@ -809,51 +811,28 @@ private:
 
     loadInfo.mCachePromise.swap(cachePromise);
     loadInfo.mCacheStatus = ScriptLoadInfo::WritingToCache;
 
     return NS_OK;
   }
 
   bool
-  Notify(WorkerStatus aStatus)
-  {
-    mWorkerPrivate->AssertIsOnWorkerThread();
-
-    if (aStatus >= Terminating && !mCanceled) {
-      mCanceled = true;
-
-      MOZ_ALWAYS_SUCCEEDS(
-        NS_DispatchToMainThread(NewRunnableMethod("ScriptLoaderRunnable::CancelMainThreadWithBindingAborted",
-                                                  this,
-                                                  &ScriptLoaderRunnable::CancelMainThreadWithBindingAborted)));
-    }
-
-    return true;
-  }
-
-  bool
   IsMainWorkerScript() const
   {
     return mIsMainScript && mWorkerScriptType == WorkerScript;
   }
 
   bool
   IsDebuggerScript() const
   {
     return mWorkerScriptType == DebuggerScript;
   }
 
   void
-  CancelMainThreadWithBindingAborted()
-  {
-    CancelMainThread(NS_BINDING_ABORTED);
-  }
-
-  void
   CancelMainThread(nsresult aCancelResult)
   {
     AssertIsOnMainThread();
 
     if (mCanceledMainThread) {
       return;
     }
 
@@ -1469,37 +1448,16 @@ private:
         MOZ_ASSERT(false, "This should never fail!");
       }
     }
   }
 };
 
 NS_IMPL_ISUPPORTS(ScriptLoaderRunnable, nsIRunnable, nsINamed)
 
-class MOZ_STACK_CLASS ScriptLoaderHolder final : public WorkerHolder
-{
-  // Raw pointer because this holder object follows the mRunnable life-time.
-  ScriptLoaderRunnable* mRunnable;
-
-public:
-  explicit ScriptLoaderHolder(ScriptLoaderRunnable* aRunnable)
-    : WorkerHolder("ScriptLoaderHolder")
-    , mRunnable(aRunnable)
-  {
-    MOZ_ASSERT(aRunnable);
-  }
-
-  virtual bool
-  Notify(WorkerStatus aStatus) override
-  {
-    mRunnable->Notify(aStatus);
-    return true;
-  }
-};
-
 NS_IMETHODIMP
 LoaderListener::OnStreamComplete(nsIStreamLoader* aLoader, nsISupports* aContext,
                                  nsresult aStatus, uint32_t aStringLen,
                                  const uint8_t* aString)
 {
   return mRunnable->OnStreamComplete(aLoader, mIndex, aStatus, aStringLen, aString);
 }
 
@@ -2272,19 +2230,24 @@ LoadAllScripts(WorkerPrivate* aWorkerPri
 
   RefPtr<ScriptLoaderRunnable> loader =
     new ScriptLoaderRunnable(aWorkerPrivate, syncLoopTarget, aLoadInfos,
                              clientInfo, controller,
                              aIsMainScript, aWorkerScriptType, aRv);
 
   NS_ASSERTION(aLoadInfos.IsEmpty(), "Should have swapped!");
 
-  ScriptLoaderHolder workerHolder(loader);
-
-  if (NS_WARN_IF(!workerHolder.HoldWorker(aWorkerPrivate, Terminating))) {
+  RefPtr<StrongWorkerRef> workerRef =
+    StrongWorkerRef::Create(aWorkerPrivate, "ScriptLoader", [loader]() {
+      NS_DispatchToMainThread(NewRunnableMethod("ScriptLoader::CancelMainThreadWithBindingAborted",
+                                                loader,
+                                                &ScriptLoaderRunnable::CancelMainThreadWithBindingAborted));
+    });
+
+  if (NS_WARN_IF(!workerRef)) {
     aRv.Throw(NS_ERROR_FAILURE);
     return;
   }
 
   if (NS_FAILED(NS_DispatchToMainThread(loader))) {
     NS_ERROR("Failed to dispatch!");
     aRv.Throw(NS_ERROR_FAILURE);
     return;