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"
51 #include "nsILocalFile.h"
52 #include "nsIStreamListener.h"
53 #include "nsNetUtil.h"
54 #include "nsIChannelEventSink.h"
55 #include "nsIInterfaceRequestor.h"
56 #include "nsContentUtils.h"
58 #include "nsPresContext.h"
59 #include "nsIPresShell.h"
60 #include "nsIDocument.h"
62 #include "nsIPrincipal.h"
63 #include "nsIScriptSecurityManager.h"
65 #include "nsDirectoryServiceUtils.h"
66 #include "nsDirectoryServiceDefs.h"
67 #include "nsIContentPolicy.h"
68 #include "nsContentPolicyUtils.h"
69 #include "nsContentErrors.h"
70 #include "nsCrossSiteListenerProxy.h"
73 static PRLogModuleInfo
*gFontDownloaderLog
= PR_NewLogModule("fontdownloader");
74 #endif /* PR_LOGGING */
76 #define LOG(args) PR_LOG(gFontDownloaderLog, PR_LOG_DEBUG, args)
77 #define LOG_ENABLED() PR_LOG_TEST(gFontDownloaderLog, PR_LOG_DEBUG)
80 nsFontFaceLoader::nsFontFaceLoader(gfxFontEntry
*aFontToLoad
, nsIURI
*aFontURI
,
82 : mFontEntry(aFontToLoad
), mFontURI(aFontURI
), mShell(aShell
)
87 nsFontFaceLoader::~nsFontFaceLoader()
92 NS_IMPL_ISUPPORTS1(nsFontFaceLoader
, nsIStreamLoaderObserver
)
95 nsFontFaceLoader::OnStreamComplete(nsIStreamLoader
* aLoader
,
96 nsISupports
* aContext
,
99 const PRUint8
* aString
)
104 nsCAutoString fontURI
;
105 mFontURI
->GetSpec(fontURI
);
106 if (NS_SUCCEEDED(aStatus
)) {
107 LOG(("fontdownloader (%p) download completed - font uri: (%s)\n",
108 this, fontURI
.get()));
110 LOG(("fontdownloader (%p) download failed - font uri: (%s) error: %8.8x\n",
111 this, fontURI
.get(), aStatus
));
118 // first, check to make sure we're not torn down
119 if (mShell
->IsDestroying()) {
123 nsPresContext
*ctx
= mShell
->GetPresContext();
128 // whether an error occurred or not, notify the user font set of the completion
129 fontUpdate
= ctx
->GetUserFontSet()->OnLoadComplete(mFontEntry
, aLoader
,
133 // when new font loaded, need to reflow
135 // Update layout for the presence of the new font. Since this is
136 // asynchronous, reflows will coalesce.
137 ctx
->UserFontSetUpdated();
138 LOG(("fontdownloader (%p) reflow\n", this));
145 nsFontFaceLoader::CheckLoadAllowed(nsIPrincipal
* aSourcePrincipal
,
147 nsISupports
* aContext
)
151 if (!aSourcePrincipal
)
154 // check content policy
155 PRInt16 shouldLoad
= nsIContentPolicy::ACCEPT
;
156 rv
= NS_CheckContentLoadPolicy(nsIContentPolicy::TYPE_FONT
,
160 EmptyCString(), // mime type
163 nsContentUtils::GetContentPolicy(),
164 nsContentUtils::GetSecurityManager());
166 if (NS_FAILED(rv
) || NS_CP_REJECTED(shouldLoad
)) {
167 return NS_ERROR_CONTENT_BLOCKED
;
173 nsUserFontSet::nsUserFontSet(nsPresContext
*aContext
)
174 : mPresContext(aContext
)
176 NS_ASSERTION(mPresContext
, "null context passed to nsUserFontSet");
179 nsUserFontSet::~nsUserFontSet()
185 nsUserFontSet::StartLoad(gfxFontEntry
*aFontToLoad
,
186 const gfxFontFaceSrc
*aFontFaceSrc
)
190 // check same-site origin
191 nsIPresShell
*ps
= mPresContext
->PresShell();
193 return NS_ERROR_FAILURE
;
195 NS_ASSERTION(aFontFaceSrc
&& !aFontFaceSrc
->mIsLocal
,
196 "bad font face url passed to fontloader");
197 NS_ASSERTION(aFontFaceSrc
->mURI
, "null font uri");
198 if (!aFontFaceSrc
->mURI
)
199 return NS_ERROR_FAILURE
;
201 // use document principal, original principal if flag set
202 // this enables user stylesheets to load font files via
204 nsCOMPtr
<nsIPrincipal
> principal
= ps
->GetDocument()->NodePrincipal();
206 NS_ASSERTION(aFontFaceSrc
->mOriginPrincipal
,
207 "null origin principal in @font-face rule");
208 if (aFontFaceSrc
->mUseOriginPrincipal
) {
209 principal
= do_QueryInterface(aFontFaceSrc
->mOriginPrincipal
);
212 rv
= nsFontFaceLoader::CheckLoadAllowed(principal
, aFontFaceSrc
->mURI
,
217 nsCAutoString fontURI
, referrerURI
;
218 aFontFaceSrc
->mURI
->GetSpec(fontURI
);
219 if (aFontFaceSrc
->mReferrer
)
220 aFontFaceSrc
->mReferrer
->GetSpec(referrerURI
);
221 LOG(("fontdownloader download blocked - font uri: (%s) "
222 "referrer uri: (%s) err: %8.8x\n",
223 fontURI
.get(), referrerURI
.get(), rv
));
229 nsRefPtr
<nsFontFaceLoader
> fontLoader
= new nsFontFaceLoader(aFontToLoad
,
233 return NS_ERROR_OUT_OF_MEMORY
;
237 nsCAutoString fontURI
, referrerURI
;
238 aFontFaceSrc
->mURI
->GetSpec(fontURI
);
239 if (aFontFaceSrc
->mReferrer
)
240 aFontFaceSrc
->mReferrer
->GetSpec(referrerURI
);
241 LOG(("fontdownloader (%p) download start - font uri: (%s) "
242 "referrer uri: (%s)\n",
243 fontLoader
.get(), fontURI
.get(), referrerURI
.get()));
247 nsCOMPtr
<nsIStreamLoader
> streamLoader
;
248 nsCOMPtr
<nsILoadGroup
> loadGroup(ps
->GetDocument()->GetDocumentLoadGroup());
250 nsCOMPtr
<nsIChannel
> channel
;
251 rv
= NS_NewChannel(getter_AddRefs(channel
),
256 nsIRequest::LOAD_NORMAL
);
258 NS_ENSURE_SUCCESS(rv
, rv
);
260 nsCOMPtr
<nsIHttpChannel
> httpChannel(do_QueryInterface(channel
));
262 httpChannel
->SetReferrer(aFontFaceSrc
->mReferrer
);
263 rv
= NS_NewStreamLoader(getter_AddRefs(streamLoader
), fontLoader
);
264 NS_ENSURE_SUCCESS(rv
, rv
);
266 PRBool inherits
= PR_FALSE
;
267 rv
= NS_URIChainHasFlags(aFontFaceSrc
->mURI
,
268 nsIProtocolHandler::URI_INHERITS_SECURITY_CONTEXT
,
270 if (NS_SUCCEEDED(rv
) && inherits
) {
271 // allow data, javascript, etc URI's
272 rv
= channel
->AsyncOpen(streamLoader
, nsnull
);
274 nsCOMPtr
<nsIStreamListener
> listener
=
275 new nsCrossSiteListenerProxy(streamLoader
, principal
, channel
,
277 NS_ENSURE_TRUE(listener
, NS_ERROR_OUT_OF_MEMORY
);
278 NS_ENSURE_SUCCESS(rv
, rv
);
280 rv
= channel
->AsyncOpen(listener
, nsnull
);