Bug 1492014 get WorkletLoadInfo from global instead of thread in Console r=baku
authorKarl Tomlinson <karlt+@karlt.net>
Tue, 18 Sep 2018 09:29:15 +0000
changeset 487188 d952654b9cc2364056b627d1fdc9414f627c7510
parent 487187 270ae583f0efefa2fb26465c342a343542ad734e
child 487189 66eb950c3f8298ffea27a91c6ac925c333a1b4b5
push id246
push userfmarier@mozilla.com
push dateSat, 13 Oct 2018 00:15:40 +0000
reviewersbaku
bugs1492014
milestone64.0a1
Bug 1492014 get WorkletLoadInfo from global instead of thread in Console r=baku Depends on D6106 Differential Revision: https://phabricator.services.mozilla.com/D6107
dom/console/Console.cpp
dom/console/Console.h
dom/worklet/WorkletGlobalScope.h
dom/worklet/WorkletImpl.cpp
dom/worklet/WorkletImpl.h
--- a/dom/console/Console.cpp
+++ b/dom/console/Console.cpp
@@ -536,33 +536,35 @@ protected:
 
 class ConsoleWorkletRunnable : public Runnable
                              , public ConsoleRunnable
 {
 protected:
   explicit ConsoleWorkletRunnable(Console* aConsole)
     : Runnable("dom::console::ConsoleWorkletRunnable")
     , mConsole(aConsole)
-    , mWorkletThread(WorkletThread::Get())
   {
     WorkletThread::AssertIsOnWorkletThread();
-    MOZ_ASSERT(mWorkletThread);
+    nsCOMPtr<WorkletGlobalScope> global = do_QueryInterface(mConsole->mGlobal);
+    MOZ_ASSERT(global);
+    mWorkletImpl = global->Impl();
+    MOZ_ASSERT(mWorkletImpl);
   }
 
   ~ConsoleWorkletRunnable() override = default;
 
   NS_IMETHOD
   Run() override
   {
     // This runnable is dispatched to main-thread first, then it goes back to
     // worklet thread.
     if (NS_IsMainThread()) {
       RunOnMainThread();
       RefPtr<ConsoleWorkletRunnable> runnable(this);
-      return mWorkletThread->DispatchRunnable(runnable.forget());
+      return mWorkletImpl->DispatchRunnable(runnable.forget());
     }
 
     WorkletThread::AssertIsOnWorkletThread();
 
     ReleaseData();
     mConsole = nullptr;
     return NS_OK;
   }
@@ -574,35 +576,29 @@ protected:
 
   // This method is called in the owning thread of the Console object.
   virtual void
   ReleaseData() = 0;
 
   // This must be released on the worker thread.
   RefPtr<Console> mConsole;
 
-  RefPtr<WorkletThread> mWorkletThread;
+  RefPtr<WorkletImpl> mWorkletImpl;
 };
 
 // This runnable appends a CallData object into the Console queue running on
 // the main-thread.
 class ConsoleCallDataWorkletRunnable final : public ConsoleWorkletRunnable
 {
 public:
   static already_AddRefed<ConsoleCallDataWorkletRunnable>
   Create(Console* aConsole, ConsoleCallData* aConsoleData)
   {
     WorkletThread::AssertIsOnWorkletThread();
 
-    RefPtr<WorkletThread> workletThread = WorkletThread::Get();
-    MOZ_ASSERT(workletThread);
-
-    aConsoleData->SetIDs(workletThread->GetWorkletLoadInfo().OuterWindowID(),
-                         workletThread->GetWorkletLoadInfo().InnerWindowID());
-
     RefPtr<ConsoleCallDataWorkletRunnable> runnable =
       new ConsoleCallDataWorkletRunnable(aConsole, aConsoleData);
 
     if (!runnable->StoreConsoleData(WorkletThread::Get()->GetJSContext(),
                                     aConsoleData)) {
       return nullptr;
     }
 
@@ -614,33 +610,35 @@ private:
                                  ConsoleCallData* aCallData)
     : ConsoleWorkletRunnable(aConsole)
     , mCallData(aCallData)
   {
     WorkletThread::AssertIsOnWorkletThread();
     MOZ_ASSERT(aCallData);
     aCallData->AssertIsOnOwningThread();
 
+    const WorkletLoadInfo& loadInfo = mWorkletImpl->LoadInfo();
+    mCallData->SetIDs(loadInfo.OuterWindowID(), loadInfo.InnerWindowID());
+
     // Marking this CallData as in use.
     mCallData->mStatus = ConsoleCallData::eInUse;
   }
 
   ~ConsoleCallDataWorkletRunnable() override
   {
     MOZ_ASSERT(!mCallData);
   }
 
   void
   RunOnMainThread() override
   {
     AutoSafeJSContext cx;
 
     JSObject* sandbox =
-      mConsole->GetOrCreateSandbox(cx,
-                                   mWorkletThread->GetWorkletLoadInfo().Principal());
+      mConsole->GetOrCreateSandbox(cx, mWorkletImpl->LoadInfo().Principal());
     JS::Rooted<JSObject*> global(cx, sandbox);
     if (NS_WARN_IF(!global)) {
       return;
     }
 
     // The CreateSandbox call returns a proxy to the actual sandbox object. We
     // don't need a proxy here.
     global = js::UncheckedUnwrap(global);
@@ -913,18 +911,17 @@ private:
   void
   RunOnMainThread() override
   {
     AssertIsOnMainThread();
 
     AutoSafeJSContext cx;
 
     JSObject* sandbox =
-      mConsole->GetOrCreateSandbox(cx,
-                                   mWorkletThread->GetWorkletLoadInfo().Principal());
+      mConsole->GetOrCreateSandbox(cx, mWorkletImpl->LoadInfo().Principal());
     JS::Rooted<JSObject*> global(cx, sandbox);
     if (NS_WARN_IF(!global)) {
       return;
     }
 
     // The CreateSandbox call returns a proxy to the actual sandbox object. We
     // don't need a proxy here.
     global = js::UncheckedUnwrap(global);
@@ -1576,17 +1573,19 @@ Console::MethodInternal(JSContext* aCx, 
           if (NS_SUCCEEDED(loadContext->GetUsePrivateBrowsing(&pb))) {
             MOZ_ASSERT(pb == !!oa.mPrivateBrowsingId);
           }
         }
       }
 #endif
     }
   } else if (WorkletThread::IsOnWorkletThread()) {
-    oa = WorkletThread::Get()->GetWorkletLoadInfo().OriginAttributesRef();
+    nsCOMPtr<WorkletGlobalScope> global = do_QueryInterface(mGlobal);
+    MOZ_ASSERT(global);
+    oa = global->Impl()->LoadInfo().OriginAttributesRef();
   } else {
     WorkerPrivate* workerPrivate = GetCurrentThreadWorkerPrivate();
     MOZ_ASSERT(workerPrivate);
     oa = workerPrivate->GetOriginAttributes();
   }
 
   callData->SetOriginAttributes(oa);
 
--- a/dom/console/Console.h
+++ b/dom/console/Console.h
@@ -492,14 +492,15 @@ private:
 
   friend class ConsoleCallData;
   friend class ConsoleCallDataWorkletRunnable;
   friend class ConsoleInstance;
   friend class ConsoleProfileWorkerRunnable;
   friend class ConsoleProfileWorkletRunnable;
   friend class ConsoleRunnable;
   friend class ConsoleWorkerRunnable;
+  friend class ConsoleWorkletRunnable;
 };
 
 } // namespace dom
 } // namespace mozilla
 
 #endif /* mozilla_dom_Console_h */
--- a/dom/worklet/WorkletGlobalScope.h
+++ b/dom/worklet/WorkletGlobalScope.h
@@ -51,16 +51,18 @@ public:
   GetGlobalJSObject() override
   {
     return GetWrapper();
   }
 
   already_AddRefed<Console>
   GetConsole(JSContext* aCx, ErrorResult& aRv);
 
+  WorkletImpl* Impl() const { return mImpl; }
+
   void
   Dump(const Optional<nsAString>& aString) const;
 
 protected:
   ~WorkletGlobalScope();;
 
 private:
   const RefPtr<WorkletImpl> mImpl;
--- a/dom/worklet/WorkletImpl.cpp
+++ b/dom/worklet/WorkletImpl.cpp
@@ -130,9 +130,17 @@ WorkletImpl::TerminateThread()
     return;
   }
 
   mWorkletThread->Terminate();
   mWorkletThread = nullptr;
   mWorkletLoadInfo.mPrincipal = nullptr;
 }
 
+nsresult
+WorkletImpl::DispatchRunnable(already_AddRefed<nsIRunnable> aRunnable)
+{
+  // TODO: bug 1492011 re ConsoleWorkletRunnable.
+  MOZ_ASSERT(mWorkletThread);
+  return mWorkletThread->DispatchRunnable(std::move(aRunnable));
+}
+
 } // namespace mozilla
--- a/dom/worklet/WorkletImpl.h
+++ b/dom/worklet/WorkletImpl.h
@@ -7,16 +7,17 @@
 #ifndef mozilla_dom_worklet_WorkletImpl_h
 #define mozilla_dom_worklet_WorkletImpl_h
 
 #include "MainThreadUtils.h"
 #include "mozilla/OriginAttributes.h"
 
 class nsPIDOMWindowInner;
 class nsIPrincipal;
+class nsIRunnable;
 
 namespace mozilla {
 
 namespace dom {
 
 class Worklet;
 class WorkletGlobalScope;
 class WorkletThread;
@@ -86,18 +87,22 @@ public:
 
   dom::WorkletThread* GetOrCreateThread();
   void TerminateThread();
 
   // Execution thread only.
   already_AddRefed<dom::WorkletGlobalScope> CreateGlobalScope(JSContext* aCx);
 
   // Any thread.
+
   const WorkletLoadInfo& LoadInfo() const { return mWorkletLoadInfo; }
 
+  // Use DispatchRunnable only when the thread is known to already exist.
+  nsresult DispatchRunnable(already_AddRefed<nsIRunnable> aRunnable);
+
 private:
   WorkletImpl(nsPIDOMWindowInner* aWindow, nsIPrincipal* aPrincipal,
               WorkletType aWorkletType);
   ~WorkletImpl();
 
   // The only WorkletLoadInfo member modified is mPrincipal which is accessed
   // on only the parent thread.
   WorkletLoadInfo mWorkletLoadInfo;