1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 // vim:cindent:ts=2:et:sw=2:
3 /* ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 1.1 (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 * http://www.mozilla.org/MPL/
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
16 * The Original Code is Mozilla Foundation code.
18 * The Initial Developer of the Original Code is
20 * Portions created by the Initial Developer are Copyright (C) 2008
21 * the Initial Developer. All Rights Reserved.
24 * John Daggett <jdaggett@mozilla.com>
26 * Alternatively, the contents of this file may be used under the terms of
27 * either of the GNU General Public License Version 2 or later (the "GPL"),
28 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 * in which case the provisions of the GPL or the LGPL are applicable instead
30 * of those above. If you wish to allow use of your version of this file only
31 * under the terms of either the GPL or the LGPL, and not to allow others to
32 * use your version of this file under the terms of the MPL, indicate your
33 * decision by deleting the provisions above and replace them with the notice
34 * and other provisions required by the GPL or the LGPL. If you do not delete
35 * the provisions above, a recipient may use your version of this file under
36 * the terms of any one of the MPL, the GPL or the LGPL.
38 * ***** END LICENSE BLOCK ***** */
40 /* code for loading in @font-face defined font data */
43 #define FORCE_PR_LOG /* Allow logging in the release build */
44 #endif /* MOZ_LOGGING */
47 #include "nsFontFaceLoader.h"
50 #include "nsILocalFile.h"
51 #include "nsIStreamListener.h"
52 #include "nsNetUtil.h"
53 #include "nsIChannelEventSink.h"
54 #include "nsIInterfaceRequestor.h"
55 #include "nsContentUtils.h"
57 #include "nsPresContext.h"
58 #include "nsIPresShell.h"
59 #include "nsIDocument.h"
61 #include "nsIPrincipal.h"
62 #include "nsIScriptSecurityManager.h"
64 #include "nsDirectoryServiceUtils.h"
65 #include "nsDirectoryServiceDefs.h"
69 static PRLogModuleInfo
*gFontDownloaderLog
= PR_NewLogModule("fontdownloader");
70 #endif /* PR_LOGGING */
72 #define LOG(args) PR_LOG(gFontDownloaderLog, PR_LOG_DEBUG, args)
73 #define LOG_ENABLED() PR_LOG_TEST(gFontDownloaderLog, PR_LOG_DEBUG)
76 #define ENFORCE_SAME_SITE_ORIGIN "gfx.downloadable_fonts.enforce_same_site_origin"
77 static PRBool gEnforceSameSiteOrigin
= PR_TRUE
;
80 CheckMayLoad(nsIDocument
* aDoc
, nsIURI
* aURI
)
82 // allow this to be disabled via a pref
83 static PRBool init
= PR_FALSE
;
87 nsContentUtils::AddBoolPrefVarCache(ENFORCE_SAME_SITE_ORIGIN
, &gEnforceSameSiteOrigin
);
90 if (!gEnforceSameSiteOrigin
)
96 nsresult rv
= aDoc
->NodePrincipal()->CheckMayLoad(aURI
, PR_TRUE
);
97 return NS_SUCCEEDED(rv
);
101 nsFontFaceLoader::nsFontFaceLoader(gfxFontEntry
*aFontToLoad
, nsIURI
*aFontURI
,
102 gfxUserFontSet::LoaderContext
*aContext
)
103 : mFontEntry(aFontToLoad
), mFontURI(aFontURI
), mLoaderContext(aContext
)
108 nsFontFaceLoader::~nsFontFaceLoader()
113 NS_IMPL_ISUPPORTS1(nsFontFaceLoader
, nsIStreamLoaderObserver
)
116 nsFontFaceLoader::OnStreamComplete(nsIStreamLoader
* aLoader
,
117 nsISupports
* aContext
,
120 const PRUint8
* aString
)
125 nsCAutoString fontURI
;
126 mFontURI
->GetSpec(fontURI
);
127 if (NS_SUCCEEDED(aStatus
)) {
128 LOG(("fontdownloader (%p) download completed - font uri: (%s)\n",
129 this, fontURI
.get()));
131 LOG(("fontdownloader (%p) download failed - font uri: (%s) error: %8.8x\n",
132 this, fontURI
.get(), aStatus
));
139 // whether an error occurred or not, notify the user font set of the completion
140 fontUpdate
= mLoaderContext
->mUserFontSet
->OnLoadComplete(mFontEntry
,
144 // when new font loaded, need to reflow
146 nsFontFaceLoaderContext
*loaderCtx
147 = static_cast<nsFontFaceLoaderContext
*> (mLoaderContext
);
149 nsIPresShell
*ps
= loaderCtx
->mPresContext
->PresShell();
151 // reflow async so that reflows coalesce
152 ps
->StyleChangeReflow();
153 LOG(("fontdownloader (%p) reflow\n", this));
161 nsFontFaceLoader::CreateHandler(gfxFontEntry
*aFontToLoad
, nsIURI
*aFontURI
,
162 gfxUserFontSet::LoaderContext
*aContext
)
164 // check same-site origin
165 nsFontFaceLoaderContext
*loaderCtx
166 = static_cast<nsFontFaceLoaderContext
*> (aContext
);
168 nsIPresShell
*ps
= loaderCtx
->mPresContext
->PresShell();
172 if (!CheckMayLoad(ps
->GetDocument(), aFontURI
))
175 nsRefPtr
<nsFontFaceLoader
> fontLoader
= new nsFontFaceLoader(aFontToLoad
,
183 nsCAutoString fontURI
;
184 aFontURI
->GetSpec(fontURI
);
185 LOG(("fontdownloader (%p) download start - font uri: (%s)\n",
186 fontLoader
.get(), fontURI
.get()));
190 nsCOMPtr
<nsIStreamLoader
> streamLoader
;
191 nsCOMPtr
<nsILoadGroup
> loadGroup(ps
->GetDocument()->GetDocumentLoadGroup());
192 nsCOMPtr
<nsIInterfaceRequestor
> sameOriginChecker
193 = nsContentUtils::GetSameOriginChecker();
195 nsresult rv
= NS_NewStreamLoader(getter_AddRefs(streamLoader
), aFontURI
,
196 fontLoader
, nsnull
, loadGroup
,
199 return NS_SUCCEEDED(rv
);