From 7fe15d1a52959f6aa5062fda7e82c0d7b04272aa Mon Sep 17 00:00:00 2001 From: Jessica Jong Date: Sat, 9 Dec 2017 11:25:14 +0800 Subject: [PATCH] Bug 1422931 - Part 2: Make webcomponents preference per-doc. r=smaug This is to fix the case where preference is restore to false when a testcase ends, but nsDocument::DeleteShell is called afterwards. So, we make the preference per-doc and set it when the document is created. The value does not change for the lifetime of the document. --- dom/base/ChildIterator.cpp | 3 ++- dom/base/nsContentUtils.cpp | 2 +- dom/base/nsDocument.cpp | 28 ++++++++++++++++++++++++++++ dom/base/nsDocument.h | 5 +++++ dom/base/nsIDocument.h | 8 ++++++++ dom/base/nsTextNode.cpp | 7 +++++++ dom/base/nsTextNode.h | 4 ++++ dom/html/HTMLSlotElement.cpp | 2 +- dom/webidl/Element.webidl | 12 ++++++------ dom/webidl/HTMLSlotElement.webidl | 2 +- dom/webidl/ShadowRoot.webidl | 2 +- dom/webidl/Text.webidl | 2 +- 12 files changed, 65 insertions(+), 12 deletions(-) diff --git a/dom/base/ChildIterator.cpp b/dom/base/ChildIterator.cpp index 6ef0c4e96d59..ad717889b317 100644 --- a/dom/base/ChildIterator.cpp +++ b/dom/base/ChildIterator.cpp @@ -12,6 +12,7 @@ #include "nsIAnonymousContentCreator.h" #include "nsIFrame.h" #include "nsCSSAnonBoxes.h" +#include "nsDocument.h" namespace mozilla { namespace dom { @@ -66,7 +67,7 @@ ExplicitChildIterator::ExplicitChildIterator(const nsIContent* aParent, mIsFirst(aStartAtBeginning), mIndexInInserted(0) { - mParentAsSlot = nsContentUtils::IsWebComponentsEnabled() ? + mParentAsSlot = nsDocument::IsWebComponentsEnabled(mParent) ? HTMLSlotElement::FromContent(mParent) : nullptr; } diff --git a/dom/base/nsContentUtils.cpp b/dom/base/nsContentUtils.cpp index 37cd08839330..c2a1a84aefc3 100644 --- a/dom/base/nsContentUtils.cpp +++ b/dom/base/nsContentUtils.cpp @@ -7534,7 +7534,7 @@ nsContentUtils::IsContentInsertionPoint(nsIContent* aContent) bool nsContentUtils::HasDistributedChildren(nsIContent* aContent) { - if (!IsWebComponentsEnabled() || !aContent) { + if (!aContent || !nsDocument::IsWebComponentsEnabled(aContent)) { return false; } diff --git a/dom/base/nsDocument.cpp b/dom/base/nsDocument.cpp index c8a6905e3ef4..6dc003fb8b2e 100644 --- a/dom/base/nsDocument.cpp +++ b/dom/base/nsDocument.cpp @@ -1581,6 +1581,10 @@ nsIDocument::nsIDocument() for (auto& cnt : mIncCounters) { cnt = 0; } + + // Set this when document is created and value stays the same for the lifetime + // of the document. + mIsWebComponentsEnabled = nsContentUtils::IsWebComponentsEnabled(); } nsDocument::nsDocument(const char* aContentType) @@ -2720,6 +2724,30 @@ nsDocument::IsSynthesized() { return synthesized; } +bool +nsDocument::IsWebComponentsEnabled(JSContext* aCx, JSObject* aObject) +{ + JS::Rooted obj(aCx, aObject); + + JSAutoCompartment ac(aCx, obj); + JS::Rooted global(aCx, JS_GetGlobalForObject(aCx, obj)); + nsCOMPtr window = + do_QueryInterface(nsJSUtils::GetStaticScriptGlobal(global)); + + nsIDocument* doc = window ? window->GetExtantDoc() : nullptr; + if (!doc) { + return false; + } + + return doc->IsWebComponentsEnabled(); +} + +bool +nsDocument::IsWebComponentsEnabled(const nsINode* aNode) +{ + return aNode->OwnerDoc()->IsWebComponentsEnabled(); +} + nsresult nsDocument::StartDocumentLoad(const char* aCommand, nsIChannel* aChannel, nsILoadGroup* aLoadGroup, diff --git a/dom/base/nsDocument.h b/dom/base/nsDocument.h index a31622ea8571..324b31e6bb8c 100644 --- a/dom/base/nsDocument.h +++ b/dom/base/nsDocument.h @@ -665,6 +665,11 @@ public: virtual void ResolveScheduledSVGPresAttrs() override; bool IsSynthesized(); + // Check whether web components are enabled for the global of aObject. + static bool IsWebComponentsEnabled(JSContext* aCx, JSObject* aObject); + // Check whether web components are enabled for the document this node belongs + // to. + static bool IsWebComponentsEnabled(const nsINode* aNode); private: void AddOnDemandBuiltInUASheet(mozilla::StyleSheet* aSheet); void SendToConsole(nsCOMArray& aMessages); diff --git a/dom/base/nsIDocument.h b/dom/base/nsIDocument.h index fcbb903a75e8..74294fa0491c 100644 --- a/dom/base/nsIDocument.h +++ b/dom/base/nsIDocument.h @@ -3256,6 +3256,11 @@ public: virtual bool AllowPaymentRequest() const = 0; virtual void SetAllowPaymentRequest(bool aAllowPaymentRequest) = 0; + bool IsWebComponentsEnabled() const + { + return mIsWebComponentsEnabled; + } + protected: bool GetUseCounter(mozilla::UseCounter aUseCounter) { @@ -3612,6 +3617,9 @@ protected: // True if the encoding menu should be disabled. bool mEncodingMenuDisabled : 1; + // True if dom.webcomponents.enabled pref is set when document is created. + bool mIsWebComponentsEnabled : 1; + // Whether