FontFaceSet.cpp 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843
  1. /* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this file,
  4. * You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #include "FontFaceSet.h"
  6. #include "gfxFontConstants.h"
  7. #include "mozilla/css/Declaration.h"
  8. #include "mozilla/css/Loader.h"
  9. #include "mozilla/dom/FontFaceSetBinding.h"
  10. #include "mozilla/dom/FontFaceSetIterator.h"
  11. #include "mozilla/dom/FontFaceSetLoadEvent.h"
  12. #include "mozilla/dom/FontFaceSetLoadEventBinding.h"
  13. #include "mozilla/dom/Promise.h"
  14. #include "mozilla/AsyncEventDispatcher.h"
  15. #include "mozilla/Logging.h"
  16. #include "mozilla/Preferences.h"
  17. #include "mozilla/SizePrintfMacros.h"
  18. #include "mozilla/Sprintf.h"
  19. #include "mozilla/Telemetry.h"
  20. #include "nsAutoPtr.h"
  21. #include "nsContentPolicyUtils.h"
  22. #include "nsCSSParser.h"
  23. #include "nsDeviceContext.h"
  24. #include "nsFontFaceLoader.h"
  25. #include "nsIConsoleService.h"
  26. #include "nsIContentPolicy.h"
  27. #include "nsIContentSecurityPolicy.h"
  28. #include "nsIDocShell.h"
  29. #include "nsIDocument.h"
  30. #include "nsINetworkPredictor.h"
  31. #include "nsIPresShell.h"
  32. #include "nsIPrincipal.h"
  33. #include "nsISupportsPriority.h"
  34. #include "nsIWebNavigation.h"
  35. #include "nsNetUtil.h"
  36. #include "nsIProtocolHandler.h"
  37. #include "nsIInputStream.h"
  38. #include "nsPresContext.h"
  39. #include "nsPrintfCString.h"
  40. #include "nsStyleSet.h"
  41. #include "nsUTF8Utils.h"
  42. #include "nsDOMNavigationTiming.h"
  43. using namespace mozilla;
  44. using namespace mozilla::css;
  45. using namespace mozilla::dom;
  46. #define LOG(args) MOZ_LOG(gfxUserFontSet::GetUserFontsLog(), mozilla::LogLevel::Debug, args)
  47. #define LOG_ENABLED() MOZ_LOG_TEST(gfxUserFontSet::GetUserFontsLog(), \
  48. LogLevel::Debug)
  49. #define FONT_LOADING_API_ENABLED_PREF "layout.css.font-loading-api.enabled"
  50. NS_IMPL_CYCLE_COLLECTION_CLASS(FontFaceSet)
  51. NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(FontFaceSet, DOMEventTargetHelper)
  52. NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mDocument);
  53. NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mReady);
  54. for (size_t i = 0; i < tmp->mRuleFaces.Length(); i++) {
  55. NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mRuleFaces[i].mFontFace);
  56. }
  57. for (size_t i = 0; i < tmp->mNonRuleFaces.Length(); i++) {
  58. NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mNonRuleFaces[i].mFontFace);
  59. }
  60. if (tmp->mUserFontSet) {
  61. NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mUserFontSet->mFontFaceSet);
  62. }
  63. NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
  64. NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(FontFaceSet, DOMEventTargetHelper)
  65. tmp->Disconnect();
  66. NS_IMPL_CYCLE_COLLECTION_UNLINK(mDocument);
  67. NS_IMPL_CYCLE_COLLECTION_UNLINK(mReady);
  68. for (size_t i = 0; i < tmp->mRuleFaces.Length(); i++) {
  69. NS_IMPL_CYCLE_COLLECTION_UNLINK(mRuleFaces[i].mFontFace);
  70. }
  71. for (size_t i = 0; i < tmp->mNonRuleFaces.Length(); i++) {
  72. NS_IMPL_CYCLE_COLLECTION_UNLINK(mNonRuleFaces[i].mFontFace);
  73. }
  74. if (tmp->mUserFontSet) {
  75. NS_IMPL_CYCLE_COLLECTION_UNLINK(mUserFontSet->mFontFaceSet);
  76. }
  77. NS_IMPL_CYCLE_COLLECTION_UNLINK(mUserFontSet);
  78. NS_IMPL_CYCLE_COLLECTION_UNLINK_END
  79. NS_IMPL_ADDREF_INHERITED(FontFaceSet, DOMEventTargetHelper)
  80. NS_IMPL_RELEASE_INHERITED(FontFaceSet, DOMEventTargetHelper)
  81. NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(FontFaceSet)
  82. NS_INTERFACE_MAP_ENTRY(nsIDOMEventListener)
  83. NS_INTERFACE_MAP_ENTRY(nsICSSLoaderObserver)
  84. NS_INTERFACE_MAP_END_INHERITING(DOMEventTargetHelper)
  85. FontFaceSet::FontFaceSet(nsPIDOMWindowInner* aWindow, nsIDocument* aDocument)
  86. : DOMEventTargetHelper(aWindow)
  87. , mDocument(aDocument)
  88. , mResolveLazilyCreatedReadyPromise(false)
  89. , mStatus(FontFaceSetLoadStatus::Loaded)
  90. , mNonRuleFacesDirty(false)
  91. , mHasLoadingFontFaces(false)
  92. , mHasLoadingFontFacesIsDirty(false)
  93. , mDelayedLoadCheck(false)
  94. {
  95. MOZ_COUNT_CTOR(FontFaceSet);
  96. nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(aWindow);
  97. // If the pref is not set, don't create the Promise (which the page wouldn't
  98. // be able to get to anyway) as it causes the window.FontFaceSet constructor
  99. // to be created.
  100. if (global && PrefEnabled()) {
  101. mResolveLazilyCreatedReadyPromise = true;
  102. }
  103. if (!mDocument->DidFireDOMContentLoaded()) {
  104. mDocument->AddSystemEventListener(NS_LITERAL_STRING("DOMContentLoaded"),
  105. this, false, false);
  106. }
  107. mDocument->CSSLoader()->AddObserver(this);
  108. mUserFontSet = new UserFontSet(this);
  109. }
  110. FontFaceSet::~FontFaceSet()
  111. {
  112. MOZ_COUNT_DTOR(FontFaceSet);
  113. Disconnect();
  114. for (auto it = mLoaders.Iter(); !it.Done(); it.Next()) {
  115. it.Get()->GetKey()->Cancel();
  116. }
  117. }
  118. JSObject*
  119. FontFaceSet::WrapObject(JSContext* aContext, JS::Handle<JSObject*> aGivenProto)
  120. {
  121. return FontFaceSetBinding::Wrap(aContext, this, aGivenProto);
  122. }
  123. void
  124. FontFaceSet::Disconnect()
  125. {
  126. RemoveDOMContentLoadedListener();
  127. if (mDocument && mDocument->CSSLoader()) {
  128. // We're null checking CSSLoader() since FontFaceSet::Disconnect() might be
  129. // being called during unlink, at which time the loader amy already have
  130. // been unlinked from the document.
  131. mDocument->CSSLoader()->RemoveObserver(this);
  132. }
  133. }
  134. void
  135. FontFaceSet::RemoveDOMContentLoadedListener()
  136. {
  137. if (mDocument) {
  138. mDocument->RemoveSystemEventListener(NS_LITERAL_STRING("DOMContentLoaded"),
  139. this, false);
  140. }
  141. }
  142. void
  143. FontFaceSet::ParseFontShorthandForMatching(
  144. const nsAString& aFont,
  145. RefPtr<FontFamilyListRefCnt>& aFamilyList,
  146. uint32_t& aWeight,
  147. int32_t& aStretch,
  148. uint8_t& aStyle,
  149. ErrorResult& aRv)
  150. {
  151. // Parse aFont as a 'font' property value.
  152. RefPtr<Declaration> declaration = new Declaration;
  153. declaration->InitializeEmpty();
  154. bool changed = false;
  155. nsCSSParser parser;
  156. parser.ParseProperty(eCSSProperty_font,
  157. aFont,
  158. mDocument->GetDocumentURI(),
  159. mDocument->GetDocumentURI(),
  160. mDocument->NodePrincipal(),
  161. declaration,
  162. &changed,
  163. /* aIsImportant */ false);
  164. // All of the properties we are interested in should have been set at once.
  165. MOZ_ASSERT(changed == (declaration->HasProperty(eCSSProperty_font_family) &&
  166. declaration->HasProperty(eCSSProperty_font_style) &&
  167. declaration->HasProperty(eCSSProperty_font_weight) &&
  168. declaration->HasProperty(eCSSProperty_font_stretch)));
  169. if (!changed) {
  170. aRv.Throw(NS_ERROR_DOM_SYNTAX_ERR);
  171. return;
  172. }
  173. nsCSSCompressedDataBlock* data = declaration->GetNormalBlock();
  174. MOZ_ASSERT(!declaration->GetImportantBlock());
  175. const nsCSSValue* family = data->ValueFor(eCSSProperty_font_family);
  176. if (family->GetUnit() != eCSSUnit_FontFamilyList) {
  177. // We got inherit, initial, unset, a system font, or a token stream.
  178. aRv.Throw(NS_ERROR_DOM_SYNTAX_ERR);
  179. return;
  180. }
  181. aFamilyList =
  182. static_cast<FontFamilyListRefCnt*>(family->GetFontFamilyListValue());
  183. int32_t weight = data->ValueFor(eCSSProperty_font_weight)->GetIntValue();
  184. // Resolve relative font weights against the initial of font-weight
  185. // (normal, which is equivalent to 400).
  186. if (weight == NS_STYLE_FONT_WEIGHT_BOLDER) {
  187. weight = NS_FONT_WEIGHT_BOLD;
  188. } else if (weight == NS_STYLE_FONT_WEIGHT_LIGHTER) {
  189. weight = NS_FONT_WEIGHT_THIN;
  190. }
  191. aWeight = weight;
  192. aStretch = data->ValueFor(eCSSProperty_font_stretch)->GetIntValue();
  193. aStyle = data->ValueFor(eCSSProperty_font_style)->GetIntValue();
  194. }
  195. static bool
  196. HasAnyCharacterInUnicodeRange(gfxUserFontEntry* aEntry,
  197. const nsAString& aInput)
  198. {
  199. const char16_t* p = aInput.Data();
  200. const char16_t* end = p + aInput.Length();
  201. while (p < end) {
  202. uint32_t c = UTF16CharEnumerator::NextChar(&p, end);
  203. if (aEntry->CharacterInUnicodeRange(c)) {
  204. return true;
  205. }
  206. }
  207. return false;
  208. }
  209. void
  210. FontFaceSet::FindMatchingFontFaces(const nsAString& aFont,
  211. const nsAString& aText,
  212. nsTArray<FontFace*>& aFontFaces,
  213. ErrorResult& aRv)
  214. {
  215. RefPtr<FontFamilyListRefCnt> familyList;
  216. uint32_t weight;
  217. int32_t stretch;
  218. uint8_t italicStyle;
  219. ParseFontShorthandForMatching(aFont, familyList, weight, stretch, italicStyle,
  220. aRv);
  221. if (aRv.Failed()) {
  222. return;
  223. }
  224. gfxFontStyle style;
  225. style.style = italicStyle;
  226. style.weight = weight;
  227. style.stretch = stretch;
  228. nsTArray<FontFaceRecord>* arrays[2];
  229. arrays[0] = &mNonRuleFaces;
  230. arrays[1] = &mRuleFaces;
  231. // Set of FontFaces that we want to return.
  232. nsTHashtable<nsPtrHashKey<FontFace>> matchingFaces;
  233. for (const FontFamilyName& fontFamilyName : familyList->GetFontlist()) {
  234. RefPtr<gfxFontFamily> family =
  235. mUserFontSet->LookupFamily(fontFamilyName.mName);
  236. if (!family) {
  237. continue;
  238. }
  239. AutoTArray<gfxFontEntry*,4> entries;
  240. bool needsBold;
  241. family->FindAllFontsForStyle(style, entries, needsBold);
  242. for (gfxFontEntry* e : entries) {
  243. FontFace::Entry* entry = static_cast<FontFace::Entry*>(e);
  244. if (HasAnyCharacterInUnicodeRange(entry, aText)) {
  245. for (FontFace* f : entry->GetFontFaces()) {
  246. matchingFaces.PutEntry(f);
  247. }
  248. }
  249. }
  250. }
  251. // Add all FontFaces in matchingFaces to aFontFaces, in the order
  252. // they appear in the FontFaceSet.
  253. for (nsTArray<FontFaceRecord>* array : arrays) {
  254. for (FontFaceRecord& record : *array) {
  255. FontFace* f = record.mFontFace;
  256. if (matchingFaces.Contains(f)) {
  257. aFontFaces.AppendElement(f);
  258. }
  259. }
  260. }
  261. }
  262. TimeStamp
  263. FontFaceSet::GetNavigationStartTimeStamp()
  264. {
  265. TimeStamp navStart;
  266. RefPtr<nsDOMNavigationTiming> timing(mDocument->GetNavigationTiming());
  267. if (timing) {
  268. navStart = timing->GetNavigationStartTimeStamp();
  269. }
  270. return navStart;
  271. }
  272. already_AddRefed<Promise>
  273. FontFaceSet::Load(JSContext* aCx,
  274. const nsAString& aFont,
  275. const nsAString& aText,
  276. ErrorResult& aRv)
  277. {
  278. FlushUserFontSet();
  279. nsTArray<RefPtr<Promise>> promises;
  280. nsTArray<FontFace*> faces;
  281. FindMatchingFontFaces(aFont, aText, faces, aRv);
  282. if (aRv.Failed()) {
  283. return nullptr;
  284. }
  285. for (FontFace* f : faces) {
  286. RefPtr<Promise> promise = f->Load(aRv);
  287. if (aRv.Failed()) {
  288. return nullptr;
  289. }
  290. if (!promises.AppendElement(promise, fallible)) {
  291. aRv.Throw(NS_ERROR_FAILURE);
  292. return nullptr;
  293. }
  294. }
  295. return Promise::All(aCx, promises, aRv);
  296. }
  297. bool
  298. FontFaceSet::Check(const nsAString& aFont,
  299. const nsAString& aText,
  300. ErrorResult& aRv)
  301. {
  302. FlushUserFontSet();
  303. nsTArray<FontFace*> faces;
  304. FindMatchingFontFaces(aFont, aText, faces, aRv);
  305. if (aRv.Failed()) {
  306. return false;
  307. }
  308. for (FontFace* f : faces) {
  309. if (f->Status() != FontFaceLoadStatus::Loaded) {
  310. return false;
  311. }
  312. }
  313. return true;
  314. }
  315. Promise*
  316. FontFaceSet::GetReady(ErrorResult& aRv)
  317. {
  318. if (!mReady) {
  319. nsCOMPtr<nsIGlobalObject> global = GetParentObject();
  320. mReady = Promise::Create(global, aRv);
  321. if (!mReady) {
  322. aRv.Throw(NS_ERROR_FAILURE);
  323. return nullptr;
  324. }
  325. if (mResolveLazilyCreatedReadyPromise) {
  326. mReady->MaybeResolve(this);
  327. mResolveLazilyCreatedReadyPromise = false;
  328. }
  329. }
  330. FlushUserFontSet();
  331. return mReady;
  332. }
  333. FontFaceSetLoadStatus
  334. FontFaceSet::Status()
  335. {
  336. FlushUserFontSet();
  337. return mStatus;
  338. }
  339. #ifdef DEBUG
  340. bool
  341. FontFaceSet::HasRuleFontFace(FontFace* aFontFace)
  342. {
  343. for (size_t i = 0; i < mRuleFaces.Length(); i++) {
  344. if (mRuleFaces[i].mFontFace == aFontFace) {
  345. return true;
  346. }
  347. }
  348. return false;
  349. }
  350. #endif
  351. FontFaceSet*
  352. FontFaceSet::Add(FontFace& aFontFace, ErrorResult& aRv)
  353. {
  354. FlushUserFontSet();
  355. if (aFontFace.IsInFontFaceSet(this)) {
  356. return this;
  357. }
  358. if (aFontFace.HasRule()) {
  359. aRv.Throw(NS_ERROR_DOM_INVALID_MODIFICATION_ERR);
  360. return nullptr;
  361. }
  362. aFontFace.AddFontFaceSet(this);
  363. #ifdef DEBUG
  364. for (const FontFaceRecord& rec : mNonRuleFaces) {
  365. MOZ_ASSERT(rec.mFontFace != &aFontFace,
  366. "FontFace should not occur in mNonRuleFaces twice");
  367. }
  368. #endif
  369. FontFaceRecord* rec = mNonRuleFaces.AppendElement();
  370. rec->mFontFace = &aFontFace;
  371. rec->mSheetType = SheetType::Unknown; // unused for mNonRuleFaces
  372. rec->mLoadEventShouldFire =
  373. aFontFace.Status() == FontFaceLoadStatus::Unloaded ||
  374. aFontFace.Status() == FontFaceLoadStatus::Loading;
  375. mNonRuleFacesDirty = true;
  376. RebuildUserFontSet();
  377. mHasLoadingFontFacesIsDirty = true;
  378. CheckLoadingStarted();
  379. return this;
  380. }
  381. void
  382. FontFaceSet::Clear()
  383. {
  384. FlushUserFontSet();
  385. if (mNonRuleFaces.IsEmpty()) {
  386. return;
  387. }
  388. for (size_t i = 0; i < mNonRuleFaces.Length(); i++) {
  389. FontFace* f = mNonRuleFaces[i].mFontFace;
  390. f->RemoveFontFaceSet(this);
  391. }
  392. mNonRuleFaces.Clear();
  393. mNonRuleFacesDirty = true;
  394. RebuildUserFontSet();
  395. mHasLoadingFontFacesIsDirty = true;
  396. CheckLoadingFinished();
  397. }
  398. bool
  399. FontFaceSet::Delete(FontFace& aFontFace)
  400. {
  401. FlushUserFontSet();
  402. if (aFontFace.HasRule()) {
  403. return false;
  404. }
  405. bool removed = false;
  406. for (size_t i = 0; i < mNonRuleFaces.Length(); i++) {
  407. if (mNonRuleFaces[i].mFontFace == &aFontFace) {
  408. mNonRuleFaces.RemoveElementAt(i);
  409. removed = true;
  410. break;
  411. }
  412. }
  413. if (!removed) {
  414. return false;
  415. }
  416. aFontFace.RemoveFontFaceSet(this);
  417. mNonRuleFacesDirty = true;
  418. RebuildUserFontSet();
  419. mHasLoadingFontFacesIsDirty = true;
  420. CheckLoadingFinished();
  421. return true;
  422. }
  423. bool
  424. FontFaceSet::HasAvailableFontFace(FontFace* aFontFace)
  425. {
  426. return aFontFace->IsInFontFaceSet(this);
  427. }
  428. bool
  429. FontFaceSet::Has(FontFace& aFontFace)
  430. {
  431. FlushUserFontSet();
  432. return HasAvailableFontFace(&aFontFace);
  433. }
  434. FontFace*
  435. FontFaceSet::GetFontFaceAt(uint32_t aIndex)
  436. {
  437. FlushUserFontSet();
  438. if (aIndex < mRuleFaces.Length()) {
  439. return mRuleFaces[aIndex].mFontFace;
  440. }
  441. aIndex -= mRuleFaces.Length();
  442. if (aIndex < mNonRuleFaces.Length()) {
  443. return mNonRuleFaces[aIndex].mFontFace;
  444. }
  445. return nullptr;
  446. }
  447. uint32_t
  448. FontFaceSet::Size()
  449. {
  450. FlushUserFontSet();
  451. // Web IDL objects can only expose array index properties up to INT32_MAX.
  452. size_t total = mRuleFaces.Length() + mNonRuleFaces.Length();
  453. return std::min<size_t>(total, INT32_MAX);
  454. }
  455. already_AddRefed<FontFaceSetIterator>
  456. FontFaceSet::Entries()
  457. {
  458. RefPtr<FontFaceSetIterator> it = new FontFaceSetIterator(this, true);
  459. return it.forget();
  460. }
  461. already_AddRefed<FontFaceSetIterator>
  462. FontFaceSet::Values()
  463. {
  464. RefPtr<FontFaceSetIterator> it = new FontFaceSetIterator(this, false);
  465. return it.forget();
  466. }
  467. void
  468. FontFaceSet::ForEach(JSContext* aCx,
  469. FontFaceSetForEachCallback& aCallback,
  470. JS::Handle<JS::Value> aThisArg,
  471. ErrorResult& aRv)
  472. {
  473. JS::Rooted<JS::Value> thisArg(aCx, aThisArg);
  474. for (size_t i = 0; i < Size(); i++) {
  475. FontFace* face = GetFontFaceAt(i);
  476. aCallback.Call(thisArg, *face, *face, *this, aRv);
  477. if (aRv.Failed()) {
  478. return;
  479. }
  480. }
  481. }
  482. void
  483. FontFaceSet::RemoveLoader(nsFontFaceLoader* aLoader)
  484. {
  485. mLoaders.RemoveEntry(aLoader);
  486. }
  487. nsresult
  488. FontFaceSet::StartLoad(gfxUserFontEntry* aUserFontEntry,
  489. const gfxFontFaceSrc* aFontFaceSrc)
  490. {
  491. nsresult rv;
  492. nsCOMPtr<nsIStreamLoader> streamLoader;
  493. nsCOMPtr<nsILoadGroup> loadGroup(mDocument->GetDocumentLoadGroup());
  494. // We're determining the security flags for font loading here based on
  495. // scheme, because we want to allow fonts to be loaded using file:
  496. // even if unique origins for file: access is enforced (allow CORS
  497. // bypass in this case).
  498. uint32_t securityFlags = 0;
  499. bool isFile = false;
  500. if (NS_SUCCEEDED(aFontFaceSrc->mURI->SchemeIs("file", &isFile)) &&
  501. isFile) {
  502. securityFlags = nsILoadInfo::SEC_ALLOW_CROSS_ORIGIN_DATA_INHERITS;
  503. } else {
  504. securityFlags = nsILoadInfo::SEC_REQUIRE_CORS_DATA_INHERITS;
  505. }
  506. nsCOMPtr<nsIChannel> channel;
  507. // Note we are calling NS_NewChannelWithTriggeringPrincipal() with both a
  508. // node and a principal. This is because the document where the font is
  509. // being loaded might have a different origin from the principal of the
  510. // stylesheet that initiated the font load.
  511. rv = NS_NewChannelWithTriggeringPrincipal(getter_AddRefs(channel),
  512. aFontFaceSrc->mURI,
  513. mDocument,
  514. aUserFontEntry->GetPrincipal(),
  515. securityFlags,
  516. nsIContentPolicy::TYPE_FONT,
  517. loadGroup);
  518. NS_ENSURE_SUCCESS(rv, rv);
  519. RefPtr<nsFontFaceLoader> fontLoader =
  520. new nsFontFaceLoader(aUserFontEntry, aFontFaceSrc->mURI, this, channel);
  521. if (LOG_ENABLED()) {
  522. LOG(("userfonts (%p) download start - font uri: (%s) "
  523. "referrer uri: (%s)\n",
  524. fontLoader.get(), aFontFaceSrc->mURI->GetSpecOrDefault().get(),
  525. aFontFaceSrc->mReferrer
  526. ? aFontFaceSrc->mReferrer->GetSpecOrDefault().get()
  527. : ""));
  528. }
  529. nsCOMPtr<nsIHttpChannel> httpChannel(do_QueryInterface(channel));
  530. if (httpChannel) {
  531. httpChannel->SetReferrerWithPolicy(aFontFaceSrc->mReferrer,
  532. mDocument->GetReferrerPolicy());
  533. nsAutoCString accept("application/font-woff;q=0.9,*/*;q=0.8");
  534. if (Preferences::GetBool(GFX_PREF_WOFF2_ENABLED)) {
  535. accept.Insert(NS_LITERAL_CSTRING("application/font-woff2;q=1.0,"), 0);
  536. }
  537. httpChannel->SetRequestHeader(NS_LITERAL_CSTRING("Accept"),
  538. accept, false);
  539. // For WOFF and WOFF2, we should tell servers/proxies/etc NOT to try
  540. // and apply additional compression at the content-encoding layer
  541. if (aFontFaceSrc->mFormatFlags & (gfxUserFontSet::FLAG_FORMAT_WOFF |
  542. gfxUserFontSet::FLAG_FORMAT_WOFF2)) {
  543. httpChannel->SetRequestHeader(NS_LITERAL_CSTRING("Accept-Encoding"),
  544. NS_LITERAL_CSTRING("identity"), false);
  545. }
  546. }
  547. nsCOMPtr<nsISupportsPriority> priorityChannel(do_QueryInterface(channel));
  548. if (priorityChannel) {
  549. priorityChannel->AdjustPriority(nsISupportsPriority::PRIORITY_HIGH);
  550. }
  551. rv = NS_NewStreamLoader(getter_AddRefs(streamLoader), fontLoader);
  552. NS_ENSURE_SUCCESS(rv, rv);
  553. mozilla::net::PredictorLearn(aFontFaceSrc->mURI, mDocument->GetDocumentURI(),
  554. nsINetworkPredictor::LEARN_LOAD_SUBRESOURCE,
  555. loadGroup);
  556. rv = channel->AsyncOpen2(streamLoader);
  557. if (NS_FAILED(rv)) {
  558. fontLoader->DropChannel(); // explicitly need to break ref cycle
  559. }
  560. if (NS_SUCCEEDED(rv)) {
  561. mLoaders.PutEntry(fontLoader);
  562. fontLoader->StartedLoading(streamLoader);
  563. aUserFontEntry->SetLoader(fontLoader); // let the font entry remember the
  564. // loader, in case we need to cancel it
  565. }
  566. return rv;
  567. }
  568. bool
  569. FontFaceSet::UpdateRules(const nsTArray<nsFontFaceRuleContainer>& aRules)
  570. {
  571. MOZ_ASSERT(mUserFontSet);
  572. // If there was a change to the mNonRuleFaces array, then there could
  573. // have been a modification to the user font set.
  574. bool modified = mNonRuleFacesDirty;
  575. mNonRuleFacesDirty = false;
  576. // reuse existing FontFace objects mapped to rules already
  577. nsDataHashtable<nsPtrHashKey<nsCSSFontFaceRule>, FontFace*> ruleFaceMap;
  578. for (size_t i = 0, i_end = mRuleFaces.Length(); i < i_end; ++i) {
  579. FontFace* f = mRuleFaces[i].mFontFace;
  580. if (!f) {
  581. continue;
  582. }
  583. ruleFaceMap.Put(f->GetRule(), f);
  584. }
  585. // The @font-face rules that make up the user font set have changed,
  586. // so we need to update the set. However, we want to preserve existing
  587. // font entries wherever possible, so that we don't discard and then
  588. // re-download resources in the (common) case where at least some of the
  589. // same rules are still present.
  590. nsTArray<FontFaceRecord> oldRecords;
  591. mRuleFaces.SwapElements(oldRecords);
  592. // Remove faces from the font family records; we need to re-insert them
  593. // because we might end up with faces in a different order even if they're
  594. // the same font entries as before. (The order can affect font selection
  595. // where multiple faces match the requested style, perhaps with overlapping
  596. // unicode-range coverage.)
  597. for (auto it = mUserFontSet->mFontFamilies.Iter(); !it.Done(); it.Next()) {
  598. it.Data()->DetachFontEntries();
  599. }
  600. // Sometimes aRules has duplicate @font-face rules in it; we should make
  601. // that not happen, but in the meantime, don't try to insert the same
  602. // FontFace object more than once into mRuleFaces. We track which
  603. // ones we've handled in this table.
  604. nsTHashtable<nsPtrHashKey<nsCSSFontFaceRule>> handledRules;
  605. for (size_t i = 0, i_end = aRules.Length(); i < i_end; ++i) {
  606. // Insert each FontFace objects for each rule into our list, migrating old
  607. // font entries if possible rather than creating new ones; set modified to
  608. // true if we detect that rule ordering has changed, or if a new entry is
  609. // created.
  610. if (handledRules.Contains(aRules[i].mRule)) {
  611. continue;
  612. }
  613. nsCSSFontFaceRule* rule = aRules[i].mRule;
  614. RefPtr<FontFace> f = ruleFaceMap.Get(rule);
  615. if (!f.get()) {
  616. f = FontFace::CreateForRule(GetParentObject(), this, rule);
  617. }
  618. InsertRuleFontFace(f, aRules[i].mSheetType, oldRecords, modified);
  619. handledRules.PutEntry(aRules[i].mRule);
  620. }
  621. for (size_t i = 0, i_end = mNonRuleFaces.Length(); i < i_end; ++i) {
  622. // Do the same for the non rule backed FontFace objects.
  623. InsertNonRuleFontFace(mNonRuleFaces[i].mFontFace, modified);
  624. }
  625. // Remove any residual families that have no font entries (i.e., they were
  626. // not defined at all by the updated set of @font-face rules).
  627. for (auto it = mUserFontSet->mFontFamilies.Iter(); !it.Done(); it.Next()) {
  628. if (it.Data()->GetFontList().IsEmpty()) {
  629. it.Remove();
  630. }
  631. }
  632. // If any FontFace objects for rules are left in the old list, note that the
  633. // set has changed (even if the new set was built entirely by migrating old
  634. // font entries).
  635. if (oldRecords.Length() > 0) {
  636. modified = true;
  637. // Any in-progress loaders for obsolete rules should be cancelled,
  638. // as the resource being downloaded will no longer be required.
  639. // We need to explicitly remove any loaders here, otherwise the loaders
  640. // will keep their "orphaned" font entries alive until they complete,
  641. // even after the oldRules array is deleted.
  642. //
  643. // XXX Now that it is possible for the author to hold on to a rule backed
  644. // FontFace object, we shouldn't cancel loading here; instead we should do
  645. // it when the FontFace is GCed, if we can detect that.
  646. size_t count = oldRecords.Length();
  647. for (size_t i = 0; i < count; ++i) {
  648. RefPtr<FontFace> f = oldRecords[i].mFontFace;
  649. gfxUserFontEntry* userFontEntry = f->GetUserFontEntry();
  650. if (userFontEntry) {
  651. nsFontFaceLoader* loader = userFontEntry->GetLoader();
  652. if (loader) {
  653. loader->Cancel();
  654. RemoveLoader(loader);
  655. }
  656. }
  657. // Any left over FontFace objects should also cease being rule backed.
  658. f->DisconnectFromRule();
  659. }
  660. }
  661. if (modified) {
  662. IncrementGeneration(true);
  663. mHasLoadingFontFacesIsDirty = true;
  664. CheckLoadingStarted();
  665. CheckLoadingFinished();
  666. }
  667. // if local rules needed to be rebuilt, they have been rebuilt at this point
  668. if (mUserFontSet->mRebuildLocalRules) {
  669. mUserFontSet->mLocalRulesUsed = false;
  670. mUserFontSet->mRebuildLocalRules = false;
  671. }
  672. if (LOG_ENABLED() && !mRuleFaces.IsEmpty()) {
  673. LOG(("userfonts (%p) userfont rules update (%s) rule count: %d",
  674. mUserFontSet.get(),
  675. (modified ? "modified" : "not modified"),
  676. (int)(mRuleFaces.Length())));
  677. }
  678. return modified;
  679. }
  680. static bool
  681. HasLocalSrc(const nsCSSValue::Array *aSrcArr)
  682. {
  683. size_t numSrc = aSrcArr->Count();
  684. for (size_t i = 0; i < numSrc; i++) {
  685. if (aSrcArr->Item(i).GetUnit() == eCSSUnit_Local_Font) {
  686. return true;
  687. }
  688. }
  689. return false;
  690. }
  691. void
  692. FontFaceSet::IncrementGeneration(bool aIsRebuild)
  693. {
  694. MOZ_ASSERT(mUserFontSet);
  695. mUserFontSet->IncrementGeneration(aIsRebuild);
  696. }
  697. void
  698. FontFaceSet::InsertNonRuleFontFace(FontFace* aFontFace,
  699. bool& aFontSetModified)
  700. {
  701. nsAutoString fontfamily;
  702. if (!aFontFace->GetFamilyName(fontfamily)) {
  703. // If there is no family name, this rule cannot contribute a
  704. // usable font, so there is no point in processing it further.
  705. return;
  706. }
  707. // Just create a new font entry if we haven't got one already.
  708. if (!aFontFace->GetUserFontEntry()) {
  709. // XXX Should we be checking mUserFontSet->mLocalRulesUsed like
  710. // InsertRuleFontFace does?
  711. RefPtr<gfxUserFontEntry> entry =
  712. FindOrCreateUserFontEntryFromFontFace(fontfamily, aFontFace,
  713. SheetType::Doc);
  714. if (!entry) {
  715. return;
  716. }
  717. aFontFace->SetUserFontEntry(entry);
  718. }
  719. aFontSetModified = true;
  720. mUserFontSet->AddUserFontEntry(fontfamily, aFontFace->GetUserFontEntry());
  721. }
  722. void
  723. FontFaceSet::InsertRuleFontFace(FontFace* aFontFace, SheetType aSheetType,
  724. nsTArray<FontFaceRecord>& aOldRecords,
  725. bool& aFontSetModified)
  726. {
  727. nsAutoString fontfamily;
  728. if (!aFontFace->GetFamilyName(fontfamily)) {
  729. // If there is no family name, this rule cannot contribute a
  730. // usable font, so there is no point in processing it further.
  731. return;
  732. }
  733. bool remove = false;
  734. size_t removeIndex;
  735. // This is a rule backed FontFace. First, we check in aOldRecords; if
  736. // the FontFace for the rule exists there, just move it to the new record
  737. // list, and put the entry into the appropriate family.
  738. for (size_t i = 0; i < aOldRecords.Length(); ++i) {
  739. FontFaceRecord& rec = aOldRecords[i];
  740. if (rec.mFontFace == aFontFace &&
  741. rec.mSheetType == aSheetType) {
  742. // if local rules were used, don't use the old font entry
  743. // for rules containing src local usage
  744. if (mUserFontSet->mLocalRulesUsed &&
  745. mUserFontSet->mRebuildLocalRules) {
  746. nsCSSValue val;
  747. aFontFace->GetDesc(eCSSFontDesc_Src, val);
  748. nsCSSUnit unit = val.GetUnit();
  749. if (unit == eCSSUnit_Array && HasLocalSrc(val.GetArrayValue())) {
  750. // Remove the old record, but wait to see if we successfully create a
  751. // new user font entry below.
  752. remove = true;
  753. removeIndex = i;
  754. break;
  755. }
  756. }
  757. gfxUserFontEntry* entry = rec.mFontFace->GetUserFontEntry();
  758. MOZ_ASSERT(entry, "FontFace should have a gfxUserFontEntry by now");
  759. mUserFontSet->AddUserFontEntry(fontfamily, entry);
  760. MOZ_ASSERT(!HasRuleFontFace(rec.mFontFace),
  761. "FontFace should not occur in mRuleFaces twice");
  762. mRuleFaces.AppendElement(rec);
  763. aOldRecords.RemoveElementAt(i);
  764. // note the set has been modified if an old rule was skipped to find
  765. // this one - something has been dropped, or ordering changed
  766. if (i > 0) {
  767. aFontSetModified = true;
  768. }
  769. return;
  770. }
  771. }
  772. // this is a new rule:
  773. RefPtr<gfxUserFontEntry> entry =
  774. FindOrCreateUserFontEntryFromFontFace(fontfamily, aFontFace, aSheetType);
  775. if (!entry) {
  776. return;
  777. }
  778. if (remove) {
  779. // Although we broke out of the aOldRecords loop above, since we found
  780. // src local usage, and we're not using the old user font entry, we still
  781. // are adding a record to mRuleFaces with the same FontFace object.
  782. // Remove the old record so that we don't have the same FontFace listed
  783. // in both mRuleFaces and oldRecords, which would cause us to call
  784. // DisconnectFromRule on a FontFace that should still be rule backed.
  785. aOldRecords.RemoveElementAt(removeIndex);
  786. }
  787. FontFaceRecord rec;
  788. rec.mFontFace = aFontFace;
  789. rec.mSheetType = aSheetType;
  790. rec.mLoadEventShouldFire =
  791. aFontFace->Status() == FontFaceLoadStatus::Unloaded ||
  792. aFontFace->Status() == FontFaceLoadStatus::Loading;
  793. aFontFace->SetUserFontEntry(entry);
  794. MOZ_ASSERT(!HasRuleFontFace(aFontFace),
  795. "FontFace should not occur in mRuleFaces twice");
  796. mRuleFaces.AppendElement(rec);
  797. // this was a new rule and font entry, so note that the set was modified
  798. aFontSetModified = true;
  799. // Add the entry to the end of the list. If an existing userfont entry was
  800. // returned by FindOrCreateUserFontEntryFromFontFace that was already stored
  801. // on the family, gfxUserFontFamily::AddFontEntry(), which AddUserFontEntry
  802. // calls, will automatically remove the earlier occurrence of the same
  803. // userfont entry.
  804. mUserFontSet->AddUserFontEntry(fontfamily, entry);
  805. }
  806. /* static */ already_AddRefed<gfxUserFontEntry>
  807. FontFaceSet::FindOrCreateUserFontEntryFromFontFace(FontFace* aFontFace)
  808. {
  809. nsAutoString fontfamily;
  810. if (!aFontFace->GetFamilyName(fontfamily)) {
  811. // If there is no family name, this rule cannot contribute a
  812. // usable font, so there is no point in processing it further.
  813. return nullptr;
  814. }
  815. return FindOrCreateUserFontEntryFromFontFace(fontfamily, aFontFace,
  816. SheetType::Doc);
  817. }
  818. /* static */ already_AddRefed<gfxUserFontEntry>
  819. FontFaceSet::FindOrCreateUserFontEntryFromFontFace(const nsAString& aFamilyName,
  820. FontFace* aFontFace,
  821. SheetType aSheetType)
  822. {
  823. FontFaceSet* set = aFontFace->GetPrimaryFontFaceSet();
  824. nsCSSValue val;
  825. nsCSSUnit unit;
  826. uint32_t weight = NS_STYLE_FONT_WEIGHT_NORMAL;
  827. int32_t stretch = NS_STYLE_FONT_STRETCH_NORMAL;
  828. uint8_t italicStyle = NS_STYLE_FONT_STYLE_NORMAL;
  829. uint32_t languageOverride = NO_FONT_LANGUAGE_OVERRIDE;
  830. uint8_t fontDisplay = NS_FONT_DISPLAY_AUTO;
  831. // set up weight
  832. aFontFace->GetDesc(eCSSFontDesc_Weight, val);
  833. unit = val.GetUnit();
  834. if (unit == eCSSUnit_Integer || unit == eCSSUnit_Enumerated) {
  835. weight = val.GetIntValue();
  836. if (weight == 0) {
  837. weight = NS_STYLE_FONT_WEIGHT_NORMAL;
  838. }
  839. } else if (unit == eCSSUnit_Normal) {
  840. weight = NS_STYLE_FONT_WEIGHT_NORMAL;
  841. } else {
  842. NS_ASSERTION(unit == eCSSUnit_Null,
  843. "@font-face weight has unexpected unit");
  844. }
  845. // set up stretch
  846. aFontFace->GetDesc(eCSSFontDesc_Stretch, val);
  847. unit = val.GetUnit();
  848. if (unit == eCSSUnit_Enumerated) {
  849. stretch = val.GetIntValue();
  850. } else if (unit == eCSSUnit_Normal) {
  851. stretch = NS_STYLE_FONT_STRETCH_NORMAL;
  852. } else {
  853. NS_ASSERTION(unit == eCSSUnit_Null,
  854. "@font-face stretch has unexpected unit");
  855. }
  856. // set up font style
  857. aFontFace->GetDesc(eCSSFontDesc_Style, val);
  858. unit = val.GetUnit();
  859. if (unit == eCSSUnit_Enumerated) {
  860. italicStyle = val.GetIntValue();
  861. } else if (unit == eCSSUnit_Normal) {
  862. italicStyle = NS_STYLE_FONT_STYLE_NORMAL;
  863. } else {
  864. NS_ASSERTION(unit == eCSSUnit_Null,
  865. "@font-face style has unexpected unit");
  866. }
  867. // set up font display
  868. aFontFace->GetDesc(eCSSFontDesc_Display, val);
  869. unit = val.GetUnit();
  870. if (unit == eCSSUnit_Enumerated) {
  871. fontDisplay = val.GetIntValue();
  872. } else {
  873. NS_ASSERTION(unit == eCSSUnit_Null,
  874. "@font-face style has unexpected unit");
  875. }
  876. // set up font features
  877. nsTArray<gfxFontFeature> featureSettings;
  878. aFontFace->GetDesc(eCSSFontDesc_FontFeatureSettings, val);
  879. unit = val.GetUnit();
  880. if (unit == eCSSUnit_Normal) {
  881. // empty list of features
  882. } else if (unit == eCSSUnit_PairList || unit == eCSSUnit_PairListDep) {
  883. nsRuleNode::ComputeFontFeatures(val.GetPairListValue(), featureSettings);
  884. } else {
  885. NS_ASSERTION(unit == eCSSUnit_Null,
  886. "@font-face font-feature-settings has unexpected unit");
  887. }
  888. // set up font language override
  889. aFontFace->GetDesc(eCSSFontDesc_FontLanguageOverride, val);
  890. unit = val.GetUnit();
  891. if (unit == eCSSUnit_Normal) {
  892. // empty feature string
  893. } else if (unit == eCSSUnit_String) {
  894. nsString stringValue;
  895. val.GetStringValue(stringValue);
  896. languageOverride = gfxFontStyle::ParseFontLanguageOverride(stringValue);
  897. } else {
  898. NS_ASSERTION(unit == eCSSUnit_Null,
  899. "@font-face font-language-override has unexpected unit");
  900. }
  901. // set up unicode-range
  902. nsAutoPtr<gfxCharacterMap> unicodeRanges;
  903. aFontFace->GetDesc(eCSSFontDesc_UnicodeRange, val);
  904. unit = val.GetUnit();
  905. if (unit == eCSSUnit_Array) {
  906. unicodeRanges = new gfxCharacterMap();
  907. const nsCSSValue::Array& sources = *val.GetArrayValue();
  908. MOZ_ASSERT(sources.Count() % 2 == 0,
  909. "odd number of entries in a unicode-range: array");
  910. for (uint32_t i = 0; i < sources.Count(); i += 2) {
  911. uint32_t min = sources[i].GetIntValue();
  912. uint32_t max = sources[i+1].GetIntValue();
  913. unicodeRanges->SetRange(min, max);
  914. }
  915. }
  916. // set up src array
  917. nsTArray<gfxFontFaceSrc> srcArray;
  918. if (aFontFace->HasFontData()) {
  919. gfxFontFaceSrc* face = srcArray.AppendElement();
  920. if (!face)
  921. return nullptr;
  922. face->mSourceType = gfxFontFaceSrc::eSourceType_Buffer;
  923. face->mBuffer = aFontFace->CreateBufferSource();
  924. } else {
  925. aFontFace->GetDesc(eCSSFontDesc_Src, val);
  926. unit = val.GetUnit();
  927. if (unit == eCSSUnit_Array) {
  928. nsCSSValue::Array* srcArr = val.GetArrayValue();
  929. size_t numSrc = srcArr->Count();
  930. for (size_t i = 0; i < numSrc; i++) {
  931. val = srcArr->Item(i);
  932. unit = val.GetUnit();
  933. gfxFontFaceSrc* face = srcArray.AppendElements(1);
  934. if (!face)
  935. return nullptr;
  936. switch (unit) {
  937. case eCSSUnit_Local_Font:
  938. val.GetStringValue(face->mLocalName);
  939. face->mSourceType = gfxFontFaceSrc::eSourceType_Local;
  940. face->mURI = nullptr;
  941. face->mFormatFlags = 0;
  942. break;
  943. case eCSSUnit_URL:
  944. face->mSourceType = gfxFontFaceSrc::eSourceType_URL;
  945. face->mURI = val.GetURLValue();
  946. face->mReferrer = val.GetURLStructValue()->mReferrer;
  947. face->mReferrerPolicy = set->mDocument->GetReferrerPolicy();
  948. face->mOriginPrincipal = val.GetURLStructValue()->mOriginPrincipal;
  949. NS_ASSERTION(face->mOriginPrincipal, "null origin principal in @font-face rule");
  950. // agent and user stylesheets are treated slightly differently,
  951. // the same-site origin check and access control headers are
  952. // enforced against the sheet principal rather than the document
  953. // principal to allow user stylesheets to include @font-face rules
  954. face->mUseOriginPrincipal = (aSheetType == SheetType::User ||
  955. aSheetType == SheetType::Agent);
  956. face->mLocalName.Truncate();
  957. face->mFormatFlags = 0;
  958. while (i + 1 < numSrc && (val = srcArr->Item(i+1),
  959. val.GetUnit() == eCSSUnit_Font_Format)) {
  960. nsDependentString valueString(val.GetStringBufferValue());
  961. if (valueString.LowerCaseEqualsASCII("woff")) {
  962. face->mFormatFlags |= gfxUserFontSet::FLAG_FORMAT_WOFF;
  963. } else if (Preferences::GetBool(GFX_PREF_WOFF2_ENABLED) &&
  964. valueString.LowerCaseEqualsASCII("woff2")) {
  965. face->mFormatFlags |= gfxUserFontSet::FLAG_FORMAT_WOFF2;
  966. } else if (valueString.LowerCaseEqualsASCII("opentype")) {
  967. face->mFormatFlags |= gfxUserFontSet::FLAG_FORMAT_OPENTYPE;
  968. } else if (valueString.LowerCaseEqualsASCII("truetype")) {
  969. face->mFormatFlags |= gfxUserFontSet::FLAG_FORMAT_TRUETYPE;
  970. } else if (valueString.LowerCaseEqualsASCII("truetype-aat")) {
  971. face->mFormatFlags |= gfxUserFontSet::FLAG_FORMAT_TRUETYPE_AAT;
  972. } else if (valueString.LowerCaseEqualsASCII("embedded-opentype")) {
  973. face->mFormatFlags |= gfxUserFontSet::FLAG_FORMAT_EOT;
  974. } else if (valueString.LowerCaseEqualsASCII("svg")) {
  975. face->mFormatFlags |= gfxUserFontSet::FLAG_FORMAT_SVG;
  976. } else {
  977. // unknown format specified, mark to distinguish from the
  978. // case where no format hints are specified
  979. face->mFormatFlags |= gfxUserFontSet::FLAG_FORMAT_UNKNOWN;
  980. }
  981. i++;
  982. }
  983. if (!face->mURI) {
  984. // if URI not valid, omit from src array
  985. srcArray.RemoveElementAt(srcArray.Length() - 1);
  986. NS_WARNING("null url in @font-face rule");
  987. continue;
  988. }
  989. break;
  990. default:
  991. NS_ASSERTION(unit == eCSSUnit_Local_Font || unit == eCSSUnit_URL,
  992. "strange unit type in font-face src array");
  993. break;
  994. }
  995. }
  996. } else {
  997. NS_ASSERTION(unit == eCSSUnit_Null, "@font-face src has unexpected unit");
  998. }
  999. }
  1000. if (srcArray.IsEmpty()) {
  1001. return nullptr;
  1002. }
  1003. RefPtr<gfxUserFontEntry> entry =
  1004. set->mUserFontSet->FindOrCreateUserFontEntry(aFamilyName, srcArray, weight,
  1005. stretch, italicStyle,
  1006. featureSettings,
  1007. languageOverride,
  1008. unicodeRanges, fontDisplay);
  1009. return entry.forget();
  1010. }
  1011. nsCSSFontFaceRule*
  1012. FontFaceSet::FindRuleForEntry(gfxFontEntry* aFontEntry)
  1013. {
  1014. NS_ASSERTION(!aFontEntry->mIsUserFontContainer, "only platform font entries");
  1015. for (uint32_t i = 0; i < mRuleFaces.Length(); ++i) {
  1016. FontFace* f = mRuleFaces[i].mFontFace;
  1017. gfxUserFontEntry* entry = f->GetUserFontEntry();
  1018. if (entry && entry->GetPlatformFontEntry() == aFontEntry) {
  1019. return f->GetRule();
  1020. }
  1021. }
  1022. return nullptr;
  1023. }
  1024. nsCSSFontFaceRule*
  1025. FontFaceSet::FindRuleForUserFontEntry(gfxUserFontEntry* aUserFontEntry)
  1026. {
  1027. for (uint32_t i = 0; i < mRuleFaces.Length(); ++i) {
  1028. FontFace* f = mRuleFaces[i].mFontFace;
  1029. if (f->GetUserFontEntry() == aUserFontEntry) {
  1030. return f->GetRule();
  1031. }
  1032. }
  1033. return nullptr;
  1034. }
  1035. nsresult
  1036. FontFaceSet::LogMessage(gfxUserFontEntry* aUserFontEntry,
  1037. const char* aMessage,
  1038. uint32_t aFlags,
  1039. nsresult aStatus)
  1040. {
  1041. nsCOMPtr<nsIConsoleService>
  1042. console(do_GetService(NS_CONSOLESERVICE_CONTRACTID));
  1043. if (!console) {
  1044. return NS_ERROR_NOT_AVAILABLE;
  1045. }
  1046. nsAutoCString familyName;
  1047. nsAutoCString fontURI;
  1048. aUserFontEntry->GetFamilyNameAndURIForLogging(familyName, fontURI);
  1049. char weightKeywordBuf[8]; // plenty to sprintf() a uint16_t
  1050. const char* weightKeyword;
  1051. const nsAFlatCString& weightKeywordString =
  1052. nsCSSProps::ValueToKeyword(aUserFontEntry->Weight(),
  1053. nsCSSProps::kFontWeightKTable);
  1054. if (weightKeywordString.Length() > 0) {
  1055. weightKeyword = weightKeywordString.get();
  1056. } else {
  1057. SprintfLiteral(weightKeywordBuf, "%u", aUserFontEntry->Weight());
  1058. weightKeyword = weightKeywordBuf;
  1059. }
  1060. nsPrintfCString message
  1061. ("downloadable font: %s "
  1062. "(font-family: \"%s\" style:%s weight:%s stretch:%s src index:%d)",
  1063. aMessage,
  1064. familyName.get(),
  1065. aUserFontEntry->IsItalic() ? "italic" : "normal",
  1066. weightKeyword,
  1067. nsCSSProps::ValueToKeyword(aUserFontEntry->Stretch(),
  1068. nsCSSProps::kFontStretchKTable).get(),
  1069. aUserFontEntry->GetSrcIndex());
  1070. if (NS_FAILED(aStatus)) {
  1071. message.AppendLiteral(": ");
  1072. switch (aStatus) {
  1073. case NS_ERROR_DOM_BAD_URI:
  1074. message.AppendLiteral("bad URI or cross-site access not allowed");
  1075. break;
  1076. case NS_ERROR_CONTENT_BLOCKED:
  1077. message.AppendLiteral("content blocked");
  1078. break;
  1079. default:
  1080. message.AppendLiteral("status=");
  1081. message.AppendInt(static_cast<uint32_t>(aStatus));
  1082. break;
  1083. }
  1084. }
  1085. message.AppendLiteral(" source: ");
  1086. message.Append(fontURI);
  1087. if (LOG_ENABLED()) {
  1088. LOG(("userfonts (%p) %s", mUserFontSet.get(), message.get()));
  1089. }
  1090. // try to give the user an indication of where the rule came from
  1091. nsCSSFontFaceRule* rule = FindRuleForUserFontEntry(aUserFontEntry);
  1092. nsString href;
  1093. nsString text;
  1094. nsresult rv;
  1095. uint32_t line = 0;
  1096. uint32_t column = 0;
  1097. if (rule) {
  1098. rv = rule->GetCssText(text);
  1099. NS_ENSURE_SUCCESS(rv, rv);
  1100. CSSStyleSheet* sheet = rule->GetStyleSheet();
  1101. // if the style sheet is removed while the font is loading can be null
  1102. if (sheet) {
  1103. nsCString spec = sheet->GetSheetURI()->GetSpecOrDefault();
  1104. CopyUTF8toUTF16(spec, href);
  1105. } else {
  1106. NS_WARNING("null parent stylesheet for @font-face rule");
  1107. href.AssignLiteral("unknown");
  1108. }
  1109. line = rule->GetLineNumber();
  1110. column = rule->GetColumnNumber();
  1111. }
  1112. nsCOMPtr<nsIScriptError> scriptError =
  1113. do_CreateInstance(NS_SCRIPTERROR_CONTRACTID, &rv);
  1114. NS_ENSURE_SUCCESS(rv, rv);
  1115. uint64_t innerWindowID = mDocument->InnerWindowID();
  1116. rv = scriptError->InitWithWindowID(NS_ConvertUTF8toUTF16(message),
  1117. href, // file
  1118. text, // src line
  1119. line,
  1120. column,
  1121. aFlags, // flags
  1122. "CSS Loader", // category (make separate?)
  1123. innerWindowID);
  1124. if (NS_SUCCEEDED(rv)) {
  1125. console->LogMessage(scriptError);
  1126. }
  1127. return NS_OK;
  1128. }
  1129. nsresult
  1130. FontFaceSet::CheckFontLoad(const gfxFontFaceSrc* aFontFaceSrc,
  1131. nsIPrincipal** aPrincipal,
  1132. bool* aBypassCache)
  1133. {
  1134. NS_ASSERTION(aFontFaceSrc &&
  1135. aFontFaceSrc->mSourceType == gfxFontFaceSrc::eSourceType_URL,
  1136. "bad font face url passed to fontloader");
  1137. // check same-site origin
  1138. NS_ASSERTION(aFontFaceSrc->mURI, "null font uri");
  1139. if (!aFontFaceSrc->mURI)
  1140. return NS_ERROR_FAILURE;
  1141. // use document principal, original principal if flag set
  1142. // this enables user stylesheets to load font files via
  1143. // @font-face rules
  1144. *aPrincipal = mDocument->NodePrincipal();
  1145. NS_ASSERTION(aFontFaceSrc->mOriginPrincipal,
  1146. "null origin principal in @font-face rule");
  1147. if (aFontFaceSrc->mUseOriginPrincipal) {
  1148. *aPrincipal = aFontFaceSrc->mOriginPrincipal;
  1149. }
  1150. *aBypassCache = false;
  1151. nsCOMPtr<nsIDocShell> docShell = mDocument->GetDocShell();
  1152. if (docShell) {
  1153. uint32_t loadType;
  1154. if (NS_SUCCEEDED(docShell->GetLoadType(&loadType))) {
  1155. if ((loadType >> 16) & nsIWebNavigation::LOAD_FLAGS_BYPASS_CACHE) {
  1156. *aBypassCache = true;
  1157. }
  1158. }
  1159. uint32_t flags;
  1160. if (NS_SUCCEEDED(docShell->GetDefaultLoadFlags(&flags))) {
  1161. if (flags & nsIRequest::LOAD_BYPASS_CACHE) {
  1162. *aBypassCache = true;
  1163. }
  1164. }
  1165. }
  1166. return NS_OK;
  1167. }
  1168. // @arg aPrincipal: generally this is mDocument->NodePrincipal() but
  1169. // might also be the original principal which enables user stylesheets
  1170. // to load font files via @font-face rules.
  1171. bool
  1172. FontFaceSet::IsFontLoadAllowed(nsIURI* aFontLocation, nsIPrincipal* aPrincipal)
  1173. {
  1174. int16_t shouldLoad = nsIContentPolicy::ACCEPT;
  1175. nsresult rv = NS_CheckContentLoadPolicy(nsIContentPolicy::TYPE_FONT,
  1176. aFontLocation,
  1177. aPrincipal,
  1178. mDocument,
  1179. EmptyCString(), // mime type
  1180. nullptr, // aExtra
  1181. &shouldLoad,
  1182. nsContentUtils::GetContentPolicy(),
  1183. nsContentUtils::GetSecurityManager());
  1184. return NS_SUCCEEDED(rv) && NS_CP_ACCEPTED(shouldLoad);
  1185. }
  1186. nsresult
  1187. FontFaceSet::SyncLoadFontData(gfxUserFontEntry* aFontToLoad,
  1188. const gfxFontFaceSrc* aFontFaceSrc,
  1189. uint8_t*& aBuffer,
  1190. uint32_t& aBufferLength)
  1191. {
  1192. nsresult rv;
  1193. nsCOMPtr<nsIChannel> channel;
  1194. // Note we are calling NS_NewChannelWithTriggeringPrincipal() with both a
  1195. // node and a principal. This is because the document where the font is
  1196. // being loaded might have a different origin from the principal of the
  1197. // stylesheet that initiated the font load.
  1198. // Further, we only get here for data: loads, so it doesn't really matter
  1199. // whether we use SEC_ALLOW_CROSS_ORIGIN_DATA_INHERITS or not, to be more
  1200. // restrictive we use SEC_REQUIRE_SAME_ORIGIN_DATA_INHERITS.
  1201. rv = NS_NewChannelWithTriggeringPrincipal(getter_AddRefs(channel),
  1202. aFontFaceSrc->mURI,
  1203. mDocument,
  1204. aFontToLoad->GetPrincipal(),
  1205. nsILoadInfo::SEC_REQUIRE_SAME_ORIGIN_DATA_INHERITS,
  1206. nsIContentPolicy::TYPE_FONT);
  1207. NS_ENSURE_SUCCESS(rv, rv);
  1208. // blocking stream is OK for data URIs
  1209. nsCOMPtr<nsIInputStream> stream;
  1210. rv = channel->Open2(getter_AddRefs(stream));
  1211. NS_ENSURE_SUCCESS(rv, rv);
  1212. uint64_t bufferLength64;
  1213. rv = stream->Available(&bufferLength64);
  1214. NS_ENSURE_SUCCESS(rv, rv);
  1215. if (bufferLength64 == 0) {
  1216. return NS_ERROR_FAILURE;
  1217. }
  1218. if (bufferLength64 > UINT32_MAX) {
  1219. return NS_ERROR_FILE_TOO_BIG;
  1220. }
  1221. aBufferLength = static_cast<uint32_t>(bufferLength64);
  1222. // read all the decoded data
  1223. aBuffer = static_cast<uint8_t*> (moz_xmalloc(sizeof(uint8_t) * aBufferLength));
  1224. if (!aBuffer) {
  1225. aBufferLength = 0;
  1226. return NS_ERROR_OUT_OF_MEMORY;
  1227. }
  1228. uint32_t numRead, totalRead = 0;
  1229. while (NS_SUCCEEDED(rv =
  1230. stream->Read(reinterpret_cast<char*>(aBuffer + totalRead),
  1231. aBufferLength - totalRead, &numRead)) &&
  1232. numRead != 0)
  1233. {
  1234. totalRead += numRead;
  1235. if (totalRead > aBufferLength) {
  1236. rv = NS_ERROR_FAILURE;
  1237. break;
  1238. }
  1239. }
  1240. // make sure there's a mime type
  1241. if (NS_SUCCEEDED(rv)) {
  1242. nsAutoCString mimeType;
  1243. rv = channel->GetContentType(mimeType);
  1244. aBufferLength = totalRead;
  1245. }
  1246. if (NS_FAILED(rv)) {
  1247. free(aBuffer);
  1248. aBuffer = nullptr;
  1249. aBufferLength = 0;
  1250. return rv;
  1251. }
  1252. return NS_OK;
  1253. }
  1254. bool
  1255. FontFaceSet::GetPrivateBrowsing()
  1256. {
  1257. nsCOMPtr<nsILoadContext> loadContext = mDocument->GetLoadContext();
  1258. return loadContext && loadContext->UsePrivateBrowsing();
  1259. }
  1260. void
  1261. FontFaceSet::OnFontFaceStatusChanged(FontFace* aFontFace)
  1262. {
  1263. MOZ_ASSERT(HasAvailableFontFace(aFontFace));
  1264. mHasLoadingFontFacesIsDirty = true;
  1265. if (aFontFace->Status() == FontFaceLoadStatus::Loading) {
  1266. CheckLoadingStarted();
  1267. } else {
  1268. MOZ_ASSERT(aFontFace->Status() == FontFaceLoadStatus::Loaded ||
  1269. aFontFace->Status() == FontFaceLoadStatus::Error);
  1270. // When a font finishes downloading, nsPresContext::UserFontSetUpdated
  1271. // will be called immediately afterwards to request a reflow of the
  1272. // relevant elements in the document. We want to wait until the reflow
  1273. // request has been done before the FontFaceSet is marked as Loaded so
  1274. // that we don't briefly set the FontFaceSet to Loaded and then Loading
  1275. // again once the reflow is pending. So we go around the event loop
  1276. // and call CheckLoadingFinished() after the reflow has been queued.
  1277. if (!mDelayedLoadCheck) {
  1278. mDelayedLoadCheck = true;
  1279. nsCOMPtr<nsIRunnable> checkTask =
  1280. NewRunnableMethod(this, &FontFaceSet::CheckLoadingFinishedAfterDelay);
  1281. NS_DispatchToMainThread(checkTask);
  1282. }
  1283. }
  1284. }
  1285. void
  1286. FontFaceSet::DidRefresh()
  1287. {
  1288. CheckLoadingFinished();
  1289. }
  1290. void
  1291. FontFaceSet::CheckLoadingFinishedAfterDelay()
  1292. {
  1293. mDelayedLoadCheck = false;
  1294. CheckLoadingFinished();
  1295. }
  1296. void
  1297. FontFaceSet::CheckLoadingStarted()
  1298. {
  1299. if (!HasLoadingFontFaces()) {
  1300. return;
  1301. }
  1302. if (mStatus == FontFaceSetLoadStatus::Loading) {
  1303. // We have already dispatched a loading event and replaced mReady
  1304. // with a fresh, unresolved promise.
  1305. return;
  1306. }
  1307. mStatus = FontFaceSetLoadStatus::Loading;
  1308. (new AsyncEventDispatcher(this, NS_LITERAL_STRING("loading"),
  1309. false))->PostDOMEvent();
  1310. if (PrefEnabled()) {
  1311. if (mReady) {
  1312. if (GetParentObject()) {
  1313. ErrorResult rv;
  1314. mReady = Promise::Create(GetParentObject(), rv);
  1315. }
  1316. }
  1317. if (!mReady) {
  1318. mResolveLazilyCreatedReadyPromise = false;
  1319. }
  1320. }
  1321. }
  1322. void
  1323. FontFaceSet::UpdateHasLoadingFontFaces()
  1324. {
  1325. mHasLoadingFontFacesIsDirty = false;
  1326. mHasLoadingFontFaces = false;
  1327. for (size_t i = 0; i < mRuleFaces.Length(); i++) {
  1328. FontFace* f = mRuleFaces[i].mFontFace;
  1329. if (f->Status() == FontFaceLoadStatus::Loading) {
  1330. mHasLoadingFontFaces = true;
  1331. return;
  1332. }
  1333. }
  1334. for (size_t i = 0; i < mNonRuleFaces.Length(); i++) {
  1335. if (mNonRuleFaces[i].mFontFace->Status() == FontFaceLoadStatus::Loading) {
  1336. mHasLoadingFontFaces = true;
  1337. return;
  1338. }
  1339. }
  1340. }
  1341. bool
  1342. FontFaceSet::HasLoadingFontFaces()
  1343. {
  1344. if (mHasLoadingFontFacesIsDirty) {
  1345. UpdateHasLoadingFontFaces();
  1346. }
  1347. return mHasLoadingFontFaces;
  1348. }
  1349. bool
  1350. FontFaceSet::MightHavePendingFontLoads()
  1351. {
  1352. // Check for FontFace objects in the FontFaceSet that are still loading.
  1353. if (HasLoadingFontFaces()) {
  1354. return true;
  1355. }
  1356. // Check for pending restyles or reflows, as they might cause fonts to
  1357. // load as new styles apply and text runs are rebuilt.
  1358. nsPresContext* presContext = GetPresContext();
  1359. if (presContext && presContext->HasPendingRestyleOrReflow()) {
  1360. return true;
  1361. }
  1362. if (mDocument) {
  1363. // We defer resolving mReady until the document as fully loaded.
  1364. if (!mDocument->DidFireDOMContentLoaded()) {
  1365. return true;
  1366. }
  1367. // And we also wait for any CSS style sheets to finish loading, as their
  1368. // styles might cause new fonts to load.
  1369. if (mDocument->CSSLoader()->HasPendingLoads()) {
  1370. return true;
  1371. }
  1372. }
  1373. return false;
  1374. }
  1375. void
  1376. FontFaceSet::CheckLoadingFinished()
  1377. {
  1378. if (mDelayedLoadCheck) {
  1379. // Wait until the runnable posted in OnFontFaceStatusChanged calls us.
  1380. return;
  1381. }
  1382. if (mStatus == FontFaceSetLoadStatus::Loaded) {
  1383. // We've already resolved mReady and dispatched the loadingdone/loadingerror
  1384. // events.
  1385. return;
  1386. }
  1387. if (MightHavePendingFontLoads()) {
  1388. // We're not finished loading yet.
  1389. return;
  1390. }
  1391. mStatus = FontFaceSetLoadStatus::Loaded;
  1392. if (mReady) {
  1393. mReady->MaybeResolve(this);
  1394. } else {
  1395. mResolveLazilyCreatedReadyPromise = true;
  1396. }
  1397. // Now dispatch the loadingdone/loadingerror events.
  1398. nsTArray<FontFace*> loaded;
  1399. nsTArray<FontFace*> failed;
  1400. for (size_t i = 0; i < mRuleFaces.Length(); i++) {
  1401. if (!mRuleFaces[i].mLoadEventShouldFire) {
  1402. continue;
  1403. }
  1404. FontFace* f = mRuleFaces[i].mFontFace;
  1405. if (f->Status() == FontFaceLoadStatus::Loaded) {
  1406. loaded.AppendElement(f);
  1407. mRuleFaces[i].mLoadEventShouldFire = false;
  1408. } else if (f->Status() == FontFaceLoadStatus::Error) {
  1409. failed.AppendElement(f);
  1410. mRuleFaces[i].mLoadEventShouldFire = false;
  1411. }
  1412. }
  1413. for (size_t i = 0; i < mNonRuleFaces.Length(); i++) {
  1414. if (!mNonRuleFaces[i].mLoadEventShouldFire) {
  1415. continue;
  1416. }
  1417. FontFace* f = mNonRuleFaces[i].mFontFace;
  1418. if (f->Status() == FontFaceLoadStatus::Loaded) {
  1419. loaded.AppendElement(f);
  1420. mNonRuleFaces[i].mLoadEventShouldFire = false;
  1421. } else if (f->Status() == FontFaceLoadStatus::Error) {
  1422. failed.AppendElement(f);
  1423. mNonRuleFaces[i].mLoadEventShouldFire = false;
  1424. }
  1425. }
  1426. DispatchLoadingFinishedEvent(NS_LITERAL_STRING("loadingdone"), loaded);
  1427. if (!failed.IsEmpty()) {
  1428. DispatchLoadingFinishedEvent(NS_LITERAL_STRING("loadingerror"), failed);
  1429. }
  1430. }
  1431. void
  1432. FontFaceSet::DispatchLoadingFinishedEvent(
  1433. const nsAString& aType,
  1434. const nsTArray<FontFace*>& aFontFaces)
  1435. {
  1436. FontFaceSetLoadEventInit init;
  1437. init.mBubbles = false;
  1438. init.mCancelable = false;
  1439. OwningNonNull<FontFace>* elements =
  1440. init.mFontfaces.AppendElements(aFontFaces.Length(), fallible);
  1441. MOZ_ASSERT(elements);
  1442. for (size_t i = 0; i < aFontFaces.Length(); i++) {
  1443. elements[i] = aFontFaces[i];
  1444. }
  1445. RefPtr<FontFaceSetLoadEvent> event =
  1446. FontFaceSetLoadEvent::Constructor(this, aType, init);
  1447. (new AsyncEventDispatcher(this, event))->PostDOMEvent();
  1448. }
  1449. // nsIDOMEventListener
  1450. NS_IMETHODIMP
  1451. FontFaceSet::HandleEvent(nsIDOMEvent* aEvent)
  1452. {
  1453. nsString type;
  1454. aEvent->GetType(type);
  1455. if (!type.EqualsLiteral("DOMContentLoaded")) {
  1456. return NS_ERROR_FAILURE;
  1457. }
  1458. RemoveDOMContentLoadedListener();
  1459. CheckLoadingFinished();
  1460. return NS_OK;
  1461. }
  1462. /* static */ bool
  1463. FontFaceSet::PrefEnabled()
  1464. {
  1465. static bool initialized = false;
  1466. static bool enabled;
  1467. if (!initialized) {
  1468. initialized = true;
  1469. Preferences::AddBoolVarCache(&enabled, FONT_LOADING_API_ENABLED_PREF);
  1470. }
  1471. return enabled;
  1472. }
  1473. // nsICSSLoaderObserver
  1474. NS_IMETHODIMP
  1475. FontFaceSet::StyleSheetLoaded(StyleSheet* aSheet,
  1476. bool aWasAlternate,
  1477. nsresult aStatus)
  1478. {
  1479. CheckLoadingFinished();
  1480. return NS_OK;
  1481. }
  1482. void
  1483. FontFaceSet::FlushUserFontSet()
  1484. {
  1485. if (mDocument) {
  1486. mDocument->FlushUserFontSet();
  1487. }
  1488. }
  1489. void
  1490. FontFaceSet::RebuildUserFontSet()
  1491. {
  1492. if (mDocument) {
  1493. mDocument->RebuildUserFontSet();
  1494. }
  1495. }
  1496. nsPresContext*
  1497. FontFaceSet::GetPresContext()
  1498. {
  1499. if (!mDocument) {
  1500. return nullptr;
  1501. }
  1502. nsIPresShell* shell = mDocument->GetShell();
  1503. return shell ? shell->GetPresContext() : nullptr;
  1504. }
  1505. // -- FontFaceSet::UserFontSet ------------------------------------------------
  1506. /* virtual */ nsresult
  1507. FontFaceSet::UserFontSet::CheckFontLoad(const gfxFontFaceSrc* aFontFaceSrc,
  1508. nsIPrincipal** aPrincipal,
  1509. bool* aBypassCache)
  1510. {
  1511. if (!mFontFaceSet) {
  1512. return NS_ERROR_FAILURE;
  1513. }
  1514. return mFontFaceSet->CheckFontLoad(aFontFaceSrc, aPrincipal, aBypassCache);
  1515. }
  1516. /* virtual */ bool
  1517. FontFaceSet::UserFontSet::IsFontLoadAllowed(nsIURI* aFontLocation,
  1518. nsIPrincipal* aPrincipal)
  1519. {
  1520. return mFontFaceSet &&
  1521. mFontFaceSet->IsFontLoadAllowed(aFontLocation, aPrincipal);
  1522. }
  1523. /* virtual */ nsresult
  1524. FontFaceSet::UserFontSet::StartLoad(gfxUserFontEntry* aUserFontEntry,
  1525. const gfxFontFaceSrc* aFontFaceSrc)
  1526. {
  1527. if (!mFontFaceSet) {
  1528. return NS_ERROR_FAILURE;
  1529. }
  1530. return mFontFaceSet->StartLoad(aUserFontEntry, aFontFaceSrc);
  1531. }
  1532. void
  1533. FontFaceSet::UserFontSet::RecordFontLoadDone(uint32_t aFontSize,
  1534. TimeStamp aDoneTime)
  1535. {
  1536. mDownloadCount++;
  1537. mDownloadSize += aFontSize;
  1538. if (!mFontFaceSet) {
  1539. return;
  1540. }
  1541. }
  1542. /* virtual */ nsresult
  1543. FontFaceSet::UserFontSet::LogMessage(gfxUserFontEntry* aUserFontEntry,
  1544. const char* aMessage,
  1545. uint32_t aFlags,
  1546. nsresult aStatus)
  1547. {
  1548. if (!mFontFaceSet) {
  1549. return NS_ERROR_FAILURE;
  1550. }
  1551. return mFontFaceSet->LogMessage(aUserFontEntry, aMessage, aFlags, aStatus);
  1552. }
  1553. /* virtual */ nsresult
  1554. FontFaceSet::UserFontSet::SyncLoadFontData(gfxUserFontEntry* aFontToLoad,
  1555. const gfxFontFaceSrc* aFontFaceSrc,
  1556. uint8_t*& aBuffer,
  1557. uint32_t& aBufferLength)
  1558. {
  1559. if (!mFontFaceSet) {
  1560. return NS_ERROR_FAILURE;
  1561. }
  1562. return mFontFaceSet->SyncLoadFontData(aFontToLoad, aFontFaceSrc,
  1563. aBuffer, aBufferLength);
  1564. }
  1565. /* virtual */ bool
  1566. FontFaceSet::UserFontSet::GetPrivateBrowsing()
  1567. {
  1568. return mFontFaceSet && mFontFaceSet->GetPrivateBrowsing();
  1569. }
  1570. /* virtual */ void
  1571. FontFaceSet::UserFontSet::DoRebuildUserFontSet()
  1572. {
  1573. if (!mFontFaceSet) {
  1574. return;
  1575. }
  1576. mFontFaceSet->RebuildUserFontSet();
  1577. }
  1578. /* virtual */ already_AddRefed<gfxUserFontEntry>
  1579. FontFaceSet::UserFontSet::CreateUserFontEntry(
  1580. const nsTArray<gfxFontFaceSrc>& aFontFaceSrcList,
  1581. uint32_t aWeight,
  1582. int32_t aStretch,
  1583. uint8_t aStyle,
  1584. const nsTArray<gfxFontFeature>& aFeatureSettings,
  1585. uint32_t aLanguageOverride,
  1586. gfxSparseBitSet* aUnicodeRanges,
  1587. uint8_t aFontDisplay)
  1588. {
  1589. RefPtr<gfxUserFontEntry> entry =
  1590. new FontFace::Entry(this, aFontFaceSrcList, aWeight, aStretch, aStyle,
  1591. aFeatureSettings, aLanguageOverride, aUnicodeRanges,
  1592. aFontDisplay);
  1593. return entry.forget();
  1594. }
  1595. #undef LOG_ENABLED
  1596. #undef LOG