Bug 1389300 - Inherit style backend into NS_NewDOMDocument. r=smaug,r=heycam
authorBobby Holley <bobbyholley@gmail.com>
Mon, 14 Aug 2017 19:50:28 -0700
changeset 374956 f10462fa4e8915711b6d34b999b00483a2bb4077
parent 374955 eb60d0337b16b4b5fc8fa7478899619aad34e653
child 374957 c5c135b17ec1e33c0e14427534455a007ad2a425
push id32343
push usercbook@mozilla.com
push dateWed, 16 Aug 2017 09:20:56 +0000
treeherdermozilla-central@0aa944d3ac94 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssmaug, heycam
bugs1389300
milestone57.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1389300 - Inherit style backend into NS_NewDOMDocument. r=smaug,r=heycam Our current machinery for enabling stylo requires a docshell - if there isn't one, we default to the Gecko style system. When getComputedStyle operates on an element without a presshell, it uses the caller's presshell instead. If the element has previously been styled with one style system (but no longer has a presshell), and the caller uses a different style backend, using the caller's style system can cause crashes when we pull bits of cached data off the DOM (like cached style attributes). So we want to throw when window.getComputedStyle(element) is called for a (window, element) pair with different style backends (which is what the next patch in this bug does). However, that causes a few failures where stylo-backed documents try to do getComputedStyle on an XHR document (which, without a docshell, will use the gecko style system). So this patch does some work to propagate the creator's style backend into various docshell-less documents. This should allow both chrome (which uses gecko) and content (which uses stylo) to use getComputedStyle on the response document for XHRs they create. Note that the second patch in this bug will make chromeWin.getComputedStyle(contentObj) throw. If we discover code that does that, we can just make it invoke the content's getComputedStyle method over Xrays. MozReview-Commit-ID: 5OsmHJKq5Ui
dom/base/DOMImplementation.cpp
dom/base/DOMParser.cpp
dom/base/nsContentUtils.cpp
dom/base/nsDocument.cpp
dom/base/nsIDocument.h
dom/xbl/nsXBLDocumentInfo.cpp
dom/xhr/XMLHttpRequestMainThread.cpp
dom/xhr/XMLHttpRequestMainThread.h
dom/xml/XMLDocument.cpp
gfx/thebes/gfxSVGGlyphs.cpp
parser/html/nsParserUtils.cpp
--- a/dom/base/DOMImplementation.cpp
+++ b/dom/base/DOMImplementation.cpp
@@ -118,17 +118,18 @@ DOMImplementation::CreateDocument(const 
 
   nsCOMPtr<nsIDOMDocument> document;
 
   rv = NS_NewDOMDocument(getter_AddRefs(document),
                          aNamespaceURI, aQualifiedName, aDoctype,
                          mDocumentURI, mBaseURI,
                          mOwner->NodePrincipal(),
                          true, scriptHandlingObject,
-                         DocumentFlavorLegacyGuess);
+                         DocumentFlavorLegacyGuess,
+                         mOwner->GetStyleBackendType());
   NS_ENSURE_SUCCESS(rv, rv);
 
   // When DOMImplementation's createDocument method is invoked with
   // namespace set to HTML Namespace use the registry of the associated
   // document to the new instance.
   nsCOMPtr<nsIDocument> doc = do_QueryInterface(document);
 
   if (aNamespaceURI.EqualsLiteral("http://www.w3.org/1999/xhtml")) {
@@ -197,17 +198,18 @@ DOMImplementation::CreateHTMLDocument(co
   NS_ENSURE_STATE(!mScriptObject || scriptHandlingObject);
 
   nsCOMPtr<nsIDOMDocument> document;
   rv = NS_NewDOMDocument(getter_AddRefs(document),
                          EmptyString(), EmptyString(),
                          doctype, mDocumentURI, mBaseURI,
                          mOwner->NodePrincipal(),
                          true, scriptHandlingObject,
-                         DocumentFlavorLegacyGuess);
+                         DocumentFlavorLegacyGuess,
+                         mOwner->GetStyleBackendType());
   NS_ENSURE_SUCCESS(rv, rv);
   nsCOMPtr<nsIDocument> doc = do_QueryInterface(document);
 
   nsCOMPtr<Element> root = doc->CreateElem(NS_LITERAL_STRING("html"), nullptr,
                                            kNameSpaceID_XHTML);
   rv = doc->AppendChildTo(root, false);
   NS_ENSURE_SUCCESS(rv, rv);
 
--- a/dom/base/DOMParser.cpp
+++ b/dom/base/DOMParser.cpp
@@ -456,18 +456,26 @@ DOMParser::SetUpDocument(DocumentFlavor 
     NS_ENSURE_TRUE(!mAttemptedInit, NS_ERROR_NOT_INITIALIZED);
     AttemptedInitMarker marker(&mAttemptedInit);
 
     nsCOMPtr<nsIPrincipal> prin = NullPrincipal::Create();
     rv = Init(prin, nullptr, nullptr, scriptHandlingObject);
     NS_ENSURE_SUCCESS(rv, rv);
   }
 
+  // Try to inherit a style backend.
+  auto styleBackend = StyleBackendType::None;
+  nsCOMPtr<nsPIDOMWindowInner> window = do_QueryInterface(mScriptHandlingObject);
+  if (window && window->GetExtantDoc()) {
+    styleBackend = window->GetExtantDoc()->GetStyleBackendType();
+  }
+
   NS_ASSERTION(mPrincipal, "Must have principal by now");
   NS_ASSERTION(mDocumentURI, "Must have document URI by now");
 
   return NS_NewDOMDocument(aResult, EmptyString(), EmptyString(), nullptr,
                            mDocumentURI, mBaseURI,
                            mPrincipal,
                            true,
                            scriptHandlingObject,
-                           aFlavor);
+                           aFlavor,
+                           styleBackend);
 }
--- a/dom/base/nsContentUtils.cpp
+++ b/dom/base/nsContentUtils.cpp
@@ -5195,17 +5195,18 @@ nsContentUtils::ConvertToPlainText(const
                                   EmptyString(),
                                   EmptyString(),
                                   nullptr,
                                   uri,
                                   uri,
                                   principal,
                                   true,
                                   nullptr,
-                                  DocumentFlavorHTML);
+                                  DocumentFlavorHTML,
+                                  StyleBackendType::None);
   NS_ENSURE_SUCCESS(rv, rv);
 
   nsCOMPtr<nsIDocument> document = do_QueryInterface(domDocument);
   rv = nsContentUtils::ParseDocumentHTML(aSourceBuffer, document,
     !(aFlags & nsIDocumentEncoder::OutputNoScriptContent));
   NS_ENSURE_SUCCESS(rv, rv);
 
   nsCOMPtr<nsIDocumentEncoder> encoder = do_CreateInstance(
--- a/dom/base/nsDocument.cpp
+++ b/dom/base/nsDocument.cpp
@@ -9933,17 +9933,18 @@ nsDocument::GetTemplateContentsOwner()
                                     EmptyString(), // aNamespaceURI
                                     EmptyString(), // aQualifiedName
                                     nullptr, // aDoctype
                                     nsIDocument::GetDocumentURI(),
                                     nsIDocument::GetDocBaseURI(),
                                     NodePrincipal(),
                                     true, // aLoadedAsData
                                     scriptObject, // aEventObject
-                                    DocumentFlavorHTML);
+                                    DocumentFlavorHTML,
+                                    mStyleBackendType);
     NS_ENSURE_SUCCESS(rv, nullptr);
 
     mTemplateContentsOwner = do_QueryInterface(domDocument);
     NS_ENSURE_TRUE(mTemplateContentsOwner, nullptr);
 
     nsDocument* doc = static_cast<nsDocument*>(mTemplateContentsOwner.get());
 
     if (!scriptObject) {
@@ -12580,16 +12581,22 @@ nsIDocument::Constructor(const GlobalObj
                          ErrorResult& rv)
 {
   nsCOMPtr<nsIScriptGlobalObject> global = do_QueryInterface(aGlobal.GetAsSupports());
   if (!global) {
     rv.Throw(NS_ERROR_UNEXPECTED);
     return nullptr;
   }
 
+  auto styleBackend = StyleBackendType::None;
+  nsCOMPtr<nsPIDOMWindowInner> window = do_QueryInterface(global);
+  if (window && window->GetExtantDoc()) {
+    styleBackend = window->GetExtantDoc()->GetStyleBackendType();
+  }
+
   nsCOMPtr<nsIScriptObjectPrincipal> prin = do_QueryInterface(aGlobal.GetAsSupports());
   if (!prin) {
     rv.Throw(NS_ERROR_UNEXPECTED);
     return nullptr;
   }
 
   nsCOMPtr<nsIURI> uri;
   NS_NewURI(getter_AddRefs(uri), "about:blank");
@@ -12604,17 +12611,18 @@ nsIDocument::Constructor(const GlobalObj
                       NullString(),
                       EmptyString(),
                       nullptr,
                       uri,
                       uri,
                       prin->GetPrincipal(),
                       true,
                       global,
-                      DocumentFlavorPlain);
+                      DocumentFlavorPlain,
+                      styleBackend);
   if (NS_FAILED(res)) {
     rv.Throw(res);
     return nullptr;
   }
 
   nsCOMPtr<nsIDocument> doc = do_QueryInterface(document);
   doc->SetReadyStateInternal(nsIDocument::READYSTATE_COMPLETE);
 
--- a/dom/base/nsIDocument.h
+++ b/dom/base/nsIDocument.h
@@ -3629,25 +3629,27 @@ NS_NewDOMDocument(nsIDOMDocument** aInst
                   const nsAString& aNamespaceURI,
                   const nsAString& aQualifiedName,
                   nsIDOMDocumentType* aDoctype,
                   nsIURI* aDocumentURI,
                   nsIURI* aBaseURI,
                   nsIPrincipal* aPrincipal,
                   bool aLoadedAsData,
                   nsIGlobalObject* aEventObject,
-                  DocumentFlavor aFlavor);
+                  DocumentFlavor aFlavor,
+                  mozilla::StyleBackendType aStyleBackend);
 
 // This is used only for xbl documents created from the startup cache.
 // Non-cached documents are created in the same manner as xml documents.
 nsresult
 NS_NewXBLDocument(nsIDOMDocument** aInstancePtrResult,
                   nsIURI* aDocumentURI,
                   nsIURI* aBaseURI,
-                  nsIPrincipal* aPrincipal);
+                  nsIPrincipal* aPrincipal,
+                  mozilla::StyleBackendType aStyleBackend);
 
 nsresult
 NS_NewPluginDocument(nsIDocument** aInstancePtrResult);
 
 inline nsIDocument*
 nsINode::GetOwnerDocument() const
 {
   nsIDocument* ownerDoc = OwnerDoc();
--- a/dom/xbl/nsXBLDocumentInfo.cpp
+++ b/dom/xbl/nsXBLDocumentInfo.cpp
@@ -221,28 +221,25 @@ nsXBLDocumentInfo::ReadPrototypeBindings
     startupCache->InvalidateCache();
     return NS_ERROR_NOT_AVAILABLE;
   }
 
   nsCOMPtr<nsIPrincipal> principal;
   nsContentUtils::GetSecurityManager()->
     GetSystemPrincipal(getter_AddRefs(principal));
 
+  auto styleBackend = aBoundDocument ? aBoundDocument->GetStyleBackendType()
+                                     : StyleBackendType::Gecko;
   nsCOMPtr<nsIDOMDocument> domdoc;
-  rv = NS_NewXBLDocument(getter_AddRefs(domdoc), aURI, nullptr, principal);
+  rv = NS_NewXBLDocument(getter_AddRefs(domdoc), aURI, nullptr, principal, styleBackend);
   NS_ENSURE_SUCCESS(rv, rv);
 
   nsCOMPtr<nsIDocument> doc = do_QueryInterface(domdoc);
   NS_ASSERTION(doc, "Must have a document!");
 
-  // Set the style backend type immediately after creating the XBL document.
-  // Assume gecko if there's no bound document.
-  doc->SetStyleBackendType(aBoundDocument ? aBoundDocument->GetStyleBackendType()
-                                          : StyleBackendType::Gecko);
-
   RefPtr<nsXBLDocumentInfo> docInfo = new nsXBLDocumentInfo(doc);
 
   while (1) {
     uint8_t flags;
     nsresult rv = stream->Read8(&flags);
     NS_ENSURE_SUCCESS(rv, rv);
     if (flags == XBLBinding_Serialize_NoMoreBindings)
       break;
--- a/dom/xhr/XMLHttpRequestMainThread.cpp
+++ b/dom/xhr/XMLHttpRequestMainThread.cpp
@@ -181,16 +181,17 @@ bool
 XMLHttpRequestMainThread::sDontWarnAboutSyncXHR = false;
 
 XMLHttpRequestMainThread::XMLHttpRequestMainThread()
   : mResponseBodyDecodedPos(0),
     mResponseCharset(nullptr),
     mResponseType(XMLHttpRequestResponseType::_empty),
     mRequestObserver(nullptr),
     mState(State::unsent),
+    mStyleBackend(StyleBackendType::None),
     mFlagSynchronous(false), mFlagAborted(false), mFlagParseBody(false),
     mFlagSyncLooping(false), mFlagBackgroundRequest(false),
     mFlagHadUploadListenersOnSend(false), mFlagACwithCredentials(false),
     mFlagTimedOut(false), mFlagDeleted(false), mFlagSend(false),
     mUploadTransferred(0), mUploadTotal(0), mUploadComplete(true),
     mProgressSinceLastProgressEvent(false),
     mRequestSentTime(0), mTimeoutMilliseconds(0),
     mErrorLoad(ErrorType::eOK), mErrorParsingXML(false),
@@ -2157,17 +2158,18 @@ XMLHttpRequestMainThread::OnStartRequest
     rv = nsContentUtils::GetSecurityManager()->
        GetChannelResultPrincipal(channel, getter_AddRefs(requestingPrincipal));
     NS_ENSURE_SUCCESS(rv, rv);
 
     rv = NS_NewDOMDocument(getter_AddRefs(responseDoc),
                            emptyStr, emptyStr, nullptr, docURI,
                            baseURI, requestingPrincipal, true, global,
                            mIsHtml ? DocumentFlavorHTML :
-                                     DocumentFlavorLegacyGuess);
+                                     DocumentFlavorLegacyGuess,
+                           mStyleBackend);
     NS_ENSURE_SUCCESS(rv, rv);
     mResponseXML = do_QueryInterface(responseDoc);
     mResponseXML->SetChromeXHRDocURI(chromeXHRDocURI);
     mResponseXML->SetChromeXHRDocBaseURI(chromeXHRDocBaseURI);
 
     // suppress parsing failure messages to console for statuses which
     // can have empty bodies (see bug 884693).
     uint32_t responseStatus;
--- a/dom/xhr/XMLHttpRequestMainThread.h
+++ b/dom/xhr/XMLHttpRequestMainThread.h
@@ -195,18 +195,23 @@ public:
   XMLHttpRequestMainThread();
 
   void Construct(nsIPrincipal* aPrincipal,
                  nsIGlobalObject* aGlobalObject,
                  nsIURI* aBaseURI = nullptr,
                  nsILoadGroup* aLoadGroup = nullptr)
   {
     MOZ_ASSERT(aPrincipal);
-    MOZ_ASSERT_IF(nsCOMPtr<nsPIDOMWindowInner> win = do_QueryInterface(
-      aGlobalObject), win->IsInnerWindow());
+    nsCOMPtr<nsPIDOMWindowInner> win = do_QueryInterface(aGlobalObject);
+    if (win) {
+      MOZ_ASSERT(win->IsInnerWindow());
+      if (win->GetExtantDoc()) {
+        mStyleBackend = win->GetExtantDoc()->GetStyleBackendType();
+      }
+    }
     mPrincipal = aPrincipal;
     BindToOwner(aGlobalObject);
     mBaseURI = aBaseURI;
     mLoadGroup = aLoadGroup;
   }
 
   void InitParameters(bool aAnon, bool aSystem);
 
@@ -738,16 +743,18 @@ protected:
 
   nsIRequestObserver* mRequestObserver;
 
   nsCOMPtr<nsIURI> mBaseURI;
   nsCOMPtr<nsILoadGroup> mLoadGroup;
 
   State mState;
 
+  StyleBackendType mStyleBackend;
+
   bool mFlagSynchronous;
   bool mFlagAborted;
   bool mFlagParseBody;
   bool mFlagSyncLooping;
   bool mFlagBackgroundRequest;
   bool mFlagHadUploadListenersOnSend;
   bool mFlagACwithCredentials;
   bool mFlagTimedOut;
--- a/dom/xml/XMLDocument.cpp
+++ b/dom/xml/XMLDocument.cpp
@@ -64,17 +64,18 @@ NS_NewDOMDocument(nsIDOMDocument** aInst
                   const nsAString& aNamespaceURI,
                   const nsAString& aQualifiedName,
                   nsIDOMDocumentType* aDoctype,
                   nsIURI* aDocumentURI,
                   nsIURI* aBaseURI,
                   nsIPrincipal* aPrincipal,
                   bool aLoadedAsData,
                   nsIGlobalObject* aEventObject,
-                  DocumentFlavor aFlavor)
+                  DocumentFlavor aFlavor,
+                  StyleBackendType aStyleBackend)
 {
   // Note: can't require that aDocumentURI/aBaseURI/aPrincipal be non-null,
   // since at least one caller (XMLHttpRequest) doesn't have decent args to
   // pass in.
 
   nsresult rv;
 
   *aInstancePtrResult = nullptr;
@@ -123,16 +124,22 @@ NS_NewDOMDocument(nsIDOMDocument** aInst
     MOZ_ASSERT(aFlavor == DocumentFlavorLegacyGuess);
     rv = NS_NewXMLDocument(getter_AddRefs(d));
   }
 
   if (NS_FAILED(rv)) {
     return rv;
   }
 
+  // If we were passed an explicit style backend for this document set it
+  // immediately after creation, before any content is inserted.
+  if (aStyleBackend != StyleBackendType::None) {
+    d->SetStyleBackendType(aStyleBackend);
+  }
+
   if (isHTML) {
     nsCOMPtr<nsIHTMLDocument> htmlDoc = do_QueryInterface(d);
     NS_ASSERTION(htmlDoc, "HTML Document doesn't implement nsIHTMLDocument?");
     htmlDoc->SetCompatibilityMode(eCompatibility_FullStandards);
     htmlDoc->SetIsXHTML(isXHTML);
   }
   nsDocument* doc = static_cast<nsDocument*>(d.get());
   doc->SetLoadedAsData(aLoadedAsData);
@@ -204,23 +211,25 @@ NS_NewXMLDocument(nsIDocument** aInstanc
 
   return NS_OK;
 }
 
 nsresult
 NS_NewXBLDocument(nsIDOMDocument** aInstancePtrResult,
                   nsIURI* aDocumentURI,
                   nsIURI* aBaseURI,
-                  nsIPrincipal* aPrincipal)
+                  nsIPrincipal* aPrincipal,
+                  StyleBackendType aStyleBackend)
 {
   nsresult rv = NS_NewDOMDocument(aInstancePtrResult,
                                   NS_LITERAL_STRING("http://www.mozilla.org/xbl"),
                                   NS_LITERAL_STRING("bindings"), nullptr,
                                   aDocumentURI, aBaseURI, aPrincipal, false,
-                                  nullptr, DocumentFlavorLegacyGuess);
+                                  nullptr, DocumentFlavorLegacyGuess,
+                                  aStyleBackend);
   NS_ENSURE_SUCCESS(rv, rv);
 
   nsCOMPtr<nsIDocument> idoc = do_QueryInterface(*aInstancePtrResult);
   nsDocument* doc = static_cast<nsDocument*>(idoc.get());
   doc->SetLoadedAsInteractiveData(true);
   doc->SetReadyStateInternal(nsIDocument::READYSTATE_COMPLETE);
 
   return NS_OK;
--- a/gfx/thebes/gfxSVGGlyphs.cpp
+++ b/gfx/thebes/gfxSVGGlyphs.cpp
@@ -356,25 +356,28 @@ gfxSVGGlyphsDocument::ParseDocument(cons
                                                    nullptr,
                                                    mSVGGlyphsDocumentURI);
  
     rv = NS_NewURI(getter_AddRefs(uri), mSVGGlyphsDocumentURI);
     NS_ENSURE_SUCCESS(rv, rv);
 
     nsCOMPtr<nsIPrincipal> principal = NullPrincipal::Create();
 
+    auto styleBackend = nsLayoutUtils::StyloEnabled() ? StyleBackendType::Servo
+                                                      : StyleBackendType::Gecko;
     nsCOMPtr<nsIDOMDocument> domDoc;
     rv = NS_NewDOMDocument(getter_AddRefs(domDoc),
                            EmptyString(),   // aNamespaceURI
                            EmptyString(),   // aQualifiedName
                            nullptr,          // aDoctype
                            uri, uri, principal,
                            false,           // aLoadedAsData
                            nullptr,          // aEventObject
-                           DocumentFlavorSVG);
+                           DocumentFlavorSVG,
+                           styleBackend);
     NS_ENSURE_SUCCESS(rv, rv);
 
     nsCOMPtr<nsIDocument> document(do_QueryInterface(domDoc));
     if (!document) {
         return NS_ERROR_FAILURE;
     }
 
     nsCOMPtr<nsIChannel> channel;
--- a/parser/html/nsParserUtils.cpp
+++ b/parser/html/nsParserUtils.cpp
@@ -82,17 +82,18 @@ nsParserUtils::Sanitize(const nsAString&
                                   EmptyString(),
                                   EmptyString(),
                                   nullptr,
                                   uri,
                                   uri,
                                   principal,
                                   true,
                                   nullptr,
-                                  DocumentFlavorHTML);
+                                  DocumentFlavorHTML,
+                                  mozilla::StyleBackendType::None);
   NS_ENSURE_SUCCESS(rv, rv);
 
   nsCOMPtr<nsIDocument> document = do_QueryInterface(domDocument);
   rv = nsContentUtils::ParseDocumentHTML(aFromStr, document, false);
   NS_ENSURE_SUCCESS(rv, rv);
 
   nsTreeSanitizer sanitizer(aFlags);
   sanitizer.Sanitize(document);