2 * Copyright (C) 2013 Google Inc. All rights reserved.
3 * Copyright (C) 2015 Apple Inc. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
29 #include "JSCustomElementInterface.h"
31 #if ENABLE(CUSTOM_ELEMENTS)
33 #include "DOMWrapperWorld.h"
34 #include "JSDOMGlobalObject.h"
35 #include "JSElement.h"
36 #include "JSHTMLElement.h"
37 #include "JSMainThreadExecState.h"
38 #include "JSMainThreadExecStateInstrumentation.h"
39 #include "ScriptExecutionContext.h"
40 #include <heap/WeakInlines.h>
41 #include <runtime/JSLock.h>
47 JSCustomElementInterface::JSCustomElementInterface(const QualifiedName& name, JSObject* constructor, JSDOMGlobalObject* globalObject)
48 : ActiveDOMCallback(globalObject->scriptExecutionContext())
50 , m_constructor(constructor)
51 , m_isolatedWorld(&globalObject->world())
55 JSCustomElementInterface::~JSCustomElementInterface()
59 RefPtr<Element> JSCustomElementInterface::constructElement(const AtomicString& tagName, ShouldClearException shouldClearException)
61 if (!canInvokeCallback())
64 Ref<JSCustomElementInterface> protect(*this);
66 JSLockHolder lock(m_isolatedWorld->vm());
71 ScriptExecutionContext* context = scriptExecutionContext();
74 ASSERT(context->isDocument());
75 JSDOMGlobalObject* globalObject = toJSDOMGlobalObject(context, *m_isolatedWorld);
76 ExecState* state = globalObject->globalExec();
78 ConstructData constructData;
79 ConstructType constructType = m_constructor->methodTable()->getConstructData(m_constructor.get(), constructData);
80 if (constructType == ConstructTypeNone) {
85 MarkedArgumentBuffer args;
86 args.append(jsStringWithCache(state, tagName));
88 InspectorInstrumentationCookie cookie = JSMainThreadExecState::instrumentFunctionConstruct(context, constructType, constructData);
89 JSValue newElement = construct(state, m_constructor.get(), constructType, constructData, args);
90 InspectorInstrumentation::didCallFunction(cookie, context);
92 if (shouldClearException == ShouldClearException::Clear && state->hadException())
93 state->clearException();
95 if (newElement.isEmpty())
98 Element* wrappedElement = JSElement::toWrapped(newElement);
101 wrappedElement->setIsCustomElement();
102 return wrappedElement;
105 void JSCustomElementInterface::attributeChanged(Element& element, const QualifiedName& attributeName, const AtomicString& oldValue, const AtomicString& newValue)
107 if (!canInvokeCallback())
110 Ref<JSCustomElementInterface> protect(*this);
112 JSLockHolder lock(m_isolatedWorld->vm());
114 ScriptExecutionContext* context = scriptExecutionContext();
118 ASSERT(context->isDocument());
119 JSDOMGlobalObject* globalObject = toJSDOMGlobalObject(context, *m_isolatedWorld);
120 ExecState* state = globalObject->globalExec();
122 JSObject* jsElement = asObject(toJS(state, globalObject, &element));
124 PropertyName attributeChanged(Identifier::fromString(state, "attributeChangedCallback"));
125 JSValue callback = jsElement->get(state, attributeChanged);
127 CallType callType = getCallData(callback, callData);
128 if (callType == CallTypeNone)
131 const AtomicString& namespaceURI = attributeName.namespaceURI();
132 MarkedArgumentBuffer args;
133 args.append(jsStringWithCache(state, attributeName.localName()));
134 args.append(oldValue == nullAtom ? jsNull() : jsStringWithCache(state, oldValue));
135 args.append(newValue == nullAtom ? jsNull() : jsStringWithCache(state, newValue));
136 args.append(namespaceURI == nullAtom ? jsNull() : jsStringWithCache(state, attributeName.namespaceURI()));
138 InspectorInstrumentationCookie cookie = JSMainThreadExecState::instrumentFunctionCall(context, callType, callData);
140 NakedPtr<Exception> exception;
141 JSMainThreadExecState::call(state, callback, callType, callData, jsElement, args, exception);
143 InspectorInstrumentation::didCallFunction(cookie, context);
146 reportException(state, exception);
149 } // namespace WebCore