blob: f31a3bdb130e69a0f420329a4c38d9d2fb146a30 [file] [log] [blame]
Ben Murdoche69819b2013-07-17 14:56:49 +01001/*
2 * Copyright (C) 2013 Google Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 *
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31#include "config.h"
32#include "bindings/v8/V8CustomElementLifecycleCallbacks.h"
33
34#include "V8Element.h"
Ben Murdoch02772c62013-07-26 10:21:05 +010035#include "bindings/v8/CustomElementBinding.h"
Ben Murdoche69819b2013-07-17 14:56:49 +010036#include "bindings/v8/DOMDataStore.h"
37#include "bindings/v8/ScriptController.h"
38#include "bindings/v8/V8Binding.h"
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +000039#include "bindings/v8/V8HiddenValue.h"
Ben Murdoch02772c62013-07-26 10:21:05 +010040#include "bindings/v8/V8PerContextData.h"
Torne (Richard Coles)1e202182013-10-18 15:46:42 +010041#include "core/dom/ExecutionContext.h"
Torne (Richard Coles)09380292014-02-21 12:17:33 +000042#include "core/inspector/InspectorInstrumentation.h"
Ben Murdoch02772c62013-07-26 10:21:05 +010043#include "wtf/PassOwnPtr.h"
Ben Murdoche69819b2013-07-17 14:56:49 +010044
45namespace WebCore {
46
47#define CALLBACK_LIST(V) \
48 V(created, Created) \
Torne (Richard Coles)09380292014-02-21 12:17:33 +000049 V(attached, Attached) \
50 V(detached, Detached) \
Ben Murdoche69819b2013-07-17 14:56:49 +010051 V(attributeChanged, AttributeChanged)
52
Torne (Richard Coles)09380292014-02-21 12:17:33 +000053PassRefPtr<V8CustomElementLifecycleCallbacks> V8CustomElementLifecycleCallbacks::create(ExecutionContext* executionContext, v8::Handle<v8::Object> prototype, v8::Handle<v8::Function> created, v8::Handle<v8::Function> attached, v8::Handle<v8::Function> detached, v8::Handle<v8::Function> attributeChanged)
Ben Murdoche69819b2013-07-17 14:56:49 +010054{
Torne (Richard Coles)1e202182013-10-18 15:46:42 +010055 v8::Isolate* isolate = toIsolate(executionContext);
Ben Murdoche69819b2013-07-17 14:56:49 +010056 // A given object can only be used as a Custom Element prototype
57 // once; see customElementIsInterfacePrototypeObject
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +000058#define SET_HIDDEN_VALUE(Value, Name) \
59 ASSERT(V8HiddenValue::getHiddenValue(isolate, prototype, V8HiddenValue::customElement##Name(isolate)).IsEmpty()); \
Ben Murdoche69819b2013-07-17 14:56:49 +010060 if (!Value.IsEmpty()) \
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +000061 V8HiddenValue::setHiddenValue(isolate, prototype, V8HiddenValue::customElement##Name(isolate), Value);
Ben Murdoche69819b2013-07-17 14:56:49 +010062
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +000063 CALLBACK_LIST(SET_HIDDEN_VALUE)
64#undef SET_HIDDEN_VALUE
Ben Murdoche69819b2013-07-17 14:56:49 +010065
Torne (Richard Coles)09380292014-02-21 12:17:33 +000066 return adoptRef(new V8CustomElementLifecycleCallbacks(executionContext, prototype, created, attached, detached, attributeChanged));
Ben Murdoche69819b2013-07-17 14:56:49 +010067}
68
Torne (Richard Coles)09380292014-02-21 12:17:33 +000069static CustomElementLifecycleCallbacks::CallbackType flagSet(v8::Handle<v8::Function> attached, v8::Handle<v8::Function> detached, v8::Handle<v8::Function> attributeChanged)
Ben Murdoche69819b2013-07-17 14:56:49 +010070{
71 // V8 Custom Elements always run created to swizzle prototypes.
72 int flags = CustomElementLifecycleCallbacks::Created;
73
Torne (Richard Coles)09380292014-02-21 12:17:33 +000074 if (!attached.IsEmpty())
75 flags |= CustomElementLifecycleCallbacks::Attached;
Ben Murdoche69819b2013-07-17 14:56:49 +010076
Torne (Richard Coles)09380292014-02-21 12:17:33 +000077 if (!detached.IsEmpty())
78 flags |= CustomElementLifecycleCallbacks::Detached;
Ben Murdoche69819b2013-07-17 14:56:49 +010079
80 if (!attributeChanged.IsEmpty())
81 flags |= CustomElementLifecycleCallbacks::AttributeChanged;
82
83 return CustomElementLifecycleCallbacks::CallbackType(flags);
84}
85
86template <typename T>
Torne (Richard Coles)a854de02013-12-18 16:25:25 +000087static void weakCallback(const v8::WeakCallbackData<T, ScopedPersistent<T> >& data)
Ben Murdoche69819b2013-07-17 14:56:49 +010088{
Torne (Richard Coles)a854de02013-12-18 16:25:25 +000089 data.GetParameter()->clear();
Ben Murdoche69819b2013-07-17 14:56:49 +010090}
91
Torne (Richard Coles)09380292014-02-21 12:17:33 +000092V8CustomElementLifecycleCallbacks::V8CustomElementLifecycleCallbacks(ExecutionContext* executionContext, v8::Handle<v8::Object> prototype, v8::Handle<v8::Function> created, v8::Handle<v8::Function> attached, v8::Handle<v8::Function> detached, v8::Handle<v8::Function> attributeChanged)
93 : CustomElementLifecycleCallbacks(flagSet(attached, detached, attributeChanged))
94 , ContextLifecycleObserver(executionContext)
Torne (Richard Coles)f79f16f2013-10-31 11:16:44 +000095 , m_owner(0)
Ben Murdochaafa69c2014-04-03 12:30:15 +010096 , m_scriptState(NewScriptState::current(toIsolate(executionContext)))
97 , m_prototype(m_scriptState->isolate(), prototype)
98 , m_created(m_scriptState->isolate(), created)
99 , m_attached(m_scriptState->isolate(), attached)
100 , m_detached(m_scriptState->isolate(), detached)
101 , m_attributeChanged(m_scriptState->isolate(), attributeChanged)
Ben Murdoche69819b2013-07-17 14:56:49 +0100102{
Torne (Richard Coles)a854de02013-12-18 16:25:25 +0000103 m_prototype.setWeak(&m_prototype, weakCallback<v8::Object>);
Ben Murdoche69819b2013-07-17 14:56:49 +0100104
105#define MAKE_WEAK(Var, _) \
106 if (!m_##Var.isEmpty()) \
Torne (Richard Coles)a854de02013-12-18 16:25:25 +0000107 m_##Var.setWeak(&m_##Var, weakCallback<v8::Function>);
Ben Murdoche69819b2013-07-17 14:56:49 +0100108
109 CALLBACK_LIST(MAKE_WEAK)
110#undef MAKE_WEAK
111}
112
Ben Murdoch02772c62013-07-26 10:21:05 +0100113V8PerContextData* V8CustomElementLifecycleCallbacks::creationContextData()
114{
Torne (Richard Coles)1e202182013-10-18 15:46:42 +0100115 if (!executionContext())
Ben Murdoch02772c62013-07-26 10:21:05 +0100116 return 0;
117
Ben Murdochaafa69c2014-04-03 12:30:15 +0100118 v8::Handle<v8::Context> context = m_scriptState->context();
Ben Murdoch02772c62013-07-26 10:21:05 +0100119 if (context.IsEmpty())
120 return 0;
121
122 return V8PerContextData::from(context);
123}
124
125V8CustomElementLifecycleCallbacks::~V8CustomElementLifecycleCallbacks()
126{
127 if (!m_owner)
128 return;
129
Ben Murdochaafa69c2014-04-03 12:30:15 +0100130 v8::HandleScope handleScope(m_scriptState->isolate());
Ben Murdoch02772c62013-07-26 10:21:05 +0100131 if (V8PerContextData* perContextData = creationContextData())
132 perContextData->clearCustomElementBinding(m_owner);
133}
134
135bool V8CustomElementLifecycleCallbacks::setBinding(CustomElementDefinition* owner, PassOwnPtr<CustomElementBinding> binding)
136{
137 ASSERT(!m_owner);
138
139 V8PerContextData* perContextData = creationContextData();
140 if (!perContextData)
141 return false;
142
143 m_owner = owner;
144
145 // Bindings retrieve the prototype when needed from per-context data.
146 perContextData->addCustomElementBinding(owner, binding);
147
148 return true;
149}
150
Ben Murdoche69819b2013-07-17 14:56:49 +0100151void V8CustomElementLifecycleCallbacks::created(Element* element)
152{
Torne (Richard Coles)09380292014-02-21 12:17:33 +0000153 // FIXME: callbacks while paused should be queued up for execution to
154 // continue then be delivered in order rather than delivered immediately.
155 // Bug 329665 tracks similar behavior for other synchronous events.
156 if (!executionContext() || executionContext()->activeDOMObjectsAreStopped())
Ben Murdoche69819b2013-07-17 14:56:49 +0100157 return;
158
Ben Murdoch7757ec22013-07-23 11:17:36 +0100159 element->setCustomElementState(Element::Upgraded);
Ben Murdoche69819b2013-07-17 14:56:49 +0100160
Ben Murdochaafa69c2014-04-03 12:30:15 +0100161 v8::Isolate* isolate = m_scriptState->isolate();
162 v8::HandleScope handleScope(isolate);
163 v8::Handle<v8::Context> context = m_scriptState->context();
Ben Murdoche69819b2013-07-17 14:56:49 +0100164 if (context.IsEmpty())
165 return;
166
167 v8::Context::Scope scope(context);
Ben Murdoche69819b2013-07-17 14:56:49 +0100168
Ben Murdochaafa69c2014-04-03 12:30:15 +0100169 v8::Handle<v8::Object> receiver = DOMDataStore::current(isolate).get<V8Element>(element, isolate);
Ben Murdoche69819b2013-07-17 14:56:49 +0100170 if (!receiver.IsEmpty()) {
171 // Swizzle the prototype of the existing wrapper. We don't need to
172 // worry about non-existent wrappers; they will get the right
173 // prototype when wrapped.
Ben Murdochaafa69c2014-04-03 12:30:15 +0100174 v8::Handle<v8::Object> prototype = m_prototype.newLocal(isolate);
Ben Murdoche69819b2013-07-17 14:56:49 +0100175 if (prototype.IsEmpty())
176 return;
177 receiver->SetPrototype(prototype);
178 }
179
Ben Murdochaafa69c2014-04-03 12:30:15 +0100180 v8::Handle<v8::Function> callback = m_created.newLocal(isolate);
Ben Murdoche69819b2013-07-17 14:56:49 +0100181 if (callback.IsEmpty())
182 return;
183
184 if (receiver.IsEmpty())
Ben Murdochaafa69c2014-04-03 12:30:15 +0100185 receiver = toV8(element, context->Global(), isolate).As<v8::Object>();
Ben Murdoche69819b2013-07-17 14:56:49 +0100186
187 ASSERT(!receiver.IsEmpty());
188
Torne (Richard Coles)09380292014-02-21 12:17:33 +0000189 InspectorInstrumentation::willExecuteCustomElementCallback(element);
190
Ben Murdoche69819b2013-07-17 14:56:49 +0100191 v8::TryCatch exceptionCatcher;
192 exceptionCatcher.SetVerbose(true);
Ben Murdochaafa69c2014-04-03 12:30:15 +0100193 ScriptController::callFunction(executionContext(), callback, receiver, 0, 0, isolate);
Ben Murdoche69819b2013-07-17 14:56:49 +0100194}
195
Torne (Richard Coles)09380292014-02-21 12:17:33 +0000196void V8CustomElementLifecycleCallbacks::attached(Element* element)
Ben Murdoche69819b2013-07-17 14:56:49 +0100197{
Torne (Richard Coles)09380292014-02-21 12:17:33 +0000198 call(m_attached, element);
Ben Murdoche69819b2013-07-17 14:56:49 +0100199}
200
Torne (Richard Coles)09380292014-02-21 12:17:33 +0000201void V8CustomElementLifecycleCallbacks::detached(Element* element)
Ben Murdoche69819b2013-07-17 14:56:49 +0100202{
Torne (Richard Coles)09380292014-02-21 12:17:33 +0000203 call(m_detached, element);
Ben Murdoche69819b2013-07-17 14:56:49 +0100204}
205
206void V8CustomElementLifecycleCallbacks::attributeChanged(Element* element, const AtomicString& name, const AtomicString& oldValue, const AtomicString& newValue)
207{
Torne (Richard Coles)09380292014-02-21 12:17:33 +0000208 // FIXME: callbacks while paused should be queued up for execution to
209 // continue then be delivered in order rather than delivered immediately.
210 // Bug 329665 tracks similar behavior for other synchronous events.
211 if (!executionContext() || executionContext()->activeDOMObjectsAreStopped())
Ben Murdoche69819b2013-07-17 14:56:49 +0100212 return;
213
Ben Murdochaafa69c2014-04-03 12:30:15 +0100214 v8::Isolate* isolate = m_scriptState->isolate();
215 v8::HandleScope handleScope(isolate);
216 v8::Handle<v8::Context> context = m_scriptState->context();
Ben Murdoche69819b2013-07-17 14:56:49 +0100217 if (context.IsEmpty())
218 return;
219
220 v8::Context::Scope scope(context);
Ben Murdoche69819b2013-07-17 14:56:49 +0100221
Ben Murdochaafa69c2014-04-03 12:30:15 +0100222 v8::Handle<v8::Object> receiver = toV8(element, context->Global(), isolate).As<v8::Object>();
Ben Murdoche69819b2013-07-17 14:56:49 +0100223 ASSERT(!receiver.IsEmpty());
224
Ben Murdochaafa69c2014-04-03 12:30:15 +0100225 v8::Handle<v8::Function> callback = m_attributeChanged.newLocal(isolate);
Ben Murdoche69819b2013-07-17 14:56:49 +0100226 if (callback.IsEmpty())
227 return;
228
229 v8::Handle<v8::Value> argv[] = {
Ben Murdochaafa69c2014-04-03 12:30:15 +0100230 v8String(isolate, name),
231 oldValue.isNull() ? v8::Handle<v8::Value>(v8::Null(isolate)) : v8::Handle<v8::Value>(v8String(isolate, oldValue)),
232 newValue.isNull() ? v8::Handle<v8::Value>(v8::Null(isolate)) : v8::Handle<v8::Value>(v8String(isolate, newValue))
Ben Murdoche69819b2013-07-17 14:56:49 +0100233 };
234
Torne (Richard Coles)09380292014-02-21 12:17:33 +0000235 InspectorInstrumentation::willExecuteCustomElementCallback(element);
236
Ben Murdoche69819b2013-07-17 14:56:49 +0100237 v8::TryCatch exceptionCatcher;
238 exceptionCatcher.SetVerbose(true);
Ben Murdochaafa69c2014-04-03 12:30:15 +0100239 ScriptController::callFunction(executionContext(), callback, receiver, WTF_ARRAY_LENGTH(argv), argv, isolate);
Ben Murdoche69819b2013-07-17 14:56:49 +0100240}
241
242void V8CustomElementLifecycleCallbacks::call(const ScopedPersistent<v8::Function>& weakCallback, Element* element)
243{
Torne (Richard Coles)09380292014-02-21 12:17:33 +0000244 // FIXME: callbacks while paused should be queued up for execution to
245 // continue then be delivered in order rather than delivered immediately.
246 // Bug 329665 tracks similar behavior for other synchronous events.
247 if (!executionContext() || executionContext()->activeDOMObjectsAreStopped())
Ben Murdoche69819b2013-07-17 14:56:49 +0100248 return;
249
Ben Murdochaafa69c2014-04-03 12:30:15 +0100250 v8::Isolate* isolate = m_scriptState->isolate();
251 v8::HandleScope handleScope(isolate);
252 v8::Handle<v8::Context> context = m_scriptState->context();
Ben Murdoche69819b2013-07-17 14:56:49 +0100253 if (context.IsEmpty())
254 return;
255
256 v8::Context::Scope scope(context);
Ben Murdoche69819b2013-07-17 14:56:49 +0100257
Ben Murdochaafa69c2014-04-03 12:30:15 +0100258 v8::Handle<v8::Function> callback = weakCallback.newLocal(isolate);
Ben Murdoche69819b2013-07-17 14:56:49 +0100259 if (callback.IsEmpty())
260 return;
261
Ben Murdochaafa69c2014-04-03 12:30:15 +0100262 v8::Handle<v8::Object> receiver = toV8(element, context->Global(), isolate).As<v8::Object>();
Ben Murdoche69819b2013-07-17 14:56:49 +0100263 ASSERT(!receiver.IsEmpty());
264
Torne (Richard Coles)09380292014-02-21 12:17:33 +0000265 InspectorInstrumentation::willExecuteCustomElementCallback(element);
266
Ben Murdoche69819b2013-07-17 14:56:49 +0100267 v8::TryCatch exceptionCatcher;
268 exceptionCatcher.SetVerbose(true);
Ben Murdochaafa69c2014-04-03 12:30:15 +0100269 ScriptController::callFunction(executionContext(), callback, receiver, 0, 0, isolate);
Ben Murdoche69819b2013-07-17 14:56:49 +0100270}
271
272} // namespace WebCore