gecko-dev/dom/xul/XULDocument.cpp
Brendan Dahl 7ebaf1cd2b Bug 1527977 - Share XUL prototype cache with XUL and XHTML. r=smaug
Create a new parser (PrototypeDocumentParser) and content sink
(PrototypeDocumentContentSink) that can be used by both XUL and XHTML.

The new parser moves the code from XULDocument that handles creating and
loading a nsXULPrototypeDocument from either the cache or the source
file. Once the parser has finished loading the prototype it notifies the
content sink. The parser is largely a stub and would be better suited
for use as a nsBaseParser, but nsHTMLDocument unfortunately needs an
nsIParser.

The new content sink has the XULDocument code responsible for the
prototype traversal that creates the DOM (XULDocument::ResumeWalk and
friends) and fires off various events.

To unify XUL and XHTML, the XHTML readystate event sequence is used in
XUL. However, the layout path of XHTML loaded from the prototype cache
more closely follows XUL, where frame initializers and layout don't
start until the entire DOM is built.

Differential Revision: https://phabricator.services.mozilla.com/D21236

--HG--
rename : dom/xul/XULDocument.cpp => dom/prototype/PrototypeDocumentContentSink.cpp
rename : parser/moz.build => dom/prototype/moz.build
rename : parser/moz.build => parser/prototype/moz.build
extra : moz-landing-system : lando
2019-03-09 01:00:23 +00:00

381 lines
11 KiB
C++

/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=4 sw=2 et tw=80: */
/* 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/. */
/*
An implementation for the XUL document. This implementation serves
as the basis for generating an NGLayout content model.
Notes
-----
1. We do some monkey business in the document observer methods to
keep the element map in sync for HTML elements. Why don't we just
do it for _all_ elements? Well, in the case of XUL elements,
which may be lazily created during frame construction, the
document observer methods will never be called because we'll be
adding the XUL nodes into the content model "quietly".
*/
#include "mozilla/ArrayUtils.h"
#include <algorithm>
#include "XULDocument.h"
#include "nsError.h"
#include "nsIBoxObject.h"
#include "nsIChromeRegistry.h"
#include "nsView.h"
#include "nsViewManager.h"
#include "nsIContentViewer.h"
#include "nsIStreamListener.h"
#include "nsITimer.h"
#include "nsDocShell.h"
#include "nsGkAtoms.h"
#include "nsXMLContentSink.h"
#include "nsXULContentSink.h"
#include "nsXULContentUtils.h"
#include "nsIStringEnumerator.h"
#include "nsDocElementCreatedNotificationRunner.h"
#include "nsNetUtil.h"
#include "nsParserCIID.h"
#include "nsPIBoxObject.h"
#include "mozilla/dom/BoxObject.h"
#include "nsString.h"
#include "nsPIDOMWindow.h"
#include "nsPIWindowRoot.h"
#include "nsXULElement.h"
#include "nsXULPrototypeCache.h"
#include "mozilla/Logging.h"
#include "nsIFrame.h"
#include "nsXBLService.h"
#include "nsCExternalHandlerService.h"
#include "nsMimeTypes.h"
#include "nsIObjectInputStream.h"
#include "nsIObjectOutputStream.h"
#include "nsContentList.h"
#include "nsISimpleEnumerator.h"
#include "nsIScriptGlobalObject.h"
#include "nsIScriptSecurityManager.h"
#include "nsNodeInfoManager.h"
#include "nsContentCreatorFunctions.h"
#include "nsContentUtils.h"
#include "nsIParser.h"
#include "nsCharsetSource.h"
#include "mozilla/StyleSheetInlines.h"
#include "mozilla/css/Loader.h"
#include "nsIScriptError.h"
#include "nsIStyleSheetLinkingElement.h"
#include "nsIObserverService.h"
#include "nsNodeUtils.h"
#include "nsIXULWindow.h"
#include "nsXULPopupManager.h"
#include "nsCCUncollectableMarker.h"
#include "nsURILoader.h"
#include "mozilla/BasicEvents.h"
#include "mozilla/CycleCollectedJSContext.h"
#include "mozilla/dom/DocumentL10n.h"
#include "mozilla/dom/Element.h"
#include "mozilla/dom/NodeInfoInlines.h"
#include "mozilla/dom/ProcessingInstruction.h"
#include "mozilla/dom/ScriptSettings.h"
#include "mozilla/dom/XULDocumentBinding.h"
#include "mozilla/dom/XULPersist.h"
#include "mozilla/EventDispatcher.h"
#include "mozilla/LoadInfo.h"
#include "mozilla/Preferences.h"
#include "nsTextNode.h"
#include "nsJSUtils.h"
#include "js/CompilationAndEvaluation.h"
#include "js/SourceText.h"
#include "mozilla/dom/URL.h"
#include "nsIContentPolicy.h"
#include "mozAutoDocUpdate.h"
#include "xpcpublic.h"
#include "mozilla/StyleSheet.h"
#include "mozilla/StyleSheetInlines.h"
#include "nsIConsoleService.h"
#include "mozilla/parser/PrototypeDocumentParser.h"
#include "mozilla/dom/PrototypeDocumentContentSink.h"
using namespace mozilla;
using namespace mozilla::dom;
//----------------------------------------------------------------------
//
// CIDs
//
static NS_DEFINE_CID(kParserCID, NS_PARSER_CID);
//----------------------------------------------------------------------
//
// Statics
//
int32_t XULDocument::gRefCnt = 0;
LazyLogModule XULDocument::gXULLog("XULDocument");
//----------------------------------------------------------------------
//
// ctors & dtors
//
namespace mozilla {
namespace dom {
XULDocument::XULDocument(void)
: XMLDocument("application/vnd.mozilla.xul+xml") {
// Override the default in Document
mCharacterSet = UTF_8_ENCODING;
mDefaultElementType = kNameSpaceID_XUL;
mType = eXUL;
mAllowXULXBL = eTriTrue;
}
XULDocument::~XULDocument() {
Preferences::UnregisterCallback(XULDocument::DirectionChanged,
"intl.uidirection", this);
}
} // namespace dom
} // namespace mozilla
nsresult NS_NewXULDocument(Document** result) {
MOZ_ASSERT(result != nullptr, "null ptr");
if (!result) return NS_ERROR_NULL_POINTER;
RefPtr<XULDocument> doc = new XULDocument();
nsresult rv;
if (NS_FAILED(rv = doc->Init())) {
return rv;
}
doc.forget(result);
return NS_OK;
}
namespace mozilla {
namespace dom {
//----------------------------------------------------------------------
//
// nsISupports interface
//
NS_IMPL_CYCLE_COLLECTION_CLASS(XULDocument)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(XULDocument, XMLDocument)
NS_ASSERTION(
!nsCCUncollectableMarker::InGeneration(cb, tmp->GetMarkedCCGeneration()),
"Shouldn't traverse XULDocument!");
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(XULDocument, XMLDocument)
// XXX We should probably unlink all the objects we traverse.
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
NS_IMPL_ISUPPORTS_CYCLE_COLLECTION_INHERITED_0(XULDocument, XMLDocument)
//----------------------------------------------------------------------
//
// Document interface
//
void XULDocument::Reset(nsIChannel* aChannel, nsILoadGroup* aLoadGroup) {
MOZ_ASSERT_UNREACHABLE("Reset");
}
void XULDocument::ResetToURI(nsIURI* aURI, nsILoadGroup* aLoadGroup,
nsIPrincipal* aPrincipal) {
MOZ_ASSERT_UNREACHABLE("ResetToURI");
}
void XULDocument::SetContentType(const nsAString& aContentType) {
NS_ASSERTION(
aContentType.EqualsLiteral("application/vnd.mozilla.xul+xml"),
"xul-documents always has content-type application/vnd.mozilla.xul+xml");
// Don't do anything, xul always has the mimetype
// application/vnd.mozilla.xul+xml
}
// This is called when the master document begins loading, whether it's
// being cached or not.
nsresult XULDocument::StartDocumentLoad(const char* aCommand,
nsIChannel* aChannel,
nsILoadGroup* aLoadGroup,
nsISupports* aContainer,
nsIStreamListener** aDocListener,
bool aReset, nsIContentSink* aSink) {
if (MOZ_LOG_TEST(gXULLog, LogLevel::Warning)) {
nsCOMPtr<nsIURI> uri;
nsresult rv = aChannel->GetOriginalURI(getter_AddRefs(uri));
if (NS_SUCCEEDED(rv)) {
nsAutoCString urlspec;
rv = uri->GetSpec(urlspec);
if (NS_SUCCEEDED(rv)) {
MOZ_LOG(gXULLog, LogLevel::Warning,
("xul: load document '%s'", urlspec.get()));
}
}
}
MOZ_ASSERT(GetReadyStateEnum() == Document::READYSTATE_UNINITIALIZED,
"Bad readyState");
SetReadyStateInternal(READYSTATE_LOADING);
mDocumentLoadGroup = do_GetWeakReference(aLoadGroup);
mChannel = aChannel;
// Get the URI. Note that this should match nsDocShell::OnLoadingSite
nsresult rv = NS_GetFinalChannelURI(aChannel, getter_AddRefs(mDocumentURI));
NS_ENSURE_SUCCESS(rv, rv);
mOriginalURI = mDocumentURI;
// Get the document's principal
nsCOMPtr<nsIPrincipal> principal;
nsContentUtils::GetSecurityManager()->GetChannelResultPrincipal(
mChannel, getter_AddRefs(principal));
principal = MaybeDowngradePrincipal(principal);
SetPrincipal(principal);
ResetStylesheetsToURI(mDocumentURI);
RetrieveRelevantHeaders(aChannel);
mParser = new mozilla::parser::PrototypeDocumentParser(mDocumentURI, this);
nsCOMPtr<nsIStreamListener> listener = mParser->GetStreamListener();
listener.forget(aDocListener);
nsCOMPtr<nsIDocShell> docShell(do_QueryInterface(aContainer));
nsCOMPtr<nsIContentSink> sink;
NS_NewPrototypeDocumentContentSink(getter_AddRefs(sink), this, mDocumentURI,
docShell, aChannel);
mParser->SetContentSink(sink);
mParser->Parse(mDocumentURI, nullptr, (void*)this);
return NS_OK;
}
void XULDocument::EndLoad() {
mSynchronousDOMContentLoaded = true;
Document::EndLoad();
}
//----------------------------------------------------------------------
//
// nsINode interface
//
nsresult XULDocument::Clone(mozilla::dom::NodeInfo* aNodeInfo,
nsINode** aResult) const {
// We don't allow cloning of a XUL document
*aResult = nullptr;
return NS_ERROR_DOM_NOT_SUPPORTED_ERR;
}
//----------------------------------------------------------------------
//
// Implementation methods
//
nsresult XULDocument::Init() {
nsresult rv = XMLDocument::Init();
NS_ENSURE_SUCCESS(rv, rv);
if (gRefCnt++ == 0) {
// ensure that the XUL prototype cache is instantiated successfully,
// so that we can use nsXULPrototypeCache::GetInstance() without
// null-checks in the rest of the class.
nsXULPrototypeCache* cache = nsXULPrototypeCache::GetInstance();
if (!cache) {
NS_ERROR("Could not instantiate nsXULPrototypeCache");
return NS_ERROR_FAILURE;
}
}
Preferences::RegisterCallback(XULDocument::DirectionChanged,
"intl.uidirection", this);
return NS_OK;
}
bool XULDocument::IsDocumentRightToLeft() {
// setting the localedir attribute on the root element forces a
// specific direction for the document.
Element* element = GetRootElement();
if (element) {
static Element::AttrValuesArray strings[] = {nsGkAtoms::ltr, nsGkAtoms::rtl,
nullptr};
switch (element->FindAttrValueIn(kNameSpaceID_None, nsGkAtoms::localedir,
strings, eCaseMatters)) {
case 0:
return false;
case 1:
return true;
default:
break; // otherwise, not a valid value, so fall through
}
}
// otherwise, get the locale from the chrome registry and
// look up the intl.uidirection.<locale> preference
nsCOMPtr<nsIXULChromeRegistry> reg =
mozilla::services::GetXULChromeRegistryService();
if (!reg) return false;
nsAutoCString package;
bool isChrome;
if (NS_SUCCEEDED(mDocumentURI->SchemeIs("chrome", &isChrome)) && isChrome) {
mDocumentURI->GetHostPort(package);
} else {
// use the 'global' package for about and resource uris.
// otherwise, just default to left-to-right.
bool isAbout, isResource;
if (NS_SUCCEEDED(mDocumentURI->SchemeIs("about", &isAbout)) && isAbout) {
package.AssignLiteral("global");
} else if (NS_SUCCEEDED(mDocumentURI->SchemeIs("resource", &isResource)) &&
isResource) {
package.AssignLiteral("global");
} else {
return false;
}
}
bool isRTL = false;
reg->IsLocaleRTL(package, &isRTL);
return isRTL;
}
void XULDocument::ResetDocumentDirection() {
DocumentStatesChanged(NS_DOCUMENT_STATE_RTL_LOCALE);
}
void XULDocument::DirectionChanged(const char* aPrefName, XULDocument* aDoc) {
// Reset the direction and restyle the document if necessary.
if (aDoc) {
aDoc->ResetDocumentDirection();
}
}
JSObject* XULDocument::WrapNode(JSContext* aCx,
JS::Handle<JSObject*> aGivenProto) {
return XULDocument_Binding::Wrap(aCx, this, aGivenProto);
}
} // namespace dom
} // namespace mozilla