diff --git a/dom/base/nsGlobalWindowInner.cpp b/dom/base/nsGlobalWindowInner.cpp index b6fe6a09eb41..077bd123fdf5 100644 --- a/dom/base/nsGlobalWindowInner.cpp +++ b/dom/base/nsGlobalWindowInner.cpp @@ -1626,6 +1626,13 @@ OriginTrials nsGlobalWindowInner::Trials() const { return OriginTrials::FromWindow(this); } +FontFaceSet* nsGlobalWindowInner::Fonts() { + if (mDoc) { + return mDoc->Fonts(); + } + return nullptr; +} + uint32_t nsGlobalWindowInner::GetPrincipalHashValue() const { if (mDoc) { return mDoc->NodePrincipal()->GetHashValue(); diff --git a/dom/base/nsGlobalWindowInner.h b/dom/base/nsGlobalWindowInner.h index e8c3ad5ffc7c..b57ef24043c5 100644 --- a/dom/base/nsGlobalWindowInner.h +++ b/dom/base/nsGlobalWindowInner.h @@ -249,6 +249,7 @@ class nsGlobalWindowInner final : public mozilla::dom::EventTarget, bool ShouldResistFingerprinting() const final; uint32_t GetPrincipalHashValue() const final; mozilla::OriginTrials Trials() const final; + mozilla::dom::FontFaceSet* Fonts() final; JSObject* GetGlobalJSObject() final { return GetWrapper(); } JSObject* GetGlobalJSObjectPreserveColor() const final { diff --git a/dom/base/nsGlobalWindowOuter.cpp b/dom/base/nsGlobalWindowOuter.cpp index 674353189dca..2337f4ef0091 100644 --- a/dom/base/nsGlobalWindowOuter.cpp +++ b/dom/base/nsGlobalWindowOuter.cpp @@ -1683,6 +1683,13 @@ OriginTrials nsGlobalWindowOuter::Trials() const { : OriginTrials(); } +FontFaceSet* nsGlobalWindowOuter::Fonts() { + if (mDoc) { + return mDoc->Fonts(); + } + return nullptr; +} + nsresult nsGlobalWindowOuter::EnsureScriptEnvironment() { if (GetWrapperPreserveColor()) { return NS_OK; diff --git a/dom/base/nsGlobalWindowOuter.h b/dom/base/nsGlobalWindowOuter.h index 70cea10edfd5..be253c9efd07 100644 --- a/dom/base/nsGlobalWindowOuter.h +++ b/dom/base/nsGlobalWindowOuter.h @@ -235,6 +235,7 @@ class nsGlobalWindowOuter final : public mozilla::dom::EventTarget, bool ShouldResistFingerprinting() const final; uint32_t GetPrincipalHashValue() const final; mozilla::OriginTrials Trials() const final; + mozilla::dom::FontFaceSet* Fonts() final; // nsIGlobalJSObjectHolder JSObject* GetGlobalJSObject() final { return GetWrapper(); } diff --git a/dom/base/nsIGlobalObject.h b/dom/base/nsIGlobalObject.h index 9d10ad58d843..fc57a642ee5a 100644 --- a/dom/base/nsIGlobalObject.h +++ b/dom/base/nsIGlobalObject.h @@ -38,6 +38,7 @@ enum class StorageAccess; namespace dom { class VoidFunction; class DebuggerNotificationManager; +class FontFaceSet; class Function; class Report; class ReportBody; @@ -252,6 +253,8 @@ class nsIGlobalObject : public nsISupports, return nullptr; } + virtual mozilla::dom::FontFaceSet* Fonts() { return nullptr; } + protected: virtual ~nsIGlobalObject(); diff --git a/layout/style/FontFace.cpp b/layout/style/FontFace.cpp index 972739105707..5cb960868479 100644 --- a/layout/style/FontFace.cpp +++ b/layout/style/FontFace.cpp @@ -20,7 +20,6 @@ #include "mozilla/ServoStyleSet.h" #include "mozilla/ServoUtils.h" #include "mozilla/StaticPrefs_layout.h" -#include "mozilla/dom/Document.h" #include "nsStyleUtil.h" namespace mozilla { @@ -72,7 +71,7 @@ NS_INTERFACE_MAP_END NS_IMPL_CYCLE_COLLECTING_ADDREF(FontFace) NS_IMPL_CYCLE_COLLECTING_RELEASE(FontFace) -FontFace::FontFace(nsISupports* aParent) +FontFace::FontFace(nsIGlobalObject* aParent) : mParent(aParent), mLoadedRejection(NS_OK) {} FontFace::~FontFace() { @@ -90,7 +89,7 @@ JSObject* FontFace::WrapObject(JSContext* aCx, } already_AddRefed FontFace::CreateForRule( - nsISupports* aGlobal, FontFaceSet* aFontFaceSet, + nsIGlobalObject* aGlobal, FontFaceSet* aFontFaceSet, RawServoFontFaceRule* aRule) { FontFaceSetImpl* setImpl = aFontFaceSet->GetImpl(); MOZ_ASSERT(setImpl); @@ -104,22 +103,20 @@ already_AddRefed FontFace::Constructor( const GlobalObject& aGlobal, const nsACString& aFamily, const UTF8StringOrArrayBufferOrArrayBufferView& aSource, const FontFaceDescriptors& aDescriptors, ErrorResult& aRv) { - nsISupports* global = aGlobal.GetAsSupports(); - nsCOMPtr window = do_QueryInterface(global); - if (!window) { + nsCOMPtr global = do_QueryInterface(aGlobal.GetAsSupports()); + + FontFaceSet* set = global->Fonts(); + if (!set) { aRv.Throw(NS_ERROR_FAILURE); return nullptr; } - Document* doc = window->GetDoc(); - if (!doc) { + FontFaceSetImpl* setImpl = set->GetImpl(); + if (!setImpl) { aRv.Throw(NS_ERROR_FAILURE); return nullptr; } - FontFaceSetImpl* setImpl = doc->Fonts()->GetImpl(); - MOZ_ASSERT(setImpl); - RefPtr obj = new FontFace(global); obj->mImpl = new FontFaceImpl(obj, setImpl); if (!obj->mImpl->SetDescriptors(aFamily, aDescriptors)) { @@ -321,18 +318,16 @@ already_AddRefed FontFace::GetURLExtraData() const { void FontFace::EnsurePromise() { MOZ_ASSERT(NS_IsMainThread()); - if (mLoaded || !mImpl) { + if (mLoaded || !mImpl || !mParent) { return; } - nsCOMPtr global = do_QueryInterface(mParent); - // If the pref is not set, don't create the Promise (which the page wouldn't // be able to get to anyway) as it causes the window.FontFace constructor // to be created. - if (global && FontFaceSet::PrefEnabled()) { + if (FontFaceSet::PrefEnabled()) { ErrorResult rv; - mLoaded = Promise::Create(global, rv); + mLoaded = Promise::Create(mParent, rv); if (mImpl->Status() == FontFaceLoadStatus::Loaded) { mLoaded->MaybeResolve(this); diff --git a/layout/style/FontFace.h b/layout/style/FontFace.h index 855395c3641d..499e9c7d48e8 100644 --- a/layout/style/FontFace.h +++ b/layout/style/FontFace.h @@ -43,11 +43,11 @@ class FontFace final : public nsISupports, public nsWrapperCache { NS_DECL_CYCLE_COLLECTING_ISUPPORTS NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(FontFace) - nsISupports* GetParentObject() const { return mParent; } + nsIGlobalObject* GetParentObject() const { return mParent; } virtual JSObject* WrapObject(JSContext* aCx, JS::Handle aGivenProto) override; - static already_AddRefed CreateForRule(nsISupports* aGlobal, + static already_AddRefed CreateForRule(nsIGlobalObject* aGlobal, FontFaceSet* aFontFaceSet, RawServoFontFaceRule* aRule); @@ -97,7 +97,7 @@ class FontFace final : public nsISupports, public nsWrapperCache { already_AddRefed GetURLExtraData() const; private: - explicit FontFace(nsISupports* aParent); + explicit FontFace(nsIGlobalObject* aParent); ~FontFace(); /** @@ -109,7 +109,7 @@ class FontFace final : public nsISupports, public nsWrapperCache { // reject mLoaded based on mStatus and mLoadedRejection. void EnsurePromise(); - nsCOMPtr mParent; + nsCOMPtr mParent; RefPtr mImpl;