1/*
2 * Copyright (C) 2013 Google Inc. All rights reserved.
3 * Copyright (C) 2015, 2016 Apple Inc. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
15 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
16 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
18 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
24 * THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#pragma once
28
29#include "ActiveDOMCallback.h"
30#include "QualifiedName.h"
31#include <JavaScriptCore/JSObject.h>
32#include <JavaScriptCore/Weak.h>
33#include <JavaScriptCore/WeakInlines.h>
34#include <wtf/Forward.h>
35#include <wtf/Function.h>
36#include <wtf/RefCounted.h>
37#include <wtf/text/AtomicStringHash.h>
38
39namespace JSC {
40class JSObject;
41class PrivateName;
42}
43
44namespace WebCore {
45
46class DOMWrapperWorld;
47class Document;
48class Element;
49class JSDOMGlobalObject;
50class MathMLElement;
51class SVGElement;
52
53class JSCustomElementInterface : public RefCounted<JSCustomElementInterface>, public ActiveDOMCallback {
54public:
55 static Ref<JSCustomElementInterface> create(const QualifiedName& name, JSC::JSObject* callback, JSDOMGlobalObject* globalObject)
56 {
57 return adoptRef(*new JSCustomElementInterface(name, callback, globalObject));
58 }
59
60 Ref<Element> constructElementWithFallback(Document&, const AtomicString&);
61 Ref<Element> constructElementWithFallback(Document&, const QualifiedName&);
62
63 void upgradeElement(Element&);
64
65 void setConnectedCallback(JSC::JSObject*);
66 bool hasConnectedCallback() const { return !!m_connectedCallback; }
67 void invokeConnectedCallback(Element&);
68
69 void setDisconnectedCallback(JSC::JSObject*);
70 bool hasDisconnectedCallback() const { return !!m_disconnectedCallback; }
71 void invokeDisconnectedCallback(Element&);
72
73 void setAdoptedCallback(JSC::JSObject*);
74 bool hasAdoptedCallback() const { return !!m_adoptedCallback; }
75 void invokeAdoptedCallback(Element&, Document& oldDocument, Document& newDocument);
76
77 void setAttributeChangedCallback(JSC::JSObject* callback, const Vector<String>& observedAttributes);
78 bool observesAttribute(const AtomicString& name) const { return m_observedAttributes.contains(name); }
79 void invokeAttributeChangedCallback(Element&, const QualifiedName&, const AtomicString& oldValue, const AtomicString& newValue);
80
81 ScriptExecutionContext* scriptExecutionContext() const { return ContextDestructionObserver::scriptExecutionContext(); }
82 JSC::JSObject* constructor() { return m_constructor.get(); }
83
84 const QualifiedName& name() const { return m_name; }
85
86 bool isUpgradingElement() const { return !m_constructionStack.isEmpty(); }
87 Element* lastElementInConstructionStack() const { return m_constructionStack.last().get(); }
88 void didUpgradeLastElementInConstructionStack();
89
90 virtual ~JSCustomElementInterface();
91
92private:
93 JSCustomElementInterface(const QualifiedName&, JSC::JSObject* callback, JSDOMGlobalObject*);
94
95 RefPtr<Element> tryToConstructCustomElement(Document&, const AtomicString&);
96
97 void invokeCallback(Element&, JSC::JSObject* callback, const WTF::Function<void(JSC::ExecState*, JSDOMGlobalObject*, JSC::MarkedArgumentBuffer&)>& addArguments = [](JSC::ExecState*, JSDOMGlobalObject*, JSC::MarkedArgumentBuffer&) { });
98
99 QualifiedName m_name;
100 JSC::Weak<JSC::JSObject> m_constructor;
101 JSC::Weak<JSC::JSObject> m_connectedCallback;
102 JSC::Weak<JSC::JSObject> m_disconnectedCallback;
103 JSC::Weak<JSC::JSObject> m_adoptedCallback;
104 JSC::Weak<JSC::JSObject> m_attributeChangedCallback;
105 Ref<DOMWrapperWorld> m_isolatedWorld;
106 Vector<RefPtr<Element>, 1> m_constructionStack;
107 HashSet<AtomicString> m_observedAttributes;
108};
109
110} // namespace WebCore
111