Files
palemoon27/layout/build/nsContentDLF.cpp
T
roytam1 365b9b17b6 import change from rmottola/Arctic-Fox:
- Bug 1136379. Clean up the nsPluginHost API a bit. (c39f45953)
- Bug 1137897 - Do not throw exception from nsPluginTag::GetMimeTypes if a plug-in has no MIME-Types. (9d77c7205)
- Bug 1103036 - Allow ContentChild to perform tasks during shutdown (342555d90)
- Bug 1140714 - Pass through remote NS_OpenAnonymousTemporaryFile failure to caller. (04f01ef2c)
- Bug 1124907 - Call SetCompositor() only when it is valid (2255eeda2)
- Bug 1145149 - Include IntegerPrintfMacros in PerfSpewer.cpp (0e755cae8)
- Bug 1146223 - IonMonkey: MIPS: Fix a typo in storeFloat32x3 (36dd95699)
- Bug 1143686 - Gtk3 - Render scrollbar thumb with margin. (384347c60)
- Bug 1143686 - Gtk3 - Render scrollbar thumb with margin. (8711babf3)
- Bug 1142074 - remove NS_{IMPORT,EXPORT}_STATIC_MEMBER_ These have been unused for quite some time, and we have other ways of flagging static variables for export/import now.  Whatever GCC bugs necessitated these macros are long gone as well. (a829ba500)
- Bug 1139361 - Remove gTLSThreadID*, NS_TLS and mozilla::threads::ID. (4c21669cb)
- Bug 1138123 - remove xpcom/sample/ as a bygone relic (2a4e33835)
- cleanup MOZ_THROW_EXPORT inspired from current FireFox removing the MOZALLOC_EXPORT hack (534f81737)
- Bug 868814 - Fold mozalloc library into mozglue. (324250a8c)
- Bug 1119482, part 1 - Use CycleCollectedJSRuntime::Get() in the various CC wrapper methods. (3c8638c57)
- Bug 1119482, part 2 - Move HoldDropJSObjects.h from xpcom/glue to xpcom/base. (c8ef6be30)
- Bug 1119482, part 3 - Move the Hold and DropJSObjects implementations to a new HoldDropJSObjects.cpp file. (8a128cabd)
- Bug 1119482, part 4 - Move IsJSHolder into HoldDropJSObjects. (154720b0d)
- Bug 1141660 - Fix bustage from bug 868814 with --disable-replace-malloc (7f74490b2)
- Followup for bug 868814 - replace non-obvious #if with simpler alternative. (2a1520452)
- Bug 1132771 - Add a test for reading all moz.build files in filesystem traversal mode; (49dcae4ea)
- Bug 1132771 - Support and test for reading without a config object; (b5fa44ed3)
- Bug 1132771 - API to return moz.build files relevant for a set of paths; (5a53e692a)
- Bug 1134072 - Remove support for post-eval sandbox callback; (fb63d7eda)
- Bug 1132771 - Implement strongly typed named tuples; (7acf71487)
- Bug 1135984 - Avoid setting an empty GYP_DIRS in contexts when reading it. (fceaf9f93)
- Bug 1134072 - Support for sub-contexts; r=glandium (41a0c2197)
- Bug 1136456 - Remove leftovers from TIERS support in mozbuild.frontend.reader. (4d5180e25)
- Bug 1132771 - Pass special types down to sandboxes via metadata; (5126bd8a6)
- Bug 1132771 - Support reading relevant moz.build files; r=glandium (6f9d6107c)
- Bug 1132771 - Add Files to moz.build with ability to define Bugzilla component; r=glandium (d5416a6cc)
- Bug 1132771 - Implement file-info mach command; r=glandium (64b7f8fad)
- Bug 1132771 - Define some bug components; r=glandium (e4d2b4645)
- Bug 1139050 - Add moz.build BUG_COMPONENT metadata for xpcom/. (ea30ac906)
- Bug 1139050, part 2 - Move nsAlgorithm.h to xpcom/base. (21abdde35)
- Bug 1134920 - Use moz_xmalloc/moz_xrealloc/free instead of nsMemory:Alloc/Realloc/Free. (5a00f0487)
- Bug 1134920 - Remove nsMemory::Alloc/Realloc/Free. (514d8e8eb)
- Bug 1120308 - tcp control channel for presentation api. (f3ee09a89)
- Bug 1125698 - Add ExpandErrorArguments() varargs helper function and remove uninitialized `dummy` va_list variable. (f9fdcd1db)
- Bug 1101627 - Add touch action regions. (6e86516f3)
- Bug 1092102 - Implement a WorkerDebuggerGlobalScope (4b3195921)
- Bug 1036967 - Introduce ScaleFactors2D. (2a80be7d6)
- Bug 1071018 - Work around an assertion (will be fixed properly in bug 1036967). (3f62cfa03)
- Bug 1023190 - Fix extra horizontal line when draw background image with repeat-x mode (5ab11f642)
- Bug 1135992 - Fix non-unified build failed in nsLayoutUtils.cpp. (8f8fcace8)
- Bug 1133492 - Extract some of nsPresShell into a separate TouchManager class. (9ac347878)
- Bug 1133492 - Extract some of nsPresShell into a separate TouchManager class. (37181a8a3)
- Bug 1122094 - Remove some unnecessary code. (6c3b1abe2)
- Don't async scroll overflowed single-line text boxes in APZ. (bug 1126090 part 6) (435d9d186)
2019-05-30 22:05:20 +08:00

423 lines
13 KiB
C++

/* -*- Mode: c++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=2 sw=2 et tw=78: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "nsCOMPtr.h"
#include "nsContentDLF.h"
#include "nsDocShell.h"
#include "nsGenericHTMLElement.h"
#include "nsGkAtoms.h"
#include "nsIComponentManager.h"
#include "nsIComponentRegistrar.h"
#include "nsIContentViewer.h"
#include "nsICategoryManager.h"
#include "nsIDocumentLoaderFactory.h"
#include "nsIDocument.h"
#include "nsIURL.h"
#include "nsNodeInfoManager.h"
#include "nsIScriptSecurityManager.h"
#include "nsString.h"
#include "nsContentCID.h"
#include "prprf.h"
#include "nsNetUtil.h"
#include "nsCRT.h"
#include "nsIViewSourceChannel.h"
#include "nsContentUtils.h"
#include "imgLoader.h"
#include "nsCharsetSource.h"
#include "nsMimeTypes.h"
#include "DecoderTraits.h"
// plugins
#include "nsIPluginHost.h"
#include "nsPluginHost.h"
static NS_DEFINE_CID(kPluginDocumentCID, NS_PLUGINDOCUMENT_CID);
// Factory code for creating variations on html documents
#undef NOISY_REGISTRY
static NS_DEFINE_IID(kHTMLDocumentCID, NS_HTMLDOCUMENT_CID);
static NS_DEFINE_IID(kXMLDocumentCID, NS_XMLDOCUMENT_CID);
static NS_DEFINE_IID(kSVGDocumentCID, NS_SVGDOCUMENT_CID);
static NS_DEFINE_IID(kVideoDocumentCID, NS_VIDEODOCUMENT_CID);
static NS_DEFINE_IID(kImageDocumentCID, NS_IMAGEDOCUMENT_CID);
static NS_DEFINE_IID(kXULDocumentCID, NS_XULDOCUMENT_CID);
already_AddRefed<nsIContentViewer> NS_NewContentViewer();
static const char* const gHTMLTypes[] = {
TEXT_HTML,
VIEWSOURCE_CONTENT_TYPE,
APPLICATION_XHTML_XML,
0
};
static const char* const gXMLTypes[] = {
TEXT_XML,
APPLICATION_XML,
APPLICATION_MATHML_XML,
APPLICATION_RDF_XML,
TEXT_RDF,
0
};
static const char* const gSVGTypes[] = {
IMAGE_SVG_XML,
0
};
static const char* const gXULTypes[] = {
TEXT_XUL,
APPLICATION_CACHED_XUL,
0
};
static bool
IsTypeInList(const nsACString& aType, const char* const aList[])
{
int32_t typeIndex;
for (typeIndex = 0; aList[typeIndex]; ++typeIndex) {
if (aType.Equals(aList[typeIndex])) {
return true;
}
}
return false;
}
nsresult
NS_NewContentDocumentLoaderFactory(nsIDocumentLoaderFactory** aResult)
{
NS_PRECONDITION(aResult, "null OUT ptr");
if (!aResult) {
return NS_ERROR_NULL_POINTER;
}
nsContentDLF* it = new nsContentDLF();
if (!it) {
return NS_ERROR_OUT_OF_MEMORY;
}
return CallQueryInterface(it, aResult);
}
nsContentDLF::nsContentDLF()
{
}
nsContentDLF::~nsContentDLF()
{
}
NS_IMPL_ISUPPORTS(nsContentDLF,
nsIDocumentLoaderFactory)
bool
MayUseXULXBL(nsIChannel* aChannel)
{
nsIScriptSecurityManager *securityManager =
nsContentUtils::GetSecurityManager();
if (!securityManager) {
return false;
}
nsCOMPtr<nsIPrincipal> principal;
securityManager->GetChannelResultPrincipal(aChannel, getter_AddRefs(principal));
NS_ENSURE_TRUE(principal, false);
return nsContentUtils::AllowXULXBLForPrincipal(principal);
}
NS_IMETHODIMP
nsContentDLF::CreateInstance(const char* aCommand,
nsIChannel* aChannel,
nsILoadGroup* aLoadGroup,
const nsACString& aContentType,
nsIDocShell* aContainer,
nsISupports* aExtraInfo,
nsIStreamListener** aDocListener,
nsIContentViewer** aDocViewer)
{
// Make a copy of aContentType, because we're possibly going to change it.
nsAutoCString contentType(aContentType);
// Are we viewing source?
nsCOMPtr<nsIViewSourceChannel> viewSourceChannel = do_QueryInterface(aChannel);
if (viewSourceChannel)
{
aCommand = "view-source";
// The parser freaks out when it sees the content-type that a
// view-source channel normally returns. Get the actual content
// type of the data. If it's known, use it; otherwise use
// text/plain.
nsAutoCString type;
viewSourceChannel->GetOriginalContentType(type);
bool knownType =
(!type.EqualsLiteral(VIEWSOURCE_CONTENT_TYPE) &&
IsTypeInList(type, gHTMLTypes)) ||
nsContentUtils::IsPlainTextType(type) ||
IsTypeInList(type, gXMLTypes) ||
IsTypeInList(type, gSVGTypes) ||
IsTypeInList(type, gXMLTypes);
if (knownType) {
viewSourceChannel->SetContentType(type);
} else if (IsImageContentType(type.get())) {
// If it's an image, we want to display it the same way we normally would.
// Also note the lifetime of "type" allows us to safely use "get()" here.
contentType = type;
} else {
viewSourceChannel->SetContentType(NS_LITERAL_CSTRING(TEXT_PLAIN));
}
} else if (aContentType.EqualsLiteral(VIEWSOURCE_CONTENT_TYPE)) {
aChannel->SetContentType(NS_LITERAL_CSTRING(TEXT_PLAIN));
contentType = TEXT_PLAIN;
}
// Try html or plaintext; both use the same document CID
if (IsTypeInList(contentType, gHTMLTypes) ||
nsContentUtils::IsPlainTextType(contentType)) {
return CreateDocument(aCommand,
aChannel, aLoadGroup,
aContainer, kHTMLDocumentCID,
aDocListener, aDocViewer);
}
if (IsTypeInList(contentType, gXMLTypes)) {
return CreateDocument(aCommand,
aChannel, aLoadGroup,
aContainer, kXMLDocumentCID,
aDocListener, aDocViewer);
}
// Try SVG
if (IsTypeInList(contentType, gSVGTypes)) {
return CreateDocument(aCommand,
aChannel, aLoadGroup,
aContainer, kSVGDocumentCID,
aDocListener, aDocViewer);
}
// Try XUL
if (IsTypeInList(contentType, gXULTypes)) {
if (!MayUseXULXBL(aChannel)) {
return NS_ERROR_REMOTE_XUL;
}
return CreateXULDocument(aCommand, aChannel, aLoadGroup, aContainer,
aExtraInfo, aDocListener, aDocViewer);
}
if (mozilla::DecoderTraits::ShouldHandleMediaType(contentType.get())) {
return CreateDocument(aCommand,
aChannel, aLoadGroup,
aContainer, kVideoDocumentCID,
aDocListener, aDocViewer);
}
// Try image types
if (IsImageContentType(contentType.get())) {
return CreateDocument(aCommand,
aChannel, aLoadGroup,
aContainer, kImageDocumentCID,
aDocListener, aDocViewer);
}
nsRefPtr<nsPluginHost> pluginHost = nsPluginHost::GetInst();
// Don't exclude disabled plugins, which will still trigger the "this plugin
// is disabled" placeholder.
if (pluginHost && pluginHost->HavePluginForType(contentType,
nsPluginHost::eExcludeNone)) {
return CreateDocument(aCommand,
aChannel, aLoadGroup,
aContainer, kPluginDocumentCID,
aDocListener, aDocViewer);
}
// If we get here, then we weren't able to create anything. Sorry!
return NS_ERROR_FAILURE;
}
NS_IMETHODIMP
nsContentDLF::CreateInstanceForDocument(nsISupports* aContainer,
nsIDocument* aDocument,
const char *aCommand,
nsIContentViewer** aContentViewer)
{
MOZ_ASSERT(aDocument);
nsCOMPtr<nsIContentViewer> contentViewer = NS_NewContentViewer();
// Bind the document to the Content Viewer
contentViewer->LoadStart(aDocument);
contentViewer.forget(aContentViewer);
return NS_OK;
}
NS_IMETHODIMP
nsContentDLF::CreateBlankDocument(nsILoadGroup *aLoadGroup,
nsIPrincipal* aPrincipal,
nsIDocument **aDocument)
{
*aDocument = nullptr;
nsresult rv = NS_ERROR_FAILURE;
// create a new blank HTML document
nsCOMPtr<nsIDocument> blankDoc(do_CreateInstance(kHTMLDocumentCID));
if (blankDoc) {
// initialize
nsCOMPtr<nsIURI> uri;
NS_NewURI(getter_AddRefs(uri), NS_LITERAL_CSTRING("about:blank"));
if (uri) {
blankDoc->ResetToURI(uri, aLoadGroup, aPrincipal);
rv = NS_OK;
}
}
// add some simple content structure
if (NS_SUCCEEDED(rv)) {
rv = NS_ERROR_FAILURE;
nsNodeInfoManager *nim = blankDoc->NodeInfoManager();
nsRefPtr<mozilla::dom::NodeInfo> htmlNodeInfo;
// generate an html html element
htmlNodeInfo = nim->GetNodeInfo(nsGkAtoms::html, 0, kNameSpaceID_XHTML,
nsIDOMNode::ELEMENT_NODE);
nsCOMPtr<nsIContent> htmlElement =
NS_NewHTMLHtmlElement(htmlNodeInfo.forget());
// generate an html head element
htmlNodeInfo = nim->GetNodeInfo(nsGkAtoms::head, 0, kNameSpaceID_XHTML,
nsIDOMNode::ELEMENT_NODE);
nsCOMPtr<nsIContent> headElement =
NS_NewHTMLHeadElement(htmlNodeInfo.forget());
// generate an html body elemment
htmlNodeInfo = nim->GetNodeInfo(nsGkAtoms::body, 0, kNameSpaceID_XHTML,
nsIDOMNode::ELEMENT_NODE);
nsCOMPtr<nsIContent> bodyElement =
NS_NewHTMLBodyElement(htmlNodeInfo.forget());
// blat in the structure
if (htmlElement && headElement && bodyElement) {
NS_ASSERTION(blankDoc->GetChildCount() == 0,
"Shouldn't have children");
rv = blankDoc->AppendChildTo(htmlElement, false);
if (NS_SUCCEEDED(rv)) {
rv = htmlElement->AppendChildTo(headElement, false);
if (NS_SUCCEEDED(rv)) {
// XXXbz Why not notifying here?
htmlElement->AppendChildTo(bodyElement, false);
}
}
}
}
// add a nice bow
if (NS_SUCCEEDED(rv)) {
blankDoc->SetDocumentCharacterSetSource(kCharsetFromDocTypeDefault);
blankDoc->SetDocumentCharacterSet(NS_LITERAL_CSTRING("UTF-8"));
*aDocument = blankDoc;
NS_ADDREF(*aDocument);
}
return rv;
}
nsresult
nsContentDLF::CreateDocument(const char* aCommand,
nsIChannel* aChannel,
nsILoadGroup* aLoadGroup,
nsIDocShell* aContainer,
const nsCID& aDocumentCID,
nsIStreamListener** aDocListener,
nsIContentViewer** aContentViewer)
{
nsresult rv = NS_ERROR_FAILURE;
nsCOMPtr<nsIURI> aURL;
rv = aChannel->GetURI(getter_AddRefs(aURL));
if (NS_FAILED(rv)) return rv;
#ifdef NOISY_CREATE_DOC
if (nullptr != aURL) {
nsAutoString tmp;
aURL->ToString(tmp);
fputs(NS_LossyConvertUTF16toASCII(tmp).get(), stdout);
printf(": creating document\n");
}
#endif
// Create the document
nsCOMPtr<nsIDocument> doc = do_CreateInstance(aDocumentCID, &rv);
NS_ENSURE_SUCCESS(rv, rv);
// Create the content viewer XXX: could reuse content viewer here!
nsCOMPtr<nsIContentViewer> contentViewer = NS_NewContentViewer();
doc->SetContainer(static_cast<nsDocShell*>(aContainer));
// Initialize the document to begin loading the data. An
// nsIStreamListener connected to the parser is returned in
// aDocListener.
rv = doc->StartDocumentLoad(aCommand, aChannel, aLoadGroup, aContainer, aDocListener, true);
NS_ENSURE_SUCCESS(rv, rv);
// Bind the document to the Content Viewer
contentViewer->LoadStart(doc);
contentViewer.forget(aContentViewer);
return NS_OK;
}
nsresult
nsContentDLF::CreateXULDocument(const char* aCommand,
nsIChannel* aChannel,
nsILoadGroup* aLoadGroup,
nsIDocShell* aContainer,
nsISupports* aExtraInfo,
nsIStreamListener** aDocListener,
nsIContentViewer** aContentViewer)
{
nsresult rv;
nsCOMPtr<nsIDocument> doc = do_CreateInstance(kXULDocumentCID, &rv);
if (NS_FAILED(rv)) return rv;
nsCOMPtr<nsIContentViewer> contentViewer = NS_NewContentViewer();
nsCOMPtr<nsIURI> aURL;
rv = aChannel->GetURI(getter_AddRefs(aURL));
if (NS_FAILED(rv)) return rv;
/*
* Initialize the document to begin loading the data...
*
* An nsIStreamListener connected to the parser is returned in
* aDocListener.
*/
doc->SetContainer(static_cast<nsDocShell*>(aContainer));
rv = doc->StartDocumentLoad(aCommand, aChannel, aLoadGroup, aContainer, aDocListener, true);
if (NS_FAILED(rv)) return rv;
/*
* Bind the document to the Content Viewer...
*/
contentViewer->LoadStart(doc);
contentViewer.forget(aContentViewer);
return NS_OK;
}
bool nsContentDLF::IsImageContentType(const char* aContentType) {
return imgLoader::SupportImageWithMimeType(aContentType);
}