nsContentDLF.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. /* -*- Mode: c++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #include "nsCOMPtr.h"
  6. #include "nsContentDLF.h"
  7. #include "nsDocShell.h"
  8. #include "nsGenericHTMLElement.h"
  9. #include "nsGkAtoms.h"
  10. #include "nsIComponentManager.h"
  11. #include "nsIComponentRegistrar.h"
  12. #include "nsIContentViewer.h"
  13. #include "nsICategoryManager.h"
  14. #include "nsIDocumentLoaderFactory.h"
  15. #include "nsIDocument.h"
  16. #include "nsIURL.h"
  17. #include "nsNodeInfoManager.h"
  18. #include "nsIScriptSecurityManager.h"
  19. #include "nsString.h"
  20. #include "nsContentCID.h"
  21. #include "prprf.h"
  22. #include "nsNetUtil.h"
  23. #include "nsCRT.h"
  24. #include "nsIViewSourceChannel.h"
  25. #include "nsContentUtils.h"
  26. #include "imgLoader.h"
  27. #include "nsCharsetSource.h"
  28. #include "nsMimeTypes.h"
  29. #include "DecoderTraits.h"
  30. // plugins
  31. #include "nsIPluginHost.h"
  32. #include "nsPluginHost.h"
  33. static NS_DEFINE_CID(kPluginDocumentCID, NS_PLUGINDOCUMENT_CID);
  34. // Factory code for creating variations on html documents
  35. #undef NOISY_REGISTRY
  36. static NS_DEFINE_IID(kHTMLDocumentCID, NS_HTMLDOCUMENT_CID);
  37. static NS_DEFINE_IID(kXMLDocumentCID, NS_XMLDOCUMENT_CID);
  38. static NS_DEFINE_IID(kSVGDocumentCID, NS_SVGDOCUMENT_CID);
  39. static NS_DEFINE_IID(kVideoDocumentCID, NS_VIDEODOCUMENT_CID);
  40. static NS_DEFINE_IID(kImageDocumentCID, NS_IMAGEDOCUMENT_CID);
  41. static NS_DEFINE_IID(kXULDocumentCID, NS_XULDOCUMENT_CID);
  42. already_AddRefed<nsIContentViewer> NS_NewContentViewer();
  43. static const char* const gHTMLTypes[] = {
  44. TEXT_HTML,
  45. VIEWSOURCE_CONTENT_TYPE,
  46. APPLICATION_XHTML_XML,
  47. APPLICATION_WAPXHTML_XML,
  48. 0
  49. };
  50. static const char* const gXMLTypes[] = {
  51. TEXT_XML,
  52. APPLICATION_XML,
  53. APPLICATION_MATHML_XML,
  54. APPLICATION_RDF_XML,
  55. TEXT_RDF,
  56. 0
  57. };
  58. static const char* const gSVGTypes[] = {
  59. IMAGE_SVG_XML,
  60. 0
  61. };
  62. static const char* const gXULTypes[] = {
  63. TEXT_XUL,
  64. APPLICATION_CACHED_XUL,
  65. 0
  66. };
  67. static bool
  68. IsTypeInList(const nsACString& aType, const char* const aList[])
  69. {
  70. int32_t typeIndex;
  71. for (typeIndex = 0; aList[typeIndex]; ++typeIndex) {
  72. if (aType.Equals(aList[typeIndex])) {
  73. return true;
  74. }
  75. }
  76. return false;
  77. }
  78. nsresult
  79. NS_NewContentDocumentLoaderFactory(nsIDocumentLoaderFactory** aResult)
  80. {
  81. NS_PRECONDITION(aResult, "null OUT ptr");
  82. if (!aResult) {
  83. return NS_ERROR_NULL_POINTER;
  84. }
  85. nsContentDLF* it = new nsContentDLF();
  86. if (!it) {
  87. return NS_ERROR_OUT_OF_MEMORY;
  88. }
  89. return CallQueryInterface(it, aResult);
  90. }
  91. nsContentDLF::nsContentDLF()
  92. {
  93. }
  94. nsContentDLF::~nsContentDLF()
  95. {
  96. }
  97. NS_IMPL_ISUPPORTS(nsContentDLF,
  98. nsIDocumentLoaderFactory)
  99. bool
  100. MayUseXULXBL(nsIChannel* aChannel)
  101. {
  102. nsIScriptSecurityManager *securityManager =
  103. nsContentUtils::GetSecurityManager();
  104. if (!securityManager) {
  105. return false;
  106. }
  107. nsCOMPtr<nsIPrincipal> principal;
  108. securityManager->GetChannelResultPrincipal(aChannel, getter_AddRefs(principal));
  109. NS_ENSURE_TRUE(principal, false);
  110. return nsContentUtils::AllowXULXBLForPrincipal(principal);
  111. }
  112. NS_IMETHODIMP
  113. nsContentDLF::CreateInstance(const char* aCommand,
  114. nsIChannel* aChannel,
  115. nsILoadGroup* aLoadGroup,
  116. const nsACString& aContentType,
  117. nsIDocShell* aContainer,
  118. nsISupports* aExtraInfo,
  119. nsIStreamListener** aDocListener,
  120. nsIContentViewer** aDocViewer)
  121. {
  122. // Make a copy of aContentType, because we're possibly going to change it.
  123. nsAutoCString contentType(aContentType);
  124. // Are we viewing source?
  125. nsCOMPtr<nsIViewSourceChannel> viewSourceChannel = do_QueryInterface(aChannel);
  126. if (viewSourceChannel)
  127. {
  128. aCommand = "view-source";
  129. // The parser freaks out when it sees the content-type that a
  130. // view-source channel normally returns. Get the actual content
  131. // type of the data. If it's known, use it; otherwise use
  132. // text/plain.
  133. nsAutoCString type;
  134. viewSourceChannel->GetOriginalContentType(type);
  135. bool knownType =
  136. (!type.EqualsLiteral(VIEWSOURCE_CONTENT_TYPE) &&
  137. IsTypeInList(type, gHTMLTypes)) ||
  138. nsContentUtils::IsPlainTextType(type) ||
  139. IsTypeInList(type, gXMLTypes) ||
  140. IsTypeInList(type, gSVGTypes) ||
  141. IsTypeInList(type, gXMLTypes);
  142. if (knownType) {
  143. viewSourceChannel->SetContentType(type);
  144. } else if (IsImageContentType(type.get())) {
  145. // If it's an image, we want to display it the same way we normally would.
  146. // Also note the lifetime of "type" allows us to safely use "get()" here.
  147. contentType = type;
  148. } else {
  149. viewSourceChannel->SetContentType(NS_LITERAL_CSTRING(TEXT_PLAIN));
  150. }
  151. } else if (aContentType.EqualsLiteral(VIEWSOURCE_CONTENT_TYPE)) {
  152. aChannel->SetContentType(NS_LITERAL_CSTRING(TEXT_PLAIN));
  153. contentType = TEXT_PLAIN;
  154. }
  155. // Try html or plaintext; both use the same document CID
  156. if (IsTypeInList(contentType, gHTMLTypes) ||
  157. nsContentUtils::IsPlainTextType(contentType)) {
  158. return CreateDocument(aCommand,
  159. aChannel, aLoadGroup,
  160. aContainer, kHTMLDocumentCID,
  161. aDocListener, aDocViewer);
  162. }
  163. // Try XML
  164. if (IsTypeInList(contentType, gXMLTypes)) {
  165. return CreateDocument(aCommand,
  166. aChannel, aLoadGroup,
  167. aContainer, kXMLDocumentCID,
  168. aDocListener, aDocViewer);
  169. }
  170. // Try SVG
  171. if (IsTypeInList(contentType, gSVGTypes)) {
  172. return CreateDocument(aCommand,
  173. aChannel, aLoadGroup,
  174. aContainer, kSVGDocumentCID,
  175. aDocListener, aDocViewer);
  176. }
  177. // Try XUL
  178. if (IsTypeInList(contentType, gXULTypes)) {
  179. if (!MayUseXULXBL(aChannel)) {
  180. return NS_ERROR_REMOTE_XUL;
  181. }
  182. return CreateXULDocument(aCommand, aChannel, aLoadGroup, aContainer,
  183. aExtraInfo, aDocListener, aDocViewer);
  184. }
  185. if (mozilla::DecoderTraits::ShouldHandleMediaType(contentType.get(),
  186. /* DecoderDoctorDiagnostics* */ nullptr)) {
  187. return CreateDocument(aCommand,
  188. aChannel, aLoadGroup,
  189. aContainer, kVideoDocumentCID,
  190. aDocListener, aDocViewer);
  191. }
  192. // Try image types
  193. if (IsImageContentType(contentType.get())) {
  194. return CreateDocument(aCommand,
  195. aChannel, aLoadGroup,
  196. aContainer, kImageDocumentCID,
  197. aDocListener, aDocViewer);
  198. }
  199. RefPtr<nsPluginHost> pluginHost = nsPluginHost::GetInst();
  200. // Don't exclude disabled plugins, which will still trigger the "this plugin
  201. // is disabled" placeholder.
  202. if (pluginHost && pluginHost->HavePluginForType(contentType,
  203. nsPluginHost::eExcludeNone)) {
  204. return CreateDocument(aCommand,
  205. aChannel, aLoadGroup,
  206. aContainer, kPluginDocumentCID,
  207. aDocListener, aDocViewer);
  208. }
  209. // If we get here, then we weren't able to create anything. Sorry!
  210. return NS_ERROR_FAILURE;
  211. }
  212. NS_IMETHODIMP
  213. nsContentDLF::CreateInstanceForDocument(nsISupports* aContainer,
  214. nsIDocument* aDocument,
  215. const char *aCommand,
  216. nsIContentViewer** aContentViewer)
  217. {
  218. MOZ_ASSERT(aDocument);
  219. nsCOMPtr<nsIContentViewer> contentViewer = NS_NewContentViewer();
  220. // Bind the document to the Content Viewer
  221. contentViewer->LoadStart(aDocument);
  222. contentViewer.forget(aContentViewer);
  223. return NS_OK;
  224. }
  225. NS_IMETHODIMP
  226. nsContentDLF::CreateBlankDocument(nsILoadGroup *aLoadGroup,
  227. nsIPrincipal* aPrincipal,
  228. nsIDocument **aDocument)
  229. {
  230. *aDocument = nullptr;
  231. nsresult rv = NS_ERROR_FAILURE;
  232. // create a new blank HTML document
  233. nsCOMPtr<nsIDocument> blankDoc(do_CreateInstance(kHTMLDocumentCID));
  234. if (blankDoc) {
  235. // initialize
  236. nsCOMPtr<nsIURI> uri;
  237. NS_NewURI(getter_AddRefs(uri), NS_LITERAL_CSTRING("about:blank"));
  238. if (uri) {
  239. blankDoc->ResetToURI(uri, aLoadGroup, aPrincipal);
  240. rv = NS_OK;
  241. }
  242. }
  243. // add some simple content structure
  244. if (NS_SUCCEEDED(rv)) {
  245. rv = NS_ERROR_FAILURE;
  246. nsNodeInfoManager *nim = blankDoc->NodeInfoManager();
  247. RefPtr<mozilla::dom::NodeInfo> htmlNodeInfo;
  248. // generate an html html element
  249. htmlNodeInfo = nim->GetNodeInfo(nsGkAtoms::html, 0, kNameSpaceID_XHTML,
  250. nsIDOMNode::ELEMENT_NODE);
  251. nsCOMPtr<nsIContent> htmlElement =
  252. NS_NewHTMLHtmlElement(htmlNodeInfo.forget());
  253. // generate an html head element
  254. htmlNodeInfo = nim->GetNodeInfo(nsGkAtoms::head, 0, kNameSpaceID_XHTML,
  255. nsIDOMNode::ELEMENT_NODE);
  256. nsCOMPtr<nsIContent> headElement =
  257. NS_NewHTMLHeadElement(htmlNodeInfo.forget());
  258. // generate an html body elemment
  259. htmlNodeInfo = nim->GetNodeInfo(nsGkAtoms::body, 0, kNameSpaceID_XHTML,
  260. nsIDOMNode::ELEMENT_NODE);
  261. nsCOMPtr<nsIContent> bodyElement =
  262. NS_NewHTMLBodyElement(htmlNodeInfo.forget());
  263. // blat in the structure
  264. if (htmlElement && headElement && bodyElement) {
  265. NS_ASSERTION(blankDoc->GetChildCount() == 0,
  266. "Shouldn't have children");
  267. rv = blankDoc->AppendChildTo(htmlElement, false);
  268. if (NS_SUCCEEDED(rv)) {
  269. rv = htmlElement->AppendChildTo(headElement, false);
  270. if (NS_SUCCEEDED(rv)) {
  271. // XXXbz Why not notifying here?
  272. htmlElement->AppendChildTo(bodyElement, false);
  273. }
  274. }
  275. }
  276. }
  277. // add a nice bow
  278. if (NS_SUCCEEDED(rv)) {
  279. blankDoc->SetDocumentCharacterSetSource(kCharsetFromDocTypeDefault);
  280. blankDoc->SetDocumentCharacterSet(NS_LITERAL_CSTRING("UTF-8"));
  281. blankDoc.forget(aDocument);
  282. }
  283. return rv;
  284. }
  285. nsresult
  286. nsContentDLF::CreateDocument(const char* aCommand,
  287. nsIChannel* aChannel,
  288. nsILoadGroup* aLoadGroup,
  289. nsIDocShell* aContainer,
  290. const nsCID& aDocumentCID,
  291. nsIStreamListener** aDocListener,
  292. nsIContentViewer** aContentViewer)
  293. {
  294. nsresult rv = NS_ERROR_FAILURE;
  295. nsCOMPtr<nsIURI> aURL;
  296. rv = aChannel->GetURI(getter_AddRefs(aURL));
  297. if (NS_FAILED(rv)) return rv;
  298. #ifdef NOISY_CREATE_DOC
  299. if (nullptr != aURL) {
  300. nsAutoString tmp;
  301. aURL->ToString(tmp);
  302. fputs(NS_LossyConvertUTF16toASCII(tmp).get(), stdout);
  303. printf(": creating document\n");
  304. }
  305. #endif
  306. // Create the document
  307. nsCOMPtr<nsIDocument> doc = do_CreateInstance(aDocumentCID, &rv);
  308. NS_ENSURE_SUCCESS(rv, rv);
  309. // Create the content viewer XXX: could reuse content viewer here!
  310. nsCOMPtr<nsIContentViewer> contentViewer = NS_NewContentViewer();
  311. doc->SetContainer(static_cast<nsDocShell*>(aContainer));
  312. // Initialize the document to begin loading the data. An
  313. // nsIStreamListener connected to the parser is returned in
  314. // aDocListener.
  315. rv = doc->StartDocumentLoad(aCommand, aChannel, aLoadGroup, aContainer, aDocListener, true);
  316. NS_ENSURE_SUCCESS(rv, rv);
  317. // Bind the document to the Content Viewer
  318. contentViewer->LoadStart(doc);
  319. contentViewer.forget(aContentViewer);
  320. return NS_OK;
  321. }
  322. nsresult
  323. nsContentDLF::CreateXULDocument(const char* aCommand,
  324. nsIChannel* aChannel,
  325. nsILoadGroup* aLoadGroup,
  326. nsIDocShell* aContainer,
  327. nsISupports* aExtraInfo,
  328. nsIStreamListener** aDocListener,
  329. nsIContentViewer** aContentViewer)
  330. {
  331. nsresult rv;
  332. nsCOMPtr<nsIDocument> doc = do_CreateInstance(kXULDocumentCID, &rv);
  333. if (NS_FAILED(rv)) return rv;
  334. nsCOMPtr<nsIContentViewer> contentViewer = NS_NewContentViewer();
  335. nsCOMPtr<nsIURI> aURL;
  336. rv = aChannel->GetURI(getter_AddRefs(aURL));
  337. if (NS_FAILED(rv)) return rv;
  338. /*
  339. * Initialize the document to begin loading the data...
  340. *
  341. * An nsIStreamListener connected to the parser is returned in
  342. * aDocListener.
  343. */
  344. doc->SetContainer(static_cast<nsDocShell*>(aContainer));
  345. rv = doc->StartDocumentLoad(aCommand, aChannel, aLoadGroup, aContainer, aDocListener, true);
  346. if (NS_FAILED(rv)) return rv;
  347. /*
  348. * Bind the document to the Content Viewer...
  349. */
  350. contentViewer->LoadStart(doc);
  351. contentViewer.forget(aContentViewer);
  352. return NS_OK;
  353. }
  354. bool nsContentDLF::IsImageContentType(const char* aContentType) {
  355. return imgLoader::SupportImageWithMimeType(aContentType);
  356. }