Bug 460926 A11y hierachy is broken on Ubuntu 8.10 (GNOME 2.24), r=Evan.Yan sr=roc
[wine-gecko.git] / layout / style / nsFontFaceLoader.cpp
blob0f2890a7178c7dedf705140fbc54ef38fbdfeec2
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 gfxUserFontSet::LoaderContext *aContext)
82 : mFontEntry(aFontToLoad), mFontURI(aFontURI), mLoaderContext(aContext)
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 // whether an error occurred or not, notify the user font set of the completion
119 fontUpdate = mLoaderContext->mUserFontSet->OnLoadComplete(mFontEntry, aLoader,
120 aString, aStringLen,
121 aStatus);
123 // when new font loaded, need to reflow
124 if (fontUpdate) {
125 nsFontFaceLoaderContext *loaderCtx
126 = static_cast<nsFontFaceLoaderContext*> (mLoaderContext);
128 nsIPresShell *ps = loaderCtx->mPresContext->PresShell();
129 if (ps) {
130 // Update layout for the presence of the new font. Since this is
131 // asynchronous, reflows will coalesce.
132 // nsPresContext::FlushUserFontSet does the same thing when we
133 // remove a user font set, for fonts becoming unavailable, or when
134 // we add one, because if we change it dynamically we need to
135 // trigger reflow to cause gfx to request the fonts.
136 ps->StyleChangeReflow();
137 LOG(("fontdownloader (%p) reflow\n", this));
141 return aStatus;
144 nsresult
145 nsFontFaceLoader::CreateHandler(gfxFontEntry *aFontToLoad,
146 const gfxFontFaceSrc *aFontFaceSrc,
147 gfxUserFontSet::LoaderContext *aContext)
149 nsresult rv;
151 // check same-site origin
152 nsFontFaceLoaderContext *loaderCtx
153 = static_cast<nsFontFaceLoaderContext*> (aContext);
155 nsIPresShell *ps = loaderCtx->mPresContext->PresShell();
156 if (!ps)
157 return NS_ERROR_FAILURE;
159 NS_ASSERTION(aFontFaceSrc && !aFontFaceSrc->mIsLocal,
160 "bad font face url passed to fontloader");
161 NS_ASSERTION(aFontFaceSrc->mURI, "null font uri");
162 if (!aFontFaceSrc->mURI)
163 return NS_ERROR_FAILURE;
165 // use document principal, original principal if flag set
166 // this enables user stylesheets to load font files via
167 // @font-face rules
168 nsCOMPtr<nsIPrincipal> principal = ps->GetDocument()->NodePrincipal();
170 NS_ASSERTION(aFontFaceSrc->mOriginPrincipal,
171 "null origin principal in @font-face rule");
172 if (aFontFaceSrc->mUseOriginPrincipal) {
173 principal = do_QueryInterface(aFontFaceSrc->mOriginPrincipal);
176 rv = CheckLoadAllowed(principal, aFontFaceSrc->mURI, ps->GetDocument());
177 if (NS_FAILED(rv)) {
178 #ifdef PR_LOGGING
179 if (LOG_ENABLED()) {
180 nsCAutoString fontURI, referrerURI;
181 aFontFaceSrc->mURI->GetSpec(fontURI);
182 if (aFontFaceSrc->mReferrer)
183 aFontFaceSrc->mReferrer->GetSpec(referrerURI);
184 LOG(("fontdownloader download blocked - font uri: (%s) "
185 "referrer uri: (%s) err: %8.8x\n",
186 fontURI.get(), referrerURI.get(), rv));
188 #endif
189 return rv;
192 nsRefPtr<nsFontFaceLoader> fontLoader = new nsFontFaceLoader(aFontToLoad,
193 aFontFaceSrc->mURI,
194 aContext);
195 if (!fontLoader)
196 return NS_ERROR_OUT_OF_MEMORY;
198 #ifdef PR_LOGGING
199 if (LOG_ENABLED()) {
200 nsCAutoString fontURI, referrerURI;
201 aFontFaceSrc->mURI->GetSpec(fontURI);
202 if (aFontFaceSrc->mReferrer)
203 aFontFaceSrc->mReferrer->GetSpec(referrerURI);
204 LOG(("fontdownloader (%p) download start - font uri: (%s) "
205 "referrer uri: (%s)\n",
206 fontLoader.get(), fontURI.get(), referrerURI.get()));
208 #endif
210 nsCOMPtr<nsIStreamLoader> streamLoader;
211 nsCOMPtr<nsILoadGroup> loadGroup(ps->GetDocument()->GetDocumentLoadGroup());
213 nsCOMPtr<nsIChannel> channel;
214 rv = NS_NewChannel(getter_AddRefs(channel),
215 aFontFaceSrc->mURI,
216 nsnull,
217 loadGroup,
218 nsnull,
219 nsIRequest::LOAD_NORMAL);
221 NS_ENSURE_SUCCESS(rv, rv);
223 nsCOMPtr<nsIHttpChannel> httpChannel(do_QueryInterface(channel));
224 if (httpChannel)
225 httpChannel->SetReferrer(aFontFaceSrc->mReferrer);
226 rv = NS_NewStreamLoader(getter_AddRefs(streamLoader), fontLoader);
227 NS_ENSURE_SUCCESS(rv, rv);
229 PRBool inherits = PR_FALSE;
230 rv = NS_URIChainHasFlags(aFontFaceSrc->mURI,
231 nsIProtocolHandler::URI_INHERITS_SECURITY_CONTEXT,
232 &inherits);
233 if (NS_SUCCEEDED(rv) && inherits) {
234 // allow data, javascript, etc URI's
235 rv = channel->AsyncOpen(streamLoader, nsnull);
236 } else {
237 nsCOMPtr<nsIStreamListener> listener =
238 new nsCrossSiteListenerProxy(streamLoader, principal, channel,
239 PR_FALSE, &rv);
240 NS_ENSURE_TRUE(listener, NS_ERROR_OUT_OF_MEMORY);
241 NS_ENSURE_SUCCESS(rv, rv);
243 rv = channel->AsyncOpen(listener, nsnull);
246 return rv;
249 nsresult
250 nsFontFaceLoader::CheckLoadAllowed(nsIPrincipal* aSourcePrincipal,
251 nsIURI* aTargetURI,
252 nsISupports* aContext)
254 nsresult rv;
256 if (!aSourcePrincipal)
257 return NS_OK;
259 // check content policy
260 PRInt16 shouldLoad = nsIContentPolicy::ACCEPT;
261 rv = NS_CheckContentLoadPolicy(nsIContentPolicy::TYPE_FONT,
262 aTargetURI,
263 aSourcePrincipal,
264 aContext,
265 EmptyCString(), // mime type
266 nsnull,
267 &shouldLoad,
268 nsContentUtils::GetContentPolicy(),
269 nsContentUtils::GetSecurityManager());
271 if (NS_FAILED(rv) || NS_CP_REJECTED(shouldLoad)) {
272 return NS_ERROR_CONTENT_BLOCKED;
275 return NS_OK;