2008-09-30 20:04:10 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
// vim:cindent:ts=2:et:sw=2:
|
2012-05-21 04:12:37 -07:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2008-09-30 20:04:10 -07:00
|
|
|
|
|
|
|
/* code for loading in @font-face defined font data */
|
|
|
|
|
2015-05-19 11:15:34 -07:00
|
|
|
#include "mozilla/Logging.h"
|
2008-09-30 20:04:10 -07:00
|
|
|
|
|
|
|
#include "nsFontFaceLoader.h"
|
|
|
|
|
2008-11-05 16:32:12 -08:00
|
|
|
#include "nsError.h"
|
2008-09-30 20:04:10 -07:00
|
|
|
#include "nsContentUtils.h"
|
2011-06-11 19:30:16 -07:00
|
|
|
#include "mozilla/Preferences.h"
|
2014-10-01 19:32:05 -07:00
|
|
|
#include "FontFaceSet.h"
|
2008-09-30 20:04:10 -07:00
|
|
|
#include "nsPresContext.h"
|
|
|
|
#include "nsIPrincipal.h"
|
|
|
|
#include "nsIScriptSecurityManager.h"
|
2014-10-01 19:32:05 -07:00
|
|
|
#include "nsIHttpChannel.h"
|
2008-11-05 16:32:12 -08:00
|
|
|
#include "nsIContentPolicy.h"
|
|
|
|
#include "nsContentPolicyUtils.h"
|
2008-09-30 20:04:10 -07:00
|
|
|
|
2013-10-07 16:15:59 -07:00
|
|
|
#include "mozilla/gfx/2D.h"
|
2011-04-12 03:53:20 -07:00
|
|
|
|
2011-06-11 19:30:16 -07:00
|
|
|
using namespace mozilla;
|
|
|
|
|
2015-06-03 15:25:57 -07:00
|
|
|
#define LOG(args) MOZ_LOG(gfxUserFontSet::GetUserFontsLog(), mozilla::LogLevel::Debug, args)
|
2015-06-03 15:22:28 -07:00
|
|
|
#define LOG_ENABLED() MOZ_LOG_TEST(gfxUserFontSet::GetUserFontsLog(), \
|
2015-06-03 15:25:57 -07:00
|
|
|
LogLevel::Debug)
|
2008-09-30 20:04:10 -07:00
|
|
|
|
2014-09-08 00:23:20 -07:00
|
|
|
nsFontFaceLoader::nsFontFaceLoader(gfxUserFontEntry* aUserFontEntry,
|
2013-09-25 06:48:20 -07:00
|
|
|
nsIURI* aFontURI,
|
2014-10-01 19:32:05 -07:00
|
|
|
mozilla::dom::FontFaceSet* aFontFaceSet,
|
2013-09-25 06:48:20 -07:00
|
|
|
nsIChannel* aChannel)
|
2014-09-08 00:23:20 -07:00
|
|
|
: mUserFontEntry(aUserFontEntry),
|
2012-12-19 01:42:25 -08:00
|
|
|
mFontURI(aFontURI),
|
2014-10-01 19:32:05 -07:00
|
|
|
mFontFaceSet(aFontFaceSet),
|
2009-01-19 01:21:29 -08:00
|
|
|
mChannel(aChannel)
|
2008-09-30 20:04:10 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsFontFaceLoader::~nsFontFaceLoader()
|
|
|
|
{
|
2014-09-08 00:23:20 -07:00
|
|
|
if (mUserFontEntry) {
|
|
|
|
mUserFontEntry->mLoader = nullptr;
|
2012-04-26 10:50:41 -07:00
|
|
|
}
|
2011-01-05 13:48:48 -08:00
|
|
|
if (mLoadTimer) {
|
|
|
|
mLoadTimer->Cancel();
|
2012-07-30 07:20:58 -07:00
|
|
|
mLoadTimer = nullptr;
|
2011-01-05 13:48:48 -08:00
|
|
|
}
|
2014-10-01 19:32:05 -07:00
|
|
|
if (mFontFaceSet) {
|
|
|
|
mFontFaceSet->RemoveLoader(this);
|
2009-01-19 01:21:29 -08:00
|
|
|
}
|
2008-09-30 20:04:10 -07:00
|
|
|
}
|
|
|
|
|
2011-01-05 13:48:48 -08:00
|
|
|
void
|
2013-09-25 06:48:20 -07:00
|
|
|
nsFontFaceLoader::StartedLoading(nsIStreamLoader* aStreamLoader)
|
2011-01-05 13:48:48 -08:00
|
|
|
{
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t loadTimeout =
|
2011-06-11 19:30:16 -07:00
|
|
|
Preferences::GetInt("gfx.downloadable_fonts.fallback_delay", 3000);
|
2011-01-05 13:48:48 -08:00
|
|
|
if (loadTimeout > 0) {
|
|
|
|
mLoadTimer = do_CreateInstance("@mozilla.org/timer;1");
|
|
|
|
if (mLoadTimer) {
|
|
|
|
mLoadTimer->InitWithFuncCallback(LoadTimerCallback,
|
|
|
|
static_cast<void*>(this),
|
|
|
|
loadTimeout,
|
|
|
|
nsITimer::TYPE_ONE_SHOT);
|
|
|
|
}
|
|
|
|
} else {
|
2014-09-24 20:16:53 -07:00
|
|
|
mUserFontEntry->mFontDataLoadingState = gfxUserFontEntry::LOADING_SLOWLY;
|
2011-01-05 13:48:48 -08:00
|
|
|
}
|
|
|
|
mStreamLoader = aStreamLoader;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-09-25 06:48:20 -07:00
|
|
|
nsFontFaceLoader::LoadTimerCallback(nsITimer* aTimer, void* aClosure)
|
2011-01-05 13:48:48 -08:00
|
|
|
{
|
2013-09-25 06:48:20 -07:00
|
|
|
nsFontFaceLoader* loader = static_cast<nsFontFaceLoader*>(aClosure);
|
2011-01-05 13:48:48 -08:00
|
|
|
|
2014-10-01 19:32:05 -07:00
|
|
|
if (!loader->mFontFaceSet) {
|
2013-03-13 22:24:40 -07:00
|
|
|
// We've been canceled
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-09-08 00:23:20 -07:00
|
|
|
gfxUserFontEntry* ufe = loader->mUserFontEntry.get();
|
2011-01-05 13:48:48 -08:00
|
|
|
bool updateUserFontSet = true;
|
|
|
|
|
|
|
|
// If the entry is loading, check whether it's >75% done; if so,
|
|
|
|
// we allow another timeout period before showing a fallback font.
|
2014-09-24 20:16:53 -07:00
|
|
|
if (ufe->mFontDataLoadingState == gfxUserFontEntry::LOADING_STARTED) {
|
2012-10-22 10:51:07 -07:00
|
|
|
int64_t contentLength;
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t numBytesRead;
|
2011-08-23 05:33:51 -07:00
|
|
|
if (NS_SUCCEEDED(loader->mChannel->GetContentLength(&contentLength)) &&
|
|
|
|
contentLength > 0 &&
|
2012-10-22 10:51:07 -07:00
|
|
|
contentLength < UINT32_MAX &&
|
2011-08-23 05:33:51 -07:00
|
|
|
NS_SUCCEEDED(loader->mStreamLoader->GetNumBytesRead(&numBytesRead)) &&
|
2012-08-22 08:56:38 -07:00
|
|
|
numBytesRead > 3 * (uint32_t(contentLength) >> 2))
|
2011-01-05 13:48:48 -08:00
|
|
|
{
|
|
|
|
// More than 3/4 the data has been downloaded, so allow 50% extra
|
|
|
|
// time and hope the remainder will arrive before the additional
|
|
|
|
// time expires.
|
2014-09-24 20:16:53 -07:00
|
|
|
ufe->mFontDataLoadingState = gfxUserFontEntry::LOADING_ALMOST_DONE;
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t delay;
|
2011-01-05 13:48:48 -08:00
|
|
|
loader->mLoadTimer->GetDelay(&delay);
|
|
|
|
loader->mLoadTimer->InitWithFuncCallback(LoadTimerCallback,
|
|
|
|
static_cast<void*>(loader),
|
|
|
|
delay >> 1,
|
|
|
|
nsITimer::TYPE_ONE_SHOT);
|
|
|
|
updateUserFontSet = false;
|
2015-01-28 23:44:59 -08:00
|
|
|
LOG(("userfonts (%p) 75%% done, resetting timer\n", loader));
|
2011-01-05 13:48:48 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the font is not 75% loaded, or if we've already timed out once
|
|
|
|
// before, we mark this entry as "loading slowly", so the fallback
|
|
|
|
// font will be used in the meantime, and tell the context to refresh.
|
|
|
|
if (updateUserFontSet) {
|
2014-09-24 20:16:53 -07:00
|
|
|
ufe->mFontDataLoadingState = gfxUserFontEntry::LOADING_SLOWLY;
|
2014-10-01 19:32:05 -07:00
|
|
|
nsPresContext* ctx = loader->mFontFaceSet->GetPresContext();
|
2013-03-13 22:24:40 -07:00
|
|
|
if (ctx) {
|
2014-10-01 19:32:05 -07:00
|
|
|
loader->mFontFaceSet->IncrementGeneration();
|
2015-03-06 00:44:23 -08:00
|
|
|
ctx->UserFontSetUpdated(loader->GetUserFontEntry());
|
2015-01-28 23:44:59 -08:00
|
|
|
LOG(("userfonts (%p) timeout reflow\n", loader));
|
2011-01-05 13:48:48 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-27 00:06:00 -07:00
|
|
|
NS_IMPL_ISUPPORTS(nsFontFaceLoader, nsIStreamLoaderObserver)
|
2008-09-30 20:04:10 -07:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2008-10-29 11:09:50 -07:00
|
|
|
nsFontFaceLoader::OnStreamComplete(nsIStreamLoader* aLoader,
|
|
|
|
nsISupports* aContext,
|
|
|
|
nsresult aStatus,
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t aStringLen,
|
|
|
|
const uint8_t* aString)
|
2008-09-30 20:04:10 -07:00
|
|
|
{
|
2014-10-01 19:32:05 -07:00
|
|
|
if (!mFontFaceSet) {
|
2009-01-19 01:21:29 -08:00
|
|
|
// We've been canceled
|
|
|
|
return aStatus;
|
|
|
|
}
|
|
|
|
|
2014-10-01 19:32:05 -07:00
|
|
|
mFontFaceSet->RemoveLoader(this);
|
2008-09-30 20:04:10 -07:00
|
|
|
|
|
|
|
if (LOG_ENABLED()) {
|
2012-09-01 19:35:17 -07:00
|
|
|
nsAutoCString fontURI;
|
2008-09-30 20:04:10 -07:00
|
|
|
mFontURI->GetSpec(fontURI);
|
|
|
|
if (NS_SUCCEEDED(aStatus)) {
|
2015-01-28 23:44:59 -08:00
|
|
|
LOG(("userfonts (%p) download completed - font uri: (%s)\n",
|
2008-09-30 20:04:10 -07:00
|
|
|
this, fontURI.get()));
|
|
|
|
} else {
|
2015-01-28 23:44:59 -08:00
|
|
|
LOG(("userfonts (%p) download failed - font uri: (%s) error: %8.8x\n",
|
2008-09-30 20:04:10 -07:00
|
|
|
this, fontURI.get(), aStatus));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-13 06:54:54 -07:00
|
|
|
if (NS_SUCCEEDED(aStatus)) {
|
|
|
|
// for HTTP requests, check whether the request _actually_ succeeded;
|
|
|
|
// the "request status" in aStatus does not necessarily indicate this,
|
|
|
|
// because HTTP responses such as 404 (Not Found) will still result in
|
|
|
|
// a success code and potentially an HTML error page from the server
|
|
|
|
// as the resulting data. We don't want to use that as a font.
|
2011-11-10 15:38:23 -08:00
|
|
|
nsCOMPtr<nsIRequest> request;
|
|
|
|
nsCOMPtr<nsIHttpChannel> httpChannel;
|
|
|
|
aLoader->GetRequest(getter_AddRefs(request));
|
|
|
|
httpChannel = do_QueryInterface(request);
|
2011-07-13 06:54:54 -07:00
|
|
|
if (httpChannel) {
|
2011-09-28 23:19:26 -07:00
|
|
|
bool succeeded;
|
2011-07-13 06:54:54 -07:00
|
|
|
nsresult rv = httpChannel->GetRequestSucceeded(&succeeded);
|
|
|
|
if (NS_SUCCEEDED(rv) && !succeeded) {
|
|
|
|
aStatus = NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-08 00:23:20 -07:00
|
|
|
// The userFontEntry is responsible for freeing the downloaded data
|
2009-08-30 06:55:24 -07:00
|
|
|
// (aString) when finished with it; the pointer is no longer valid
|
2014-09-24 20:16:53 -07:00
|
|
|
// after FontDataDownloadComplete returns.
|
2011-07-13 06:54:54 -07:00
|
|
|
// This is called even in the case of a failed download (HTTP 404, etc),
|
|
|
|
// as there may still be data to be freed (e.g. an error page),
|
2014-09-24 20:16:53 -07:00
|
|
|
// and we need to load the next source.
|
2015-06-26 18:39:54 -07:00
|
|
|
nsPresContext* ctx = mFontFaceSet->GetPresContext();
|
2014-09-24 20:16:53 -07:00
|
|
|
bool fontUpdate =
|
|
|
|
mUserFontEntry->FontDataDownloadComplete(aString, aStringLen, aStatus);
|
2008-09-30 20:04:10 -07:00
|
|
|
|
|
|
|
// when new font loaded, need to reflow
|
2015-06-26 18:39:54 -07:00
|
|
|
if (fontUpdate && ctx) {
|
2008-12-08 08:08:05 -08:00
|
|
|
// Update layout for the presence of the new font. Since this is
|
|
|
|
// asynchronous, reflows will coalesce.
|
2015-03-06 00:44:23 -08:00
|
|
|
ctx->UserFontSetUpdated(mUserFontEntry);
|
2015-01-28 23:44:59 -08:00
|
|
|
LOG(("userfonts (%p) reflow\n", this));
|
2008-09-30 20:04:10 -07:00
|
|
|
}
|
|
|
|
|
2013-03-13 22:24:40 -07:00
|
|
|
// done with font set
|
2014-10-01 19:32:05 -07:00
|
|
|
mFontFaceSet = nullptr;
|
2013-03-13 22:24:40 -07:00
|
|
|
if (mLoadTimer) {
|
|
|
|
mLoadTimer->Cancel();
|
|
|
|
mLoadTimer = nullptr;
|
|
|
|
}
|
|
|
|
|
2009-08-30 06:55:24 -07:00
|
|
|
return NS_SUCCESS_ADOPTED_DATA;
|
2008-09-30 20:04:10 -07:00
|
|
|
}
|
|
|
|
|
2009-01-19 01:21:29 -08:00
|
|
|
void
|
|
|
|
nsFontFaceLoader::Cancel()
|
|
|
|
{
|
2014-09-24 20:16:53 -07:00
|
|
|
mUserFontEntry->mFontDataLoadingState = gfxUserFontEntry::NOT_LOADING;
|
2014-09-08 00:23:20 -07:00
|
|
|
mUserFontEntry->mLoader = nullptr;
|
2014-10-01 19:32:05 -07:00
|
|
|
mFontFaceSet = nullptr;
|
2011-01-05 13:48:48 -08:00
|
|
|
if (mLoadTimer) {
|
|
|
|
mLoadTimer->Cancel();
|
2012-07-30 07:20:58 -07:00
|
|
|
mLoadTimer = nullptr;
|
2011-01-05 13:48:48 -08:00
|
|
|
}
|
2009-01-19 01:21:29 -08:00
|
|
|
mChannel->Cancel(NS_BINDING_ABORTED);
|
|
|
|
}
|
|
|
|
|
2008-11-05 16:32:12 -08:00
|
|
|
nsresult
|
2008-12-11 23:31:51 -08:00
|
|
|
nsFontFaceLoader::CheckLoadAllowed(nsIPrincipal* aSourcePrincipal,
|
|
|
|
nsIURI* aTargetURI,
|
|
|
|
nsISupports* aContext)
|
2008-09-30 20:04:10 -07:00
|
|
|
{
|
2008-11-05 16:32:12 -08:00
|
|
|
nsresult rv;
|
2012-03-15 20:31:01 -07:00
|
|
|
|
2008-12-11 23:31:51 -08:00
|
|
|
if (!aSourcePrincipal)
|
|
|
|
return NS_OK;
|
2010-05-23 23:45:18 -07:00
|
|
|
|
|
|
|
// check with the security manager
|
2013-09-25 06:48:20 -07:00
|
|
|
nsIScriptSecurityManager* secMan = nsContentUtils::GetSecurityManager();
|
2010-05-23 23:45:18 -07:00
|
|
|
rv = secMan->CheckLoadURIWithPrincipal(aSourcePrincipal, aTargetURI,
|
|
|
|
nsIScriptSecurityManager::STANDARD);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2008-12-11 23:31:51 -08:00
|
|
|
// check content policy
|
2012-08-22 08:56:38 -07:00
|
|
|
int16_t shouldLoad = nsIContentPolicy::ACCEPT;
|
2008-12-11 23:31:51 -08:00
|
|
|
rv = NS_CheckContentLoadPolicy(nsIContentPolicy::TYPE_FONT,
|
|
|
|
aTargetURI,
|
|
|
|
aSourcePrincipal,
|
|
|
|
aContext,
|
|
|
|
EmptyCString(), // mime type
|
2012-07-30 07:20:58 -07:00
|
|
|
nullptr,
|
2008-12-11 23:31:51 -08:00
|
|
|
&shouldLoad,
|
|
|
|
nsContentUtils::GetContentPolicy(),
|
|
|
|
nsContentUtils::GetSecurityManager());
|
|
|
|
|
|
|
|
if (NS_FAILED(rv) || NS_CP_REJECTED(shouldLoad)) {
|
|
|
|
return NS_ERROR_CONTENT_BLOCKED;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|