Bug 449371 Firefox/Thunderbird crashes at exit [@ gdk_display_x11_finalize], p=Brian...
[wine-gecko.git] / layout / build / nsContentDLF.cpp
blob070589e4c9f9ac584d3525b79f720ba4058f6449
1 /* -*- Mode: c++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=2 sw=2 et tw=78: */
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 Communicator client code.
18 * The Initial Developer of the Original Code is
19 * Netscape Communications Corporation.
20 * Portions created by the Initial Developer are Copyright (C) 1998
21 * the Initial Developer. All Rights Reserved.
23 * Contributor(s):
25 * Alternatively, the contents of this file may be used under the terms of
26 * either of the GNU General Public License Version 2 or later (the "GPL"),
27 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
37 * ***** END LICENSE BLOCK ***** */
38 #include "nsCOMPtr.h"
39 #include "nsContentDLF.h"
40 #include "nsGenericHTMLElement.h"
41 #include "nsGkAtoms.h"
42 #include "nsIComponentManager.h"
43 #include "nsIComponentRegistrar.h"
44 #include "nsICategoryManager.h"
45 #include "nsIDocumentLoaderFactory.h"
46 #include "nsIDocument.h"
47 #include "nsIDocumentViewer.h"
48 #include "nsIURL.h"
49 #include "nsICSSStyleSheet.h"
50 #include "nsNodeInfo.h"
51 #include "nsNodeInfoManager.h"
52 #include "nsString.h"
53 #include "nsContentCID.h"
54 #include "prprf.h"
55 #include "nsNetUtil.h"
56 #include "nsICSSLoader.h"
57 #include "nsCRT.h"
58 #include "nsIViewSourceChannel.h"
60 #include "imgILoader.h"
61 #include "nsIParser.h"
63 // plugins
64 #include "nsIPluginManager.h"
65 #include "nsIPluginHost.h"
66 static NS_DEFINE_CID(kPluginManagerCID, NS_PLUGINMANAGER_CID);
67 static NS_DEFINE_CID(kPluginDocumentCID, NS_PLUGINDOCUMENT_CID);
69 // URL for the "user agent" style sheet
70 #define UA_CSS_URL "resource://gre/res/ua.css"
72 // Factory code for creating variations on html documents
74 #undef NOISY_REGISTRY
76 static NS_DEFINE_IID(kHTMLDocumentCID, NS_HTMLDOCUMENT_CID);
77 static NS_DEFINE_IID(kXMLDocumentCID, NS_XMLDOCUMENT_CID);
78 #ifdef MOZ_SVG
79 static NS_DEFINE_IID(kSVGDocumentCID, NS_SVGDOCUMENT_CID);
80 #endif
81 static NS_DEFINE_IID(kImageDocumentCID, NS_IMAGEDOCUMENT_CID);
82 static NS_DEFINE_IID(kXULDocumentCID, NS_XULDOCUMENT_CID);
84 nsresult
85 NS_NewDocumentViewer(nsIDocumentViewer** aResult);
87 // XXXbz if you change the MIME types here, be sure to update
88 // nsIParser.h and DetermineParseMode in nsParser.cpp accordingly.
89 static const char* const gHTMLTypes[] = {
90 "text/html",
91 "text/plain",
92 "text/css",
93 "text/javascript",
94 "text/ecmascript",
95 "application/javascript",
96 "application/ecmascript",
97 "application/x-javascript",
98 #ifdef MOZ_VIEW_SOURCE
99 "application/x-view-source", //XXX I wish I could just use nsMimeTypes.h here
100 #endif
101 "application/xhtml+xml",
105 static const char* const gXMLTypes[] = {
106 "text/xml",
107 "application/xml",
108 "application/rdf+xml",
109 "text/rdf",
113 #ifdef MOZ_SVG
114 static const char* const gSVGTypes[] = {
115 "image/svg+xml",
119 PRBool NS_SVGEnabled();
120 #endif
122 static const char* const gXULTypes[] = {
123 "application/vnd.mozilla.xul+xml",
124 "mozilla.application/cached-xul",
128 nsICSSStyleSheet* nsContentDLF::gUAStyleSheet;
130 nsresult
131 NS_NewContentDocumentLoaderFactory(nsIDocumentLoaderFactory** aResult)
133 NS_PRECONDITION(aResult, "null OUT ptr");
134 if (!aResult) {
135 return NS_ERROR_NULL_POINTER;
137 nsContentDLF* it = new nsContentDLF();
138 if (!it) {
139 return NS_ERROR_OUT_OF_MEMORY;
142 return CallQueryInterface(it, aResult);
145 nsContentDLF::nsContentDLF()
149 nsContentDLF::~nsContentDLF()
153 NS_IMPL_ISUPPORTS1(nsContentDLF,
154 nsIDocumentLoaderFactory)
156 NS_IMETHODIMP
157 nsContentDLF::CreateInstance(const char* aCommand,
158 nsIChannel* aChannel,
159 nsILoadGroup* aLoadGroup,
160 const char* aContentType,
161 nsISupports* aContainer,
162 nsISupports* aExtraInfo,
163 nsIStreamListener** aDocListener,
164 nsIContentViewer** aDocViewer)
166 EnsureUAStyleSheet();
168 // Are we viewing source?
169 #ifdef MOZ_VIEW_SOURCE
170 nsCOMPtr<nsIViewSourceChannel> viewSourceChannel = do_QueryInterface(aChannel);
171 if (viewSourceChannel)
173 aCommand = "view-source";
175 // The parser freaks out when it sees the content-type that a
176 // view-source channel normally returns. Get the actual content
177 // type of the data. If it's known, use it; otherwise use
178 // text/plain.
179 nsCAutoString type;
180 viewSourceChannel->GetOriginalContentType(type);
181 PRBool knownType = PR_FALSE;
182 PRInt32 typeIndex;
183 for (typeIndex = 0; gHTMLTypes[typeIndex] && !knownType; ++typeIndex) {
184 if (type.Equals(gHTMLTypes[typeIndex]) &&
185 !type.EqualsLiteral("application/x-view-source")) {
186 knownType = PR_TRUE;
190 for (typeIndex = 0; gXMLTypes[typeIndex] && !knownType; ++typeIndex) {
191 if (type.Equals(gXMLTypes[typeIndex])) {
192 knownType = PR_TRUE;
196 #ifdef MOZ_SVG
197 if (NS_SVGEnabled()) {
198 for (typeIndex = 0; gSVGTypes[typeIndex] && !knownType; ++typeIndex) {
199 if (type.Equals(gSVGTypes[typeIndex])) {
200 knownType = PR_TRUE;
204 #endif // MOZ_SVG
206 for (typeIndex = 0; gXULTypes[typeIndex] && !knownType; ++typeIndex) {
207 if (type.Equals(gXULTypes[typeIndex])) {
208 knownType = PR_TRUE;
212 if (knownType) {
213 viewSourceChannel->SetContentType(type);
214 } else {
215 viewSourceChannel->SetContentType(NS_LITERAL_CSTRING("text/plain"));
217 } else if (0 == PL_strcmp("application/x-view-source", aContentType)) {
218 aChannel->SetContentType(NS_LITERAL_CSTRING("text/plain"));
219 aContentType = "text/plain";
221 #endif
222 // Try html
223 int typeIndex=0;
224 while(gHTMLTypes[typeIndex]) {
225 if (0 == PL_strcmp(gHTMLTypes[typeIndex++], aContentType)) {
226 return CreateDocument(aCommand,
227 aChannel, aLoadGroup,
228 aContainer, kHTMLDocumentCID,
229 aDocListener, aDocViewer);
233 // Try XML
234 typeIndex = 0;
235 while(gXMLTypes[typeIndex]) {
236 if (0== PL_strcmp(gXMLTypes[typeIndex++], aContentType)) {
237 return CreateDocument(aCommand,
238 aChannel, aLoadGroup,
239 aContainer, kXMLDocumentCID,
240 aDocListener, aDocViewer);
244 #ifdef MOZ_SVG
245 if (NS_SVGEnabled()) {
246 // Try SVG
247 typeIndex = 0;
248 while(gSVGTypes[typeIndex]) {
249 if (!PL_strcmp(gSVGTypes[typeIndex++], aContentType)) {
250 return CreateDocument(aCommand,
251 aChannel, aLoadGroup,
252 aContainer, kSVGDocumentCID,
253 aDocListener, aDocViewer);
257 #endif
259 // Try XUL
260 typeIndex = 0;
261 while (gXULTypes[typeIndex]) {
262 if (0 == PL_strcmp(gXULTypes[typeIndex++], aContentType)) {
263 return CreateXULDocument(aCommand,
264 aChannel, aLoadGroup,
265 aContentType, aContainer,
266 aExtraInfo, aDocListener, aDocViewer);
270 // Try image types
271 nsCOMPtr<imgILoader> loader(do_GetService("@mozilla.org/image/loader;1"));
272 PRBool isReg = PR_FALSE;
273 loader->SupportImageWithMimeType(aContentType, &isReg);
274 if (isReg) {
275 return CreateDocument(aCommand,
276 aChannel, aLoadGroup,
277 aContainer, kImageDocumentCID,
278 aDocListener, aDocViewer);
281 nsCOMPtr<nsIPluginHost> ph (do_GetService(kPluginManagerCID));
282 if(ph && NS_SUCCEEDED(ph->IsPluginEnabledForType(aContentType))) {
283 return CreateDocument(aCommand,
284 aChannel, aLoadGroup,
285 aContainer, kPluginDocumentCID,
286 aDocListener, aDocViewer);
290 // If we get here, then we weren't able to create anything. Sorry!
291 return NS_ERROR_FAILURE;
295 NS_IMETHODIMP
296 nsContentDLF::CreateInstanceForDocument(nsISupports* aContainer,
297 nsIDocument* aDocument,
298 const char *aCommand,
299 nsIContentViewer** aDocViewerResult)
301 nsresult rv = NS_ERROR_FAILURE;
303 EnsureUAStyleSheet();
305 do {
306 nsCOMPtr<nsIDocumentViewer> docv;
307 rv = NS_NewDocumentViewer(getter_AddRefs(docv));
308 if (NS_FAILED(rv))
309 break;
311 docv->SetUAStyleSheet(static_cast<nsIStyleSheet*>(gUAStyleSheet));
313 // Bind the document to the Content Viewer
314 nsIContentViewer* cv = static_cast<nsIContentViewer*>(docv.get());
315 rv = cv->LoadStart(aDocument);
316 NS_ADDREF(*aDocViewerResult = cv);
317 } while (PR_FALSE);
319 return rv;
322 NS_IMETHODIMP
323 nsContentDLF::CreateBlankDocument(nsILoadGroup *aLoadGroup,
324 nsIPrincipal* aPrincipal,
325 nsIDocument **aDocument)
327 *aDocument = nsnull;
329 nsresult rv = NS_ERROR_FAILURE;
331 // create a new blank HTML document
332 nsCOMPtr<nsIDocument> blankDoc(do_CreateInstance(kHTMLDocumentCID));
334 if (blankDoc) {
335 // initialize
336 nsCOMPtr<nsIURI> uri;
337 NS_NewURI(getter_AddRefs(uri), NS_LITERAL_CSTRING("about:blank"));
338 if (uri) {
339 blankDoc->ResetToURI(uri, aLoadGroup, aPrincipal);
340 rv = NS_OK;
344 // add some simple content structure
345 if (NS_SUCCEEDED(rv)) {
346 rv = NS_ERROR_FAILURE;
348 nsNodeInfoManager *nim = blankDoc->NodeInfoManager();
350 nsCOMPtr<nsINodeInfo> htmlNodeInfo;
352 // generate an html html element
353 nim->GetNodeInfo(nsGkAtoms::html, 0, kNameSpaceID_None,
354 getter_AddRefs(htmlNodeInfo));
355 nsCOMPtr<nsIContent> htmlElement = NS_NewHTMLHtmlElement(htmlNodeInfo);
357 // generate an html head element
358 nim->GetNodeInfo(nsGkAtoms::head, 0, kNameSpaceID_None,
359 getter_AddRefs(htmlNodeInfo));
360 nsCOMPtr<nsIContent> headElement = NS_NewHTMLHeadElement(htmlNodeInfo);
362 // generate an html body element
363 nim->GetNodeInfo(nsGkAtoms::body, 0, kNameSpaceID_None,
364 getter_AddRefs(htmlNodeInfo));
365 nsCOMPtr<nsIContent> bodyElement = NS_NewHTMLBodyElement(htmlNodeInfo);
367 // blat in the structure
368 if (htmlElement && headElement && bodyElement) {
369 NS_ASSERTION(blankDoc->GetChildCount() == 0,
370 "Shouldn't have children");
371 rv = blankDoc->AppendChildTo(htmlElement, PR_FALSE);
372 if (NS_SUCCEEDED(rv)) {
373 rv = htmlElement->AppendChildTo(headElement, PR_FALSE);
375 if (NS_SUCCEEDED(rv)) {
376 // XXXbz Why not notifying here?
377 htmlElement->AppendChildTo(bodyElement, PR_FALSE);
383 // add a nice bow
384 if (NS_SUCCEEDED(rv)) {
385 blankDoc->SetDocumentCharacterSetSource(kCharsetFromDocTypeDefault);
386 blankDoc->SetDocumentCharacterSet(NS_LITERAL_CSTRING("UTF-8"));
388 *aDocument = blankDoc;
389 NS_ADDREF(*aDocument);
391 return rv;
395 nsresult
396 nsContentDLF::CreateDocument(const char* aCommand,
397 nsIChannel* aChannel,
398 nsILoadGroup* aLoadGroup,
399 nsISupports* aContainer,
400 const nsCID& aDocumentCID,
401 nsIStreamListener** aDocListener,
402 nsIContentViewer** aDocViewer)
404 nsresult rv = NS_ERROR_FAILURE;
406 nsCOMPtr<nsIURI> aURL;
407 rv = aChannel->GetURI(getter_AddRefs(aURL));
408 if (NS_FAILED(rv)) return rv;
410 #ifdef NOISY_CREATE_DOC
411 if (nsnull != aURL) {
412 nsAutoString tmp;
413 aURL->ToString(tmp);
414 fputs(NS_LossyConvertUTF16toASCII(tmp).get(), stdout);
415 printf(": creating document\n");
417 #endif
419 nsCOMPtr<nsIDocument> doc;
420 nsCOMPtr<nsIDocumentViewer> docv;
421 do {
422 // Create the document
423 doc = do_CreateInstance(aDocumentCID, &rv);
424 if (NS_FAILED(rv))
425 break;
427 // Create the document viewer XXX: could reuse document viewer here!
428 rv = NS_NewDocumentViewer(getter_AddRefs(docv));
429 if (NS_FAILED(rv))
430 break;
431 docv->SetUAStyleSheet(gUAStyleSheet);
433 doc->SetContainer(aContainer);
435 // Initialize the document to begin loading the data. An
436 // nsIStreamListener connected to the parser is returned in
437 // aDocListener.
438 rv = doc->StartDocumentLoad(aCommand, aChannel, aLoadGroup, aContainer, aDocListener, PR_TRUE);
439 if (NS_FAILED(rv))
440 break;
442 // Bind the document to the Content Viewer
443 rv = docv->LoadStart(doc);
444 *aDocViewer = docv;
445 NS_IF_ADDREF(*aDocViewer);
446 } while (PR_FALSE);
448 return rv;
451 nsresult
452 nsContentDLF::CreateXULDocument(const char* aCommand,
453 nsIChannel* aChannel,
454 nsILoadGroup* aLoadGroup,
455 const char* aContentType,
456 nsISupports* aContainer,
457 nsISupports* aExtraInfo,
458 nsIStreamListener** aDocListener,
459 nsIContentViewer** aDocViewer)
461 nsresult rv;
462 nsCOMPtr<nsIDocument> doc = do_CreateInstance(kXULDocumentCID, &rv);
463 if (NS_FAILED(rv)) return rv;
465 nsCOMPtr<nsIDocumentViewer> docv;
466 rv = NS_NewDocumentViewer(getter_AddRefs(docv));
467 if (NS_FAILED(rv)) return rv;
469 // Load the UA style sheet if we haven't already done that
470 docv->SetUAStyleSheet(gUAStyleSheet);
472 nsCOMPtr<nsIURI> aURL;
473 rv = aChannel->GetURI(getter_AddRefs(aURL));
474 if (NS_FAILED(rv)) return rv;
477 * Initialize the document to begin loading the data...
479 * An nsIStreamListener connected to the parser is returned in
480 * aDocListener.
483 doc->SetContainer(aContainer);
485 rv = doc->StartDocumentLoad(aCommand, aChannel, aLoadGroup, aContainer, aDocListener, PR_TRUE);
486 if (NS_SUCCEEDED(rv)) {
488 * Bind the document to the Content Viewer...
490 rv = docv->LoadStart(doc);
491 *aDocViewer = docv;
492 NS_IF_ADDREF(*aDocViewer);
495 return rv;
498 static nsresult
499 RegisterTypes(nsICategoryManager* aCatMgr,
500 const char* const* aTypes,
501 PRBool aPersist = PR_TRUE)
503 nsresult rv = NS_OK;
504 while (*aTypes) {
505 const char* contentType = *aTypes++;
506 #ifdef NOISY_REGISTRY
507 printf("Register %s => %s\n", contractid, aPath);
508 #endif
509 // add the MIME types layout can handle to the handlers category.
510 // this allows users of layout's viewers (the docshell for example)
511 // to query the types of viewers layout can create.
512 rv = aCatMgr->AddCategoryEntry("Gecko-Content-Viewers", contentType,
513 "@mozilla.org/content/document-loader-factory;1",
514 aPersist, PR_TRUE, nsnull);
515 if (NS_FAILED(rv)) break;
517 return rv;
520 static nsresult UnregisterTypes(nsICategoryManager* aCatMgr,
521 const char* const* aTypes)
523 nsresult rv = NS_OK;
524 while (*aTypes) {
525 const char* contentType = *aTypes++;
526 rv = aCatMgr->DeleteCategoryEntry("Gecko-Content-Viewers", contentType, PR_TRUE);
527 if (NS_FAILED(rv)) break;
529 return rv;
533 #ifdef MOZ_SVG
534 NS_IMETHODIMP
535 nsContentDLF::RegisterSVG()
537 nsresult rv;
538 nsCOMPtr<nsICategoryManager> catmgr(do_GetService(NS_CATEGORYMANAGER_CONTRACTID, &rv));
539 if (NS_FAILED(rv)) return rv;
541 return RegisterTypes(catmgr, gSVGTypes, PR_FALSE);
544 NS_IMETHODIMP
545 nsContentDLF::UnregisterSVG()
547 nsresult rv;
548 nsCOMPtr<nsICategoryManager> catmgr(do_GetService(NS_CATEGORYMANAGER_CONTRACTID, &rv));
549 if (NS_FAILED(rv)) return rv;
551 return UnregisterTypes(catmgr, gSVGTypes);
553 #endif
555 NS_IMETHODIMP
556 nsContentDLF::RegisterDocumentFactories(nsIComponentManager* aCompMgr,
557 nsIFile* aPath,
558 const char *aLocation,
559 const char *aType,
560 const nsModuleComponentInfo* aInfo)
562 nsresult rv;
564 nsCOMPtr<nsICategoryManager> catmgr(do_GetService(NS_CATEGORYMANAGER_CONTRACTID, &rv));
565 if (NS_FAILED(rv)) return rv;
567 do {
568 rv = RegisterTypes(catmgr, gHTMLTypes);
569 if (NS_FAILED(rv))
570 break;
571 rv = RegisterTypes(catmgr, gXMLTypes);
572 if (NS_FAILED(rv))
573 break;
574 rv = RegisterTypes(catmgr, gXULTypes);
575 if (NS_FAILED(rv))
576 break;
577 } while (PR_FALSE);
578 return rv;
581 NS_IMETHODIMP
582 nsContentDLF::UnregisterDocumentFactories(nsIComponentManager* aCompMgr,
583 nsIFile* aPath,
584 const char* aRegistryLocation,
585 const nsModuleComponentInfo* aInfo)
587 nsresult rv;
588 nsCOMPtr<nsICategoryManager> catmgr(do_GetService(NS_CATEGORYMANAGER_CONTRACTID, &rv));
589 if (NS_FAILED(rv)) return rv;
591 do {
592 rv = UnregisterTypes(catmgr, gHTMLTypes);
593 if (NS_FAILED(rv))
594 break;
595 rv = UnregisterTypes(catmgr, gXMLTypes);
596 if (NS_FAILED(rv))
597 break;
598 #ifdef MOZ_SVG
599 rv = UnregisterTypes(catmgr, gSVGTypes);
600 if (NS_FAILED(rv))
601 break;
602 #endif
603 rv = UnregisterTypes(catmgr, gXULTypes);
604 if (NS_FAILED(rv))
605 break;
606 } while (PR_FALSE);
608 return rv;
611 /* static */ nsresult
612 nsContentDLF::EnsureUAStyleSheet()
614 if (gUAStyleSheet)
615 return NS_OK;
617 // Load the UA style sheet
618 nsCOMPtr<nsIURI> uri;
619 nsresult rv = NS_NewURI(getter_AddRefs(uri), NS_LITERAL_CSTRING(UA_CSS_URL));
620 if (NS_FAILED(rv)) {
621 #ifdef DEBUG
622 printf("*** open of %s failed: error=%x\n", UA_CSS_URL, rv);
623 #endif
624 return rv;
626 nsCOMPtr<nsICSSLoader> cssLoader;
627 NS_NewCSSLoader(getter_AddRefs(cssLoader));
628 if (!cssLoader)
629 return NS_ERROR_OUT_OF_MEMORY;
630 rv = cssLoader->LoadSheetSync(uri, PR_TRUE, &gUAStyleSheet);
631 #ifdef DEBUG
632 if (NS_FAILED(rv))
633 printf("*** open of %s failed: error=%x\n", UA_CSS_URL, rv);
634 #endif
635 return rv;