Bug 458861. Validate TrueType headers before activating downloaded font. r=roc, sr...
[wine-gecko.git] / layout / style / nsFontFaceLoader.cpp
blob5208247f3a1447c88db00c3311601838e57d30c5
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
14 * License.
16 * The Original Code is Mozilla Foundation code.
18 * The Initial Developer of the Original Code is
19 * Mozilla Foundation.
20 * Portions created by the Initial Developer are Copyright (C) 2008
21 * the Initial Developer. All Rights Reserved.
23 * Contributor(s):
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 */
42 #ifdef MOZ_LOGGING
43 #define FORCE_PR_LOG /* Allow logging in the release build */
44 #endif /* MOZ_LOGGING */
45 #include "prlog.h"
47 #include "nsFontFaceLoader.h"
49 #include "nsIFile.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"
60 #include "nsIFrame.h"
61 #include "nsIPrincipal.h"
62 #include "nsIScriptSecurityManager.h"
64 #include "nsDirectoryServiceUtils.h"
65 #include "nsDirectoryServiceDefs.h"
68 #ifdef PR_LOGGING
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;
79 static PRBool
80 CheckMayLoad(nsIDocument* aDoc, nsIURI* aURI)
82 // allow this to be disabled via a pref
83 static PRBool init = PR_FALSE;
85 if (!init) {
86 init = PR_TRUE;
87 nsContentUtils::AddBoolPrefVarCache(ENFORCE_SAME_SITE_ORIGIN, &gEnforceSameSiteOrigin);
90 if (!gEnforceSameSiteOrigin)
91 return PR_TRUE;
93 if (!aDoc)
94 return PR_FALSE;
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)
115 NS_IMETHODIMP
116 nsFontFaceLoader::OnStreamComplete(nsIStreamLoader* aLoader,
117 nsISupports* aContext,
118 nsresult aStatus,
119 PRUint32 aStringLen,
120 const PRUint8* aString)
123 #ifdef PR_LOGGING
124 if (LOG_ENABLED()) {
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()));
130 } else {
131 LOG(("fontdownloader (%p) download failed - font uri: (%s) error: %8.8x\n",
132 this, fontURI.get(), aStatus));
135 #endif
137 PRBool fontUpdate;
139 // whether an error occurred or not, notify the user font set of the completion
140 fontUpdate = mLoaderContext->mUserFontSet->OnLoadComplete(mFontEntry,
141 aString, aStringLen,
142 aStatus);
144 // when new font loaded, need to reflow
145 if (fontUpdate) {
146 nsFontFaceLoaderContext *loaderCtx
147 = static_cast<nsFontFaceLoaderContext*> (mLoaderContext);
149 nsIPresShell *ps = loaderCtx->mPresContext->PresShell();
150 if (ps) {
151 // reflow async so that reflows coalesce
152 ps->StyleChangeReflow();
153 LOG(("fontdownloader (%p) reflow\n", this));
157 return aStatus;
160 PRBool
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();
169 if (!ps)
170 return PR_FALSE;
172 if (!CheckMayLoad(ps->GetDocument(), aFontURI))
173 return PR_FALSE;
175 nsRefPtr<nsFontFaceLoader> fontLoader = new nsFontFaceLoader(aFontToLoad,
176 aFontURI,
177 aContext);
178 if (!fontLoader)
179 return PR_FALSE;
181 #ifdef PR_LOGGING
182 if (LOG_ENABLED()) {
183 nsCAutoString fontURI;
184 aFontURI->GetSpec(fontURI);
185 LOG(("fontdownloader (%p) download start - font uri: (%s)\n",
186 fontLoader.get(), fontURI.get()));
188 #endif
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,
197 sameOriginChecker);
199 return NS_SUCCEEDED(rv);