1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "FontFaceSetImpl.h"
9 #include "gfxFontConstants.h"
10 #include "gfxFontSrcPrincipal.h"
11 #include "gfxFontSrcURI.h"
12 #include "gfxFontUtils.h"
13 #include "gfxPlatformFontList.h"
14 #include "mozilla/css/Loader.h"
15 #include "mozilla/dom/CSSFontFaceRule.h"
16 #include "mozilla/dom/DocumentInlines.h"
17 #include "mozilla/dom/Event.h"
18 #include "mozilla/dom/FontFaceImpl.h"
19 #include "mozilla/dom/FontFaceSet.h"
20 #include "mozilla/dom/FontFaceSetBinding.h"
21 #include "mozilla/dom/FontFaceSetLoadEvent.h"
22 #include "mozilla/dom/FontFaceSetLoadEventBinding.h"
23 #include "mozilla/dom/Promise.h"
24 #include "mozilla/dom/WorkerCommon.h"
25 #include "mozilla/dom/WorkerRunnable.h"
26 #include "mozilla/FontPropertyTypes.h"
27 #include "mozilla/AsyncEventDispatcher.h"
28 #include "mozilla/BasePrincipal.h"
29 #include "mozilla/Logging.h"
30 #include "mozilla/Preferences.h"
31 #include "mozilla/PresShell.h"
32 #include "mozilla/PresShellInlines.h"
33 #include "mozilla/ServoBindings.h"
34 #include "mozilla/ServoCSSParser.h"
35 #include "mozilla/ServoStyleSet.h"
36 #include "mozilla/ServoUtils.h"
37 #include "mozilla/Sprintf.h"
38 #include "mozilla/StaticPrefs_layout.h"
39 #include "mozilla/Telemetry.h"
40 #include "mozilla/LoadInfo.h"
41 #include "nsComponentManagerUtils.h"
42 #include "nsContentUtils.h"
43 #include "nsDeviceContext.h"
44 #include "nsFontFaceLoader.h"
45 #include "nsIConsoleService.h"
46 #include "nsIContentPolicy.h"
47 #include "nsIDocShell.h"
48 #include "nsILoadContext.h"
49 #include "nsIPrincipal.h"
50 #include "nsIWebNavigation.h"
51 #include "nsNetUtil.h"
52 #include "nsIInputStream.h"
53 #include "nsLayoutUtils.h"
54 #include "nsPresContext.h"
55 #include "nsPrintfCString.h"
56 #include "nsUTF8Utils.h"
57 #include "nsDOMNavigationTiming.h"
58 #include "ReferrerInfo.h"
60 using namespace mozilla
;
61 using namespace mozilla::css
;
62 using namespace mozilla::dom
;
65 MOZ_LOG(gfxUserFontSet::GetUserFontsLog(), mozilla::LogLevel::Debug, args)
66 #define LOG_ENABLED() \
67 MOZ_LOG_TEST(gfxUserFontSet::GetUserFontsLog(), LogLevel::Debug)
69 NS_IMPL_ISUPPORTS0(FontFaceSetImpl
)
71 FontFaceSetImpl::FontFaceSetImpl(FontFaceSet
* aOwner
)
72 : mMutex("mozilla::dom::FontFaceSetImpl"),
74 mStatus(FontFaceSetLoadStatus::Loaded
),
75 mNonRuleFacesDirty(false),
76 mHasLoadingFontFaces(false),
77 mHasLoadingFontFacesIsDirty(false),
78 mDelayedLoadCheck(false),
80 mPrivateBrowsing(false) {}
82 FontFaceSetImpl::~FontFaceSetImpl() {
83 // Assert that we don't drop any FontFaceSet objects during a Servo traversal,
84 // since PostTraversalTask objects can hold raw pointers to FontFaceSets.
85 MOZ_ASSERT(!gfxFontUtils::IsInServoTraversal());
90 void FontFaceSetImpl::DestroyLoaders() {
91 mMutex
.AssertCurrentThreadIn();
92 if (mLoaders
.IsEmpty()) {
95 if (NS_IsMainThread()) {
96 for (const auto& key
: mLoaders
.Keys()) {
103 class DestroyLoadersRunnable final
: public Runnable
{
105 explicit DestroyLoadersRunnable(FontFaceSetImpl
* aFontFaceSet
)
106 : Runnable("FontFaceSetImpl::DestroyLoaders"),
107 mFontFaceSet(aFontFaceSet
) {}
110 ~DestroyLoadersRunnable() override
= default;
112 NS_IMETHOD
Run() override
{
113 RecursiveMutexAutoLock
lock(mFontFaceSet
->mMutex
);
114 mFontFaceSet
->DestroyLoaders();
118 // We need to save a reference to the FontFaceSetImpl because the
119 // loaders contain a non-owning reference to it.
120 RefPtr
<FontFaceSetImpl
> mFontFaceSet
;
123 auto runnable
= MakeRefPtr
<DestroyLoadersRunnable
>(this);
124 NS_DispatchToMainThread(runnable
);
127 void FontFaceSetImpl::Destroy() {
128 nsTArray
<FontFaceRecord
> nonRuleFaces
;
129 nsRefPtrHashtable
<nsCStringHashKey
, gfxUserFontFamily
> fontFamilies
;
132 RecursiveMutexAutoLock
lock(mMutex
);
134 nonRuleFaces
= std::move(mNonRuleFaces
);
135 fontFamilies
= std::move(mFontFamilies
);
139 if (gfxPlatformFontList
* fp
= gfxPlatformFontList::PlatformFontList()) {
140 fp
->RemoveUserFontSet(this);
144 void FontFaceSetImpl::ParseFontShorthandForMatching(
145 const nsACString
& aFont
, StyleFontFamilyList
& aFamilyList
,
146 FontWeight
& aWeight
, FontStretch
& aStretch
, FontSlantStyle
& aStyle
,
148 RefPtr
<URLExtraData
> url
= GetURLExtraData();
150 aRv
.ThrowInvalidStateError("Missing URLExtraData");
154 if (!ServoCSSParser::ParseFontShorthandForMatching(
155 aFont
, url
, aFamilyList
, aStyle
, aStretch
, aWeight
)) {
156 aRv
.ThrowSyntaxError("Invalid font shorthand");
161 static bool HasAnyCharacterInUnicodeRange(gfxUserFontEntry
* aEntry
,
162 const nsAString
& aInput
) {
163 const char16_t
* p
= aInput
.Data();
164 const char16_t
* end
= p
+ aInput
.Length();
167 uint32_t c
= UTF16CharEnumerator::NextChar(&p
, end
);
168 if (aEntry
->CharacterInUnicodeRange(c
)) {
175 void FontFaceSetImpl::FindMatchingFontFaces(const nsACString
& aFont
,
176 const nsAString
& aText
,
177 nsTArray
<FontFace
*>& aFontFaces
,
179 RecursiveMutexAutoLock
lock(mMutex
);
181 StyleFontFamilyList familyList
;
184 FontSlantStyle italicStyle
;
185 ParseFontShorthandForMatching(aFont
, familyList
, weight
, stretch
, italicStyle
,
192 style
.style
= italicStyle
;
193 style
.weight
= weight
;
194 style
.stretch
= stretch
;
196 // Set of FontFaces that we want to return.
197 nsTHashSet
<FontFace
*> matchingFaces
;
199 for (const StyleSingleFontFamily
& fontFamilyName
: familyList
.list
.AsSpan()) {
200 if (!fontFamilyName
.IsFamilyName()) {
204 const auto& name
= fontFamilyName
.AsFamilyName();
205 RefPtr
<gfxFontFamily
> family
=
206 LookupFamily(nsAtomCString(name
.name
.AsAtom()));
212 AutoTArray
<gfxFontEntry
*, 4> entries
;
213 family
->FindAllFontsForStyle(style
, entries
);
215 for (gfxFontEntry
* e
: entries
) {
216 FontFaceImpl::Entry
* entry
= static_cast<FontFaceImpl::Entry
*>(e
);
217 if (HasAnyCharacterInUnicodeRange(entry
, aText
)) {
218 entry
->FindFontFaceOwners(matchingFaces
);
223 if (matchingFaces
.IsEmpty()) {
227 // Add all FontFaces in matchingFaces to aFontFaces, in the order
228 // they appear in the FontFaceSet.
229 FindMatchingFontFaces(matchingFaces
, aFontFaces
);
232 void FontFaceSetImpl::FindMatchingFontFaces(
233 const nsTHashSet
<FontFace
*>& aMatchingFaces
,
234 nsTArray
<FontFace
*>& aFontFaces
) {
235 RecursiveMutexAutoLock
lock(mMutex
);
236 for (FontFaceRecord
& record
: mNonRuleFaces
) {
237 FontFace
* owner
= record
.mFontFace
->GetOwner();
238 if (owner
&& aMatchingFaces
.Contains(owner
)) {
239 aFontFaces
.AppendElement(owner
);
244 bool FontFaceSetImpl::ReadyPromiseIsPending() const {
245 RecursiveMutexAutoLock
lock(mMutex
);
246 return mOwner
&& mOwner
->ReadyPromiseIsPending();
249 FontFaceSetLoadStatus
FontFaceSetImpl::Status() {
250 RecursiveMutexAutoLock
lock(mMutex
);
255 bool FontFaceSetImpl::Add(FontFaceImpl
* aFontFace
, ErrorResult
& aRv
) {
256 RecursiveMutexAutoLock
lock(mMutex
);
259 if (aFontFace
->IsInFontFaceSet(this)) {
263 if (aFontFace
->HasRule()) {
264 aRv
.ThrowInvalidModificationError(
265 "Can't add face to FontFaceSet that comes from an @font-face rule");
269 aFontFace
->AddFontFaceSet(this);
272 for (const FontFaceRecord
& rec
: mNonRuleFaces
) {
273 MOZ_ASSERT(rec
.mFontFace
!= aFontFace
,
274 "FontFace should not occur in mNonRuleFaces twice");
278 FontFaceRecord
* rec
= mNonRuleFaces
.AppendElement();
279 rec
->mFontFace
= aFontFace
;
280 rec
->mOrigin
= Nothing();
282 mNonRuleFacesDirty
= true;
283 MarkUserFontSetDirty();
284 mHasLoadingFontFacesIsDirty
= true;
285 CheckLoadingStarted();
289 void FontFaceSetImpl::Clear() {
290 RecursiveMutexAutoLock
lock(mMutex
);
293 if (mNonRuleFaces
.IsEmpty()) {
297 for (size_t i
= 0; i
< mNonRuleFaces
.Length(); i
++) {
298 FontFaceImpl
* f
= mNonRuleFaces
[i
].mFontFace
;
299 f
->RemoveFontFaceSet(this);
302 mNonRuleFaces
.Clear();
303 mNonRuleFacesDirty
= true;
304 MarkUserFontSetDirty();
305 mHasLoadingFontFacesIsDirty
= true;
306 CheckLoadingFinished();
309 bool FontFaceSetImpl::Delete(FontFaceImpl
* aFontFace
) {
310 RecursiveMutexAutoLock
lock(mMutex
);
313 if (aFontFace
->HasRule()) {
317 bool removed
= false;
318 for (size_t i
= 0; i
< mNonRuleFaces
.Length(); i
++) {
319 if (mNonRuleFaces
[i
].mFontFace
== aFontFace
) {
320 mNonRuleFaces
.RemoveElementAt(i
);
329 aFontFace
->RemoveFontFaceSet(this);
331 mNonRuleFacesDirty
= true;
332 MarkUserFontSetDirty();
333 mHasLoadingFontFacesIsDirty
= true;
334 CheckLoadingFinished();
338 bool FontFaceSetImpl::HasAvailableFontFace(FontFaceImpl
* aFontFace
) {
339 return aFontFace
->IsInFontFaceSet(this);
342 void FontFaceSetImpl::RemoveLoader(nsFontFaceLoader
* aLoader
) {
343 RecursiveMutexAutoLock
lock(mMutex
);
344 mLoaders
.RemoveEntry(aLoader
);
347 void FontFaceSetImpl::InsertNonRuleFontFace(FontFaceImpl
* aFontFace
) {
348 gfxUserFontAttributes attr
;
349 if (!aFontFace
->GetAttributes(attr
)) {
350 // If there is no family name, this rule cannot contribute a
351 // usable font, so there is no point in processing it further.
355 nsAutoCString
family(attr
.mFamilyName
);
357 // Just create a new font entry if we haven't got one already.
358 if (!aFontFace
->GetUserFontEntry()) {
359 // XXX Should we be checking mLocalRulesUsed like InsertRuleFontFace does?
360 RefPtr
<gfxUserFontEntry
> entry
= FindOrCreateUserFontEntryFromFontFace(
361 aFontFace
, std::move(attr
), StyleOrigin::Author
);
365 aFontFace
->SetUserFontEntry(entry
);
367 AddUserFontEntry(family
, aFontFace
->GetUserFontEntry());
370 void FontFaceSetImpl::UpdateUserFontEntry(gfxUserFontEntry
* aEntry
,
371 gfxUserFontAttributes
&& aAttr
) {
372 MOZ_ASSERT(NS_IsMainThread());
374 bool resetFamilyName
= !aEntry
->mFamilyName
.IsEmpty() &&
375 aEntry
->mFamilyName
!= aAttr
.mFamilyName
;
376 // aFontFace already has a user font entry, so we update its attributes
377 // rather than creating a new one.
378 aEntry
->UpdateAttributes(std::move(aAttr
));
379 // If the family name has changed, remove the entry from its current family
380 // and clear the mFamilyName field so it can be reset when added to a new
382 if (resetFamilyName
) {
383 RefPtr
<gfxUserFontFamily
> family
= LookupFamily(aEntry
->mFamilyName
);
385 family
->RemoveFontEntry(aEntry
);
387 aEntry
->mFamilyName
.Truncate(0);
391 class FontFaceSetImpl::UpdateUserFontEntryRunnable final
392 : public WorkerMainThreadRunnable
{
394 UpdateUserFontEntryRunnable(FontFaceSetImpl
* aSet
, gfxUserFontEntry
* aEntry
,
395 gfxUserFontAttributes
& aAttr
)
396 : WorkerMainThreadRunnable(
397 GetCurrentThreadWorkerPrivate(),
398 "FontFaceSetImpl :: FindOrCreateUserFontEntryFromFontFace"_ns
),
403 bool MainThreadRun() override
{
404 mSet
->UpdateUserFontEntry(mEntry
, std::move(mAttr
));
409 FontFaceSetImpl
* mSet
;
410 gfxUserFontEntry
* mEntry
;
411 gfxUserFontAttributes
& mAttr
;
414 // TODO(emilio): Should this take an nsAtom* aFamilyName instead?
416 // All callers have one handy.
418 already_AddRefed
<gfxUserFontEntry
>
419 FontFaceSetImpl::FindOrCreateUserFontEntryFromFontFace(
420 FontFaceImpl
* aFontFace
, gfxUserFontAttributes
&& aAttr
,
421 StyleOrigin aOrigin
) {
422 FontFaceSetImpl
* set
= aFontFace
->GetPrimaryFontFaceSet();
424 RefPtr
<gfxUserFontEntry
> existingEntry
= aFontFace
->GetUserFontEntry();
426 if (NS_IsMainThread()) {
427 set
->UpdateUserFontEntry(existingEntry
, std::move(aAttr
));
430 MakeRefPtr
<UpdateUserFontEntryRunnable
>(set
, existingEntry
, aAttr
);
431 IgnoredErrorResult ignoredRv
;
432 task
->Dispatch(Canceling
, ignoredRv
);
434 return existingEntry
.forget();
438 nsTArray
<gfxFontFaceSrc
> srcArray
;
440 if (aFontFace
->HasFontData()) {
441 gfxFontFaceSrc
* face
= srcArray
.AppendElement();
446 face
->mSourceType
= gfxFontFaceSrc::eSourceType_Buffer
;
447 face
->mBuffer
= aFontFace
->TakeBufferSource();
449 size_t len
= aAttr
.mSources
.Length();
450 for (size_t i
= 0; i
< len
; ++i
) {
451 gfxFontFaceSrc
* face
= srcArray
.AppendElement();
452 const auto& component
= aAttr
.mSources
[i
];
453 switch (component
.tag
) {
454 case StyleFontFaceSourceListComponent::Tag::Local
: {
455 nsAtom
* atom
= component
.AsLocal();
456 face
->mLocalName
.Append(nsAtomCString(atom
));
457 face
->mSourceType
= gfxFontFaceSrc::eSourceType_Local
;
458 face
->mURI
= nullptr;
459 face
->mFormatHint
= StyleFontFaceSourceFormatKeyword::None
;
463 case StyleFontFaceSourceListComponent::Tag::Url
: {
464 face
->mSourceType
= gfxFontFaceSrc::eSourceType_URL
;
465 const StyleCssUrl
* url
= component
.AsUrl();
466 nsIURI
* uri
= url
->GetURI();
467 face
->mURI
= uri
? new gfxFontSrcURI(uri
) : nullptr;
468 const URLExtraData
& extraData
= url
->ExtraData();
469 face
->mReferrerInfo
= extraData
.ReferrerInfo();
471 // agent and user stylesheets are treated slightly differently,
472 // the same-site origin check and access control headers are
473 // enforced against the sheet principal rather than the document
474 // principal to allow user stylesheets to include @font-face rules
475 if (aOrigin
== StyleOrigin::User
||
476 aOrigin
== StyleOrigin::UserAgent
) {
477 face
->mUseOriginPrincipal
= true;
478 face
->mOriginPrincipal
= new gfxFontSrcPrincipal(
479 extraData
.Principal(), extraData
.Principal());
482 face
->mLocalName
.Truncate();
483 face
->mFormatHint
= StyleFontFaceSourceFormatKeyword::None
;
484 face
->mTechFlags
= StyleFontFaceSourceTechFlags::Empty();
487 // Check for a format hint.
488 const auto& next
= aAttr
.mSources
[i
+ 1];
490 case StyleFontFaceSourceListComponent::Tag::FormatHintKeyword
:
491 face
->mFormatHint
= next
.format_hint_keyword
._0
;
494 case StyleFontFaceSourceListComponent::Tag::FormatHintString
: {
495 nsDependentCSubstring
valueString(
496 reinterpret_cast<const char*>(
497 next
.format_hint_string
.utf8_bytes
),
498 next
.format_hint_string
.length
);
500 if (valueString
.LowerCaseEqualsASCII("woff")) {
501 face
->mFormatHint
= StyleFontFaceSourceFormatKeyword::Woff
;
502 } else if (valueString
.LowerCaseEqualsASCII("woff2")) {
503 face
->mFormatHint
= StyleFontFaceSourceFormatKeyword::Woff2
;
504 } else if (valueString
.LowerCaseEqualsASCII("opentype")) {
506 StyleFontFaceSourceFormatKeyword::Opentype
;
507 } else if (valueString
.LowerCaseEqualsASCII("truetype")) {
509 StyleFontFaceSourceFormatKeyword::Truetype
;
510 } else if (valueString
.LowerCaseEqualsASCII("truetype-aat")) {
512 StyleFontFaceSourceFormatKeyword::Truetype
;
513 } else if (valueString
.LowerCaseEqualsASCII(
514 "embedded-opentype")) {
516 StyleFontFaceSourceFormatKeyword::EmbeddedOpentype
;
517 } else if (valueString
.LowerCaseEqualsASCII("svg")) {
518 face
->mFormatHint
= StyleFontFaceSourceFormatKeyword::Svg
;
519 } else if (StaticPrefs::layout_css_font_variations_enabled()) {
520 // Non-standard values that Firefox accepted, for back-compat;
521 // these are superseded by the tech() function.
522 if (valueString
.LowerCaseEqualsASCII("woff-variations")) {
523 face
->mFormatHint
= StyleFontFaceSourceFormatKeyword::Woff
;
524 } else if (valueString
.LowerCaseEqualsASCII(
525 "woff2-variations")) {
526 face
->mFormatHint
= StyleFontFaceSourceFormatKeyword::Woff2
;
527 } else if (valueString
.LowerCaseEqualsASCII(
528 "opentype-variations")) {
530 StyleFontFaceSourceFormatKeyword::Opentype
;
531 } else if (valueString
.LowerCaseEqualsASCII(
532 "truetype-variations")) {
534 StyleFontFaceSourceFormatKeyword::Truetype
;
537 StyleFontFaceSourceFormatKeyword::Unknown
;
540 // unknown format specified, mark to distinguish from the
541 // case where no format hints are specified
542 face
->mFormatHint
= StyleFontFaceSourceFormatKeyword::Unknown
;
547 case StyleFontFaceSourceListComponent::Tag::TechFlags
:
548 case StyleFontFaceSourceListComponent::Tag::Local
:
549 case StyleFontFaceSourceListComponent::Tag::Url
:
555 // Check for a set of font-technologies flags.
556 const auto& next
= aAttr
.mSources
[i
+ 1];
557 if (next
.IsTechFlags()) {
558 face
->mTechFlags
= next
.AsTechFlags();
564 // if URI not valid, omit from src array
565 srcArray
.RemoveLastElement();
566 NS_WARNING("null url in @font-face rule");
572 case StyleFontFaceSourceListComponent::Tag::FormatHintKeyword
:
573 case StyleFontFaceSourceListComponent::Tag::FormatHintString
:
574 case StyleFontFaceSourceListComponent::Tag::TechFlags
:
575 MOZ_ASSERT_UNREACHABLE(
576 "Should always come after a URL source, and be consumed already");
582 if (srcArray
.IsEmpty()) {
586 return set
->FindOrCreateUserFontEntry(std::move(srcArray
), std::move(aAttr
));
589 nsresult
FontFaceSetImpl::LogMessage(gfxUserFontEntry
* aUserFontEntry
,
590 uint32_t aSrcIndex
, const char* aMessage
,
591 uint32_t aFlags
, nsresult aStatus
) {
592 nsAutoCString familyName
;
593 nsAutoCString fontURI
;
594 aUserFontEntry
->GetFamilyNameAndURIForLogging(aSrcIndex
, familyName
, fontURI
);
596 nsAutoCString weightString
;
597 aUserFontEntry
->Weight().ToString(weightString
);
598 nsAutoCString stretchString
;
599 aUserFontEntry
->Stretch().ToString(stretchString
);
600 nsPrintfCString
message(
601 "downloadable font: %s "
602 "(font-family: \"%s\" style:%s weight:%s stretch:%s src index:%d)",
603 aMessage
, familyName
.get(),
604 aUserFontEntry
->IsItalic() ? "italic" : "normal", // XXX todo: oblique?
605 weightString
.get(), stretchString
.get(), aSrcIndex
);
607 if (NS_FAILED(aStatus
)) {
608 message
.AppendLiteral(": ");
610 case NS_ERROR_DOM_BAD_URI
:
611 message
.AppendLiteral("bad URI or cross-site access not allowed");
613 case NS_ERROR_CONTENT_BLOCKED
:
614 message
.AppendLiteral("content blocked");
617 message
.AppendLiteral("status=");
618 message
.AppendInt(static_cast<uint32_t>(aStatus
));
622 message
.AppendLiteral(" source: ");
623 message
.Append(fontURI
);
625 LOG(("userfonts (%p) %s", this, message
.get()));
627 if (GetCurrentThreadWorkerPrivate()) {
628 // TODO(aosmond): Log to the console for workers. See bug 1778537.
632 nsCOMPtr
<nsIConsoleService
> console(
633 do_GetService(NS_CONSOLESERVICE_CONTRACTID
));
635 return NS_ERROR_NOT_AVAILABLE
;
638 // try to give the user an indication of where the rule came from
639 StyleLockedFontFaceRule
* rule
= FindRuleForUserFontEntry(aUserFontEntry
);
645 Servo_FontFaceRule_GetCssText(rule
, &text
);
646 Servo_FontFaceRule_GetSourceLocation(rule
, &line
, &column
);
647 // FIXME We need to figure out an approach to get the style sheet
648 // of this raw rule. See bug 1450903.
650 StyleSheet
* sheet
= rule
->GetStyleSheet();
651 // if the style sheet is removed while the font is loading can be null
653 nsCString spec
= sheet
->GetSheetURI()->GetSpecOrDefault();
654 CopyUTF8toUTF16(spec
, href
);
656 NS_WARNING("null parent stylesheet for @font-face rule");
657 href
.AssignLiteral("unknown");
660 // Leave href empty if we don't know how to get the correct sheet.
664 nsCOMPtr
<nsIScriptError
> scriptError
=
665 do_CreateInstance(NS_SCRIPTERROR_CONTRACTID
, &rv
);
666 NS_ENSURE_SUCCESS(rv
, rv
);
668 rv
= scriptError
->InitWithWindowID(NS_ConvertUTF8toUTF16(message
),
670 NS_ConvertUTF8toUTF16(text
), // src line
673 "CSS Loader", // category (make separate?)
675 if (NS_SUCCEEDED(rv
)) {
676 console
->LogMessage(scriptError
);
682 nsresult
FontFaceSetImpl::SyncLoadFontData(gfxUserFontEntry
* aFontToLoad
,
683 const gfxFontFaceSrc
* aFontFaceSrc
,
685 uint32_t& aBufferLength
) {
686 nsCOMPtr
<nsIChannel
> channel
;
687 nsresult rv
= CreateChannelForSyncLoadFontData(getter_AddRefs(channel
),
688 aFontToLoad
, aFontFaceSrc
);
689 NS_ENSURE_SUCCESS(rv
, rv
);
691 // blocking stream is OK for data URIs
692 nsCOMPtr
<nsIInputStream
> stream
;
693 rv
= channel
->Open(getter_AddRefs(stream
));
694 NS_ENSURE_SUCCESS(rv
, rv
);
696 uint64_t bufferLength64
;
697 rv
= stream
->Available(&bufferLength64
);
698 NS_ENSURE_SUCCESS(rv
, rv
);
699 if (bufferLength64
== 0) {
700 return NS_ERROR_FAILURE
;
702 if (bufferLength64
> UINT32_MAX
) {
703 return NS_ERROR_FILE_TOO_BIG
;
705 aBufferLength
= static_cast<uint32_t>(bufferLength64
);
707 // read all the decoded data
708 aBuffer
= static_cast<uint8_t*>(malloc(sizeof(uint8_t) * aBufferLength
));
711 return NS_ERROR_OUT_OF_MEMORY
;
714 uint32_t numRead
, totalRead
= 0;
716 rv
= stream
->Read(reinterpret_cast<char*>(aBuffer
+ totalRead
),
717 aBufferLength
- totalRead
, &numRead
)) &&
719 totalRead
+= numRead
;
720 if (totalRead
> aBufferLength
) {
721 rv
= NS_ERROR_FAILURE
;
726 // make sure there's a mime type
727 if (NS_SUCCEEDED(rv
)) {
728 nsAutoCString mimeType
;
729 rv
= channel
->GetContentType(mimeType
);
730 aBufferLength
= totalRead
;
743 void FontFaceSetImpl::OnFontFaceStatusChanged(FontFaceImpl
* aFontFace
) {
744 gfxFontUtils::AssertSafeThreadOrServoFontMetricsLocked();
745 RecursiveMutexAutoLock
lock(mMutex
);
746 MOZ_ASSERT(HasAvailableFontFace(aFontFace
));
748 mHasLoadingFontFacesIsDirty
= true;
750 if (aFontFace
->Status() == FontFaceLoadStatus::Loading
) {
751 CheckLoadingStarted();
753 MOZ_ASSERT(aFontFace
->Status() == FontFaceLoadStatus::Loaded
||
754 aFontFace
->Status() == FontFaceLoadStatus::Error
);
755 // When a font finishes downloading, nsPresContext::UserFontSetUpdated
756 // will be called immediately afterwards to request a reflow of the
757 // relevant elements in the document. We want to wait until the reflow
758 // request has been done before the FontFaceSet is marked as Loaded so
759 // that we don't briefly set the FontFaceSet to Loaded and then Loading
760 // again once the reflow is pending. So we go around the event loop
761 // and call CheckLoadingFinished() after the reflow has been queued.
762 if (!mDelayedLoadCheck
) {
763 mDelayedLoadCheck
= true;
764 DispatchCheckLoadingFinishedAfterDelay();
769 void FontFaceSetImpl::DispatchCheckLoadingFinishedAfterDelay() {
770 gfxFontUtils::AssertSafeThreadOrServoFontMetricsLocked();
772 if (ServoStyleSet
* set
= gfxFontUtils::CurrentServoStyleSet()) {
773 // See comments in Gecko_GetFontMetrics.
775 // We can't just dispatch the runnable below if we're not on the main
776 // thread, since it needs to take a strong reference to the FontFaceSet,
777 // and being a DOM object, FontFaceSet doesn't support thread-safe
780 PostTraversalTask::DispatchFontFaceSetCheckLoadingFinishedAfterDelay(
785 DispatchToOwningThread(
786 "FontFaceSetImpl::DispatchCheckLoadingFinishedAfterDelay",
787 [self
= RefPtr
{this}]() { self
->CheckLoadingFinishedAfterDelay(); });
790 void FontFaceSetImpl::CheckLoadingFinishedAfterDelay() {
791 RecursiveMutexAutoLock
lock(mMutex
);
792 mDelayedLoadCheck
= false;
793 CheckLoadingFinished();
796 void FontFaceSetImpl::CheckLoadingStarted() {
797 gfxFontUtils::AssertSafeThreadOrServoFontMetricsLocked();
798 RecursiveMutexAutoLock
lock(mMutex
);
800 if (!HasLoadingFontFaces()) {
804 if (mStatus
== FontFaceSetLoadStatus::Loading
) {
805 // We have already dispatched a loading event and replaced mReady
806 // with a fresh, unresolved promise.
810 mStatus
= FontFaceSetLoadStatus::Loading
;
812 if (IsOnOwningThread()) {
817 DispatchToOwningThread("FontFaceSetImpl::CheckLoadingStarted",
818 [self
= RefPtr
{this}]() { self
->OnLoadingStarted(); });
821 void FontFaceSetImpl::OnLoadingStarted() {
822 RecursiveMutexAutoLock
lock(mMutex
);
824 mOwner
->DispatchLoadingEventAndReplaceReadyPromise();
828 void FontFaceSetImpl::UpdateHasLoadingFontFaces() {
829 RecursiveMutexAutoLock
lock(mMutex
);
830 mHasLoadingFontFacesIsDirty
= false;
831 mHasLoadingFontFaces
= false;
832 for (size_t i
= 0; i
< mNonRuleFaces
.Length(); i
++) {
833 if (mNonRuleFaces
[i
].mFontFace
->Status() == FontFaceLoadStatus::Loading
) {
834 mHasLoadingFontFaces
= true;
840 bool FontFaceSetImpl::HasLoadingFontFaces() {
841 RecursiveMutexAutoLock
lock(mMutex
);
842 if (mHasLoadingFontFacesIsDirty
) {
843 UpdateHasLoadingFontFaces();
845 return mHasLoadingFontFaces
;
848 bool FontFaceSetImpl::MightHavePendingFontLoads() {
849 // Check for FontFace objects in the FontFaceSet that are still loading.
850 return HasLoadingFontFaces();
853 void FontFaceSetImpl::CheckLoadingFinished() {
854 RecursiveMutexAutoLock
lock(mMutex
);
855 if (mDelayedLoadCheck
) {
856 // Wait until the runnable posted in OnFontFaceStatusChanged calls us.
860 if (!ReadyPromiseIsPending()) {
861 // We've already resolved mReady (or set the flag to do that lazily) and
862 // dispatched the loadingdone/loadingerror events.
866 if (MightHavePendingFontLoads()) {
867 // We're not finished loading yet.
871 mStatus
= FontFaceSetLoadStatus::Loaded
;
873 if (IsOnOwningThread()) {
878 DispatchToOwningThread(
879 "FontFaceSetImpl::CheckLoadingFinished",
880 [self
= RefPtr
{this}]() { self
->OnLoadingFinished(); });
883 void FontFaceSetImpl::OnLoadingFinished() {
884 RecursiveMutexAutoLock
lock(mMutex
);
886 mOwner
->MaybeResolve();
890 void FontFaceSetImpl::RefreshStandardFontLoadPrincipal() {
891 RecursiveMutexAutoLock
lock(mMutex
);
892 mAllowedFontLoads
.Clear();
893 IncrementGeneration(false);
897 // ------------------------------------------------
899 already_AddRefed
<gfxFontSrcPrincipal
>
900 FontFaceSetImpl::GetStandardFontLoadPrincipal() const {
901 RecursiveMutexAutoLock
lock(mMutex
);
902 return RefPtr
{mStandardFontLoadPrincipal
}.forget();
905 void FontFaceSetImpl::RecordFontLoadDone(uint32_t aFontSize
,
906 TimeStamp aDoneTime
) {
908 mDownloadSize
+= aFontSize
;
909 Telemetry::Accumulate(Telemetry::WEBFONT_SIZE
, aFontSize
/ 1024);
911 TimeStamp navStart
= GetNavigationStartTimeStamp();
913 if (navStart
!= zero
) {
914 Telemetry::AccumulateTimeDelta(Telemetry::WEBFONT_DOWNLOAD_TIME_AFTER_START
,
915 navStart
, aDoneTime
);
919 void FontFaceSetImpl::DoRebuildUserFontSet() { MarkUserFontSetDirty(); }
921 already_AddRefed
<gfxUserFontEntry
> FontFaceSetImpl::CreateUserFontEntry(
922 nsTArray
<gfxFontFaceSrc
>&& aFontFaceSrcList
,
923 gfxUserFontAttributes
&& aAttr
) {
924 RefPtr
<gfxUserFontEntry
> entry
= new FontFaceImpl::Entry(
925 this, std::move(aFontFaceSrcList
), std::move(aAttr
));
926 return entry
.forget();
929 void FontFaceSetImpl::ForgetLocalFaces() {
930 // We cannot hold our lock at the same time as the gfxUserFontFamily lock, so
931 // we need to make a copy of the table first.
932 nsTArray
<RefPtr
<gfxUserFontFamily
>> fontFamilies
;
934 RecursiveMutexAutoLock
lock(mMutex
);
935 fontFamilies
.SetCapacity(mFontFamilies
.Count());
936 for (const auto& fam
: mFontFamilies
.Values()) {
937 fontFamilies
.AppendElement(fam
);
941 for (const auto& fam
: fontFamilies
) {
942 ForgetLocalFace(fam
);
946 already_AddRefed
<gfxUserFontFamily
> FontFaceSetImpl::GetFamily(
947 const nsACString
& aFamilyName
) {
948 RecursiveMutexAutoLock
lock(mMutex
);
949 return gfxUserFontSet::GetFamily(aFamilyName
);