2008-10-01 07:04:10 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
// vim:cindent:ts=2:et:sw=2:
|
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* The Original Code is Mozilla Foundation code.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is
|
|
|
|
* Mozilla Foundation.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 2008
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
* John Daggett <jdaggett@mozilla.com>
|
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either of the GNU General Public License Version 2 or later (the "GPL"),
|
|
|
|
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
|
|
|
|
|
|
|
/* 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-06 03:32:12 +03:00
|
|
|
#include "nsError.h"
|
2008-10-01 07:04:10 +04:00
|
|
|
#include "nsIFile.h"
|
|
|
|
#include "nsILocalFile.h"
|
|
|
|
#include "nsIStreamListener.h"
|
|
|
|
#include "nsNetUtil.h"
|
|
|
|
#include "nsIChannelEventSink.h"
|
|
|
|
#include "nsIInterfaceRequestor.h"
|
|
|
|
#include "nsContentUtils.h"
|
2011-06-12 06:30:16 +04:00
|
|
|
#include "mozilla/Preferences.h"
|
2008-10-01 07:04:10 +04:00
|
|
|
|
|
|
|
#include "nsPresContext.h"
|
|
|
|
#include "nsIPresShell.h"
|
|
|
|
#include "nsIDocument.h"
|
|
|
|
#include "nsIFrame.h"
|
|
|
|
#include "nsIPrincipal.h"
|
|
|
|
#include "nsIScriptSecurityManager.h"
|
|
|
|
|
|
|
|
#include "nsDirectoryServiceUtils.h"
|
|
|
|
#include "nsDirectoryServiceDefs.h"
|
2008-11-06 03:32:12 +03:00
|
|
|
#include "nsIContentPolicy.h"
|
|
|
|
#include "nsContentPolicyUtils.h"
|
|
|
|
#include "nsContentErrors.h"
|
|
|
|
#include "nsCrossSiteListenerProxy.h"
|
2010-04-23 23:53:41 +04:00
|
|
|
#include "nsIContentSecurityPolicy.h"
|
|
|
|
#include "nsIChannelPolicy.h"
|
|
|
|
#include "nsChannelPolicy.h"
|
2008-10-01 07:04:10 +04:00
|
|
|
|
2011-07-12 15:24:36 +04:00
|
|
|
#include "nsIConsoleService.h"
|
|
|
|
|
2011-04-12 14:53:20 +04:00
|
|
|
#include "nsStyleSet.h"
|
2011-08-25 12:46:01 +04:00
|
|
|
#include "nsPrintfCString.h"
|
2011-04-12 14:53:20 +04:00
|
|
|
|
2011-06-12 06:30:16 +04:00
|
|
|
using namespace mozilla;
|
|
|
|
|
2008-10-01 07:04:10 +04:00
|
|
|
#ifdef PR_LOGGING
|
|
|
|
static PRLogModuleInfo *gFontDownloaderLog = PR_NewLogModule("fontdownloader");
|
|
|
|
#endif /* PR_LOGGING */
|
|
|
|
|
|
|
|
#define LOG(args) PR_LOG(gFontDownloaderLog, PR_LOG_DEBUG, args)
|
|
|
|
#define LOG_ENABLED() PR_LOG_TEST(gFontDownloaderLog, PR_LOG_DEBUG)
|
|
|
|
|
|
|
|
|
2011-04-28 09:04:45 +04:00
|
|
|
nsFontFaceLoader::nsFontFaceLoader(gfxProxyFontEntry *aProxy, nsIURI *aFontURI,
|
2009-01-19 12:21:29 +03:00
|
|
|
nsUserFontSet *aFontSet, nsIChannel *aChannel)
|
2011-04-28 09:04:45 +04:00
|
|
|
: mFontEntry(aProxy), mFontURI(aFontURI), mFontSet(aFontSet),
|
2009-01-19 12:21:29 +03:00
|
|
|
mChannel(aChannel)
|
2008-10-01 07:04:10 +04:00
|
|
|
{
|
2011-08-09 11:51:46 +04:00
|
|
|
mFontFamily = aProxy->Family();
|
2008-10-01 07:04:10 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsFontFaceLoader::~nsFontFaceLoader()
|
|
|
|
{
|
2011-01-06 00:48:48 +03:00
|
|
|
if (mLoadTimer) {
|
|
|
|
mLoadTimer->Cancel();
|
|
|
|
mLoadTimer = nsnull;
|
|
|
|
}
|
2009-01-19 12:21:29 +03:00
|
|
|
if (mFontSet) {
|
|
|
|
mFontSet->RemoveLoader(this);
|
|
|
|
}
|
2008-10-01 07:04:10 +04:00
|
|
|
}
|
|
|
|
|
2011-01-06 00:48:48 +03:00
|
|
|
void
|
|
|
|
nsFontFaceLoader::StartedLoading(nsIStreamLoader *aStreamLoader)
|
|
|
|
{
|
2011-06-12 06:30:16 +04:00
|
|
|
PRInt32 loadTimeout =
|
|
|
|
Preferences::GetInt("gfx.downloadable_fonts.fallback_delay", 3000);
|
2011-01-06 00:48:48 +03: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-28 09:04:45 +04:00
|
|
|
mFontEntry->mLoadingState = gfxProxyFontEntry::LOADING_SLOWLY;
|
2011-01-06 00:48:48 +03:00
|
|
|
}
|
|
|
|
mStreamLoader = aStreamLoader;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsFontFaceLoader::LoadTimerCallback(nsITimer *aTimer, void *aClosure)
|
|
|
|
{
|
|
|
|
nsFontFaceLoader *loader = static_cast<nsFontFaceLoader*>(aClosure);
|
|
|
|
|
2011-04-28 09:04:45 +04:00
|
|
|
gfxProxyFontEntry *pe = loader->mFontEntry.get();
|
2011-01-06 00:48:48 +03: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) {
|
|
|
|
PRInt32 contentLength;
|
|
|
|
PRUint32 numBytesRead;
|
2011-08-23 16:33:51 +04:00
|
|
|
if (NS_SUCCEEDED(loader->mChannel->GetContentLength(&contentLength)) &&
|
|
|
|
contentLength > 0 &&
|
|
|
|
NS_SUCCEEDED(loader->mStreamLoader->GetNumBytesRead(&numBytesRead)) &&
|
2011-01-06 00:48:48 +03:00
|
|
|
numBytesRead > 3 * (PRUint32(contentLength) >> 2))
|
|
|
|
{
|
|
|
|
// 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;
|
|
|
|
PRUint32 delay;
|
|
|
|
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;
|
|
|
|
nsPresContext *ctx = loader->mFontSet->GetPresContext();
|
|
|
|
NS_ASSERTION(ctx, "fontSet doesn't have a presContext?");
|
|
|
|
gfxUserFontSet *fontSet;
|
|
|
|
if (ctx && (fontSet = ctx->GetUserFontSet()) != nsnull) {
|
|
|
|
fontSet->IncrementGeneration();
|
|
|
|
ctx->UserFontSetUpdated();
|
|
|
|
LOG(("fontdownloader (%p) timeout reflow\n", loader));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-10-29 21:09:50 +03:00
|
|
|
NS_IMPL_ISUPPORTS1(nsFontFaceLoader, nsIStreamLoaderObserver)
|
2008-10-01 07:04:10 +04:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2008-10-29 21:09:50 +03:00
|
|
|
nsFontFaceLoader::OnStreamComplete(nsIStreamLoader* aLoader,
|
|
|
|
nsISupports* aContext,
|
|
|
|
nsresult aStatus,
|
|
|
|
PRUint32 aStringLen,
|
|
|
|
const PRUint8* aString)
|
2008-10-01 07:04:10 +04:00
|
|
|
{
|
2009-01-19 12:21:29 +03:00
|
|
|
if (!mFontSet) {
|
|
|
|
// We've been canceled
|
|
|
|
return aStatus;
|
|
|
|
}
|
|
|
|
|
|
|
|
mFontSet->RemoveLoader(this);
|
2008-10-01 07:04:10 +04:00
|
|
|
|
|
|
|
#ifdef PR_LOGGING
|
|
|
|
if (LOG_ENABLED()) {
|
|
|
|
nsCAutoString fontURI;
|
|
|
|
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
|
|
|
|
|
2009-01-19 12:21:29 +03:00
|
|
|
nsPresContext *ctx = mFontSet->GetPresContext();
|
|
|
|
NS_ASSERTION(ctx && !ctx->PresShell()->IsDestroying(),
|
|
|
|
"We should have been canceled already");
|
2008-12-12 10:31:51 +03:00
|
|
|
|
2008-10-01 07:04:10 +04:00
|
|
|
// whether an error occurred or not, notify the user font set of the completion
|
2009-01-07 07:45:41 +03:00
|
|
|
gfxUserFontSet *userFontSet = ctx->GetUserFontSet();
|
|
|
|
if (!userFontSet) {
|
|
|
|
return aStatus;
|
|
|
|
}
|
2009-08-30 17:55:24 +04:00
|
|
|
|
2011-07-13 17:54:54 +04: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-11 03:38:23 +04:00
|
|
|
nsCOMPtr<nsIRequest> request;
|
|
|
|
nsCOMPtr<nsIHttpChannel> httpChannel;
|
|
|
|
aLoader->GetRequest(getter_AddRefs(request));
|
|
|
|
httpChannel = do_QueryInterface(request);
|
2011-07-13 17:54:54 +04:00
|
|
|
if (httpChannel) {
|
2011-09-29 10:19:26 +04:00
|
|
|
bool succeeded;
|
2011-07-13 17:54:54 +04:00
|
|
|
nsresult rv = httpChannel->GetRequestSucceeded(&succeeded);
|
|
|
|
if (NS_SUCCEEDED(rv) && !succeeded) {
|
|
|
|
aStatus = NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-08-30 17:55:24 +04: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 17:54:54 +04: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.
|
2011-09-29 10:19:26 +04:00
|
|
|
bool fontUpdate = userFontSet->OnLoadComplete(mFontEntry,
|
2009-01-19 12:21:29 +03:00
|
|
|
aString, aStringLen,
|
|
|
|
aStatus);
|
2008-10-01 07:04:10 +04:00
|
|
|
|
|
|
|
// when new font loaded, need to reflow
|
|
|
|
if (fontUpdate) {
|
2008-12-08 19:08:05 +03:00
|
|
|
// Update layout for the presence of the new font. Since this is
|
|
|
|
// asynchronous, reflows will coalesce.
|
2008-12-12 10:31:51 +03:00
|
|
|
ctx->UserFontSetUpdated();
|
2008-12-08 19:08:05 +03:00
|
|
|
LOG(("fontdownloader (%p) reflow\n", this));
|
2008-10-01 07:04:10 +04:00
|
|
|
}
|
|
|
|
|
2009-08-30 17:55:24 +04:00
|
|
|
return NS_SUCCESS_ADOPTED_DATA;
|
2008-10-01 07:04:10 +04:00
|
|
|
}
|
|
|
|
|
2009-01-19 12:21:29 +03:00
|
|
|
void
|
|
|
|
nsFontFaceLoader::Cancel()
|
|
|
|
{
|
2011-04-28 09:04:45 +04:00
|
|
|
mFontEntry->mLoadingState = gfxProxyFontEntry::NOT_LOADING;
|
2009-01-19 12:21:29 +03:00
|
|
|
mFontSet = nsnull;
|
2011-01-06 00:48:48 +03:00
|
|
|
if (mLoadTimer) {
|
|
|
|
mLoadTimer->Cancel();
|
|
|
|
mLoadTimer = nsnull;
|
|
|
|
}
|
2009-01-19 12:21:29 +03:00
|
|
|
mChannel->Cancel(NS_BINDING_ABORTED);
|
|
|
|
}
|
|
|
|
|
2008-11-06 03:32:12 +03:00
|
|
|
nsresult
|
2008-12-12 10:31:51 +03:00
|
|
|
nsFontFaceLoader::CheckLoadAllowed(nsIPrincipal* aSourcePrincipal,
|
|
|
|
nsIURI* aTargetURI,
|
|
|
|
nsISupports* aContext)
|
2008-10-01 07:04:10 +04:00
|
|
|
{
|
2008-11-06 03:32:12 +03:00
|
|
|
nsresult rv;
|
2012-03-16 07:31:01 +04:00
|
|
|
|
2008-12-12 10:31:51 +03:00
|
|
|
if (!aSourcePrincipal)
|
|
|
|
return NS_OK;
|
2010-05-24 10:45:18 +04:00
|
|
|
|
|
|
|
// check with the security manager
|
|
|
|
nsIScriptSecurityManager *secMan = nsContentUtils::GetSecurityManager();
|
|
|
|
rv = secMan->CheckLoadURIWithPrincipal(aSourcePrincipal, aTargetURI,
|
|
|
|
nsIScriptSecurityManager::STANDARD);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2008-12-12 10:31:51 +03:00
|
|
|
// check content policy
|
|
|
|
PRInt16 shouldLoad = nsIContentPolicy::ACCEPT;
|
|
|
|
rv = NS_CheckContentLoadPolicy(nsIContentPolicy::TYPE_FONT,
|
|
|
|
aTargetURI,
|
|
|
|
aSourcePrincipal,
|
|
|
|
aContext,
|
|
|
|
EmptyCString(), // mime type
|
|
|
|
nsnull,
|
|
|
|
&shouldLoad,
|
|
|
|
nsContentUtils::GetContentPolicy(),
|
|
|
|
nsContentUtils::GetSecurityManager());
|
|
|
|
|
|
|
|
if (NS_FAILED(rv) || NS_CP_REJECTED(shouldLoad)) {
|
|
|
|
return NS_ERROR_CONTENT_BLOCKED;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2012-03-16 07:31:01 +04:00
|
|
|
|
2008-12-12 10:31:51 +03:00
|
|
|
nsUserFontSet::nsUserFontSet(nsPresContext *aContext)
|
|
|
|
: mPresContext(aContext)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(mPresContext, "null context passed to nsUserFontSet");
|
2009-01-19 12:21:29 +03:00
|
|
|
mLoaders.Init();
|
2008-12-12 10:31:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nsUserFontSet::~nsUserFontSet()
|
|
|
|
{
|
2009-01-19 12:21:29 +03:00
|
|
|
NS_ASSERTION(mLoaders.Count() == 0, "mLoaders should have been emptied");
|
|
|
|
}
|
2008-12-12 10:31:51 +03:00
|
|
|
|
2009-01-19 12:21:29 +03:00
|
|
|
static PLDHashOperator DestroyIterator(nsPtrHashKey<nsFontFaceLoader>* aKey,
|
|
|
|
void* aUserArg)
|
|
|
|
{
|
|
|
|
aKey->GetKey()->Cancel();
|
|
|
|
return PL_DHASH_REMOVE;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsUserFontSet::Destroy()
|
|
|
|
{
|
|
|
|
mPresContext = nsnull;
|
|
|
|
mLoaders.EnumerateEntries(DestroyIterator, nsnull);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsUserFontSet::RemoveLoader(nsFontFaceLoader *aLoader)
|
|
|
|
{
|
|
|
|
mLoaders.RemoveEntry(aLoader);
|
2008-12-12 10:31:51 +03:00
|
|
|
}
|
2008-10-01 07:04:10 +04:00
|
|
|
|
2012-03-16 07:31:01 +04:00
|
|
|
nsresult
|
2011-04-28 09:04:45 +04:00
|
|
|
nsUserFontSet::StartLoad(gfxProxyFontEntry *aProxy,
|
|
|
|
const gfxFontFaceSrc *aFontFaceSrc)
|
2008-12-12 10:31:51 +03:00
|
|
|
{
|
|
|
|
nsresult rv;
|
2012-03-16 07:31:01 +04:00
|
|
|
nsIPrincipal *principal = nsnull;
|
2012-03-16 07:31:01 +04:00
|
|
|
|
2012-03-16 07:31:01 +04:00
|
|
|
rv = CheckFontLoad(aProxy, aFontFaceSrc, &principal);
|
2012-03-16 07:31:01 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2008-12-12 10:31:51 +03:00
|
|
|
nsIPresShell *ps = mPresContext->PresShell();
|
2008-10-01 07:04:10 +04:00
|
|
|
if (!ps)
|
2008-11-06 03:32:12 +03:00
|
|
|
return NS_ERROR_FAILURE;
|
2009-01-19 12:21:29 +03:00
|
|
|
|
|
|
|
nsCOMPtr<nsIStreamLoader> streamLoader;
|
|
|
|
nsCOMPtr<nsILoadGroup> loadGroup(ps->GetDocument()->GetDocumentLoadGroup());
|
|
|
|
|
|
|
|
nsCOMPtr<nsIChannel> channel;
|
2010-04-23 23:53:41 +04:00
|
|
|
// get Content Security Policy from principal to pass into channel
|
|
|
|
nsCOMPtr<nsIChannelPolicy> channelPolicy;
|
|
|
|
nsCOMPtr<nsIContentSecurityPolicy> csp;
|
|
|
|
rv = principal->GetCsp(getter_AddRefs(csp));
|
|
|
|
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 12:21:29 +03:00
|
|
|
rv = NS_NewChannel(getter_AddRefs(channel),
|
|
|
|
aFontFaceSrc->mURI,
|
|
|
|
nsnull,
|
|
|
|
loadGroup,
|
|
|
|
nsnull,
|
2010-04-23 23:53:41 +04:00
|
|
|
nsIRequest::LOAD_NORMAL,
|
|
|
|
channelPolicy);
|
|
|
|
|
2009-01-19 12:21:29 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsRefPtr<nsFontFaceLoader> fontLoader =
|
2011-04-28 09:04:45 +04:00
|
|
|
new nsFontFaceLoader(aProxy, aFontFaceSrc->mURI, this, channel);
|
2009-01-19 12:21:29 +03:00
|
|
|
|
2008-10-29 21:09:50 +03:00
|
|
|
if (!fontLoader)
|
2008-11-06 03:32:12 +03:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2008-10-01 07:04:10 +04:00
|
|
|
|
2008-10-29 21:09:50 +03:00
|
|
|
#ifdef PR_LOGGING
|
|
|
|
if (LOG_ENABLED()) {
|
2008-11-06 03:32:12 +03:00
|
|
|
nsCAutoString fontURI, referrerURI;
|
2008-11-27 06:50:16 +03:00
|
|
|
aFontFaceSrc->mURI->GetSpec(fontURI);
|
|
|
|
if (aFontFaceSrc->mReferrer)
|
|
|
|
aFontFaceSrc->mReferrer->GetSpec(referrerURI);
|
2008-11-06 03:32:12 +03:00
|
|
|
LOG(("fontdownloader (%p) download start - font uri: (%s) "
|
2012-03-16 07:31:01 +04:00
|
|
|
"referrer uri: (%s)\n",
|
2008-11-06 03:32:12 +03:00
|
|
|
fontLoader.get(), fontURI.get(), referrerURI.get()));
|
2008-10-29 21:09:50 +03:00
|
|
|
}
|
2012-03-16 07:31:01 +04:00
|
|
|
#endif
|
2008-10-29 21:09:50 +03:00
|
|
|
|
2008-11-06 03:32:12 +03:00
|
|
|
nsCOMPtr<nsIHttpChannel> httpChannel(do_QueryInterface(channel));
|
|
|
|
if (httpChannel)
|
2008-11-27 06:50:16 +03:00
|
|
|
httpChannel->SetReferrer(aFontFaceSrc->mReferrer);
|
2008-11-06 03:32:12 +03:00
|
|
|
rv = NS_NewStreamLoader(getter_AddRefs(streamLoader), fontLoader);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2012-03-16 07:31:01 +04:00
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool inherits = false;
|
2008-11-27 06:50:16 +03:00
|
|
|
rv = NS_URIChainHasFlags(aFontFaceSrc->mURI,
|
|
|
|
nsIProtocolHandler::URI_INHERITS_SECURITY_CONTEXT,
|
|
|
|
&inherits);
|
|
|
|
if (NS_SUCCEEDED(rv) && inherits) {
|
|
|
|
// allow data, javascript, etc URI's
|
2008-11-06 03:32:12 +03:00
|
|
|
rv = channel->AsyncOpen(streamLoader, nsnull);
|
|
|
|
} else {
|
|
|
|
nsCOMPtr<nsIStreamListener> listener =
|
2012-03-16 07:31:01 +04:00
|
|
|
new nsCORSListenerProxy(streamLoader, principal, channel,
|
2011-10-17 18:59:28 +04:00
|
|
|
false, &rv);
|
2009-01-31 11:58:42 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
fontLoader->DropChannel(); // explicitly need to break ref cycle
|
|
|
|
}
|
2008-11-06 03:32:12 +03:00
|
|
|
NS_ENSURE_TRUE(listener, NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = channel->AsyncOpen(listener, nsnull);
|
|
|
|
}
|
|
|
|
|
2009-01-19 12:21:29 +03:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
mLoaders.PutEntry(fontLoader);
|
2011-01-06 00:48:48 +03:00
|
|
|
fontLoader->StartedLoading(streamLoader);
|
2009-01-19 12:21:29 +03:00
|
|
|
}
|
|
|
|
|
2008-11-06 03:32:12 +03:00
|
|
|
return rv;
|
|
|
|
}
|
2011-04-12 14:53:20 +04:00
|
|
|
|
2012-02-02 15:24:22 +04:00
|
|
|
static PLDHashOperator DetachFontEntries(const nsAString& aKey,
|
|
|
|
nsRefPtr<gfxMixedFontFamily>& aFamily,
|
|
|
|
void* aUserArg)
|
|
|
|
{
|
|
|
|
aFamily->DetachFontEntries();
|
|
|
|
return PL_DHASH_NEXT;
|
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2011-04-12 14:53:20 +04:00
|
|
|
nsUserFontSet::UpdateRules(const nsTArray<nsFontFaceRuleContainer>& aRules)
|
|
|
|
{
|
2011-09-29 10:19:26 +04:00
|
|
|
bool modified = false;
|
2011-05-01 19:10:20 +04:00
|
|
|
|
2011-04-28 09:04:45 +04:00
|
|
|
// destroy any current loaders, as the entries they refer to
|
|
|
|
// may be about to get replaced
|
2011-05-01 19:10:20 +04:00
|
|
|
if (mLoaders.Count() > 0) {
|
2011-10-17 18:59:28 +04:00
|
|
|
modified = true; // trigger reflow so that any necessary downloads
|
2011-05-01 19:10:20 +04:00
|
|
|
// will be reinitiated
|
|
|
|
}
|
2011-04-28 09:04:45 +04:00
|
|
|
mLoaders.EnumerateEntries(DestroyIterator, nsnull);
|
|
|
|
|
2011-04-12 14:53:20 +04:00
|
|
|
nsTArray<FontFaceRuleRecord> oldRules;
|
|
|
|
mRules.SwapElements(oldRules);
|
|
|
|
|
|
|
|
// destroy the font family records; we need to re-create them
|
|
|
|
// because we might end up with faces in a different order,
|
|
|
|
// even if they're the same font entries as before
|
2012-02-02 15:24:22 +04:00
|
|
|
mFontFamilies.Enumerate(DetachFontEntries, nsnull);
|
2011-04-12 14:53:20 +04:00
|
|
|
mFontFamilies.Clear();
|
|
|
|
|
|
|
|
for (PRUint32 i = 0, i_end = aRules.Length(); i < i_end; ++i) {
|
|
|
|
// insert each rule into our list, migrating old font entries if possible
|
|
|
|
// rather than creating new ones; set modified to true if we detect
|
|
|
|
// that rule ordering has changed, or if a new entry is created
|
|
|
|
InsertRule(aRules[i].mRule, aRules[i].mSheetType, oldRules, modified);
|
|
|
|
}
|
|
|
|
|
|
|
|
// if any rules are left in the old list, note that the set has changed
|
|
|
|
if (oldRules.Length() > 0) {
|
2011-10-17 18:59:28 +04:00
|
|
|
modified = true;
|
2011-04-12 14:53:20 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (modified) {
|
|
|
|
IncrementGeneration();
|
|
|
|
}
|
|
|
|
|
|
|
|
return modified;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsUserFontSet::InsertRule(nsCSSFontFaceRule *aRule, PRUint8 aSheetType,
|
|
|
|
nsTArray<FontFaceRuleRecord>& aOldRules,
|
2011-09-29 10:19:26 +04:00
|
|
|
bool& aFontSetModified)
|
2011-04-12 14:53:20 +04:00
|
|
|
{
|
2011-04-08 05:23:46 +04:00
|
|
|
NS_ABORT_IF_FALSE(aRule->GetType() == mozilla::css::Rule::FONT_FACE_RULE,
|
2011-04-12 14:53:20 +04:00
|
|
|
"InsertRule passed a non-fontface CSS rule");
|
|
|
|
|
|
|
|
// set up family name
|
|
|
|
nsAutoString fontfamily;
|
|
|
|
nsCSSValue val;
|
|
|
|
PRUint32 unit;
|
|
|
|
|
|
|
|
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
|
|
|
|
for (PRUint32 i = 0; i < aOldRules.Length(); ++i) {
|
|
|
|
const FontFaceRuleRecord& ruleRec = aOldRules[i];
|
|
|
|
if (ruleRec.mContainer.mRule == aRule &&
|
|
|
|
ruleRec.mContainer.mSheetType == aSheetType) {
|
|
|
|
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 18:59:28 +04:00
|
|
|
aFontSetModified = true;
|
2011-04-12 14:53:20 +04:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// this is a new rule:
|
|
|
|
|
|
|
|
PRUint32 weight = NS_STYLE_FONT_WEIGHT_NORMAL;
|
|
|
|
PRUint32 stretch = NS_STYLE_FONT_STRETCH_NORMAL;
|
2012-03-15 13:04:46 +04:00
|
|
|
PRUint32 italicStyle = NS_STYLE_FONT_STYLE_NORMAL;
|
2012-04-26 10:24:26 +04:00
|
|
|
nsString languageOverride;
|
2011-04-12 14:53:20 +04:00
|
|
|
|
|
|
|
// set up weight
|
|
|
|
aRule->GetDesc(eCSSFontDesc_Weight, val);
|
|
|
|
unit = val.GetUnit();
|
|
|
|
if (unit == eCSSUnit_Integer || unit == eCSSUnit_Enumerated) {
|
|
|
|
weight = val.GetIntValue();
|
|
|
|
} 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 13:04:46 +04:00
|
|
|
italicStyle = NS_STYLE_FONT_STYLE_NORMAL;
|
2011-04-12 14:53:20 +04:00
|
|
|
} else {
|
|
|
|
NS_ASSERTION(unit == eCSSUnit_Null,
|
|
|
|
"@font-face style has unexpected unit");
|
|
|
|
}
|
|
|
|
|
|
|
|
// set up font features
|
2012-04-26 10:24:26 +04:00
|
|
|
nsTArray<gfxFontFeature> featureSettings;
|
2011-04-12 14:53:20 +04:00
|
|
|
aRule->GetDesc(eCSSFontDesc_FontFeatureSettings, val);
|
|
|
|
unit = val.GetUnit();
|
|
|
|
if (unit == eCSSUnit_Normal) {
|
2012-04-26 10:24:26 +04:00
|
|
|
// empty list of features
|
|
|
|
} else if (unit == eCSSUnit_PairList || unit == eCSSUnit_PairListDep) {
|
|
|
|
nsRuleNode::ComputeFontFeatures(val.GetPairListValue(), featureSettings);
|
2011-04-12 14:53:20 +04: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) {
|
|
|
|
val.GetStringValue(languageOverride);
|
|
|
|
} 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) {
|
|
|
|
nsCSSValue::Array *srcArr = val.GetArrayValue();
|
|
|
|
size_t numSrc = srcArr->Count();
|
|
|
|
|
|
|
|
for (size_t i = 0; i < numSrc; i++) {
|
|
|
|
val = srcArr->Item(i);
|
|
|
|
unit = val.GetUnit();
|
|
|
|
gfxFontFaceSrc *face = srcArray.AppendElements(1);
|
|
|
|
if (!face)
|
|
|
|
return;
|
|
|
|
|
|
|
|
switch (unit) {
|
|
|
|
|
|
|
|
case eCSSUnit_Local_Font:
|
|
|
|
val.GetStringValue(face->mLocalName);
|
2011-10-17 18:59:28 +04:00
|
|
|
face->mIsLocal = true;
|
2011-04-12 14:53:20 +04:00
|
|
|
face->mURI = nsnull;
|
|
|
|
face->mFormatFlags = 0;
|
|
|
|
break;
|
|
|
|
case eCSSUnit_URL:
|
2011-10-17 18:59:28 +04:00
|
|
|
face->mIsLocal = false;
|
2011-04-12 14:53:20 +04:00
|
|
|
face->mURI = val.GetURLValue();
|
|
|
|
NS_ASSERTION(face->mURI, "null url in @font-face rule");
|
|
|
|
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++;
|
|
|
|
}
|
|
|
|
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 18:59:28 +04:00
|
|
|
aFontSetModified = true;
|
2011-04-12 14:53:20 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsUserFontSet::ReplaceFontEntry(gfxProxyFontEntry *aProxy,
|
|
|
|
gfxFontEntry *aFontEntry)
|
|
|
|
{
|
|
|
|
for (PRUint32 i = 0; i < mRules.Length(); ++i) {
|
|
|
|
if (mRules[i].mFontEntry == aProxy) {
|
|
|
|
mRules[i].mFontEntry = aFontEntry;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2012-02-06 00:10:50 +04:00
|
|
|
gfxMixedFontFamily *family =
|
|
|
|
static_cast<gfxMixedFontFamily*>(aProxy->Family());
|
|
|
|
if (family) {
|
|
|
|
family->ReplaceFontEntry(aProxy, aFontEntry);
|
|
|
|
}
|
2011-04-12 14:53:20 +04:00
|
|
|
}
|
2011-06-16 10:31:36 +04:00
|
|
|
|
|
|
|
nsCSSFontFaceRule*
|
|
|
|
nsUserFontSet::FindRuleForEntry(gfxFontEntry *aFontEntry)
|
|
|
|
{
|
|
|
|
for (PRUint32 i = 0; i < mRules.Length(); ++i) {
|
|
|
|
if (mRules[i].mFontEntry == aFontEntry) {
|
|
|
|
return mRules[i].mContainer.mRule;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nsnull;
|
|
|
|
}
|
2011-07-12 15:24:36 +04:00
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsUserFontSet::LogMessage(gfxProxyFontEntry *aProxy,
|
|
|
|
const char *aMessage,
|
|
|
|
PRUint32 aFlags,
|
|
|
|
nsresult aStatus)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIConsoleService>
|
|
|
|
console(do_GetService(NS_CONSOLESERVICE_CONTRACTID));
|
|
|
|
if (!console) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_ConvertUTF16toUTF8 familyName(aProxy->FamilyName());
|
|
|
|
nsCAutoString fontURI;
|
2012-01-05 14:14:11 +04:00
|
|
|
if (aProxy->mSrcIndex == aProxy->mSrcList.Length()) {
|
|
|
|
fontURI.AppendLiteral("(end of source list)");
|
2011-07-15 18:16:24 +04:00
|
|
|
} else {
|
2012-01-05 14:14:11 +04:00
|
|
|
if (aProxy->mSrcList[aProxy->mSrcIndex].mURI) {
|
|
|
|
aProxy->mSrcList[aProxy->mSrcIndex].mURI->GetSpec(fontURI);
|
|
|
|
} else {
|
|
|
|
fontURI.AppendLiteral("(invalid URI)");
|
|
|
|
}
|
2011-07-15 18:16:24 +04:00
|
|
|
}
|
2011-07-12 15:24:36 +04:00
|
|
|
|
|
|
|
char weightKeywordBuf[8]; // plenty to sprintf() a PRUint16
|
|
|
|
const char *weightKeyword;
|
|
|
|
const nsAFlatCString& weightKeywordString =
|
|
|
|
nsCSSProps::ValueToKeyword(aProxy->Weight(),
|
|
|
|
nsCSSProps::kFontWeightKTable);
|
|
|
|
if (weightKeywordString.Length() > 0) {
|
|
|
|
weightKeyword = weightKeywordString.get();
|
|
|
|
} else {
|
|
|
|
sprintf(weightKeywordBuf, "%u", aProxy->Weight());
|
|
|
|
weightKeyword = weightKeywordBuf;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsPrintfCString
|
2012-04-24 22:43:00 +04:00
|
|
|
msg("downloadable font: %s "
|
2011-07-12 15:24:36 +04: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);
|
|
|
|
|
|
|
|
if (aStatus != 0) {
|
|
|
|
msg.Append(": ");
|
|
|
|
switch (aStatus) {
|
|
|
|
case NS_ERROR_DOM_BAD_URI:
|
|
|
|
msg.Append("bad URI or cross-site access not allowed");
|
|
|
|
break;
|
|
|
|
case NS_ERROR_CONTENT_BLOCKED:
|
|
|
|
msg.Append("content blocked");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
msg.Append("status=");
|
|
|
|
msg.AppendInt(aStatus);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
msg.Append("\nsource: ");
|
|
|
|
msg.Append(fontURI);
|
|
|
|
|
|
|
|
#ifdef PR_LOGGING
|
|
|
|
if (PR_LOG_TEST(sUserFontsLog, PR_LOG_DEBUG)) {
|
|
|
|
PR_LOG(sUserFontsLog, PR_LOG_DEBUG,
|
|
|
|
("userfonts (%p) %s", this, msg.get()));
|
|
|
|
}
|
|
|
|
#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);
|
|
|
|
rv = sheet->GetHref(href);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
2011-12-22 01:51:29 +04:00
|
|
|
nsCOMPtr<nsIScriptError> scriptError =
|
2011-07-12 15:24:36 +04:00
|
|
|
do_CreateInstance(NS_SCRIPTERROR_CONTRACTID, &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2011-08-25 00:44:35 +04:00
|
|
|
PRUint64 innerWindowID = GetPresContext()->Document()->InnerWindowID();
|
2011-07-12 15:24:36 +04:00
|
|
|
rv = scriptError->InitWithWindowID(NS_ConvertUTF8toUTF16(msg).get(),
|
|
|
|
href.get(), // file
|
|
|
|
text.get(), // src line
|
|
|
|
0, 0, // line & column number
|
|
|
|
aFlags, // flags
|
|
|
|
"CSS Loader", // category (make separate?)
|
2011-08-25 00:44:35 +04:00
|
|
|
innerWindowID);
|
2011-12-22 01:51:29 +04:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
console->LogMessage(scriptError);
|
2011-07-12 15:24:36 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2012-03-16 07:31:01 +04:00
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsUserFontSet::CheckFontLoad(gfxProxyFontEntry *aFontToLoad,
|
|
|
|
const gfxFontFaceSrc *aFontFaceSrc,
|
2012-03-16 07:31:01 +04:00
|
|
|
nsIPrincipal **aPrincipal)
|
2012-03-16 07:31:01 +04:00
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
// check same-site origin
|
|
|
|
nsIPresShell *ps = mPresContext->PresShell();
|
|
|
|
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-16 07:31:01 +04:00
|
|
|
*aPrincipal = ps->GetDocument()->NodePrincipal();
|
2012-03-16 07:31:01 +04:00
|
|
|
|
|
|
|
NS_ASSERTION(aFontFaceSrc->mOriginPrincipal,
|
|
|
|
"null origin principal in @font-face rule");
|
|
|
|
if (aFontFaceSrc->mUseOriginPrincipal) {
|
2012-03-16 07:31:01 +04:00
|
|
|
nsCOMPtr<nsIPrincipal> p = do_QueryInterface(aFontFaceSrc->mOriginPrincipal);
|
|
|
|
*aPrincipal = p;
|
2012-03-16 07:31:01 +04:00
|
|
|
}
|
|
|
|
|
2012-03-16 07:31:01 +04:00
|
|
|
rv = nsFontFaceLoader::CheckLoadAllowed(*aPrincipal, aFontFaceSrc->mURI,
|
2012-03-16 07:31:01 +04:00
|
|
|
ps->GetDocument());
|
|
|
|
if (NS_FAILED(rv)) {
|
2012-03-16 07:31:01 +04:00
|
|
|
LogMessage(aFontToLoad, "download not allowed",
|
|
|
|
nsIScriptError::errorFlag, rv);
|
2012-03-16 07:31:01 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsUserFontSet::SyncLoadFontData(gfxProxyFontEntry *aFontToLoad,
|
|
|
|
const gfxFontFaceSrc *aFontFaceSrc,
|
|
|
|
PRUint8* &aBuffer,
|
|
|
|
PRUint32 &aBufferLength)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
2012-03-16 07:31:01 +04:00
|
|
|
nsIPrincipal *principal = nsnull;
|
2012-03-16 07:31:01 +04:00
|
|
|
|
2012-03-16 07:31:01 +04:00
|
|
|
rv = CheckFontLoad(aFontToLoad, aFontFaceSrc, &principal);
|
2012-03-16 07:31:01 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIChannel> channel;
|
|
|
|
// get Content Security Policy from principal to pass into channel
|
|
|
|
nsCOMPtr<nsIChannelPolicy> channelPolicy;
|
|
|
|
nsCOMPtr<nsIContentSecurityPolicy> csp;
|
|
|
|
rv = principal->GetCsp(getter_AddRefs(csp));
|
|
|
|
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,
|
|
|
|
nsnull,
|
|
|
|
nsnull,
|
|
|
|
nsnull,
|
|
|
|
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);
|
|
|
|
|
|
|
|
rv = stream->Available(&aBufferLength);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
if (aBufferLength == 0) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// read all the decoded data
|
|
|
|
aBuffer = static_cast<PRUint8*> (NS_Alloc(sizeof(PRUint8) * aBufferLength));
|
|
|
|
if (!aBuffer) {
|
|
|
|
aBufferLength = 0;
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRUint32 numRead, totalRead = 0;
|
|
|
|
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)) {
|
|
|
|
nsCAutoString mimeType;
|
|
|
|
rv = channel->GetContentType(mimeType);
|
|
|
|
aBufferLength = totalRead;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_Free(aBuffer);
|
|
|
|
aBuffer = nsnull;
|
|
|
|
aBufferLength = 0;
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|