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
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.
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 ***** */
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"
49 #include "nsICSSStyleSheet.h"
50 #include "nsNodeInfo.h"
51 #include "nsNodeInfoManager.h"
53 #include "nsContentCID.h"
55 #include "nsNetUtil.h"
56 #include "nsICSSLoader.h"
58 #include "nsIViewSourceChannel.h"
60 #include "imgILoader.h"
61 #include "nsIParser.h"
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
76 static NS_DEFINE_IID(kHTMLDocumentCID
, NS_HTMLDOCUMENT_CID
);
77 static NS_DEFINE_IID(kXMLDocumentCID
, NS_XMLDOCUMENT_CID
);
79 static NS_DEFINE_IID(kSVGDocumentCID
, NS_SVGDOCUMENT_CID
);
81 static NS_DEFINE_IID(kImageDocumentCID
, NS_IMAGEDOCUMENT_CID
);
82 static NS_DEFINE_IID(kXULDocumentCID
, NS_XULDOCUMENT_CID
);
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
[] = {
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
101 "application/xhtml+xml",
105 static const char* const gXMLTypes
[] = {
108 "application/rdf+xml",
114 static const char* const gSVGTypes
[] = {
119 PRBool
NS_SVGEnabled();
122 static const char* const gXULTypes
[] = {
123 "application/vnd.mozilla.xul+xml",
124 "mozilla.application/cached-xul",
128 nsICSSStyleSheet
* nsContentDLF::gUAStyleSheet
;
131 NS_NewContentDocumentLoaderFactory(nsIDocumentLoaderFactory
** aResult
)
133 NS_PRECONDITION(aResult
, "null OUT ptr");
135 return NS_ERROR_NULL_POINTER
;
137 nsContentDLF
* it
= new nsContentDLF();
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
)
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
180 viewSourceChannel
->GetOriginalContentType(type
);
181 PRBool knownType
= PR_FALSE
;
183 for (typeIndex
= 0; gHTMLTypes
[typeIndex
] && !knownType
; ++typeIndex
) {
184 if (type
.Equals(gHTMLTypes
[typeIndex
]) &&
185 !type
.EqualsLiteral("application/x-view-source")) {
190 for (typeIndex
= 0; gXMLTypes
[typeIndex
] && !knownType
; ++typeIndex
) {
191 if (type
.Equals(gXMLTypes
[typeIndex
])) {
197 if (NS_SVGEnabled()) {
198 for (typeIndex
= 0; gSVGTypes
[typeIndex
] && !knownType
; ++typeIndex
) {
199 if (type
.Equals(gSVGTypes
[typeIndex
])) {
206 for (typeIndex
= 0; gXULTypes
[typeIndex
] && !knownType
; ++typeIndex
) {
207 if (type
.Equals(gXULTypes
[typeIndex
])) {
213 viewSourceChannel
->SetContentType(type
);
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";
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
);
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
);
245 if (NS_SVGEnabled()) {
248 while(gSVGTypes
[typeIndex
]) {
249 if (!PL_strcmp(gSVGTypes
[typeIndex
++], aContentType
)) {
250 return CreateDocument(aCommand
,
251 aChannel
, aLoadGroup
,
252 aContainer
, kSVGDocumentCID
,
253 aDocListener
, aDocViewer
);
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
);
271 nsCOMPtr
<imgILoader
> loader(do_GetService("@mozilla.org/image/loader;1"));
272 PRBool isReg
= PR_FALSE
;
273 loader
->SupportImageWithMimeType(aContentType
, &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
;
296 nsContentDLF::CreateInstanceForDocument(nsISupports
* aContainer
,
297 nsIDocument
* aDocument
,
298 const char *aCommand
,
299 nsIContentViewer
** aDocViewerResult
)
301 nsresult rv
= NS_ERROR_FAILURE
;
303 EnsureUAStyleSheet();
306 nsCOMPtr
<nsIDocumentViewer
> docv
;
307 rv
= NS_NewDocumentViewer(getter_AddRefs(docv
));
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
);
323 nsContentDLF::CreateBlankDocument(nsILoadGroup
*aLoadGroup
,
324 nsIPrincipal
* aPrincipal
,
325 nsIDocument
**aDocument
)
329 nsresult rv
= NS_ERROR_FAILURE
;
331 // create a new blank HTML document
332 nsCOMPtr
<nsIDocument
> blankDoc(do_CreateInstance(kHTMLDocumentCID
));
336 nsCOMPtr
<nsIURI
> uri
;
337 NS_NewURI(getter_AddRefs(uri
), NS_LITERAL_CSTRING("about:blank"));
339 blankDoc
->ResetToURI(uri
, aLoadGroup
, aPrincipal
);
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
);
384 if (NS_SUCCEEDED(rv
)) {
385 blankDoc
->SetDocumentCharacterSetSource(kCharsetFromDocTypeDefault
);
386 blankDoc
->SetDocumentCharacterSet(NS_LITERAL_CSTRING("UTF-8"));
388 *aDocument
= blankDoc
;
389 NS_ADDREF(*aDocument
);
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
) {
414 fputs(NS_LossyConvertUTF16toASCII(tmp
).get(), stdout
);
415 printf(": creating document\n");
419 nsCOMPtr
<nsIDocument
> doc
;
420 nsCOMPtr
<nsIDocumentViewer
> docv
;
422 // Create the document
423 doc
= do_CreateInstance(aDocumentCID
, &rv
);
427 // Create the document viewer XXX: could reuse document viewer here!
428 rv
= NS_NewDocumentViewer(getter_AddRefs(docv
));
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
438 rv
= doc
->StartDocumentLoad(aCommand
, aChannel
, aLoadGroup
, aContainer
, aDocListener
, PR_TRUE
);
442 // Bind the document to the Content Viewer
443 rv
= docv
->LoadStart(doc
);
445 NS_IF_ADDREF(*aDocViewer
);
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
)
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
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
);
492 NS_IF_ADDREF(*aDocViewer
);
499 RegisterTypes(nsICategoryManager
* aCatMgr
,
500 const char* const* aTypes
,
501 PRBool aPersist
= PR_TRUE
)
505 const char* contentType
= *aTypes
++;
506 #ifdef NOISY_REGISTRY
507 printf("Register %s => %s\n", contractid
, aPath
);
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;
520 static nsresult
UnregisterTypes(nsICategoryManager
* aCatMgr
,
521 const char* const* aTypes
)
525 const char* contentType
= *aTypes
++;
526 rv
= aCatMgr
->DeleteCategoryEntry("Gecko-Content-Viewers", contentType
, PR_TRUE
);
527 if (NS_FAILED(rv
)) break;
535 nsContentDLF::RegisterSVG()
538 nsCOMPtr
<nsICategoryManager
> catmgr(do_GetService(NS_CATEGORYMANAGER_CONTRACTID
, &rv
));
539 if (NS_FAILED(rv
)) return rv
;
541 return RegisterTypes(catmgr
, gSVGTypes
, PR_FALSE
);
545 nsContentDLF::UnregisterSVG()
548 nsCOMPtr
<nsICategoryManager
> catmgr(do_GetService(NS_CATEGORYMANAGER_CONTRACTID
, &rv
));
549 if (NS_FAILED(rv
)) return rv
;
551 return UnregisterTypes(catmgr
, gSVGTypes
);
556 nsContentDLF::RegisterDocumentFactories(nsIComponentManager
* aCompMgr
,
558 const char *aLocation
,
560 const nsModuleComponentInfo
* aInfo
)
564 nsCOMPtr
<nsICategoryManager
> catmgr(do_GetService(NS_CATEGORYMANAGER_CONTRACTID
, &rv
));
565 if (NS_FAILED(rv
)) return rv
;
568 rv
= RegisterTypes(catmgr
, gHTMLTypes
);
571 rv
= RegisterTypes(catmgr
, gXMLTypes
);
574 rv
= RegisterTypes(catmgr
, gXULTypes
);
582 nsContentDLF::UnregisterDocumentFactories(nsIComponentManager
* aCompMgr
,
584 const char* aRegistryLocation
,
585 const nsModuleComponentInfo
* aInfo
)
588 nsCOMPtr
<nsICategoryManager
> catmgr(do_GetService(NS_CATEGORYMANAGER_CONTRACTID
, &rv
));
589 if (NS_FAILED(rv
)) return rv
;
592 rv
= UnregisterTypes(catmgr
, gHTMLTypes
);
595 rv
= UnregisterTypes(catmgr
, gXMLTypes
);
599 rv
= UnregisterTypes(catmgr
, gSVGTypes
);
603 rv
= UnregisterTypes(catmgr
, gXULTypes
);
611 /* static */ nsresult
612 nsContentDLF::EnsureUAStyleSheet()
617 // Load the UA style sheet
618 nsCOMPtr
<nsIURI
> uri
;
619 nsresult rv
= NS_NewURI(getter_AddRefs(uri
), NS_LITERAL_CSTRING(UA_CSS_URL
));
622 printf("*** open of %s failed: error=%x\n", UA_CSS_URL
, rv
);
626 nsCOMPtr
<nsICSSLoader
> cssLoader
;
627 NS_NewCSSLoader(getter_AddRefs(cssLoader
));
629 return NS_ERROR_OUT_OF_MEMORY
;
630 rv
= cssLoader
->LoadSheetSync(uri
, PR_TRUE
, &gUAStyleSheet
);
633 printf("*** open of %s failed: error=%x\n", UA_CSS_URL
, rv
);