From 921224f8f497e439fb55a8b15ca2c3f5bf38237c Mon Sep 17 00:00:00 2001 From: Boris Zbarsky Date: Tue, 22 Nov 2016 22:41:51 -0500 Subject: [PATCH] Bug 1319255 part 3. Don't create special elements for and unless webcomponents are enabled. r=wchen --- dom/base/nsDocument.cpp | 25 +++++++++++++++++++++++-- dom/base/nsDocument.h | 6 ++++++ dom/html/HTMLContentElement.cpp | 23 ++++++++++++++++++++++- dom/html/HTMLShadowElement.cpp | 23 ++++++++++++++++++++++- 4 files changed, 73 insertions(+), 4 deletions(-) diff --git a/dom/base/nsDocument.cpp b/dom/base/nsDocument.cpp index 5bb20c75ddd1..80aaded37ae1 100644 --- a/dom/base/nsDocument.cpp +++ b/dom/base/nsDocument.cpp @@ -5731,7 +5731,28 @@ nsDocument::IsWebComponentsEnabled(JSContext* aCx, JSObject* aObject) nsCOMPtr window = do_QueryInterface(nsJSUtils::GetStaticScriptGlobal(global)); - if (window) { + return IsWebComponentsEnabled(window); +} + +bool +nsDocument::IsWebComponentsEnabled(dom::NodeInfo* aNodeInfo) +{ + if (Preferences::GetBool("dom.webcomponents.enabled")) { + return true; + } + + nsIDocument* doc = aNodeInfo->GetDocument(); + // Use GetScopeObject() here so that data documents work the same way as the + // main document they're associated with. + nsCOMPtr window = + do_QueryInterface(doc->GetScopeObject()); + return IsWebComponentsEnabled(window); +} + +bool +nsDocument::IsWebComponentsEnabled(nsPIDOMWindowInner* aWindow) +{ + if (aWindow) { nsresult rv; nsCOMPtr permMgr = do_GetService(NS_PERMISSIONMANAGER_CONTRACTID, &rv); @@ -5739,7 +5760,7 @@ nsDocument::IsWebComponentsEnabled(JSContext* aCx, JSObject* aObject) uint32_t perm; rv = permMgr->TestPermissionFromWindow( - window, "moz-extremely-unstable-and-will-change-webcomponents", &perm); + aWindow, "moz-extremely-unstable-and-will-change-webcomponents", &perm); NS_ENSURE_SUCCESS(rv, false); return perm == nsIPermissionManager::ALLOW_ACTION; diff --git a/dom/base/nsDocument.h b/dom/base/nsDocument.h index adab0027edbb..a6eb8647d0e6 100644 --- a/dom/base/nsDocument.h +++ b/dom/base/nsDocument.h @@ -1382,7 +1382,13 @@ public: virtual already_AddRefed GetCustomElementRegistry() override; + // 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 global of the document + // this nodeinfo comes from. + static bool IsWebComponentsEnabled(mozilla::dom::NodeInfo* aNodeInfo); + // Check whether web components are enabled for the given window. + static bool IsWebComponentsEnabled(nsPIDOMWindowInner* aWindow); RefPtr mListenerManager; RefPtr mDOMStyleSheets; diff --git a/dom/html/HTMLContentElement.cpp b/dom/html/HTMLContentElement.cpp index bbc780845713..01c0158a0387 100644 --- a/dom/html/HTMLContentElement.cpp +++ b/dom/html/HTMLContentElement.cpp @@ -6,6 +6,7 @@ #include "mozilla/dom/HTMLContentElement.h" #include "mozilla/dom/HTMLContentElementBinding.h" +#include "mozilla/dom/HTMLUnknownElement.h" #include "mozilla/dom/NodeListBinding.h" #include "mozilla/dom/ShadowRoot.h" #include "mozilla/css/StyleRule.h" @@ -17,8 +18,28 @@ #include "nsRuleProcessorData.h" #include "nsRuleWalker.h" #include "nsCSSParser.h" +#include "nsDocument.h" -NS_IMPL_NS_NEW_HTML_ELEMENT(Content) +// Expand NS_IMPL_NS_NEW_HTML_ELEMENT(Content) to add check for web components +// being enabled. +nsGenericHTMLElement* +NS_NewHTMLContentElement(already_AddRefed&& aNodeInfo, + mozilla::dom::FromParser aFromParser) +{ + // When this check is removed, remove the nsDocument.h and + // HTMLUnknownElement.h includes. Also remove nsINode::IsHTMLContentElement. + // + // We have to jump through some hoops to be able to produce both NodeInfo* and + // already_AddRefed& for our callees. + RefPtr nodeInfo(aNodeInfo); + if (!nsDocument::IsWebComponentsEnabled(nodeInfo)) { + already_AddRefed nodeInfoArg(nodeInfo.forget()); + return new mozilla::dom::HTMLUnknownElement(nodeInfoArg); + } + + already_AddRefed nodeInfoArg(nodeInfo.forget()); + return new mozilla::dom::HTMLContentElement(nodeInfoArg); +} using namespace mozilla::dom; diff --git a/dom/html/HTMLShadowElement.cpp b/dom/html/HTMLShadowElement.cpp index 3d4c6ab723e0..8824c2875f7f 100644 --- a/dom/html/HTMLShadowElement.cpp +++ b/dom/html/HTMLShadowElement.cpp @@ -8,10 +8,31 @@ #include "ChildIterator.h" #include "nsContentUtils.h" +#include "nsDocument.h" #include "mozilla/dom/HTMLShadowElement.h" +#include "mozilla/dom/HTMLUnknownElement.h" #include "mozilla/dom/HTMLShadowElementBinding.h" -NS_IMPL_NS_NEW_HTML_ELEMENT(Shadow) +// Expand NS_IMPL_NS_NEW_HTML_ELEMENT(Shadow) to add check for web components +// being enabled. +nsGenericHTMLElement* +NS_NewHTMLShadowElement(already_AddRefed&& aNodeInfo, + mozilla::dom::FromParser aFromParser) +{ + // When this check is removed, remove the nsDocument.h and + // HTMLUnknownElement.h includes. Also remove nsINode::IsHTMLShadowElement. + // + // We have to jump through some hoops to be able to produce both NodeInfo* and + // already_AddRefed& for our callees. + RefPtr nodeInfo(aNodeInfo); + if (!nsDocument::IsWebComponentsEnabled(nodeInfo)) { + already_AddRefed nodeInfoArg(nodeInfo.forget()); + return new mozilla::dom::HTMLUnknownElement(nodeInfoArg); + } + + already_AddRefed nodeInfoArg(nodeInfo.forget()); + return new mozilla::dom::HTMLShadowElement(nodeInfoArg); +} using namespace mozilla::dom;