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.
This commit is contained in:
Jessica Jong 2017-12-09 11:25:14 +08:00
Родитель b519c97d26
Коммит 7fe15d1a52
12 изменённых файлов: 65 добавлений и 12 удалений

Просмотреть файл

@ -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;
}

Просмотреть файл

@ -7534,7 +7534,7 @@ nsContentUtils::IsContentInsertionPoint(nsIContent* aContent)
bool
nsContentUtils::HasDistributedChildren(nsIContent* aContent)
{
if (!IsWebComponentsEnabled() || !aContent) {
if (!aContent || !nsDocument::IsWebComponentsEnabled(aContent)) {
return false;
}

Просмотреть файл

@ -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<JSObject*> obj(aCx, aObject);
JSAutoCompartment ac(aCx, obj);
JS::Rooted<JSObject*> global(aCx, JS_GetGlobalForObject(aCx, obj));
nsCOMPtr<nsPIDOMWindowInner> 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,

Просмотреть файл

@ -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<nsISecurityConsoleMessage>& aMessages);

Просмотреть файл

@ -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 <style scoped> support is enabled in this document.
enum { eScopedStyle_Unknown, eScopedStyle_Disabled, eScopedStyle_Enabled };
unsigned int mIsScopedStyleEnabled : 2;

Просмотреть файл

@ -21,6 +21,7 @@
#ifdef DEBUG
#include "nsRange.h"
#endif
#include "nsDocument.h"
using namespace mozilla;
using namespace mozilla::dom;
@ -157,6 +158,12 @@ void nsTextNode::UnbindFromTree(bool aDeep, bool aNullParent)
nsGenericDOMDataNode::UnbindFromTree(aDeep, aNullParent);
}
bool
nsTextNode::IsWebComponentsEnabled(JSContext* aCx, JSObject* aObject)
{
return nsDocument::IsWebComponentsEnabled(aCx, aObject);
}
#ifdef DEBUG
void
nsTextNode::List(FILE* out, int32_t aIndent) const

Просмотреть файл

@ -75,6 +75,10 @@ public:
virtual nsIDOMNode* AsDOMNode() override { return this; }
// Need to have a copy here because including nsDocument.h in this file will
// fail to build on Windows.
static bool IsWebComponentsEnabled(JSContext* aCx, JSObject* aObject);
#ifdef DEBUG
virtual void List(FILE* out, int32_t aIndent) const override;
virtual void DumpContent(FILE* out, int32_t aIndent, bool aDumpAll) const override;

Просмотреть файл

@ -16,7 +16,7 @@ NS_NewHTMLSlotElement(already_AddRefed<mozilla::dom::NodeInfo>&& aNodeInfo,
mozilla::dom::FromParser aFromParser)
{
RefPtr<mozilla::dom::NodeInfo> nodeInfo(aNodeInfo);
if (nsContentUtils::IsWebComponentsEnabled()) {
if (nsDocument::IsWebComponentsEnabled(nodeInfo->GetDocument())) {
already_AddRefed<mozilla::dom::NodeInfo> nodeInfoArg(nodeInfo.forget());
return new mozilla::dom::HTMLSlotElement(nodeInfoArg);
}

Просмотреть файл

@ -254,19 +254,19 @@ dictionary ShadowRootInit {
// https://dom.spec.whatwg.org/#element
partial interface Element {
// Shadow DOM v1
[Throws, Pref="dom.webcomponents.enabled"]
[Throws, Func="nsDocument::IsWebComponentsEnabled"]
ShadowRoot attachShadow(ShadowRootInit shadowRootInitDict);
[BinaryName="shadowRootByMode", Pref="dom.webcomponents.enabled"]
[BinaryName="shadowRootByMode", Func="nsDocument::IsWebComponentsEnabled"]
readonly attribute ShadowRoot? shadowRoot;
[BinaryName="assignedSlotByMode", Pref="dom.webcomponents.enabled"]
[BinaryName="assignedSlotByMode", Func="nsDocument::IsWebComponentsEnabled"]
readonly attribute HTMLSlotElement? assignedSlot;
[CEReactions, Unscopable, SetterThrows, Pref="dom.webcomponents.enabled"]
[CEReactions, Unscopable, SetterThrows, Func="nsDocument::IsWebComponentsEnabled"]
attribute DOMString slot;
// [deprecated] Shadow DOM v0
[Throws, Pref="dom.webcomponents.enabled"]
[Throws, Func="nsDocument::IsWebComponentsEnabled"]
ShadowRoot createShadowRoot();
[Pref="dom.webcomponents.enabled"]
[Func="nsDocument::IsWebComponentsEnabled"]
NodeList getDestinationInsertionPoints();
};

Просмотреть файл

@ -11,7 +11,7 @@
* and create derivative works of this document.
*/
[Pref="dom.webcomponents.enabled", Exposed=Window, HTMLConstructor]
[Func="nsDocument::IsWebComponentsEnabled", Exposed=Window, HTMLConstructor]
interface HTMLSlotElement : HTMLElement {
[CEReactions, SetterThrows] attribute DOMString name;
sequence<Node> assignedNodes(optional AssignedNodesOptions options);

Просмотреть файл

@ -17,7 +17,7 @@ enum ShadowRootMode {
};
// https://dom.spec.whatwg.org/#shadowroot
[Pref="dom.webcomponents.enabled"]
[Func="nsDocument::IsWebComponentsEnabled"]
interface ShadowRoot : DocumentFragment
{
// Shadow DOM v1

Просмотреть файл

@ -19,7 +19,7 @@ interface Text : CharacterData {
};
partial interface Text {
[BinaryName="assignedSlotByMode", Pref="dom.webcomponents.enabled"]
[BinaryName="assignedSlotByMode", Func="nsTextNode::IsWebComponentsEnabled"]
readonly attribute HTMLSlotElement? assignedSlot;
};