Bug 1438945 - Part 10: RemoteWorkerObserver. r=asuth
authorAndrea Marchesini <amarchesini@mozilla.com>
Mon, 19 Nov 2018 15:18:33 -0800
changeset 503572 9482aa753a31fd3adc22704e4c3ad1466f110150
parent 503571 b3d9a379c852669a1fa005148bee537b99c91c6f
child 503573 2a1a9e2c2010d94954a3d346a30c9690551a7bcf
push id10290
push userffxbld-merge
push dateMon, 03 Dec 2018 16:23:23 +0000
treeherdermozilla-beta@700bed2445e6 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersasuth
bugs1438945
milestone65.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 1438945 - Part 10: RemoteWorkerObserver. r=asuth
dom/workers/remoteworkers/RemoteWorkerController.cpp
dom/workers/remoteworkers/RemoteWorkerController.h
dom/workers/sharedworkers/SharedWorkerManager.cpp
dom/workers/sharedworkers/SharedWorkerManager.h
--- a/dom/workers/remoteworkers/RemoteWorkerController.cpp
+++ b/dom/workers/remoteworkers/RemoteWorkerController.cpp
@@ -13,33 +13,37 @@
 
 namespace mozilla {
 
 using namespace ipc;
 
 namespace dom {
 
 /* static */ already_AddRefed<RemoteWorkerController>
-RemoteWorkerController::Create(const RemoteWorkerData& aData)
+RemoteWorkerController::Create(const RemoteWorkerData& aData,
+                               RemoteWorkerObserver* aObserver)
 {
   AssertIsOnBackgroundThread();
   MOZ_ASSERT(XRE_IsParentProcess());
+  MOZ_ASSERT(aObserver);
 
-  RefPtr<RemoteWorkerController> controller = new RemoteWorkerController();
+  RefPtr<RemoteWorkerController> controller =
+    new RemoteWorkerController(aObserver);
 
   RefPtr<RemoteWorkerManager> manager = RemoteWorkerManager::GetOrCreate();
   MOZ_ASSERT(manager);
 
   manager->Launch(controller, aData);
 
   return controller.forget();
 }
 
-RemoteWorkerController::RemoteWorkerController()
-  : mState(ePending)
+RemoteWorkerController::RemoteWorkerController(RemoteWorkerObserver* aObserver)
+  : mObserver(aObserver)
+  , mState(ePending)
 {
   AssertIsOnBackgroundThread();
   MOZ_ASSERT(XRE_IsParentProcess());
 }
 
 RemoteWorkerController::~RemoteWorkerController()
 {
   AssertIsOnBackgroundThread();
@@ -59,19 +63,25 @@ RemoteWorkerController::SetWorkerActor(R
 
 void
 RemoteWorkerController::CreationFailed()
 {
   AssertIsOnBackgroundThread();
   MOZ_ASSERT(XRE_IsParentProcess());
   MOZ_ASSERT(mState == ePending || mState == eTerminated);
 
-  // TODO: maybe notification?
+  if (mState == eTerminated) {
+    MOZ_ASSERT(!mActor);
+    MOZ_ASSERT(mPendingOps.IsEmpty());
+    // Nothing to do.
+    return;
+  }
 
   Shutdown();
+  mObserver->CreationFailed();
 }
 
 void
 RemoteWorkerController::CreationSucceeded()
 {
   AssertIsOnBackgroundThread();
   MOZ_ASSERT(XRE_IsParentProcess());
   MOZ_ASSERT(mState == ePending || mState == eTerminated);
@@ -81,17 +91,17 @@ RemoteWorkerController::CreationSucceede
     MOZ_ASSERT(mPendingOps.IsEmpty());
     // Nothing to do.
     return;
   }
 
   MOZ_ASSERT(mActor);
   mState = eReady;
 
-  // TODO: maybe notification?
+  mObserver->CreationSucceeded();
 
   for (UniquePtr<Op>& op : mPendingOps) {
     switch (op->mType) {
       case Op::eTerminate:
         Terminate();
         break;
 
       case Op::eSuspend:
@@ -133,27 +143,27 @@ RemoteWorkerController::CreationSucceede
 }
 
 void
 RemoteWorkerController::ErrorPropagation(const ErrorValue& aValue)
 {
   AssertIsOnBackgroundThread();
   MOZ_ASSERT(XRE_IsParentProcess());
 
-  // TODO: error propagation
+  mObserver->ErrorReceived(aValue);
 }
 
 void
 RemoteWorkerController::WorkerTerminated()
 {
   AssertIsOnBackgroundThread();
   MOZ_ASSERT(XRE_IsParentProcess());
   MOZ_ASSERT(mState == eReady);
 
-  // TODO: worker terminated
+  mObserver->Terminated();
   Shutdown();
 }
 
 void
 RemoteWorkerController::Shutdown()
 {
   AssertIsOnBackgroundThread();
   MOZ_ASSERT(XRE_IsParentProcess());
--- a/dom/workers/remoteworkers/RemoteWorkerController.h
+++ b/dom/workers/remoteworkers/RemoteWorkerController.h
@@ -78,26 +78,45 @@ namespace dom {
  *    In case there were pending operations, they are now executed.
  */
 
 class ErrorValue;
 class MessagePortIdentifier;
 class RemoteWorkerManager;
 class RemoteWorkerParent;
 
+class RemoteWorkerObserver
+{
+public:
+  NS_INLINE_DECL_PURE_VIRTUAL_REFCOUNTING
+
+  virtual void
+  CreationFailed() = 0;
+
+  virtual void
+  CreationSucceeded() = 0;
+
+  virtual void
+  ErrorReceived(const ErrorValue& aValue) = 0;
+
+  virtual void
+  Terminated() = 0;
+};
+
 class RemoteWorkerController final
 {
   friend class RemoteWorkerManager;
   friend class RemoteWorkerParent;
 
 public:
   NS_INLINE_DECL_REFCOUNTING(RemoteWorkerController)
 
   static already_AddRefed<RemoteWorkerController>
-  Create(const RemoteWorkerData& aData);
+  Create(const RemoteWorkerData& aData,
+         RemoteWorkerObserver* aObserver);
 
   void
   AddWindowID(uint64_t aWindowID);
 
   void
   RemoveWindowID(uint64_t aWindowID);
 
   void
@@ -114,17 +133,17 @@ public:
 
   void
   Freeze();
 
   void
   Thaw();
 
 private:
-  RemoteWorkerController();
+  explicit RemoteWorkerController(RemoteWorkerObserver* aObserver);
   ~RemoteWorkerController();
 
   void
   SetWorkerActor(RemoteWorkerParent* aActor);
 
   void
   ErrorPropagation(const ErrorValue& aValue);
 
@@ -135,24 +154,25 @@ private:
   Shutdown();
 
   void
   CreationFailed();
 
   void
   CreationSucceeded();
 
+  RefPtr<RemoteWorkerObserver> mObserver;
+  RefPtr<RemoteWorkerParent> mActor;
+
   enum {
     ePending,
     eReady,
     eTerminated,
   } mState;
 
-  RefPtr<RemoteWorkerParent> mActor;
-
   struct Op {
     enum Type {
       eTerminate,
       eSuspend,
       eResume,
       eFreeze,
       eThaw,
       ePortIdentifier,
--- a/dom/workers/sharedworkers/SharedWorkerManager.cpp
+++ b/dom/workers/sharedworkers/SharedWorkerManager.cpp
@@ -46,17 +46,17 @@ SharedWorkerManager::~SharedWorkerManage
 bool
 SharedWorkerManager::MaybeCreateRemoteWorker(const RemoteWorkerData& aData,
                                              uint64_t aWindowID,
                                              const MessagePortIdentifier& aPortIdentifier)
 {
   AssertIsOnBackgroundThread();
 
   if (!mRemoteWorkerController) {
-    mRemoteWorkerController = RemoteWorkerController::Create(aData);
+    mRemoteWorkerController = RemoteWorkerController::Create(aData, this);
     if (NS_WARN_IF(!mRemoteWorkerController)) {
       return false;
     }
   }
 
   if (aWindowID) {
     mRemoteWorkerController->AddWindowID(aWindowID);
   }
@@ -183,117 +183,47 @@ SharedWorkerManager::UpdateFrozen()
 }
 
 bool
 SharedWorkerManager::IsSecureContext() const
 {
   return mIsSecureContext;
 }
 
-/* TODO
 void
-SharedWorkerManager::BroadcastErrorToActorsOnMainThread(const WorkerErrorReport* aReport,
-                                                        bool aIsErrorEvent)
+SharedWorkerManager::CreationFailed()
 {
-  MOZ_ASSERT(NS_IsMainThread());
-
-  ErrorValue value;
-  if (aIsErrorEvent) {
-    nsTArray<ErrorDataNote> notes;
-    for (size_t i = 0, len = aReport->mNotes.Length(); i < len; i++) {
-      const WorkerErrorNote& note = aReport->mNotes.ElementAt(i);
-      notes.AppendElement(ErrorDataNote(note.mLineNumber, note.mColumnNumber,
-                                        note.mMessage, note.mFilename));
-    }
+  AssertIsOnBackgroundThread();
 
-    ErrorData data(aReport->mLineNumber,
-                   aReport->mColumnNumber,
-                   aReport->mFlags,
-                   aReport->mMessage,
-                   aReport->mFilename,
-                   aReport->mLine,
-                   notes);
-    value = data;
-  } else {
-    value = void_t();
+  for (SharedWorkerParent* actor : mActors) {
+    Unused << actor->SendError(NS_ERROR_FAILURE);
   }
-
-  RefPtr<SharedWorkerManager> self = this;
-  nsCOMPtr<nsIRunnable> r =
-    NS_NewRunnableFunction("SharedWorkerManager::BroadcastErrorToActorsOnMainThread",
-                           [self, value]() {
-    self->BroadcastErrorToActors(value);
-  });
-
-  mPBackgroundEventTarget->Dispatch(r.forget(), NS_DISPATCH_NORMAL);
 }
 
 void
-SharedWorkerManager::BroadcastErrorToActors(const ErrorValue& aValue)
+SharedWorkerManager::CreationSucceeded()
+{
+  AssertIsOnBackgroundThread();
+  // Nothing to do here.
+}
+
+void
+SharedWorkerManager::ErrorReceived(const ErrorValue& aValue)
 {
   AssertIsOnBackgroundThread();
 
   for (SharedWorkerParent* actor : mActors) {
     Unused << actor->SendError(aValue);
   }
 }
 
 void
-SharedWorkerManager::CloseActorsOnMainThread()
-{
-  MOZ_ASSERT(NS_IsMainThread());
-  CloseOnMainThread();
-
-  RefPtr<SharedWorkerManager> self = this;
-  nsCOMPtr<nsIRunnable> r =
-    NS_NewRunnableFunction("SharedWorkerManager::CloseActorsOnMainThread",
-                           [self]() {
-    self->CloseActors();
-  });
-
-  mPBackgroundEventTarget->Dispatch(r.forget(), NS_DISPATCH_NORMAL);
-}
-
-void
-SharedWorkerManager::CloseActors()
+SharedWorkerManager::Terminated()
 {
   AssertIsOnBackgroundThread();
 
   for (SharedWorkerParent* actor : mActors) {
     Unused << actor->SendTerminate();
   }
 }
 
-void
-SharedWorkerManager::FlushReportsToActorsOnMainThread(nsIConsoleReportCollector* aReporter)
-{
-  MOZ_ASSERT(NS_IsMainThread());
-
-  AutoTArray<uint64_t, 10> windowIDs;
-  for (SharedWorkerParent* actor : mActors) {
-    uint64_t windowID = actor->WindowID();
-    if (windowID && !windowIDs.Contains(windowID)) {
-      windowIDs.AppendElement(windowID);
-    }
-  }
-
-  bool reportErrorToBrowserConsole = true;
-
-  // Flush the reports.
-  for (uint32_t index = 0; index < windowIDs.Length(); index++) {
-    aReporter->FlushReportsToConsole(windowIDs[index],
-      nsIConsoleReportCollector::ReportAction::Save);
-    reportErrorToBrowserConsole = false;
-  }
-
-  // Finally report to browser console if there is no any window or shared
-  // worker.
-  if (reportErrorToBrowserConsole) {
-    aReporter->FlushReportsToConsole(0);
-    return;
-  }
-
-  aReporter->ClearConsoleReports();
-}
-*/
-
 } // dom namespace
 } // mozilla namespace
--- a/dom/workers/sharedworkers/SharedWorkerManager.h
+++ b/dom/workers/sharedworkers/SharedWorkerManager.h
@@ -2,46 +2,60 @@
 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
 /* 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 mozilla_dom_SharedWorkerManager_h
 #define mozilla_dom_SharedWorkerManager_h
 
+#include "mozilla/dom/RemoteWorkerController.h"
 #include "nsISupportsImpl.h"
 #include "nsTArray.h"
 
 class nsIPrincipal;
 
 namespace mozilla {
 namespace dom {
 
 class MessagePortIdentifier;
 class RemoteWorkerData;
-class RemoteWorkerController;
 class SharedWorkerParent;
 
-class SharedWorkerManager final
+class SharedWorkerManager final : public RemoteWorkerObserver
 {
 public:
-  NS_INLINE_DECL_THREADSAFE_REFCOUNTING(SharedWorkerManager);
+  NS_INLINE_DECL_THREADSAFE_REFCOUNTING(SharedWorkerManager, override);
 
   // Called on main-thread thread methods
 
   SharedWorkerManager(nsIEventTarget* aPBackgroundEventTarget,
                       const RemoteWorkerData& aData,
                       nsIPrincipal* aLoadingPrincipal);
 
   bool
   MatchOnMainThread(const nsACString& aDomain,
                     const nsACString& aScriptURL,
                     const nsAString& aName,
                     nsIPrincipal* aLoadingPrincipal) const;
 
+  // RemoteWorkerObserver
+
+  void
+  CreationFailed() override;
+
+  void
+  CreationSucceeded() override;
+
+  void
+  ErrorReceived(const ErrorValue& aValue) override;
+
+  void
+  Terminated() override;
+
   // Called on PBackground thread methods
 
   bool
   MaybeCreateRemoteWorker(const RemoteWorkerData& aData,
                           uint64_t aWindowID,
                           const MessagePortIdentifier& aPortIdentifier);
 
   void