Bug 1438945 - Part 11: selection of RemoteWorker actors. r=asuth,mrbkap
authorAndrea Marchesini <amarchesini@mozilla.com>
Mon, 19 Nov 2018 15:18:33 -0800
changeset 503573 2a1a9e2c2010d94954a3d346a30c9690551a7bcf
parent 503572 9482aa753a31fd3adc22704e4c3ad1466f110150
child 503574 6f7cba5101c17b5f2ffea4501bebc9ae0dc873b4
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, mrbkap
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 11: selection of RemoteWorker actors. r=asuth,mrbkap
dom/workers/remoteworkers/RemoteWorkerController.cpp
dom/workers/remoteworkers/RemoteWorkerController.h
dom/workers/remoteworkers/RemoteWorkerManager.cpp
dom/workers/remoteworkers/RemoteWorkerManager.h
dom/workers/sharedworkers/SharedWorkerManager.cpp
dom/workers/sharedworkers/SharedWorkerManager.h
dom/workers/sharedworkers/SharedWorkerService.cpp
--- a/dom/workers/remoteworkers/RemoteWorkerController.cpp
+++ b/dom/workers/remoteworkers/RemoteWorkerController.cpp
@@ -14,29 +14,30 @@
 namespace mozilla {
 
 using namespace ipc;
 
 namespace dom {
 
 /* static */ already_AddRefed<RemoteWorkerController>
 RemoteWorkerController::Create(const RemoteWorkerData& aData,
-                               RemoteWorkerObserver* aObserver)
+                               RemoteWorkerObserver* aObserver,
+                               base::ProcessId aProcessId)
 {
   AssertIsOnBackgroundThread();
   MOZ_ASSERT(XRE_IsParentProcess());
   MOZ_ASSERT(aObserver);
 
   RefPtr<RemoteWorkerController> controller =
     new RemoteWorkerController(aObserver);
 
   RefPtr<RemoteWorkerManager> manager = RemoteWorkerManager::GetOrCreate();
   MOZ_ASSERT(manager);
 
-  manager->Launch(controller, aData);
+  manager->Launch(controller, aData, aProcessId);
 
   return controller.forget();
 }
 
 RemoteWorkerController::RemoteWorkerController(RemoteWorkerObserver* aObserver)
   : mObserver(aObserver)
   , mState(ePending)
 {
--- a/dom/workers/remoteworkers/RemoteWorkerController.h
+++ b/dom/workers/remoteworkers/RemoteWorkerController.h
@@ -106,17 +106,18 @@ class RemoteWorkerController final
   friend class RemoteWorkerManager;
   friend class RemoteWorkerParent;
 
 public:
   NS_INLINE_DECL_REFCOUNTING(RemoteWorkerController)
 
   static already_AddRefed<RemoteWorkerController>
   Create(const RemoteWorkerData& aData,
-         RemoteWorkerObserver* aObserver);
+         RemoteWorkerObserver* aObserver,
+         base::ProcessId = 0);
 
   void
   AddWindowID(uint64_t aWindowID);
 
   void
   RemoveWindowID(uint64_t aWindowID);
 
   void
--- a/dom/workers/remoteworkers/RemoteWorkerManager.cpp
+++ b/dom/workers/remoteworkers/RemoteWorkerManager.cpp
@@ -86,22 +86,23 @@ RemoteWorkerManager::UnregisterActor(Rem
   MOZ_ASSERT(aActor);
   MOZ_ASSERT(mActors.Contains(aActor));
 
   mActors.RemoveElement(aActor);
 }
 
 void
 RemoteWorkerManager::Launch(RemoteWorkerController* aController,
-                            const RemoteWorkerData& aData)
+                            const RemoteWorkerData& aData,
+                            base::ProcessId aProcessId)
 {
   AssertIsOnBackgroundThread();
   MOZ_ASSERT(XRE_IsParentProcess());
 
-  RemoteWorkerServiceParent* targetActor = SelectTargetActor();
+  RemoteWorkerServiceParent* targetActor = SelectTargetActor(aData, aProcessId);
 
   // If there is not an available actor, let's store the data, and let's spawn a
   // new process.
   if (!targetActor) {
     // If this is the first time we have a pending launching, we must keep alive
     // the manager.
     if (mPendings.IsEmpty()) {
       AddRef();
@@ -151,24 +152,46 @@ RemoteWorkerManager::AsyncCreationFailed
                            [controller]() {
       controller->CreationFailed();
   });
 
   NS_DispatchToCurrentThread(r.forget());
 }
 
 RemoteWorkerServiceParent*
-RemoteWorkerManager::SelectTargetActor()
+RemoteWorkerManager::SelectTargetActor(const RemoteWorkerData& aData,
+                                       base::ProcessId aProcessId)
 {
   AssertIsOnBackgroundThread();
   MOZ_ASSERT(XRE_IsParentProcess());
 
-  // TODO
+  if (mActors.IsEmpty()) {
+    return nullptr;
+  }
 
-  return nullptr;
+  // System principal workers should run on the parent process.
+  if (aData.principalInfo().type() == PrincipalInfo::TSystemPrincipalInfo) {
+    for (RemoteWorkerServiceParent* actor : mActors) {
+      if (actor->OtherPid() == 0) {
+        return actor;
+      }
+    }
+  }
+
+  for (RemoteWorkerServiceParent* actor : mActors) {
+    // Let's execute the RemoteWorker on the same process but not on the parent
+    // process.
+    if (aProcessId && actor->OtherPid() == aProcessId) {
+      return actor;
+    }
+  }
+
+  // Let's choose an actor, randomly.
+  uint32_t id = uint32_t(rand()) % mActors.Length();
+  return mActors[id];
 }
 
 void
 RemoteWorkerManager::LaunchNewContentProcess()
 {
   AssertIsOnBackgroundThread();
   MOZ_ASSERT(XRE_IsParentProcess());
 
--- a/dom/workers/remoteworkers/RemoteWorkerManager.h
+++ b/dom/workers/remoteworkers/RemoteWorkerManager.h
@@ -2,16 +2,17 @@
 /* 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_RemoteWorkerManager_h
 #define mozilla_dom_RemoteWorkerManager_h
 
+#include "base/process.h"
 #include "mozilla/dom/RemoteWorkerTypes.h"
 #include "nsISupportsImpl.h"
 #include "nsTArray.h"
 
 namespace mozilla {
 namespace dom {
 
 class RemoteWorkerController;
@@ -29,24 +30,26 @@ public:
   void
   RegisterActor(RemoteWorkerServiceParent* aActor);
 
   void
   UnregisterActor(RemoteWorkerServiceParent* aActor);
 
   void
   Launch(RemoteWorkerController* aController,
-         const RemoteWorkerData& aData);
+         const RemoteWorkerData& aData,
+         base::ProcessId aProcessId);
 
 private:
   RemoteWorkerManager();
   ~RemoteWorkerManager();
 
   RemoteWorkerServiceParent*
-  SelectTargetActor();
+  SelectTargetActor(const RemoteWorkerData& aData,
+                    base::ProcessId aProcessId);
 
   void
   LaunchInternal(RemoteWorkerController* aController,
                  RemoteWorkerServiceParent* aTargetActor,
                  const RemoteWorkerData& aData);
 
   void
   LaunchNewContentProcess();
--- a/dom/workers/sharedworkers/SharedWorkerManager.cpp
+++ b/dom/workers/sharedworkers/SharedWorkerManager.cpp
@@ -41,22 +41,24 @@ SharedWorkerManager::~SharedWorkerManage
                   target, mLoadingPrincipal.forget());
   NS_ProxyRelease("SharedWorkerManager::mRemoteWorkerController",
                   mPBackgroundEventTarget, mRemoteWorkerController.forget());
 }
 
 bool
 SharedWorkerManager::MaybeCreateRemoteWorker(const RemoteWorkerData& aData,
                                              uint64_t aWindowID,
-                                             const MessagePortIdentifier& aPortIdentifier)
+                                             const MessagePortIdentifier& aPortIdentifier,
+                                             base::ProcessId aProcessId)
 {
   AssertIsOnBackgroundThread();
 
   if (!mRemoteWorkerController) {
-    mRemoteWorkerController = RemoteWorkerController::Create(aData, this);
+    mRemoteWorkerController =
+      RemoteWorkerController::Create(aData, this, aProcessId);
     if (NS_WARN_IF(!mRemoteWorkerController)) {
       return false;
     }
   }
 
   if (aWindowID) {
     mRemoteWorkerController->AddWindowID(aWindowID);
   }
--- a/dom/workers/sharedworkers/SharedWorkerManager.h
+++ b/dom/workers/sharedworkers/SharedWorkerManager.h
@@ -51,17 +51,18 @@ public:
   void
   Terminated() override;
 
   // Called on PBackground thread methods
 
   bool
   MaybeCreateRemoteWorker(const RemoteWorkerData& aData,
                           uint64_t aWindowID,
-                          const MessagePortIdentifier& aPortIdentifier);
+                          const MessagePortIdentifier& aPortIdentifier,
+                          base::ProcessId aProcessId);
 
   void
   AddActor(SharedWorkerParent* aParent);
 
   void
   RemoveActor(SharedWorkerParent* aParent);
 
   void
--- a/dom/workers/sharedworkers/SharedWorkerService.cpp
+++ b/dom/workers/sharedworkers/SharedWorkerService.cpp
@@ -151,17 +151,18 @@ public:
   {}
 
   NS_IMETHOD
   Run()
   {
     AssertIsOnBackgroundThread();
 
     if (NS_WARN_IF(!mManager->MaybeCreateRemoteWorker(mData, mWindowID,
-                                                      mPortIdentifier))) {
+                                                      mPortIdentifier,
+                                                      mActor->OtherPid()))) {
       mActor->ErrorPropagation(NS_ERROR_FAILURE);
       return NS_OK;
     }
 
     mManager->AddActor(mActor);
     mActor->ManagerCreated(mManager);
     return NS_OK;
   }