Bug 470455 - test_database_sync_embed_visits.js leaks, r=sdwilsh
[wine-gecko.git] / layout / style / nsFontFaceLoader.cpp
blob893d3c0570913423b77c57f8b71ad2ad279c7120
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 "nsError.h"
50 #include "nsIFile.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"
61 #include "nsIFrame.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"
72 #ifdef PR_LOGGING
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,
81 nsIPresShell *aShell)
82 : mFontEntry(aFontToLoad), mFontURI(aFontURI), mShell(aShell)
87 nsFontFaceLoader::~nsFontFaceLoader()
92 NS_IMPL_ISUPPORTS1(nsFontFaceLoader, nsIStreamLoaderObserver)
94 NS_IMETHODIMP
95 nsFontFaceLoader::OnStreamComplete(nsIStreamLoader* aLoader,
96 nsISupports* aContext,
97 nsresult aStatus,
98 PRUint32 aStringLen,
99 const PRUint8* aString)
102 #ifdef PR_LOGGING
103 if (LOG_ENABLED()) {
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()));
109 } else {
110 LOG(("fontdownloader (%p) download failed - font uri: (%s) error: %8.8x\n",
111 this, fontURI.get(), aStatus));
114 #endif
116 PRBool fontUpdate;
118 // first, check to make sure we're not torn down
119 if (mShell->IsDestroying()) {
120 return aStatus;
123 nsPresContext *ctx = mShell->GetPresContext();
124 if (!ctx) {
125 return aStatus;
128 // whether an error occurred or not, notify the user font set of the completion
129 fontUpdate = ctx->GetUserFontSet()->OnLoadComplete(mFontEntry, aLoader,
130 aString, aStringLen,
131 aStatus);
133 // when new font loaded, need to reflow
134 if (fontUpdate) {
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));
141 return aStatus;
144 nsresult
145 nsFontFaceLoader::CheckLoadAllowed(nsIPrincipal* aSourcePrincipal,
146 nsIURI* aTargetURI,
147 nsISupports* aContext)
149 nsresult rv;
151 if (!aSourcePrincipal)
152 return NS_OK;
154 // check content policy
155 PRInt16 shouldLoad = nsIContentPolicy::ACCEPT;
156 rv = NS_CheckContentLoadPolicy(nsIContentPolicy::TYPE_FONT,
157 aTargetURI,
158 aSourcePrincipal,
159 aContext,
160 EmptyCString(), // mime type
161 nsnull,
162 &shouldLoad,
163 nsContentUtils::GetContentPolicy(),
164 nsContentUtils::GetSecurityManager());
166 if (NS_FAILED(rv) || NS_CP_REJECTED(shouldLoad)) {
167 return NS_ERROR_CONTENT_BLOCKED;
170 return NS_OK;
173 nsUserFontSet::nsUserFontSet(nsPresContext *aContext)
174 : mPresContext(aContext)
176 NS_ASSERTION(mPresContext, "null context passed to nsUserFontSet");
179 nsUserFontSet::~nsUserFontSet()
184 nsresult
185 nsUserFontSet::StartLoad(gfxFontEntry *aFontToLoad,
186 const gfxFontFaceSrc *aFontFaceSrc)
188 nsresult rv;
190 // check same-site origin
191 nsIPresShell *ps = mPresContext->PresShell();
192 if (!ps)
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
203 // @font-face rules
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,
213 ps->GetDocument());
214 if (NS_FAILED(rv)) {
215 #ifdef PR_LOGGING
216 if (LOG_ENABLED()) {
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));
225 #endif
226 return rv;
229 nsRefPtr<nsFontFaceLoader> fontLoader = new nsFontFaceLoader(aFontToLoad,
230 aFontFaceSrc->mURI,
231 ps);
232 if (!fontLoader)
233 return NS_ERROR_OUT_OF_MEMORY;
235 #ifdef PR_LOGGING
236 if (LOG_ENABLED()) {
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()));
245 #endif
247 nsCOMPtr<nsIStreamLoader> streamLoader;
248 nsCOMPtr<nsILoadGroup> loadGroup(ps->GetDocument()->GetDocumentLoadGroup());
250 nsCOMPtr<nsIChannel> channel;
251 rv = NS_NewChannel(getter_AddRefs(channel),
252 aFontFaceSrc->mURI,
253 nsnull,
254 loadGroup,
255 nsnull,
256 nsIRequest::LOAD_NORMAL);
258 NS_ENSURE_SUCCESS(rv, rv);
260 nsCOMPtr<nsIHttpChannel> httpChannel(do_QueryInterface(channel));
261 if (httpChannel)
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,
269 &inherits);
270 if (NS_SUCCEEDED(rv) && inherits) {
271 // allow data, javascript, etc URI's
272 rv = channel->AsyncOpen(streamLoader, nsnull);
273 } else {
274 nsCOMPtr<nsIStreamListener> listener =
275 new nsCrossSiteListenerProxy(streamLoader, principal, channel,
276 PR_FALSE, &rv);
277 NS_ENSURE_TRUE(listener, NS_ERROR_OUT_OF_MEMORY);
278 NS_ENSURE_SUCCESS(rv, rv);
280 rv = channel->AsyncOpen(listener, nsnull);
283 return rv;