From 90a2ab9e31bd4066927fc13b9abd0b38125c8273 Mon Sep 17 00:00:00 2001 From: Moonchild Date: Sun, 15 Aug 2021 11:45:53 +0000 Subject: Issue mcp-graveyard/UXP#1797 - Implement WindowOrWorkerGlobalScope.origin Resolves #1797 --- dom/base/nsGlobalWindow.cpp | 7 +++++++ dom/base/nsGlobalWindow.h | 1 + dom/webidl/WindowOrWorkerGlobalScope.webidl | 5 ++--- dom/workers/WorkerPrivate.cpp | 3 +++ dom/workers/WorkerPrivate.h | 6 ++++++ dom/workers/WorkerScope.cpp | 7 +++++++ dom/workers/WorkerScope.h | 3 +++ dom/workers/Workers.h | 1 + 8 files changed, 30 insertions(+), 3 deletions(-) diff --git a/dom/base/nsGlobalWindow.cpp b/dom/base/nsGlobalWindow.cpp index 293a522efe..fa2f4ad798 100644 --- a/dom/base/nsGlobalWindow.cpp +++ b/dom/base/nsGlobalWindow.cpp @@ -9892,6 +9892,13 @@ nsGlobalWindow::Btoa(const nsAString& aBinaryData, aError = nsContentUtils::Btoa(aBinaryData, aAsciiBase64String); } +void +nsGlobalWindow::GetOrigin(nsAString& aOrigin) +{ + MOZ_ASSERT(IsInnerWindow()); + nsContentUtils::GetUTFOrigin(GetPrincipal(), aOrigin); +} + //***************************************************************************** // nsGlobalWindow::nsIDOMEventTarget //***************************************************************************** diff --git a/dom/base/nsGlobalWindow.h b/dom/base/nsGlobalWindow.h index c9c661dd53..00ac023fce 100644 --- a/dom/base/nsGlobalWindow.h +++ b/dom/base/nsGlobalWindow.h @@ -958,6 +958,7 @@ public: mozilla::ErrorResult& aError); void Btoa(const nsAString& aBinaryData, nsAString& aAsciiBase64String, mozilla::ErrorResult& aError); + void GetOrigin(nsAString& aOrigin); mozilla::dom::DOMStorage* GetSessionStorage(mozilla::ErrorResult& aError); mozilla::dom::DOMStorage* GetLocalStorage(mozilla::ErrorResult& aError); diff --git a/dom/webidl/WindowOrWorkerGlobalScope.webidl b/dom/webidl/WindowOrWorkerGlobalScope.webidl index 8f0d0029ff..dddf2d17c4 100644 --- a/dom/webidl/WindowOrWorkerGlobalScope.webidl +++ b/dom/webidl/WindowOrWorkerGlobalScope.webidl @@ -13,9 +13,8 @@ // https://html.spec.whatwg.org/multipage/webappapis.html#windoworworkerglobalscope-mixin [NoInterfaceObject, Exposed=(Window,Worker)] interface WindowOrWorkerGlobalScope { - // XXXbz We don't implement 'origin' yet on either window or worker globals. - // See bug 1306170. - // [Replaceable] readonly attribute USVString origin; + // .origin on Window or Worker globals + [Replaceable] readonly attribute USVString origin; // base64 utility methods [Throws] diff --git a/dom/workers/WorkerPrivate.cpp b/dom/workers/WorkerPrivate.cpp index 5edf7dc4a5..1d9459cd07 100644 --- a/dom/workers/WorkerPrivate.cpp +++ b/dom/workers/WorkerPrivate.cpp @@ -1759,6 +1759,7 @@ WorkerLoadInfo::StealFrom(WorkerLoadInfo& aOther) mPrincipalInfo = aOther.mPrincipalInfo.forget(); mDomain = aOther.mDomain; + mOrigin = aOther.mOrigin; mServiceWorkerCacheName = aOther.mServiceWorkerCacheName; mWindowID = aOther.mWindowID; mServiceWorkerID = aOther.mServiceWorkerID; @@ -3623,6 +3624,8 @@ WorkerPrivateParent::SetPrincipal(nsIPrincipal* aPrincipal, mLoadInfo.mPrincipalInfo = new PrincipalInfo(); mLoadInfo.mOriginAttributes = nsContentUtils::GetOriginAttributes(aLoadGroup); + nsContentUtils::GetUTFOrigin(aPrincipal, mLoadInfo.mOrigin); + MOZ_ALWAYS_SUCCEEDS( PrincipalToPrincipalInfo(aPrincipal, mLoadInfo.mPrincipalInfo)); } diff --git a/dom/workers/WorkerPrivate.h b/dom/workers/WorkerPrivate.h index ee84448068..b638c1ef11 100644 --- a/dom/workers/WorkerPrivate.h +++ b/dom/workers/WorkerPrivate.h @@ -624,6 +624,12 @@ public: return mLoadInfo.mPrincipal; } + const nsAString& + Origin() const + { + return mLoadInfo.mOrigin; + } + nsILoadGroup* GetLoadGroup() const { diff --git a/dom/workers/WorkerScope.cpp b/dom/workers/WorkerScope.cpp index 081f0f1065..ec42364b52 100644 --- a/dom/workers/WorkerScope.cpp +++ b/dom/workers/WorkerScope.cpp @@ -356,6 +356,13 @@ WorkerGlobalScope::Btoa(const nsAString& aBtoa, nsAString& aOutput, ErrorResult& aRv = nsContentUtils::Btoa(aBtoa, aOutput); } +void +WorkerGlobalScope::GetOrigin(nsAString& aOrigin) const +{ + mWorkerPrivate->AssertIsOnWorkerThread(); + aOrigin = mWorkerPrivate->Origin(); +} + void WorkerGlobalScope::Dump(const Optional& aString) const { diff --git a/dom/workers/WorkerScope.h b/dom/workers/WorkerScope.h index 43b6d633ca..8d06152da2 100644 --- a/dom/workers/WorkerScope.h +++ b/dom/workers/WorkerScope.h @@ -145,6 +145,9 @@ public: void Btoa(const nsAString& aBtoa, nsAString& aOutput, ErrorResult& aRv) const; + void + GetOrigin(nsAString& aOrigin) const; + IMPL_EVENT_HANDLER(online) IMPL_EVENT_HANDLER(offline) diff --git a/dom/workers/Workers.h b/dom/workers/Workers.h index b7d75b09c8..6b0a0158d2 100644 --- a/dom/workers/Workers.h +++ b/dom/workers/Workers.h @@ -245,6 +245,7 @@ struct WorkerLoadInfo nsAutoPtr mPrincipalInfo; nsCString mDomain; + nsString mOrigin; // Derived from mPrincipal; can be used on worker threads. nsString mServiceWorkerCacheName; -- cgit v1.2.3