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 */
|
|
|
|
|
|
|
|
#ifdef MOZ_LOGGING
|
|
|
|
#define FORCE_PR_LOG /* Allow logging in the release build */
|
|
|
|
#endif /* MOZ_LOGGING */
|
|
|
|
#include "prlog.h"
|
|
|
|
|
|
|
|
#include "nsFontFaceLoader.h"
|
|
|
|
|
2008-11-05 16:32:12 -08:00
|
|
|
#include "nsError.h"
|
2008-09-30 20:04:10 -07:00
|
|
|
#include "nsNetUtil.h"
|
|
|
|
#include "nsContentUtils.h"
|
2011-06-11 19:30:16 -07:00
|
|
|
#include "mozilla/Preferences.h"
|
2008-09-30 20:04:10 -07:00
|
|
|
|
|
|
|
#include "nsPresContext.h"
|
|
|
|
#include "nsIPresShell.h"
|
|
|
|
#include "nsIPrincipal.h"
|
|
|
|
#include "nsIScriptSecurityManager.h"
|
|
|
|
|
2008-11-05 16:32:12 -08:00
|
|
|
#include "nsIContentPolicy.h"
|
|
|
|
#include "nsContentPolicyUtils.h"
|
|
|
|
#include "nsCrossSiteListenerProxy.h"
|
2010-04-23 12:53:41 -07:00
|
|
|
#include "nsIContentSecurityPolicy.h"
|
2013-05-21 21:39:39 -07:00
|
|
|
#include "nsIDocShell.h"
|
|
|
|
#include "nsIWebNavigation.h"
|
2013-11-16 06:34:25 -08:00
|
|
|
#include "nsISupportsPriority.h"
|
2014-06-03 13:37:46 -07:00
|
|
|
#include "nsINetworkPredictor.h"
|
2008-09-30 20:04:10 -07:00
|
|
|
|
2011-07-12 04:24:36 -07:00
|
|
|
#include "nsIConsoleService.h"
|
|
|
|
|
2011-04-12 03:53:20 -07:00
|
|
|
#include "nsStyleSet.h"
|
2011-08-25 01:46:01 -07:00
|
|
|
#include "nsPrintfCString.h"
|
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;
|
|
|
|
|
2008-09-30 20:04:10 -07:00
|
|
|
#ifdef PR_LOGGING
|
2013-09-25 06:48:20 -07:00
|
|
|
static PRLogModuleInfo*
|
2012-10-29 16:32:10 -07:00
|
|
|
GetFontDownloaderLog()
|
|
|
|
{
|
2013-09-25 06:48:20 -07:00
|
|
|
static PRLogModuleInfo* sLog;
|
2012-10-29 16:32:10 -07:00
|
|
|
if (!sLog)
|
|
|
|
sLog = PR_NewLogModule("fontdownloader");
|
|
|
|
return sLog;
|
|
|
|
}
|
2008-09-30 20:04:10 -07:00
|
|
|
#endif /* PR_LOGGING */
|
|
|
|
|
2012-10-29 16:32:10 -07:00
|
|
|
#define LOG(args) PR_LOG(GetFontDownloaderLog(), PR_LOG_DEBUG, args)
|
|
|
|
#define LOG_ENABLED() PR_LOG_TEST(GetFontDownloaderLog(), PR_LOG_DEBUG)
|
2008-09-30 20:04:10 -07:00
|
|
|
|
|
|
|
|
2013-09-25 06:48:20 -07:00
|
|
|
nsFontFaceLoader::nsFontFaceLoader(gfxMixedFontFamily* aFontFamily,
|
|
|
|
gfxProxyFontEntry* aProxy,
|
|
|
|
nsIURI* aFontURI,
|
|
|
|
nsUserFontSet* aFontSet,
|
|
|
|
nsIChannel* aChannel)
|
2012-12-19 01:42:25 -08:00
|
|
|
: mFontFamily(aFontFamily),
|
|
|
|
mFontEntry(aProxy),
|
|
|
|
mFontURI(aFontURI),
|
|
|
|
mFontSet(aFontSet),
|
2009-01-19 01:21:29 -08:00
|
|
|
mChannel(aChannel)
|
2008-09-30 20:04:10 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsFontFaceLoader::~nsFontFaceLoader()
|
|
|
|
{
|
2012-04-26 10:50:41 -07:00
|
|
|
if (mFontEntry) {
|
2012-07-30 07:20:58 -07:00
|
|
|
mFontEntry->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
|
|
|
}
|
2009-01-19 01:21:29 -08:00
|
|
|
if (mFontSet) {
|
|
|
|
mFontSet->RemoveLoader(this);
|
|
|
|
}
|
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 {
|
2011-04-27 22:04:45 -07:00
|
|
|
mFontEntry->mLoadingState = gfxProxyFontEntry::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
|
|
|
|
2013-03-13 22:24:40 -07:00
|
|
|
if (!loader->mFontSet) {
|
|
|
|
// We've been canceled
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-09-25 06:48:20 -07:00
|
|
|
gfxProxyFontEntry* pe = loader->mFontEntry.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.
|
|
|
|
if (pe->mLoadingState == gfxProxyFontEntry::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.
|
|
|
|
pe->mLoadingState = gfxProxyFontEntry::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;
|
|
|
|
LOG(("fontdownloader (%p) 75%% done, resetting timer\n", loader));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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) {
|
|
|
|
pe->mLoadingState = gfxProxyFontEntry::LOADING_SLOWLY;
|
2013-09-25 06:48:20 -07:00
|
|
|
gfxUserFontSet* fontSet = loader->mFontSet;
|
|
|
|
nsPresContext* ctx = loader->mFontSet->GetPresContext();
|
2013-03-13 22:24:40 -07:00
|
|
|
NS_ASSERTION(ctx, "userfontset doesn't have a presContext?");
|
|
|
|
if (ctx) {
|
2011-01-05 13:48:48 -08:00
|
|
|
fontSet->IncrementGeneration();
|
|
|
|
ctx->UserFontSetUpdated();
|
|
|
|
LOG(("fontdownloader (%p) timeout reflow\n", loader));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2009-01-19 01:21:29 -08:00
|
|
|
if (!mFontSet) {
|
|
|
|
// We've been canceled
|
|
|
|
return aStatus;
|
|
|
|
}
|
|
|
|
|
|
|
|
mFontSet->RemoveLoader(this);
|
2008-09-30 20:04:10 -07:00
|
|
|
|
|
|
|
#ifdef PR_LOGGING
|
|
|
|
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)) {
|
|
|
|
LOG(("fontdownloader (%p) download completed - font uri: (%s)\n",
|
|
|
|
this, fontURI.get()));
|
|
|
|
} else {
|
|
|
|
LOG(("fontdownloader (%p) download failed - font uri: (%s) error: %8.8x\n",
|
|
|
|
this, fontURI.get(), aStatus));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-09-25 06:48:20 -07:00
|
|
|
nsPresContext* ctx = mFontSet->GetPresContext();
|
2009-01-19 01:21:29 -08:00
|
|
|
NS_ASSERTION(ctx && !ctx->PresShell()->IsDestroying(),
|
|
|
|
"We should have been canceled already");
|
2008-12-11 23:31:51 -08:00
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-08-30 06:55:24 -07:00
|
|
|
// The userFontSet is responsible for freeing the downloaded data
|
|
|
|
// (aString) when finished with it; the pointer is no longer valid
|
|
|
|
// after OnLoadComplete 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),
|
|
|
|
// and we need the fontSet to initiate loading the next source.
|
2013-03-13 22:24:40 -07:00
|
|
|
bool fontUpdate = mFontSet->OnLoadComplete(mFontFamily, mFontEntry, aString,
|
|
|
|
aStringLen, aStatus);
|
2008-09-30 20:04:10 -07:00
|
|
|
|
|
|
|
// when new font loaded, need to reflow
|
|
|
|
if (fontUpdate) {
|
2008-12-08 08:08:05 -08:00
|
|
|
// Update layout for the presence of the new font. Since this is
|
|
|
|
// asynchronous, reflows will coalesce.
|
2008-12-11 23:31:51 -08:00
|
|
|
ctx->UserFontSetUpdated();
|
2008-12-08 08:08:05 -08:00
|
|
|
LOG(("fontdownloader (%p) reflow\n", this));
|
2008-09-30 20:04:10 -07:00
|
|
|
}
|
|
|
|
|
2013-03-13 22:24:40 -07:00
|
|
|
// done with font set
|
|
|
|
mFontSet = nullptr;
|
|
|
|
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()
|
|
|
|
{
|
2011-04-27 22:04:45 -07:00
|
|
|
mFontEntry->mLoadingState = gfxProxyFontEntry::NOT_LOADING;
|
2012-07-30 07:20:58 -07:00
|
|
|
mFontEntry->mLoader = nullptr;
|
|
|
|
mFontSet = 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;
|
|
|
|
}
|
2012-03-15 20:31:01 -07:00
|
|
|
|
2013-09-25 06:48:20 -07:00
|
|
|
nsUserFontSet::nsUserFontSet(nsPresContext* aContext)
|
2008-12-11 23:31:51 -08:00
|
|
|
: mPresContext(aContext)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(mPresContext, "null context passed to nsUserFontSet");
|
|
|
|
}
|
|
|
|
|
|
|
|
nsUserFontSet::~nsUserFontSet()
|
|
|
|
{
|
2009-01-19 01:21:29 -08:00
|
|
|
NS_ASSERTION(mLoaders.Count() == 0, "mLoaders should have been emptied");
|
|
|
|
}
|
2008-12-11 23:31:51 -08:00
|
|
|
|
2009-01-19 01:21:29 -08:00
|
|
|
static PLDHashOperator DestroyIterator(nsPtrHashKey<nsFontFaceLoader>* aKey,
|
|
|
|
void* aUserArg)
|
|
|
|
{
|
|
|
|
aKey->GetKey()->Cancel();
|
|
|
|
return PL_DHASH_REMOVE;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsUserFontSet::Destroy()
|
|
|
|
{
|
2012-07-30 07:20:58 -07:00
|
|
|
mPresContext = nullptr;
|
|
|
|
mLoaders.EnumerateEntries(DestroyIterator, nullptr);
|
2013-06-11 15:20:04 -07:00
|
|
|
mRules.Clear();
|
2009-01-19 01:21:29 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-09-25 06:48:20 -07:00
|
|
|
nsUserFontSet::RemoveLoader(nsFontFaceLoader* aLoader)
|
2009-01-19 01:21:29 -08:00
|
|
|
{
|
|
|
|
mLoaders.RemoveEntry(aLoader);
|
2008-12-11 23:31:51 -08:00
|
|
|
}
|
2008-09-30 20:04:10 -07:00
|
|
|
|
2012-03-15 20:31:01 -07:00
|
|
|
nsresult
|
2013-09-25 06:48:20 -07:00
|
|
|
nsUserFontSet::StartLoad(gfxMixedFontFamily* aFamily,
|
|
|
|
gfxProxyFontEntry* aProxy,
|
|
|
|
const gfxFontFaceSrc* aFontFaceSrc)
|
2008-12-11 23:31:51 -08:00
|
|
|
{
|
|
|
|
nsresult rv;
|
2012-03-15 20:31:01 -07:00
|
|
|
|
2013-09-25 06:48:20 -07:00
|
|
|
nsIPresShell* ps = mPresContext->PresShell();
|
2008-09-30 20:04:10 -07:00
|
|
|
if (!ps)
|
2008-11-05 16:32:12 -08:00
|
|
|
return NS_ERROR_FAILURE;
|
2009-01-19 01:21:29 -08:00
|
|
|
|
|
|
|
nsCOMPtr<nsIStreamLoader> streamLoader;
|
|
|
|
nsCOMPtr<nsILoadGroup> loadGroup(ps->GetDocument()->GetDocumentLoadGroup());
|
|
|
|
|
|
|
|
nsCOMPtr<nsIChannel> channel;
|
2010-04-23 12:53:41 -07:00
|
|
|
// get Content Security Policy from principal to pass into channel
|
|
|
|
nsCOMPtr<nsIChannelPolicy> channelPolicy;
|
|
|
|
nsCOMPtr<nsIContentSecurityPolicy> csp;
|
2012-12-10 01:31:07 -08:00
|
|
|
rv = aProxy->mPrincipal->GetCsp(getter_AddRefs(csp));
|
2010-04-23 12:53:41 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
if (csp) {
|
|
|
|
channelPolicy = do_CreateInstance("@mozilla.org/nschannelpolicy;1");
|
|
|
|
channelPolicy->SetContentSecurityPolicy(csp);
|
|
|
|
channelPolicy->SetLoadType(nsIContentPolicy::TYPE_FONT);
|
|
|
|
}
|
2009-01-19 01:21:29 -08:00
|
|
|
rv = NS_NewChannel(getter_AddRefs(channel),
|
|
|
|
aFontFaceSrc->mURI,
|
2012-07-30 07:20:58 -07:00
|
|
|
nullptr,
|
2009-01-19 01:21:29 -08:00
|
|
|
loadGroup,
|
2012-07-30 07:20:58 -07:00
|
|
|
nullptr,
|
2010-04-23 12:53:41 -07:00
|
|
|
nsIRequest::LOAD_NORMAL,
|
|
|
|
channelPolicy);
|
|
|
|
|
2009-01-19 01:21:29 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsRefPtr<nsFontFaceLoader> fontLoader =
|
2012-12-19 01:42:25 -08:00
|
|
|
new nsFontFaceLoader(aFamily, aProxy, aFontFaceSrc->mURI, this, channel);
|
2009-01-19 01:21:29 -08:00
|
|
|
|
2008-10-29 11:09:50 -07:00
|
|
|
if (!fontLoader)
|
2008-11-05 16:32:12 -08:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2008-09-30 20:04:10 -07:00
|
|
|
|
2008-10-29 11:09:50 -07:00
|
|
|
#ifdef PR_LOGGING
|
|
|
|
if (LOG_ENABLED()) {
|
2012-09-01 19:35:17 -07:00
|
|
|
nsAutoCString fontURI, referrerURI;
|
2008-11-26 19:50:16 -08:00
|
|
|
aFontFaceSrc->mURI->GetSpec(fontURI);
|
|
|
|
if (aFontFaceSrc->mReferrer)
|
|
|
|
aFontFaceSrc->mReferrer->GetSpec(referrerURI);
|
2008-11-05 16:32:12 -08:00
|
|
|
LOG(("fontdownloader (%p) download start - font uri: (%s) "
|
2012-03-15 20:31:01 -07:00
|
|
|
"referrer uri: (%s)\n",
|
2008-11-05 16:32:12 -08:00
|
|
|
fontLoader.get(), fontURI.get(), referrerURI.get()));
|
2008-10-29 11:09:50 -07:00
|
|
|
}
|
2012-03-15 20:31:01 -07:00
|
|
|
#endif
|
2008-10-29 11:09:50 -07:00
|
|
|
|
2008-11-05 16:32:12 -08:00
|
|
|
nsCOMPtr<nsIHttpChannel> httpChannel(do_QueryInterface(channel));
|
|
|
|
if (httpChannel)
|
2008-11-26 19:50:16 -08:00
|
|
|
httpChannel->SetReferrer(aFontFaceSrc->mReferrer);
|
2013-11-16 06:34:25 -08:00
|
|
|
nsCOMPtr<nsISupportsPriority> priorityChannel(do_QueryInterface(channel));
|
|
|
|
if (priorityChannel) {
|
|
|
|
priorityChannel->AdjustPriority(nsISupportsPriority::PRIORITY_HIGH);
|
|
|
|
}
|
|
|
|
|
2008-11-05 16:32:12 -08:00
|
|
|
rv = NS_NewStreamLoader(getter_AddRefs(streamLoader), fontLoader);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2012-03-15 20:31:01 -07:00
|
|
|
|
2013-10-25 14:56:56 -07:00
|
|
|
nsIDocument *document = ps->GetDocument();
|
2014-06-03 13:37:46 -07:00
|
|
|
mozilla::net::PredictorLearn(aFontFaceSrc->mURI, document->GetDocumentURI(),
|
|
|
|
nsINetworkPredictor::LEARN_LOAD_SUBRESOURCE,
|
|
|
|
loadGroup);
|
2013-10-25 14:56:56 -07:00
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool inherits = false;
|
2008-11-26 19:50:16 -08:00
|
|
|
rv = NS_URIChainHasFlags(aFontFaceSrc->mURI,
|
|
|
|
nsIProtocolHandler::URI_INHERITS_SECURITY_CONTEXT,
|
|
|
|
&inherits);
|
|
|
|
if (NS_SUCCEEDED(rv) && inherits) {
|
|
|
|
// allow data, javascript, etc URI's
|
2012-07-30 07:20:58 -07:00
|
|
|
rv = channel->AsyncOpen(streamLoader, nullptr);
|
2008-11-05 16:32:12 -08:00
|
|
|
} else {
|
2012-09-18 19:16:23 -07:00
|
|
|
nsRefPtr<nsCORSListenerProxy> listener =
|
2012-12-10 01:31:07 -08:00
|
|
|
new nsCORSListenerProxy(streamLoader, aProxy->mPrincipal, false);
|
2012-09-18 19:16:23 -07:00
|
|
|
rv = listener->Init(channel);
|
2012-11-21 10:31:57 -08:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
rv = channel->AsyncOpen(listener, nullptr);
|
|
|
|
}
|
2009-01-31 00:58:42 -08:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
fontLoader->DropChannel(); // explicitly need to break ref cycle
|
|
|
|
}
|
2008-11-05 16:32:12 -08:00
|
|
|
}
|
|
|
|
|
2009-01-19 01:21:29 -08:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
mLoaders.PutEntry(fontLoader);
|
2011-01-05 13:48:48 -08:00
|
|
|
fontLoader->StartedLoading(streamLoader);
|
2012-04-26 10:50:41 -07:00
|
|
|
aProxy->mLoader = fontLoader; // let the font entry remember the loader,
|
|
|
|
// in case we need to cancel it
|
2009-01-19 01:21:29 -08:00
|
|
|
}
|
|
|
|
|
2008-11-05 16:32:12 -08:00
|
|
|
return rv;
|
|
|
|
}
|
2011-04-12 03:53:20 -07:00
|
|
|
|
2012-02-02 03:24:22 -08:00
|
|
|
static PLDHashOperator DetachFontEntries(const nsAString& aKey,
|
|
|
|
nsRefPtr<gfxMixedFontFamily>& aFamily,
|
|
|
|
void* aUserArg)
|
|
|
|
{
|
|
|
|
aFamily->DetachFontEntries();
|
|
|
|
return PL_DHASH_NEXT;
|
|
|
|
}
|
|
|
|
|
2013-08-12 02:07:55 -07:00
|
|
|
static PLDHashOperator RemoveIfEmpty(const nsAString& aKey,
|
|
|
|
nsRefPtr<gfxMixedFontFamily>& aFamily,
|
|
|
|
void* aUserArg)
|
|
|
|
{
|
|
|
|
return aFamily->GetFontList().Length() ? PL_DHASH_NEXT : PL_DHASH_REMOVE;
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2011-04-12 03:53:20 -07:00
|
|
|
nsUserFontSet::UpdateRules(const nsTArray<nsFontFaceRuleContainer>& aRules)
|
|
|
|
{
|
2011-09-28 23:19:26 -07:00
|
|
|
bool modified = false;
|
2011-05-01 08:10:20 -07:00
|
|
|
|
2013-08-12 02:07:55 -07:00
|
|
|
// The @font-face rules that make up the user font set have changed,
|
|
|
|
// so we need to update the set. However, we want to preserve existing
|
|
|
|
// font entries wherever possible, so that we don't discard and then
|
|
|
|
// re-download resources in the (common) case where at least some of the
|
|
|
|
// same rules are still present.
|
2011-04-27 22:04:45 -07:00
|
|
|
|
2011-04-12 03:53:20 -07:00
|
|
|
nsTArray<FontFaceRuleRecord> oldRules;
|
|
|
|
mRules.SwapElements(oldRules);
|
|
|
|
|
2013-08-12 02:07:55 -07:00
|
|
|
// Remove faces from the font family records; we need to re-insert them
|
|
|
|
// because we might end up with faces in a different order even if they're
|
|
|
|
// the same font entries as before. (The order can affect font selection
|
|
|
|
// where multiple faces match the requested style, perhaps with overlapping
|
|
|
|
// unicode-range coverage.)
|
2012-07-30 07:20:58 -07:00
|
|
|
mFontFamilies.Enumerate(DetachFontEntries, nullptr);
|
2011-04-12 03:53:20 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t i = 0, i_end = aRules.Length(); i < i_end; ++i) {
|
2013-08-12 02:07:55 -07:00
|
|
|
// Insert each rule into our list, migrating old font entries if possible
|
2011-04-12 03:53:20 -07:00
|
|
|
// rather than creating new ones; set modified to true if we detect
|
2013-08-12 02:07:55 -07:00
|
|
|
// that rule ordering has changed, or if a new entry is created.
|
2011-04-12 03:53:20 -07:00
|
|
|
InsertRule(aRules[i].mRule, aRules[i].mSheetType, oldRules, modified);
|
|
|
|
}
|
|
|
|
|
2013-08-12 02:07:55 -07:00
|
|
|
// Remove any residual families that have no font entries (i.e., they were
|
|
|
|
// not defined at all by the updated set of @font-face rules).
|
|
|
|
mFontFamilies.Enumerate(RemoveIfEmpty, nullptr);
|
|
|
|
|
|
|
|
// If any rules are left in the old list, note that the set has changed
|
|
|
|
// (even if the new set was built entirely by migrating old font entries).
|
2011-04-12 03:53:20 -07:00
|
|
|
if (oldRules.Length() > 0) {
|
2011-10-17 07:59:28 -07:00
|
|
|
modified = true;
|
2013-08-12 02:07:55 -07:00
|
|
|
// Any in-progress loaders for obsolete rules should be cancelled,
|
|
|
|
// as the resource being downloaded will no longer be required.
|
|
|
|
// We need to explicitly remove any loaders here, otherwise the loaders
|
|
|
|
// will keep their "orphaned" font entries alive until they complete,
|
|
|
|
// even after the oldRules array is deleted.
|
2012-04-26 10:50:41 -07:00
|
|
|
size_t count = oldRules.Length();
|
|
|
|
for (size_t i = 0; i < count; ++i) {
|
2013-09-25 06:48:20 -07:00
|
|
|
gfxFontEntry* fe = oldRules[i].mFontEntry;
|
2012-04-26 10:50:41 -07:00
|
|
|
if (!fe->mIsProxy) {
|
|
|
|
continue;
|
|
|
|
}
|
2013-09-25 06:48:20 -07:00
|
|
|
gfxProxyFontEntry* proxy = static_cast<gfxProxyFontEntry*>(fe);
|
|
|
|
nsFontFaceLoader* loader = proxy->mLoader;
|
2013-08-12 02:07:55 -07:00
|
|
|
if (loader) {
|
|
|
|
loader->Cancel();
|
|
|
|
RemoveLoader(loader);
|
2012-04-26 10:50:41 -07:00
|
|
|
}
|
|
|
|
}
|
2011-04-12 03:53:20 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (modified) {
|
|
|
|
IncrementGeneration();
|
|
|
|
}
|
|
|
|
|
2014-04-17 07:17:22 -07:00
|
|
|
// local rules have been rebuilt, so clear the flag
|
|
|
|
mLocalRulesUsed = false;
|
|
|
|
|
2011-04-12 03:53:20 -07:00
|
|
|
return modified;
|
|
|
|
}
|
|
|
|
|
2014-04-17 07:17:22 -07:00
|
|
|
static bool
|
|
|
|
HasLocalSrc(const nsCSSValue::Array *aSrcArr)
|
|
|
|
{
|
|
|
|
size_t numSrc = aSrcArr->Count();
|
|
|
|
for (size_t i = 0; i < numSrc; i++) {
|
|
|
|
if (aSrcArr->Item(i).GetUnit() == eCSSUnit_Local_Font) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-04-12 03:53:20 -07:00
|
|
|
void
|
2013-09-25 06:48:20 -07:00
|
|
|
nsUserFontSet::InsertRule(nsCSSFontFaceRule* aRule, uint8_t aSheetType,
|
2011-04-12 03:53:20 -07:00
|
|
|
nsTArray<FontFaceRuleRecord>& aOldRules,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool& aFontSetModified)
|
2011-04-12 03:53:20 -07:00
|
|
|
{
|
2011-04-07 18:23:46 -07:00
|
|
|
NS_ABORT_IF_FALSE(aRule->GetType() == mozilla::css::Rule::FONT_FACE_RULE,
|
2011-04-12 03:53:20 -07:00
|
|
|
"InsertRule passed a non-fontface CSS rule");
|
|
|
|
|
|
|
|
// set up family name
|
|
|
|
nsAutoString fontfamily;
|
|
|
|
nsCSSValue val;
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t unit;
|
2011-04-12 03:53:20 -07:00
|
|
|
|
|
|
|
aRule->GetDesc(eCSSFontDesc_Family, val);
|
|
|
|
unit = val.GetUnit();
|
|
|
|
if (unit == eCSSUnit_String) {
|
|
|
|
val.GetStringValue(fontfamily);
|
|
|
|
} else {
|
|
|
|
NS_ASSERTION(unit == eCSSUnit_Null,
|
|
|
|
"@font-face family name has unexpected unit");
|
|
|
|
}
|
|
|
|
if (fontfamily.IsEmpty()) {
|
|
|
|
// If there is no family name, this rule cannot contribute a
|
|
|
|
// usable font, so there is no point in processing it further.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// first, we check in oldRules; if the rule exists there, just move it
|
|
|
|
// to the new rule list, and put the entry into the appropriate family
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t i = 0; i < aOldRules.Length(); ++i) {
|
2011-04-12 03:53:20 -07:00
|
|
|
const FontFaceRuleRecord& ruleRec = aOldRules[i];
|
2014-04-17 07:17:22 -07:00
|
|
|
|
2011-04-12 03:53:20 -07:00
|
|
|
if (ruleRec.mContainer.mRule == aRule &&
|
|
|
|
ruleRec.mContainer.mSheetType == aSheetType) {
|
2014-04-17 07:17:22 -07:00
|
|
|
|
|
|
|
// if local rules were used, don't use the old font entry
|
|
|
|
// for rules containing src local usage
|
|
|
|
if (mLocalRulesUsed) {
|
|
|
|
aRule->GetDesc(eCSSFontDesc_Src, val);
|
|
|
|
unit = val.GetUnit();
|
|
|
|
if (unit == eCSSUnit_Array && HasLocalSrc(val.GetArrayValue())) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-04-12 03:53:20 -07:00
|
|
|
AddFontFace(fontfamily, ruleRec.mFontEntry);
|
|
|
|
mRules.AppendElement(ruleRec);
|
|
|
|
aOldRules.RemoveElementAt(i);
|
|
|
|
// note the set has been modified if an old rule was skipped to find
|
|
|
|
// this one - something has been dropped, or ordering changed
|
|
|
|
if (i > 0) {
|
2011-10-17 07:59:28 -07:00
|
|
|
aFontSetModified = true;
|
2011-04-12 03:53:20 -07:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// this is a new rule:
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t weight = NS_STYLE_FONT_WEIGHT_NORMAL;
|
2013-08-12 02:07:55 -07:00
|
|
|
int32_t stretch = NS_STYLE_FONT_STRETCH_NORMAL;
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t italicStyle = NS_STYLE_FONT_STYLE_NORMAL;
|
2014-06-29 21:25:21 -07:00
|
|
|
uint32_t languageOverride = NO_FONT_LANGUAGE_OVERRIDE;
|
2011-04-12 03:53:20 -07:00
|
|
|
|
|
|
|
// set up weight
|
|
|
|
aRule->GetDesc(eCSSFontDesc_Weight, val);
|
|
|
|
unit = val.GetUnit();
|
|
|
|
if (unit == eCSSUnit_Integer || unit == eCSSUnit_Enumerated) {
|
|
|
|
weight = val.GetIntValue();
|
2014-06-29 21:25:11 -07:00
|
|
|
if (weight == 0) {
|
2014-07-09 04:03:19 -07:00
|
|
|
weight = NS_STYLE_FONT_WEIGHT_NORMAL;
|
2014-06-29 21:25:11 -07:00
|
|
|
}
|
2011-04-12 03:53:20 -07:00
|
|
|
} else if (unit == eCSSUnit_Normal) {
|
|
|
|
weight = NS_STYLE_FONT_WEIGHT_NORMAL;
|
|
|
|
} else {
|
|
|
|
NS_ASSERTION(unit == eCSSUnit_Null,
|
|
|
|
"@font-face weight has unexpected unit");
|
|
|
|
}
|
|
|
|
|
|
|
|
// set up stretch
|
|
|
|
aRule->GetDesc(eCSSFontDesc_Stretch, val);
|
|
|
|
unit = val.GetUnit();
|
|
|
|
if (unit == eCSSUnit_Enumerated) {
|
|
|
|
stretch = val.GetIntValue();
|
|
|
|
} else if (unit == eCSSUnit_Normal) {
|
|
|
|
stretch = NS_STYLE_FONT_STRETCH_NORMAL;
|
|
|
|
} else {
|
|
|
|
NS_ASSERTION(unit == eCSSUnit_Null,
|
|
|
|
"@font-face stretch has unexpected unit");
|
|
|
|
}
|
|
|
|
|
|
|
|
// set up font style
|
|
|
|
aRule->GetDesc(eCSSFontDesc_Style, val);
|
|
|
|
unit = val.GetUnit();
|
|
|
|
if (unit == eCSSUnit_Enumerated) {
|
|
|
|
italicStyle = val.GetIntValue();
|
|
|
|
} else if (unit == eCSSUnit_Normal) {
|
2012-03-15 02:04:46 -07:00
|
|
|
italicStyle = NS_STYLE_FONT_STYLE_NORMAL;
|
2011-04-12 03:53:20 -07:00
|
|
|
} else {
|
|
|
|
NS_ASSERTION(unit == eCSSUnit_Null,
|
|
|
|
"@font-face style has unexpected unit");
|
|
|
|
}
|
|
|
|
|
|
|
|
// set up font features
|
2012-04-25 23:24:26 -07:00
|
|
|
nsTArray<gfxFontFeature> featureSettings;
|
2011-04-12 03:53:20 -07:00
|
|
|
aRule->GetDesc(eCSSFontDesc_FontFeatureSettings, val);
|
|
|
|
unit = val.GetUnit();
|
|
|
|
if (unit == eCSSUnit_Normal) {
|
2012-04-25 23:24:26 -07:00
|
|
|
// empty list of features
|
|
|
|
} else if (unit == eCSSUnit_PairList || unit == eCSSUnit_PairListDep) {
|
|
|
|
nsRuleNode::ComputeFontFeatures(val.GetPairListValue(), featureSettings);
|
2011-04-12 03:53:20 -07:00
|
|
|
} else {
|
|
|
|
NS_ASSERTION(unit == eCSSUnit_Null,
|
|
|
|
"@font-face font-feature-settings has unexpected unit");
|
|
|
|
}
|
|
|
|
|
|
|
|
// set up font language override
|
|
|
|
aRule->GetDesc(eCSSFontDesc_FontLanguageOverride, val);
|
|
|
|
unit = val.GetUnit();
|
|
|
|
if (unit == eCSSUnit_Normal) {
|
|
|
|
// empty feature string
|
|
|
|
} else if (unit == eCSSUnit_String) {
|
2014-06-29 21:25:21 -07:00
|
|
|
nsString stringValue;
|
|
|
|
val.GetStringValue(stringValue);
|
|
|
|
languageOverride = gfxFontStyle::ParseFontLanguageOverride(stringValue);
|
2011-04-12 03:53:20 -07:00
|
|
|
} else {
|
|
|
|
NS_ASSERTION(unit == eCSSUnit_Null,
|
|
|
|
"@font-face font-language-override has unexpected unit");
|
|
|
|
}
|
|
|
|
|
|
|
|
// set up src array
|
|
|
|
nsTArray<gfxFontFaceSrc> srcArray;
|
|
|
|
|
|
|
|
aRule->GetDesc(eCSSFontDesc_Src, val);
|
|
|
|
unit = val.GetUnit();
|
|
|
|
if (unit == eCSSUnit_Array) {
|
2013-09-25 06:48:20 -07:00
|
|
|
nsCSSValue::Array* srcArr = val.GetArrayValue();
|
2011-04-12 03:53:20 -07:00
|
|
|
size_t numSrc = srcArr->Count();
|
|
|
|
|
|
|
|
for (size_t i = 0; i < numSrc; i++) {
|
|
|
|
val = srcArr->Item(i);
|
|
|
|
unit = val.GetUnit();
|
2013-09-25 06:48:20 -07:00
|
|
|
gfxFontFaceSrc* face = srcArray.AppendElements(1);
|
2011-04-12 03:53:20 -07:00
|
|
|
if (!face)
|
|
|
|
return;
|
|
|
|
|
|
|
|
switch (unit) {
|
|
|
|
|
|
|
|
case eCSSUnit_Local_Font:
|
|
|
|
val.GetStringValue(face->mLocalName);
|
2011-10-17 07:59:28 -07:00
|
|
|
face->mIsLocal = true;
|
2012-07-30 07:20:58 -07:00
|
|
|
face->mURI = nullptr;
|
2011-04-12 03:53:20 -07:00
|
|
|
face->mFormatFlags = 0;
|
|
|
|
break;
|
|
|
|
case eCSSUnit_URL:
|
2011-10-17 07:59:28 -07:00
|
|
|
face->mIsLocal = false;
|
2011-04-12 03:53:20 -07:00
|
|
|
face->mURI = val.GetURLValue();
|
|
|
|
face->mReferrer = val.GetURLStructValue()->mReferrer;
|
|
|
|
face->mOriginPrincipal = val.GetURLStructValue()->mOriginPrincipal;
|
|
|
|
NS_ASSERTION(face->mOriginPrincipal, "null origin principal in @font-face rule");
|
|
|
|
|
|
|
|
// agent and user stylesheets are treated slightly differently,
|
|
|
|
// the same-site origin check and access control headers are
|
|
|
|
// enforced against the sheet principal rather than the document
|
|
|
|
// principal to allow user stylesheets to include @font-face rules
|
|
|
|
face->mUseOriginPrincipal = (aSheetType == nsStyleSet::eUserSheet ||
|
|
|
|
aSheetType == nsStyleSet::eAgentSheet);
|
|
|
|
|
|
|
|
face->mLocalName.Truncate();
|
|
|
|
face->mFormatFlags = 0;
|
|
|
|
while (i + 1 < numSrc && (val = srcArr->Item(i+1),
|
|
|
|
val.GetUnit() == eCSSUnit_Font_Format)) {
|
|
|
|
nsDependentString valueString(val.GetStringBufferValue());
|
|
|
|
if (valueString.LowerCaseEqualsASCII("woff")) {
|
|
|
|
face->mFormatFlags |= FLAG_FORMAT_WOFF;
|
|
|
|
} else if (valueString.LowerCaseEqualsASCII("opentype")) {
|
|
|
|
face->mFormatFlags |= FLAG_FORMAT_OPENTYPE;
|
|
|
|
} else if (valueString.LowerCaseEqualsASCII("truetype")) {
|
|
|
|
face->mFormatFlags |= FLAG_FORMAT_TRUETYPE;
|
|
|
|
} else if (valueString.LowerCaseEqualsASCII("truetype-aat")) {
|
|
|
|
face->mFormatFlags |= FLAG_FORMAT_TRUETYPE_AAT;
|
|
|
|
} else if (valueString.LowerCaseEqualsASCII("embedded-opentype")) {
|
|
|
|
face->mFormatFlags |= FLAG_FORMAT_EOT;
|
|
|
|
} else if (valueString.LowerCaseEqualsASCII("svg")) {
|
|
|
|
face->mFormatFlags |= FLAG_FORMAT_SVG;
|
|
|
|
} else {
|
|
|
|
// unknown format specified, mark to distinguish from the
|
|
|
|
// case where no format hints are specified
|
|
|
|
face->mFormatFlags |= FLAG_FORMAT_UNKNOWN;
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
2013-10-22 04:06:20 -07:00
|
|
|
if (!face->mURI) {
|
|
|
|
// if URI not valid, omit from src array
|
|
|
|
srcArray.RemoveElementAt(srcArray.Length() - 1);
|
|
|
|
NS_WARNING("null url in @font-face rule");
|
|
|
|
continue;
|
|
|
|
}
|
2011-04-12 03:53:20 -07:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
NS_ASSERTION(unit == eCSSUnit_Local_Font || unit == eCSSUnit_URL,
|
|
|
|
"strange unit type in font-face src array");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
NS_ASSERTION(unit == eCSSUnit_Null, "@font-face src has unexpected unit");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (srcArray.Length() > 0) {
|
|
|
|
FontFaceRuleRecord ruleRec;
|
|
|
|
ruleRec.mContainer.mRule = aRule;
|
|
|
|
ruleRec.mContainer.mSheetType = aSheetType;
|
|
|
|
ruleRec.mFontEntry = AddFontFace(fontfamily, srcArray,
|
|
|
|
weight, stretch, italicStyle,
|
|
|
|
featureSettings, languageOverride);
|
|
|
|
if (ruleRec.mFontEntry) {
|
|
|
|
mRules.AppendElement(ruleRec);
|
|
|
|
}
|
|
|
|
// this was a new rule and fontEntry, so note that the set was modified
|
2011-10-17 07:59:28 -07:00
|
|
|
aFontSetModified = true;
|
2011-04-12 03:53:20 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-09-25 06:48:20 -07:00
|
|
|
nsUserFontSet::ReplaceFontEntry(gfxMixedFontFamily* aFamily,
|
|
|
|
gfxProxyFontEntry* aProxy,
|
|
|
|
gfxFontEntry* aFontEntry)
|
2011-04-12 03:53:20 -07:00
|
|
|
{
|
2013-08-12 02:07:55 -07:00
|
|
|
// aProxy is being supplanted by the "real" font aFontEntry, so we need to
|
|
|
|
// update any rules that refer to it. Note that there may be multiple rules
|
|
|
|
// that refer to the same proxy - e.g. if a stylesheet was loaded multiple
|
|
|
|
// times, so that several identical @font-face rules are present.
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t i = 0; i < mRules.Length(); ++i) {
|
2011-04-12 03:53:20 -07:00
|
|
|
if (mRules[i].mFontEntry == aProxy) {
|
|
|
|
mRules[i].mFontEntry = aFontEntry;
|
|
|
|
}
|
|
|
|
}
|
2012-12-19 01:42:25 -08:00
|
|
|
aFamily->ReplaceFontEntry(aProxy, aFontEntry);
|
2011-04-12 03:53:20 -07:00
|
|
|
}
|
2011-06-15 23:31:36 -07:00
|
|
|
|
|
|
|
nsCSSFontFaceRule*
|
2013-09-25 06:48:20 -07:00
|
|
|
nsUserFontSet::FindRuleForEntry(gfxFontEntry* aFontEntry)
|
2011-06-15 23:31:36 -07:00
|
|
|
{
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t i = 0; i < mRules.Length(); ++i) {
|
2011-06-15 23:31:36 -07:00
|
|
|
if (mRules[i].mFontEntry == aFontEntry) {
|
|
|
|
return mRules[i].mContainer.mRule;
|
|
|
|
}
|
|
|
|
}
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2011-06-15 23:31:36 -07:00
|
|
|
}
|
2011-07-12 04:24:36 -07:00
|
|
|
|
|
|
|
nsresult
|
2013-09-25 06:48:20 -07:00
|
|
|
nsUserFontSet::LogMessage(gfxMixedFontFamily* aFamily,
|
|
|
|
gfxProxyFontEntry* aProxy,
|
|
|
|
const char* aMessage,
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t aFlags,
|
2011-07-12 04:24:36 -07:00
|
|
|
nsresult aStatus)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIConsoleService>
|
|
|
|
console(do_GetService(NS_CONSOLESERVICE_CONTRACTID));
|
|
|
|
if (!console) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
2014-07-09 04:04:14 -07:00
|
|
|
NS_ConvertUTF16toUTF8 familyName(aProxy->mFamilyName);
|
2012-09-01 19:35:17 -07:00
|
|
|
nsAutoCString fontURI;
|
2012-01-05 02:14:11 -08:00
|
|
|
if (aProxy->mSrcIndex == aProxy->mSrcList.Length()) {
|
|
|
|
fontURI.AppendLiteral("(end of source list)");
|
2011-07-15 07:16:24 -07:00
|
|
|
} else {
|
2012-01-05 02:14:11 -08:00
|
|
|
if (aProxy->mSrcList[aProxy->mSrcIndex].mURI) {
|
|
|
|
aProxy->mSrcList[aProxy->mSrcIndex].mURI->GetSpec(fontURI);
|
|
|
|
} else {
|
|
|
|
fontURI.AppendLiteral("(invalid URI)");
|
|
|
|
}
|
2011-07-15 07:16:24 -07:00
|
|
|
}
|
2011-07-12 04:24:36 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
char weightKeywordBuf[8]; // plenty to sprintf() a uint16_t
|
2013-09-25 06:48:20 -07:00
|
|
|
const char* weightKeyword;
|
2011-07-12 04:24:36 -07:00
|
|
|
const nsAFlatCString& weightKeywordString =
|
|
|
|
nsCSSProps::ValueToKeyword(aProxy->Weight(),
|
|
|
|
nsCSSProps::kFontWeightKTable);
|
|
|
|
if (weightKeywordString.Length() > 0) {
|
|
|
|
weightKeyword = weightKeywordString.get();
|
|
|
|
} else {
|
|
|
|
sprintf(weightKeywordBuf, "%u", aProxy->Weight());
|
|
|
|
weightKeyword = weightKeywordBuf;
|
|
|
|
}
|
|
|
|
|
2013-02-17 22:56:32 -08:00
|
|
|
nsPrintfCString message
|
|
|
|
("downloadable font: %s "
|
2011-07-12 04:24:36 -07:00
|
|
|
"(font-family: \"%s\" style:%s weight:%s stretch:%s src index:%d)",
|
|
|
|
aMessage,
|
|
|
|
familyName.get(),
|
|
|
|
aProxy->IsItalic() ? "italic" : "normal",
|
|
|
|
weightKeyword,
|
|
|
|
nsCSSProps::ValueToKeyword(aProxy->Stretch(),
|
|
|
|
nsCSSProps::kFontStretchKTable).get(),
|
|
|
|
aProxy->mSrcIndex);
|
|
|
|
|
2012-08-07 07:58:47 -07:00
|
|
|
if (NS_FAILED(aStatus)) {
|
2014-05-21 20:48:51 -07:00
|
|
|
message.AppendLiteral(": ");
|
2011-07-12 04:24:36 -07:00
|
|
|
switch (aStatus) {
|
|
|
|
case NS_ERROR_DOM_BAD_URI:
|
2014-05-21 20:48:51 -07:00
|
|
|
message.AppendLiteral("bad URI or cross-site access not allowed");
|
2011-07-12 04:24:36 -07:00
|
|
|
break;
|
|
|
|
case NS_ERROR_CONTENT_BLOCKED:
|
2014-05-21 20:48:51 -07:00
|
|
|
message.AppendLiteral("content blocked");
|
2011-07-12 04:24:36 -07:00
|
|
|
break;
|
|
|
|
default:
|
2014-05-21 20:48:51 -07:00
|
|
|
message.AppendLiteral("status=");
|
2013-02-17 22:56:32 -08:00
|
|
|
message.AppendInt(static_cast<uint32_t>(aStatus));
|
2011-07-12 04:24:36 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-05-21 20:48:51 -07:00
|
|
|
message.AppendLiteral("\nsource: ");
|
2013-02-17 22:56:32 -08:00
|
|
|
message.Append(fontURI);
|
2011-07-12 04:24:36 -07:00
|
|
|
|
|
|
|
#ifdef PR_LOGGING
|
2012-10-29 16:32:10 -07:00
|
|
|
if (PR_LOG_TEST(GetUserFontsLog(), PR_LOG_DEBUG)) {
|
|
|
|
PR_LOG(GetUserFontsLog(), PR_LOG_DEBUG,
|
2013-02-17 22:56:32 -08:00
|
|
|
("userfonts (%p) %s", this, message.get()));
|
2011-07-12 04:24:36 -07:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// try to give the user an indication of where the rule came from
|
|
|
|
nsCSSFontFaceRule* rule = FindRuleForEntry(aProxy);
|
|
|
|
nsString href;
|
|
|
|
nsString text;
|
|
|
|
nsresult rv;
|
|
|
|
if (rule) {
|
|
|
|
rv = rule->GetCssText(text);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
nsCOMPtr<nsIDOMCSSStyleSheet> sheet;
|
|
|
|
rv = rule->GetParentStyleSheet(getter_AddRefs(sheet));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2013-04-02 23:13:17 -07:00
|
|
|
// if the style sheet is removed while the font is loading can be null
|
|
|
|
if (sheet) {
|
|
|
|
rv = sheet->GetHref(href);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
} else {
|
|
|
|
NS_WARNING("null parent stylesheet for @font-face rule");
|
|
|
|
href.AssignLiteral("unknown");
|
|
|
|
}
|
2011-07-12 04:24:36 -07:00
|
|
|
}
|
|
|
|
|
2011-12-21 13:51:29 -08:00
|
|
|
nsCOMPtr<nsIScriptError> scriptError =
|
2011-07-12 04:24:36 -07:00
|
|
|
do_CreateInstance(NS_SCRIPTERROR_CONTRACTID, &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint64_t innerWindowID = GetPresContext()->Document()->InnerWindowID();
|
2013-02-17 22:56:32 -08:00
|
|
|
rv = scriptError->InitWithWindowID(NS_ConvertUTF8toUTF16(message),
|
2012-09-09 16:29:12 -07:00
|
|
|
href, // file
|
|
|
|
text, // src line
|
2011-07-12 04:24:36 -07:00
|
|
|
0, 0, // line & column number
|
|
|
|
aFlags, // flags
|
|
|
|
"CSS Loader", // category (make separate?)
|
2011-08-24 13:44:35 -07:00
|
|
|
innerWindowID);
|
2011-12-21 13:51:29 -08:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
console->LogMessage(scriptError);
|
2011-07-12 04:24:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2012-03-15 20:31:01 -07:00
|
|
|
|
|
|
|
nsresult
|
2013-09-25 06:48:20 -07:00
|
|
|
nsUserFontSet::CheckFontLoad(const gfxFontFaceSrc* aFontFaceSrc,
|
|
|
|
nsIPrincipal** aPrincipal,
|
|
|
|
bool* aBypassCache)
|
2012-03-15 20:31:01 -07:00
|
|
|
{
|
|
|
|
// check same-site origin
|
2013-09-25 06:48:20 -07:00
|
|
|
nsIPresShell* ps = mPresContext->PresShell();
|
2012-03-15 20:31:01 -07:00
|
|
|
if (!ps)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
NS_ASSERTION(aFontFaceSrc && !aFontFaceSrc->mIsLocal,
|
|
|
|
"bad font face url passed to fontloader");
|
|
|
|
NS_ASSERTION(aFontFaceSrc->mURI, "null font uri");
|
|
|
|
if (!aFontFaceSrc->mURI)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
// use document principal, original principal if flag set
|
|
|
|
// this enables user stylesheets to load font files via
|
|
|
|
// @font-face rules
|
2012-03-15 20:31:01 -07:00
|
|
|
*aPrincipal = ps->GetDocument()->NodePrincipal();
|
2012-03-15 20:31:01 -07:00
|
|
|
|
|
|
|
NS_ASSERTION(aFontFaceSrc->mOriginPrincipal,
|
|
|
|
"null origin principal in @font-face rule");
|
|
|
|
if (aFontFaceSrc->mUseOriginPrincipal) {
|
2012-12-10 01:31:07 -08:00
|
|
|
*aPrincipal = aFontFaceSrc->mOriginPrincipal;
|
2012-03-15 20:31:01 -07:00
|
|
|
}
|
|
|
|
|
2013-05-21 21:39:39 -07:00
|
|
|
nsresult rv = nsFontFaceLoader::CheckLoadAllowed(*aPrincipal,
|
|
|
|
aFontFaceSrc->mURI,
|
|
|
|
ps->GetDocument());
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
*aBypassCache = false;
|
2013-11-15 08:32:12 -08:00
|
|
|
|
|
|
|
nsCOMPtr<nsIDocShell> docShell = ps->GetDocument()->GetDocShell();
|
|
|
|
if (docShell) {
|
|
|
|
uint32_t loadType;
|
|
|
|
if (NS_SUCCEEDED(docShell->GetLoadType(&loadType))) {
|
|
|
|
if ((loadType >> 16) & nsIWebNavigation::LOAD_FLAGS_BYPASS_CACHE) {
|
|
|
|
*aBypassCache = true;
|
2013-05-21 21:39:39 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
2012-03-15 20:31:01 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2013-09-25 06:48:20 -07:00
|
|
|
nsUserFontSet::SyncLoadFontData(gfxProxyFontEntry* aFontToLoad,
|
|
|
|
const gfxFontFaceSrc* aFontFaceSrc,
|
|
|
|
uint8_t*& aBuffer,
|
|
|
|
uint32_t& aBufferLength)
|
2012-03-15 20:31:01 -07:00
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIChannel> channel;
|
|
|
|
// get Content Security Policy from principal to pass into channel
|
|
|
|
nsCOMPtr<nsIChannelPolicy> channelPolicy;
|
|
|
|
nsCOMPtr<nsIContentSecurityPolicy> csp;
|
2012-12-10 01:31:07 -08:00
|
|
|
rv = aFontToLoad->mPrincipal->GetCsp(getter_AddRefs(csp));
|
2012-03-15 20:31:01 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
if (csp) {
|
|
|
|
channelPolicy = do_CreateInstance("@mozilla.org/nschannelpolicy;1");
|
|
|
|
channelPolicy->SetContentSecurityPolicy(csp);
|
|
|
|
channelPolicy->SetLoadType(nsIContentPolicy::TYPE_FONT);
|
|
|
|
}
|
|
|
|
rv = NS_NewChannel(getter_AddRefs(channel),
|
|
|
|
aFontFaceSrc->mURI,
|
2012-07-30 07:20:58 -07:00
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
2012-03-15 20:31:01 -07:00
|
|
|
nsIRequest::LOAD_NORMAL,
|
|
|
|
channelPolicy);
|
|
|
|
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// blocking stream is OK for data URIs
|
|
|
|
nsCOMPtr<nsIInputStream> stream;
|
|
|
|
rv = channel->Open(getter_AddRefs(stream));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint64_t bufferLength64;
|
2012-08-10 19:44:11 -07:00
|
|
|
rv = stream->Available(&bufferLength64);
|
2012-03-15 20:31:01 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2012-08-10 19:44:11 -07:00
|
|
|
if (bufferLength64 == 0) {
|
2012-03-15 20:31:01 -07:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2012-09-27 23:57:33 -07:00
|
|
|
if (bufferLength64 > UINT32_MAX) {
|
2012-08-10 19:44:11 -07:00
|
|
|
return NS_ERROR_FILE_TOO_BIG;
|
|
|
|
}
|
2012-08-22 08:56:38 -07:00
|
|
|
aBufferLength = static_cast<uint32_t>(bufferLength64);
|
2012-03-15 20:31:01 -07:00
|
|
|
|
|
|
|
// read all the decoded data
|
2012-08-22 08:56:38 -07:00
|
|
|
aBuffer = static_cast<uint8_t*> (NS_Alloc(sizeof(uint8_t) * aBufferLength));
|
2012-03-15 20:31:01 -07:00
|
|
|
if (!aBuffer) {
|
|
|
|
aBufferLength = 0;
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t numRead, totalRead = 0;
|
2012-03-15 20:31:01 -07:00
|
|
|
while (NS_SUCCEEDED(rv =
|
|
|
|
stream->Read(reinterpret_cast<char*>(aBuffer + totalRead),
|
|
|
|
aBufferLength - totalRead, &numRead)) &&
|
|
|
|
numRead != 0)
|
|
|
|
{
|
|
|
|
totalRead += numRead;
|
|
|
|
if (totalRead > aBufferLength) {
|
|
|
|
rv = NS_ERROR_FAILURE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// make sure there's a mime type
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2012-09-01 19:35:17 -07:00
|
|
|
nsAutoCString mimeType;
|
2012-03-15 20:31:01 -07:00
|
|
|
rv = channel->GetContentType(mimeType);
|
|
|
|
aBufferLength = totalRead;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_Free(aBuffer);
|
2012-07-30 07:20:58 -07:00
|
|
|
aBuffer = nullptr;
|
2012-03-15 20:31:01 -07:00
|
|
|
aBufferLength = 0;
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2013-05-21 23:42:30 -07:00
|
|
|
|
|
|
|
bool
|
|
|
|
nsUserFontSet::GetPrivateBrowsing()
|
|
|
|
{
|
2013-09-25 06:48:20 -07:00
|
|
|
nsIPresShell* ps = mPresContext->PresShell();
|
2013-05-21 23:42:30 -07:00
|
|
|
if (!ps) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-11-15 08:32:12 -08:00
|
|
|
nsCOMPtr<nsILoadContext> loadContext = ps->GetDocument()->GetLoadContext();
|
2013-05-21 23:42:30 -07:00
|
|
|
return loadContext && loadContext->UsePrivateBrowsing();
|
|
|
|
}
|
2014-04-17 07:17:22 -07:00
|
|
|
|
|
|
|
void
|
|
|
|
nsUserFontSet::DoRebuildUserFontSet()
|
|
|
|
{
|
2014-05-01 00:54:11 -07:00
|
|
|
if (!mPresContext) {
|
|
|
|
// AFAICS, this can only happen if someone has already called Destroy() on
|
|
|
|
// this font-set, which means it is in the process of being torn down --
|
|
|
|
// so there's no point trying to update its rules.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-04-17 07:17:22 -07:00
|
|
|
mPresContext->RebuildUserFontSet();
|
|
|
|
}
|