Bug 1591892 - Expose {window, self}.crossOriginIsolated r=bzbarsky
authorYaron Tausky <ytausky@mozilla.com>
Fri, 15 Nov 2019 12:25:57 +0000
changeset 502160 8a556d81b030513a068e8820f90e3940e675fbda
parent 502159 d215b3534e043455cad3bf14a90e5f80e9f8cdd0
child 502161 5ea9ed99c555bc7e07a8dfc9dfbbdb6eaa77c6e4
push id114172
push userdluca@mozilla.com
push dateTue, 19 Nov 2019 11:31:10 +0000
treeherdermozilla-inbound@b5c5ba07d3db [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbzbarsky
bugs1591892
milestone72.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 1591892 - Expose {window, self}.crossOriginIsolated r=bzbarsky Differential Revision: https://phabricator.services.mozilla.com/D52532
dom/base/nsGlobalWindowInner.cpp
dom/base/nsGlobalWindowInner.h
dom/base/nsGlobalWindowOuter.cpp
dom/webidl/WindowOrWorkerGlobalScope.webidl
dom/workers/Worker.cpp
dom/workers/WorkerPrivate.cpp
dom/workers/WorkerPrivate.h
dom/workers/WorkerScope.cpp
dom/workers/WorkerScope.h
--- a/dom/base/nsGlobalWindowInner.cpp
+++ b/dom/base/nsGlobalWindowInner.cpp
@@ -2354,24 +2354,30 @@ void nsGlobalWindowInner::NoteDOMContent
 void nsGlobalWindowInner::UpdateTopInnerWindow() {
   if (IsTopInnerWindow() || !mTopInnerWindow) {
     return;
   }
 
   mTopInnerWindow->UpdateWebSocketCount(-(int32_t)mNumOfOpenWebSockets);
 }
 
-bool nsGlobalWindowInner::IsCrossOriginIsolated() const {
+bool nsGlobalWindowInner::IsSharedMemoryAllowed() const {
   MOZ_ASSERT(NS_IsMainThread());
 
   if (StaticPrefs::
           dom_postMessage_sharedArrayBuffer_bypassCOOP_COEP_insecure_enabled()) {
     return true;
   }
 
+  return CrossOriginIsolated();
+}
+
+bool nsGlobalWindowInner::CrossOriginIsolated() const {
+  MOZ_ASSERT(NS_IsMainThread());
+
   if (!StaticPrefs::dom_postMessage_sharedArrayBuffer_withCOOP_COEP()) {
     return false;
   }
 
   RefPtr<BrowsingContext> bc = GetBrowsingContext();
   MOZ_DIAGNOSTIC_ASSERT(bc);
   if (bc->Top()->GetOpenerPolicy() !=
       nsILoadInfo::OPENER_POLICY_SAME_ORIGIN_EMBEDDER_POLICY_REQUIRE_CORP) {
--- a/dom/base/nsGlobalWindowInner.h
+++ b/dom/base/nsGlobalWindowInner.h
@@ -935,18 +935,20 @@ class nsGlobalWindowInner final : public
   bool HasOpenerForInitialContentBrowser();
 
   void UpdateTopInnerWindow();
 
   virtual bool IsInSyncOperation() override {
     return GetExtantDoc() && GetExtantDoc()->IsInSyncOperation();
   }
 
+  bool IsSharedMemoryAllowed() const;
+
   // https://whatpr.org/html/4734/structured-data.html#cross-origin-isolated
-  bool IsCrossOriginIsolated() const;
+  bool CrossOriginIsolated() const;
 
  protected:
   // Web IDL helpers
 
   // Redefine the property called aPropName on this window object to be a value
   // property with the value aValue, much like we would do for a [Replaceable]
   // property in IDL.
   void RedefineProperty(JSContext* aCx, const char* aPropName,
--- a/dom/base/nsGlobalWindowOuter.cpp
+++ b/dom/base/nsGlobalWindowOuter.cpp
@@ -6053,17 +6053,17 @@ void nsGlobalWindowOuter::PostMessageMoz
   // event creation and dispatch.
   RefPtr<PostMessageEvent> event = new PostMessageEvent(
       sourceBc, origin, this, providedPrincipal,
       callerInnerWindow ? callerInnerWindow->WindowID() : 0, callerDocumentURI,
       callerAgentClusterId);
 
   JS::CloneDataPolicy clonePolicy;
   if (GetDocGroup() && callerInnerWindow &&
-      callerInnerWindow->IsCrossOriginIsolated()) {
+      callerInnerWindow->IsSharedMemoryAllowed()) {
     clonePolicy.allowSharedMemory();
   }
   event->Write(aCx, aMessage, aTransfer, clonePolicy, aError);
   if (NS_WARN_IF(aError.Failed())) {
     return;
   }
 
   if (mDoc &&
--- a/dom/webidl/WindowOrWorkerGlobalScope.webidl
+++ b/dom/webidl/WindowOrWorkerGlobalScope.webidl
@@ -9,16 +9,17 @@
  * https://w3c.github.io/webappsec-secure-contexts/#monkey-patching-global-object
  * https://w3c.github.io/ServiceWorker/#self-caches
  */
 
 // https://html.spec.whatwg.org/multipage/webappapis.html#windoworworkerglobalscope-mixin
 [Exposed=(Window,Worker)]
 interface mixin WindowOrWorkerGlobalScope {
   [Replaceable] readonly attribute USVString origin;
+  readonly attribute boolean crossOriginIsolated;
 
   // base64 utility methods
   [Throws]
   DOMString btoa(DOMString btoa);
   [Throws]
   DOMString atob(DOMString atob);
 
   // timers
--- a/dom/workers/Worker.cpp
+++ b/dom/workers/Worker.cpp
@@ -99,22 +99,22 @@ void Worker::PostMessage(JSContext* aCx,
             ? ProfileTimelineWorkerOperationType::SerializeDataOnMainThread
             : ProfileTimelineWorkerOperationType::SerializeDataOffMainThread,
         MarkerTracingType::START);
   }
 
   JS::CloneDataPolicy clonePolicy;
   if (NS_IsMainThread()) {
     nsGlobalWindowInner* win = nsContentUtils::CallerInnerWindow();
-    if (win && win->IsCrossOriginIsolated()) {
+    if (win && win->IsSharedMemoryAllowed()) {
       clonePolicy.allowSharedMemory();
     }
   } else {
     WorkerPrivate* worker = GetCurrentThreadWorkerPrivate();
-    if (worker && worker->IsCrossOriginIsolated()) {
+    if (worker && worker->IsSharedMemoryAllowed()) {
       clonePolicy.allowSharedMemory();
     }
   }
 
   runnable->Write(aCx, aMessage, transferable, clonePolicy, aRv);
 
   if (isTimelineRecording) {
     end = MakeUnique<WorkerTimelineMarker>(
--- a/dom/workers/WorkerPrivate.cpp
+++ b/dom/workers/WorkerPrivate.cpp
@@ -3945,17 +3945,17 @@ void WorkerPrivate::PostMessageToParent(
     start = MakeUnique<WorkerTimelineMarker>(
         NS_IsMainThread()
             ? ProfileTimelineWorkerOperationType::SerializeDataOnMainThread
             : ProfileTimelineWorkerOperationType::SerializeDataOffMainThread,
         MarkerTracingType::START);
   }
 
   JS::CloneDataPolicy clonePolicy;
-  if (IsCrossOriginIsolated()) {
+  if (IsSharedMemoryAllowed()) {
     clonePolicy.allowSharedMemory();
   }
   runnable->Write(aCx, aMessage, transferable, clonePolicy, aRv);
 
   if (isTimelineRecording) {
     end = MakeUnique<WorkerTimelineMarker>(
         NS_IsMainThread()
             ? ProfileTimelineWorkerOperationType::SerializeDataOnMainThread
@@ -4969,24 +4969,30 @@ const nsAString& WorkerPrivate::Id() {
     mId = ComputeWorkerPrivateId();
   }
 
   MOZ_ASSERT(!mId.IsEmpty());
 
   return mId;
 }
 
-bool WorkerPrivate::IsCrossOriginIsolated() const {
+bool WorkerPrivate::IsSharedMemoryAllowed() const {
   AssertIsOnWorkerThread();
 
   if (StaticPrefs::
           dom_postMessage_sharedArrayBuffer_bypassCOOP_COEP_insecure_enabled()) {
     return true;
   }
 
+  return CrossOriginIsolated();
+}
+
+bool WorkerPrivate::CrossOriginIsolated() const {
+  AssertIsOnWorkerThread();
+
   if (!StaticPrefs::dom_postMessage_sharedArrayBuffer_withCOOP_COEP()) {
     return false;
   }
 
   return mAgentClusterOpenerPolicy ==
          nsILoadInfo::OPENER_POLICY_SAME_ORIGIN_EMBEDDER_POLICY_REQUIRE_CORP;
 }
 
--- a/dom/workers/WorkerPrivate.h
+++ b/dom/workers/WorkerPrivate.h
@@ -891,18 +891,20 @@ class WorkerPrivate : public RelativeTim
 #endif
 
   void StartCancelingTimer();
 
   const nsAString& Id();
 
   const nsID& AgentClusterId() const { return mAgentClusterId; }
 
+  bool IsSharedMemoryAllowed() const;
+
   // https://whatpr.org/html/4734/structured-data.html#cross-origin-isolated
-  bool IsCrossOriginIsolated() const;
+  bool CrossOriginIsolated() const;
 
  private:
   WorkerPrivate(
       WorkerPrivate* aParent, const nsAString& aScriptURL, bool aIsChromeWorker,
       WorkerType aWorkerType, const nsAString& aWorkerName,
       const nsACString& aServiceWorkerScope, WorkerLoadInfo& aLoadInfo,
       nsString&& aId, const nsID& aAgentClusterId,
       const nsILoadInfo::CrossOriginOpenerPolicy aAgentClusterOpenerPolicy);
--- a/dom/workers/WorkerScope.cpp
+++ b/dom/workers/WorkerScope.cpp
@@ -381,16 +381,20 @@ int32_t WorkerGlobalScope::SetTimeoutOrI
   return mWorkerPrivate->SetTimeout(aCx, handler, aTimeout, aIsInterval, aRv);
 }
 
 void WorkerGlobalScope::GetOrigin(nsAString& aOrigin) const {
   mWorkerPrivate->AssertIsOnWorkerThread();
   aOrigin = mWorkerPrivate->Origin();
 }
 
+bool WorkerGlobalScope::CrossOriginIsolated() const {
+  return mWorkerPrivate->CrossOriginIsolated();
+}
+
 void WorkerGlobalScope::Atob(const nsAString& aAtob, nsAString& aOutput,
                              ErrorResult& aRv) const {
   mWorkerPrivate->AssertIsOnWorkerThread();
   aRv = nsContentUtils::Atob(aAtob, aOutput);
 }
 
 void WorkerGlobalScope::Btoa(const nsAString& aBtoa, nsAString& aOutput,
                              ErrorResult& aRv) const {
--- a/dom/workers/WorkerScope.h
+++ b/dom/workers/WorkerScope.h
@@ -137,16 +137,17 @@ class WorkerGlobalScope : public DOMEven
   int32_t SetInterval(JSContext* aCx, const nsAString& aHandler,
                       const int32_t aTimeout,
                       const Sequence<JS::Value>& /* unused */,
                       ErrorResult& aRv);
   MOZ_CAN_RUN_SCRIPT
   void ClearInterval(int32_t aHandle);
 
   void GetOrigin(nsAString& aOrigin) const;
+  bool CrossOriginIsolated() const;
 
   void Atob(const nsAString& aAtob, nsAString& aOutput, ErrorResult& aRv) const;
   void Btoa(const nsAString& aBtoa, nsAString& aOutput, ErrorResult& aRv) const;
 
   IMPL_EVENT_HANDLER(online)
   IMPL_EVENT_HANDLER(offline)
   IMPL_EVENT_HANDLER(rejectionhandled)
   IMPL_EVENT_HANDLER(unhandledrejection)