diff --git a/dom/webidl/FontFace.webidl b/dom/webidl/FontFace.webidl index 241f818daaa0..810638803206 100644 --- a/dom/webidl/FontFace.webidl +++ b/dom/webidl/FontFace.webidl @@ -31,8 +31,8 @@ enum FontFaceLoadStatus { "unloaded", "loading", "loaded", "error" }; // Bug 1072107 is for exposing this in workers. // [Exposed=(Window,Worker)] -[Pref="layout.css.font-loading-api.enabled", - Exposed=Window] +[Func="FontFaceSet::IsEnabled", + Exposed=(Window,Worker)] interface FontFace { [Throws] constructor(UTF8String family, diff --git a/dom/webidl/FontFaceSet.webidl b/dom/webidl/FontFaceSet.webidl index dde31bc74acd..5fa07ace84eb 100644 --- a/dom/webidl/FontFaceSet.webidl +++ b/dom/webidl/FontFaceSet.webidl @@ -19,7 +19,7 @@ dictionary FontFaceSetIteratorResult // To implement FontFaceSet's iterator until we can use setlike. [LegacyNoInterfaceObject, - Exposed=Window] + Exposed=(Window,Worker)] interface FontFaceSetIterator { [Throws] FontFaceSetIteratorResult next(); }; @@ -28,8 +28,8 @@ callback FontFaceSetForEachCallback = void (FontFace value, FontFace key, FontFa enum FontFaceSetLoadStatus { "loading", "loaded" }; -[Pref="layout.css.font-loading-api.enabled", - Exposed=Window] +[Func="FontFaceSet::IsEnabled", + Exposed=(Window,Worker)] interface FontFaceSet : EventTarget { // Bug 1072762 is for the FontFaceSet constructor. // constructor(sequence initialFaces); diff --git a/dom/webidl/FontFaceSetLoadEvent.webidl b/dom/webidl/FontFaceSetLoadEvent.webidl index 761cf17a8cb5..11d4b65646a7 100644 --- a/dom/webidl/FontFaceSetLoadEvent.webidl +++ b/dom/webidl/FontFaceSetLoadEvent.webidl @@ -14,8 +14,8 @@ dictionary FontFaceSetLoadEventInit : EventInit { sequence fontfaces = []; }; -[Pref="layout.css.font-loading-api.enabled", - Exposed=Window] +[Func="FontFaceSet::IsEnabled", + Exposed=(Window,Worker)] interface FontFaceSetLoadEvent : Event { constructor(DOMString type, optional FontFaceSetLoadEventInit eventInitDict = {}); diff --git a/dom/webidl/FontFaceSource.webidl b/dom/webidl/FontFaceSource.webidl index 0c5c29904341..ed189336316c 100644 --- a/dom/webidl/FontFaceSource.webidl +++ b/dom/webidl/FontFaceSource.webidl @@ -12,6 +12,6 @@ interface mixin FontFaceSource { - [Pref="layout.css.font-loading-api.enabled"] + [Func="FontFaceSet::IsEnabled"] readonly attribute FontFaceSet fonts; }; diff --git a/dom/webidl/WorkerGlobalScope.webidl b/dom/webidl/WorkerGlobalScope.webidl index 0b77a43031ca..05db5894488f 100644 --- a/dom/webidl/WorkerGlobalScope.webidl +++ b/dom/webidl/WorkerGlobalScope.webidl @@ -33,11 +33,9 @@ interface WorkerGlobalScope : EventTarget { }; WorkerGlobalScope includes GlobalCrypto; +WorkerGlobalScope includes FontFaceSource; WorkerGlobalScope includes WindowOrWorkerGlobalScope; -// Not implemented yet: bug 1072107. -// WorkerGlobalScope implements FontFaceSource; - // Mozilla extensions partial interface WorkerGlobalScope { diff --git a/dom/workers/WorkerScope.cpp b/dom/workers/WorkerScope.cpp index 1fb63f3054fd..0b221cb4a4df 100644 --- a/dom/workers/WorkerScope.cpp +++ b/dom/workers/WorkerScope.cpp @@ -57,6 +57,7 @@ #include "mozilla/dom/DedicatedWorkerGlobalScopeBinding.h" #include "mozilla/dom/DOMString.h" #include "mozilla/dom/Fetch.h" +#include "mozilla/dom/FontFaceSet.h" #include "mozilla/dom/IDBFactory.h" #include "mozilla/dom/ImageBitmap.h" #include "mozilla/dom/ImageBitmapSource.h" @@ -383,6 +384,7 @@ NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(WorkerGlobalScope, NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mWebTaskScheduler) NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mLocation) NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mNavigator) + NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mFontFaceSet) NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mIndexedDB) NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mCacheStorage) NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mDebuggerNotificationManager) @@ -398,6 +400,7 @@ NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(WorkerGlobalScope, } NS_IMPL_CYCLE_COLLECTION_UNLINK(mLocation) NS_IMPL_CYCLE_COLLECTION_UNLINK(mNavigator) + NS_IMPL_CYCLE_COLLECTION_UNLINK(mFontFaceSet) NS_IMPL_CYCLE_COLLECTION_UNLINK(mIndexedDB) NS_IMPL_CYCLE_COLLECTION_UNLINK(mCacheStorage) NS_IMPL_CYCLE_COLLECTION_UNLINK(mDebuggerNotificationManager) @@ -467,6 +470,17 @@ already_AddRefed WorkerGlobalScope::GetExistingNavigator() return navigator.forget(); } +FontFaceSet* WorkerGlobalScope::Fonts() { + AssertIsOnWorkerThread(); + + if (!mFontFaceSet) { + mFontFaceSet = FontFaceSet::CreateForWorker(this, mWorkerPrivate); + MOZ_ASSERT(mFontFaceSet); + } + + return mFontFaceSet; +} + OnErrorEventHandlerNonNull* WorkerGlobalScope::GetOnerror() { AssertIsOnWorkerThread(); diff --git a/dom/workers/WorkerScope.h b/dom/workers/WorkerScope.h index 1aa1d266306f..fe5dfdbad518 100644 --- a/dom/workers/WorkerScope.h +++ b/dom/workers/WorkerScope.h @@ -57,6 +57,7 @@ class DOMString; class DebuggerNotificationManager; enum class EventCallbackDebuggerNotificationType : uint8_t; class EventHandlerNonNull; +class FontFaceSet; class Function; class IDBFactory; class OnErrorEventHandlerNonNull; @@ -234,6 +235,8 @@ class WorkerGlobalScope : public WorkerGlobalScopeBase { already_AddRefed GetExistingNavigator() const; + FontFaceSet* Fonts() final; + void ImportScripts(JSContext* aCx, const Sequence& aScriptURLs, ErrorResult& aRv); @@ -347,6 +350,7 @@ class WorkerGlobalScope : public WorkerGlobalScopeBase { RefPtr mCrypto; RefPtr mLocation; RefPtr mNavigator; + RefPtr mFontFaceSet; RefPtr mPerformance; RefPtr mIndexedDB; RefPtr mCacheStorage; diff --git a/layout/style/FontFace.cpp b/layout/style/FontFace.cpp index 25f9dd93ee14..2c35e238478d 100644 --- a/layout/style/FontFace.cpp +++ b/layout/style/FontFace.cpp @@ -303,7 +303,7 @@ void FontFace::EnsurePromise() { // 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 (FontFaceSet::PrefEnabled()) { + if (FontFaceSet::IsEnabled()) { ErrorResult rv; mLoaded = Promise::Create(mParent, rv); diff --git a/layout/style/FontFaceSet.cpp b/layout/style/FontFaceSet.cpp index 98e32d976d35..ae8965c0d7f1 100644 --- a/layout/style/FontFaceSet.cpp +++ b/layout/style/FontFaceSet.cpp @@ -107,6 +107,13 @@ FontFaceSet::~FontFaceSet() { Destroy(); } +/* static */ bool FontFaceSet::IsEnabled() { + if (NS_IsMainThread()) { + return StaticPrefs::layout_css_font_loading_api_enabled(); + } + return StaticPrefs::layout_css_font_loading_api_workers_enabled(); +} + /* static */ already_AddRefed FontFaceSet::CreateForDocument( dom::Document* aDocument) { RefPtr set = new FontFaceSet(aDocument->GetScopeObject()); @@ -387,7 +394,7 @@ void FontFaceSet::DispatchLoadingEventAndReplaceReadyPromise() { (new AsyncEventDispatcher(this, u"loading"_ns, CanBubble::eNo)) ->PostDOMEvent(); - if (PrefEnabled()) { + if (IsEnabled()) { if (mReady && mReady->State() != Promise::PromiseState::Pending) { if (GetParentObject()) { ErrorResult rv; @@ -461,11 +468,6 @@ void FontFaceSet::DispatchLoadingFinishedEvent( (new AsyncEventDispatcher(this, event))->PostDOMEvent(); } -/* static */ -bool FontFaceSet::PrefEnabled() { - return StaticPrefs::layout_css_font_loading_api_enabled(); -} - void FontFaceSet::FlushUserFontSet() { mImpl->FlushUserFontSet(); } void FontFaceSet::RefreshStandardFontLoadPrincipal() { diff --git a/layout/style/FontFaceSet.h b/layout/style/FontFaceSet.h index 7730ab41c3db..305bbb87140f 100644 --- a/layout/style/FontFaceSet.h +++ b/layout/style/FontFaceSet.h @@ -37,6 +37,12 @@ class FontFaceSet final : public DOMEventTargetHelper { NS_DECL_ISUPPORTS_INHERITED NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(FontFaceSet, DOMEventTargetHelper) + static bool IsEnabled(); + + static bool IsEnabled(JSContext* aCx, JSObject* aObj) { + return IsEnabled(); + } + static already_AddRefed CreateForDocument( dom::Document* aDocument); @@ -55,11 +61,6 @@ class FontFaceSet final : public DOMEventTargetHelper { */ void DidRefresh(); - /** - * Returns whether the "layout.css.font-loading-api.enabled" pref is true. - */ - static bool PrefEnabled(); - void FlushUserFontSet(); void RefreshStandardFontLoadPrincipal(); diff --git a/layout/style/FontFaceSetImpl.cpp b/layout/style/FontFaceSetImpl.cpp index 2dfe615a5818..8d5568713bd4 100644 --- a/layout/style/FontFaceSetImpl.cpp +++ b/layout/style/FontFaceSetImpl.cpp @@ -906,11 +906,6 @@ void FontFaceSetImpl::OnLoadingFinished() { } } -/* static */ -bool FontFaceSetImpl::PrefEnabled() { - return StaticPrefs::layout_css_font_loading_api_enabled(); -} - void FontFaceSetImpl::RefreshStandardFontLoadPrincipal() { RecursiveMutexAutoLock lock(mMutex); mAllowedFontLoads.Clear(); diff --git a/layout/style/FontFaceSetImpl.h b/layout/style/FontFaceSetImpl.h index 265cd28f72a0..48612fd47a9c 100644 --- a/layout/style/FontFaceSetImpl.h +++ b/layout/style/FontFaceSetImpl.h @@ -121,11 +121,6 @@ class FontFaceSetImpl : public nsISupports, public gfxUserFontSet { */ virtual void DidRefresh() { MOZ_ASSERT_UNREACHABLE("Not implemented!"); } - /** - * Returns whether the "layout.css.font-loading-api.enabled" pref is true. - */ - static bool PrefEnabled(); - virtual void FlushUserFontSet() {} static nsPresContext* GetPresContextFor(gfxUserFontSet* aUserFontSet) { diff --git a/modules/libpref/init/StaticPrefList.yaml b/modules/libpref/init/StaticPrefList.yaml index dba1a12767d0..8c6c62facc14 100644 --- a/modules/libpref/init/StaticPrefList.yaml +++ b/modules/libpref/init/StaticPrefList.yaml @@ -7738,10 +7738,16 @@ # Is support for document.fonts enabled? - name: layout.css.font-loading-api.enabled - type: bool + type: RelaxedAtomicBool value: true mirror: always +# Is support for workerGlobalScope.fonts enabled? +- name: layout.css.font-loading-api.workers.enabled + type: RelaxedAtomicBool + value: false + mirror: always + # Is support for the @font-face metrics override descriptors enabled? - name: layout.css.font-metrics-overrides.enabled type: RelaxedAtomicBool diff --git a/testing/profiles/web-platform/user.js b/testing/profiles/web-platform/user.js index f61818055914..0f932cf261ce 100644 --- a/testing/profiles/web-platform/user.js +++ b/testing/profiles/web-platform/user.js @@ -75,3 +75,4 @@ user_pref("layout.css.prefers-color-scheme.content-override", 1); // Force OffscreenCanvas support user_pref("gfx.offscreencanvas.enabled", true); user_pref("dom.workers.requestAnimationFrame", true); +user_pref("layout.css.font-loading-api.workers.enabled", true); diff --git a/testing/web-platform/meta/css/css-font-loading/fontfaceset-load-css-wide-keywords.html.ini b/testing/web-platform/meta/css/css-font-loading/fontfaceset-load-css-wide-keywords.html.ini deleted file mode 100644 index ee6a71143826..000000000000 --- a/testing/web-platform/meta/css/css-font-loading/fontfaceset-load-css-wide-keywords.html.ini +++ /dev/null @@ -1,14 +0,0 @@ -[fontfaceset-load-css-wide-keywords.html] - expected: ERROR - [Loading CSS-wide keyword "revert" causes SyntaxError (worker)] - expected: NOTRUN - - [Loading CSS-wide keyword "unset" causes SyntaxError (worker)] - expected: NOTRUN - - [Loading CSS-wide keyword "initial" causes SyntaxError (worker)] - expected: TIMEOUT - - [Loading CSS-wide keyword "inherit" causes SyntaxError (worker)] - expected: NOTRUN - diff --git a/testing/web-platform/meta/css/css-font-loading/fontfaceset-load-var.html.ini b/testing/web-platform/meta/css/css-font-loading/fontfaceset-load-var.html.ini deleted file mode 100644 index 0fac0f0fcd33..000000000000 --- a/testing/web-platform/meta/css/css-font-loading/fontfaceset-load-var.html.ini +++ /dev/null @@ -1,8 +0,0 @@ -[fontfaceset-load-var.html] - expected: ERROR - [Loading "var(--x, 10px) serif" causes SyntaxError (worker)] - expected: NOTRUN - - [Loading "var(--x) serif" causes SyntaxError (worker)] - expected: TIMEOUT -