Bug 1438945 - Part 7: SharedWorker can be intercepted by a ServiceWorker. r=asuth
authorAndrea Marchesini <amarchesini@mozilla.com>
Mon, 19 Nov 2018 15:18:32 -0800
changeset 503569 a38f567faf3faedcc609ced953aefa09d73e0fd6
parent 503568 d87a2d39f76f7e0ec05dcfb6387c2abda16ad545
child 503570 6059af9c85adc62a51d9b38b9cdcba3904d4d824
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 7: SharedWorker can be intercepted by a ServiceWorker. r=asuth
dom/workers/WorkerLoadInfo.h
dom/workers/sharedworkers/SharedWorkerManager.cpp
--- a/dom/workers/WorkerLoadInfo.h
+++ b/dom/workers/WorkerLoadInfo.h
@@ -60,16 +60,23 @@ struct WorkerLoadInfo
   {
     NS_DECL_ISUPPORTS
 
   public:
     InterfaceRequestor(nsIPrincipal* aPrincipal, nsILoadGroup* aLoadGroup);
     void MaybeAddTabChild(nsILoadGroup* aLoadGroup);
     NS_IMETHOD GetInterface(const nsIID& aIID, void** aSink) override;
 
+    void SetOuterRequestor(nsIInterfaceRequestor* aOuterRequestor)
+    {
+      MOZ_ASSERT(!mOuterRequestor);
+      MOZ_ASSERT(aOuterRequestor);
+      mOuterRequestor = aOuterRequestor;
+    }
+
   private:
     ~InterfaceRequestor() { }
 
     already_AddRefed<nsITabChild> GetAnyLiveTabChild();
 
     nsCOMPtr<nsILoadContext> mLoadContext;
     nsCOMPtr<nsIInterfaceRequestor> mOuterRequestor;
 
--- a/dom/workers/sharedworkers/SharedWorkerManager.cpp
+++ b/dom/workers/sharedworkers/SharedWorkerManager.cpp
@@ -4,22 +4,24 @@
  * 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/. */
 
 #include "SharedWorkerManager.h"
 #include "SharedWorkerParent.h"
 #include "SharedWorkerService.h"
 #include "mozilla/dom/IndexedDatabaseManager.h"
 #include "mozilla/dom/PSharedWorker.h"
+#include "mozilla/dom/ServiceWorkerInterceptController.h"
 #include "mozilla/dom/WorkerError.h"
 #include "mozilla/dom/WorkerPrivate.h"
 #include "mozilla/dom/WorkerRunnable.h"
 #include "mozilla/dom/workerinternals/ScriptLoader.h"
 #include "mozilla/ipc/BackgroundParent.h"
 #include "nsIConsoleReportCollector.h"
+#include "nsINetworkInterceptController.h"
 #include "nsIPrincipal.h"
 #include "nsNetUtil.h"
 #include "nsProxyRelease.h"
 
 namespace mozilla {
 namespace dom {
 
 using workerinternals::ChannelFromScriptURLMainThread;
@@ -49,16 +51,51 @@ private:
   nsresult
   Cancel() override
   {
     MessagePort::ForceClose(mPortIdentifier);
     return WorkerRunnable::Cancel();
   }
 };
 
+class SharedWorkerInterfaceRequestor final : public nsIInterfaceRequestor
+{
+public:
+  NS_DECL_ISUPPORTS
+
+  SharedWorkerInterfaceRequestor()
+    : mSWController(new ServiceWorkerInterceptController())
+  {}
+
+  NS_IMETHOD
+  GetInterface(const nsIID& aIID, void** aSink) override
+  {
+    MOZ_ASSERT(NS_IsMainThread());
+
+    if (aIID.Equals(NS_GET_IID(nsINetworkInterceptController))) {
+      // If asked for the network intercept controller, ask the outer requestor,
+      // which could be the docshell.
+      RefPtr<ServiceWorkerInterceptController> swController = mSWController;
+      swController.forget(aSink);
+      return NS_OK;
+    }
+
+    return NS_NOINTERFACE;
+  }
+
+private:
+  ~SharedWorkerInterfaceRequestor() = default;
+
+  RefPtr<ServiceWorkerInterceptController> mSWController;
+};
+
+NS_IMPL_ADDREF(SharedWorkerInterfaceRequestor)
+NS_IMPL_RELEASE(SharedWorkerInterfaceRequestor)
+NS_IMPL_QUERY_INTERFACE(SharedWorkerInterfaceRequestor, nsIInterfaceRequestor)
+
 } // anonymous
 
 SharedWorkerManager::SharedWorkerManager(nsIEventTarget* aPBackgroundEventTarget,
                                          const SharedWorkerLoadInfo& aInfo,
                                          nsIPrincipal* aPrincipal,
                                          nsIPrincipal* aLoadingPrincipal)
   : mPBackgroundEventTarget(aPBackgroundEventTarget)
   , mInfo(aInfo)
@@ -128,17 +165,25 @@ SharedWorkerManager::CreateWorkerOnMainT
   // based on the script CSP headers during load in ScriptLoader.
   info.mEvalAllowed = true;
   info.mReportCSPViolations = false;
   info.mSecureContext = mInfo.isSecureContext()
     ? WorkerLoadInfo::eSecureContext : WorkerLoadInfo::eInsecureContext;
 
   WorkerPrivate::OverrideLoadInfoLoadGroup(info, info.mLoadingPrincipal);
 
+  RefPtr<SharedWorkerInterfaceRequestor> requestor =
+    new SharedWorkerInterfaceRequestor();
+  info.mInterfaceRequestor->SetOuterRequestor(requestor);
+
   rv = info.SetPrincipalOnMainThread(info.mPrincipal, info.mLoadGroup);
+  if (NS_WARN_IF(NS_FAILED(rv))) {
+    return rv;
+  }
+
   Maybe<ClientInfo> clientInfo;
   if (mInfo.clientInfo().type() == OptionalIPCClientInfo::TIPCClientInfo) {
     clientInfo.emplace(ClientInfo(mInfo.clientInfo().get_IPCClientInfo()));
   }
 
   // Top level workers' main script use the document charset for the script
   // uri encoding.
   rv = ChannelFromScriptURLMainThread(info.mLoadingPrincipal,