nsFontFaceLoader.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. // vim:cindent:ts=2:et:sw=2:
  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
  5. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  6. /* code for loading in @font-face defined font data */
  7. #include "mozilla/Logging.h"
  8. #include "nsFontFaceLoader.h"
  9. #include "nsError.h"
  10. #include "nsContentUtils.h"
  11. #include "mozilla/Preferences.h"
  12. #include "mozilla/Telemetry.h"
  13. #include "FontFaceSet.h"
  14. #include "nsPresContext.h"
  15. #include "nsIPrincipal.h"
  16. #include "nsIScriptSecurityManager.h"
  17. #include "nsIHttpChannel.h"
  18. #include "nsIContentPolicy.h"
  19. #include "nsContentPolicyUtils.h"
  20. #include "mozilla/gfx/2D.h"
  21. using namespace mozilla;
  22. using namespace mozilla::dom;
  23. #define LOG(args) MOZ_LOG(gfxUserFontSet::GetUserFontsLog(), mozilla::LogLevel::Debug, args)
  24. #define LOG_ENABLED() MOZ_LOG_TEST(gfxUserFontSet::GetUserFontsLog(), \
  25. LogLevel::Debug)
  26. static uint32_t
  27. GetFallbackDelay()
  28. {
  29. return Preferences::GetInt("gfx.downloadable_fonts.fallback_delay", 3000);
  30. }
  31. static uint32_t
  32. GetShortFallbackDelay()
  33. {
  34. return Preferences::GetInt("gfx.downloadable_fonts.fallback_delay_short", 100);
  35. }
  36. nsFontFaceLoader::nsFontFaceLoader(gfxUserFontEntry* aUserFontEntry,
  37. nsIURI* aFontURI,
  38. FontFaceSet* aFontFaceSet,
  39. nsIChannel* aChannel)
  40. : mUserFontEntry(aUserFontEntry),
  41. mFontURI(aFontURI),
  42. mFontFaceSet(aFontFaceSet),
  43. mChannel(aChannel)
  44. {
  45. mStartTime = TimeStamp::Now();
  46. }
  47. nsFontFaceLoader::~nsFontFaceLoader()
  48. {
  49. if (mUserFontEntry) {
  50. mUserFontEntry->mLoader = nullptr;
  51. }
  52. if (mLoadTimer) {
  53. mLoadTimer->Cancel();
  54. mLoadTimer = nullptr;
  55. }
  56. if (mFontFaceSet) {
  57. mFontFaceSet->RemoveLoader(this);
  58. }
  59. }
  60. void
  61. nsFontFaceLoader::StartedLoading(nsIStreamLoader* aStreamLoader)
  62. {
  63. int32_t loadTimeout;
  64. uint8_t fontDisplay = GetFontDisplay();
  65. if (fontDisplay == NS_FONT_DISPLAY_AUTO ||
  66. fontDisplay == NS_FONT_DISPLAY_BLOCK) {
  67. loadTimeout = GetFallbackDelay();
  68. } else {
  69. loadTimeout = GetShortFallbackDelay();
  70. }
  71. if (loadTimeout > 0) {
  72. mLoadTimer = do_CreateInstance("@mozilla.org/timer;1");
  73. if (mLoadTimer) {
  74. mLoadTimer->InitWithFuncCallback(LoadTimerCallback,
  75. static_cast<void*>(this),
  76. loadTimeout,
  77. nsITimer::TYPE_ONE_SHOT);
  78. }
  79. } else {
  80. mUserFontEntry->mFontDataLoadingState = gfxUserFontEntry::LOADING_SLOWLY;
  81. }
  82. mStreamLoader = aStreamLoader;
  83. }
  84. /* static */ void
  85. nsFontFaceLoader::LoadTimerCallback(nsITimer* aTimer, void* aClosure)
  86. {
  87. nsFontFaceLoader* loader = static_cast<nsFontFaceLoader*>(aClosure);
  88. if (!loader->mFontFaceSet) {
  89. // We've been canceled
  90. return;
  91. }
  92. gfxUserFontEntry* ufe = loader->mUserFontEntry.get();
  93. uint8_t fontDisplay = loader->GetFontDisplay();
  94. // Depending upon the value of the font-display descriptor for the font,
  95. // their may be one or two timeouts associated with each font. The LOADING_SLOWLY
  96. // state indicates that the fallback font is shown. The LOADING_TIMED_OUT
  97. // state indicates that the fallback font is shown *and* the downloaded font
  98. // resource will not replace the fallback font when the load completes.
  99. bool updateUserFontSet = true;
  100. switch (fontDisplay) {
  101. case NS_FONT_DISPLAY_AUTO:
  102. case NS_FONT_DISPLAY_BLOCK:
  103. // If the entry is loading, check whether it's >75% done; if so,
  104. // we allow another timeout period before showing a fallback font.
  105. if (ufe->mFontDataLoadingState == gfxUserFontEntry::LOADING_STARTED) {
  106. int64_t contentLength;
  107. uint32_t numBytesRead;
  108. if (NS_SUCCEEDED(loader->mChannel->GetContentLength(&contentLength)) &&
  109. contentLength > 0 &&
  110. contentLength < UINT32_MAX &&
  111. NS_SUCCEEDED(loader->mStreamLoader->GetNumBytesRead(&numBytesRead)) &&
  112. numBytesRead > 3 * (uint32_t(contentLength) >> 2))
  113. {
  114. // More than 3/4 the data has been downloaded, so allow 50% extra
  115. // time and hope the remainder will arrive before the additional
  116. // time expires.
  117. ufe->mFontDataLoadingState = gfxUserFontEntry::LOADING_ALMOST_DONE;
  118. uint32_t delay;
  119. loader->mLoadTimer->GetDelay(&delay);
  120. loader->mLoadTimer->InitWithFuncCallback(LoadTimerCallback,
  121. static_cast<void*>(loader),
  122. delay >> 1,
  123. nsITimer::TYPE_ONE_SHOT);
  124. updateUserFontSet = false;
  125. LOG(("userfonts (%p) 75%% done, resetting timer\n", loader));
  126. }
  127. }
  128. if (updateUserFontSet) {
  129. ufe->mFontDataLoadingState = gfxUserFontEntry::LOADING_SLOWLY;
  130. }
  131. break;
  132. case NS_FONT_DISPLAY_SWAP:
  133. ufe->mFontDataLoadingState = gfxUserFontEntry::LOADING_SLOWLY;
  134. break;
  135. case NS_FONT_DISPLAY_FALLBACK: {
  136. if (ufe->mFontDataLoadingState == gfxUserFontEntry::LOADING_STARTED) {
  137. ufe->mFontDataLoadingState = gfxUserFontEntry::LOADING_SLOWLY;
  138. } else {
  139. ufe->mFontDataLoadingState = gfxUserFontEntry::LOADING_TIMED_OUT;
  140. updateUserFontSet = false;
  141. }
  142. break;
  143. }
  144. case NS_FONT_DISPLAY_OPTIONAL:
  145. ufe->mFontDataLoadingState = gfxUserFontEntry::LOADING_TIMED_OUT;
  146. break;
  147. default:
  148. NS_NOTREACHED("strange font-display value");
  149. break;
  150. }
  151. // If the font is not 75% loaded, or if we've already timed out once
  152. // before, we mark this entry as "loading slowly", so the fallback
  153. // font will be used in the meantime, and tell the context to refresh.
  154. if (updateUserFontSet) {
  155. nsTArray<gfxUserFontSet*> fontSets;
  156. ufe->GetUserFontSets(fontSets);
  157. for (gfxUserFontSet* fontSet : fontSets) {
  158. nsPresContext* ctx = FontFaceSet::GetPresContextFor(fontSet);
  159. if (ctx) {
  160. fontSet->IncrementGeneration();
  161. ctx->UserFontSetUpdated(ufe);
  162. LOG(("userfonts (%p) timeout reflow for pres context %p display %d\n",
  163. loader, ctx, fontDisplay));
  164. }
  165. }
  166. }
  167. }
  168. NS_IMPL_ISUPPORTS(nsFontFaceLoader, nsIStreamLoaderObserver)
  169. NS_IMETHODIMP
  170. nsFontFaceLoader::OnStreamComplete(nsIStreamLoader* aLoader,
  171. nsISupports* aContext,
  172. nsresult aStatus,
  173. uint32_t aStringLen,
  174. const uint8_t* aString)
  175. {
  176. if (!mFontFaceSet) {
  177. // We've been canceled
  178. return aStatus;
  179. }
  180. mFontFaceSet->RemoveLoader(this);
  181. TimeStamp doneTime = TimeStamp::Now();
  182. TimeDuration downloadTime = doneTime - mStartTime;
  183. uint32_t downloadTimeMS = uint32_t(downloadTime.ToMilliseconds());
  184. if (GetFontDisplay() == NS_FONT_DISPLAY_FALLBACK) {
  185. uint32_t loadTimeout = GetFallbackDelay();
  186. if (downloadTimeMS > loadTimeout &&
  187. (mUserFontEntry->mFontDataLoadingState ==
  188. gfxUserFontEntry::LOADING_SLOWLY)) {
  189. mUserFontEntry->mFontDataLoadingState =
  190. gfxUserFontEntry::LOADING_TIMED_OUT;
  191. }
  192. }
  193. if (LOG_ENABLED()) {
  194. if (NS_SUCCEEDED(aStatus)) {
  195. LOG(("userfonts (%p) download completed - font uri: (%s) time: %d ms\n",
  196. this, mFontURI->GetSpecOrDefault().get(), downloadTimeMS));
  197. } else {
  198. LOG(("userfonts (%p) download failed - font uri: (%s) error: %8.8x\n",
  199. this, mFontURI->GetSpecOrDefault().get(), aStatus));
  200. }
  201. }
  202. if (NS_SUCCEEDED(aStatus)) {
  203. // for HTTP requests, check whether the request _actually_ succeeded;
  204. // the "request status" in aStatus does not necessarily indicate this,
  205. // because HTTP responses such as 404 (Not Found) will still result in
  206. // a success code and potentially an HTML error page from the server
  207. // as the resulting data. We don't want to use that as a font.
  208. nsCOMPtr<nsIRequest> request;
  209. nsCOMPtr<nsIHttpChannel> httpChannel;
  210. aLoader->GetRequest(getter_AddRefs(request));
  211. httpChannel = do_QueryInterface(request);
  212. if (httpChannel) {
  213. bool succeeded;
  214. nsresult rv = httpChannel->GetRequestSucceeded(&succeeded);
  215. if (NS_SUCCEEDED(rv) && !succeeded) {
  216. aStatus = NS_ERROR_NOT_AVAILABLE;
  217. }
  218. }
  219. }
  220. // The userFontEntry is responsible for freeing the downloaded data
  221. // (aString) when finished with it; the pointer is no longer valid
  222. // after FontDataDownloadComplete returns.
  223. // This is called even in the case of a failed download (HTTP 404, etc),
  224. // as there may still be data to be freed (e.g. an error page),
  225. // and we need to load the next source.
  226. bool fontUpdate =
  227. mUserFontEntry->FontDataDownloadComplete(aString, aStringLen, aStatus);
  228. mFontFaceSet->GetUserFontSet()->RecordFontLoadDone(aStringLen, doneTime);
  229. // when new font loaded, need to reflow
  230. if (fontUpdate) {
  231. nsTArray<gfxUserFontSet*> fontSets;
  232. mUserFontEntry->GetUserFontSets(fontSets);
  233. for (gfxUserFontSet* fontSet : fontSets) {
  234. nsPresContext* ctx = FontFaceSet::GetPresContextFor(fontSet);
  235. if (ctx) {
  236. // Update layout for the presence of the new font. Since this is
  237. // asynchronous, reflows will coalesce.
  238. ctx->UserFontSetUpdated(mUserFontEntry);
  239. LOG(("userfonts (%p) reflow for pres context %p\n", this, ctx));
  240. }
  241. }
  242. }
  243. // done with font set
  244. mFontFaceSet = nullptr;
  245. if (mLoadTimer) {
  246. mLoadTimer->Cancel();
  247. mLoadTimer = nullptr;
  248. }
  249. return NS_SUCCESS_ADOPTED_DATA;
  250. }
  251. void
  252. nsFontFaceLoader::Cancel()
  253. {
  254. mUserFontEntry->mFontDataLoadingState = gfxUserFontEntry::NOT_LOADING;
  255. mUserFontEntry->mLoader = nullptr;
  256. mFontFaceSet = nullptr;
  257. if (mLoadTimer) {
  258. mLoadTimer->Cancel();
  259. mLoadTimer = nullptr;
  260. }
  261. mChannel->Cancel(NS_BINDING_ABORTED);
  262. }
  263. uint8_t
  264. nsFontFaceLoader::GetFontDisplay()
  265. {
  266. uint8_t fontDisplay = NS_FONT_DISPLAY_AUTO;
  267. if (Preferences::GetBool("layout.css.font-display.enabled")) {
  268. fontDisplay = mUserFontEntry->GetFontDisplay();
  269. }
  270. return fontDisplay;
  271. }