blob: 206ce6b197a95d828f2830cd6412a7f6070df1f7 [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"
39#include "bindings/v8/V8HiddenPropertyName.h"
Ben Murdoch02772c62013-07-26 10:21:05 +010040#include "bindings/v8/V8PerContextData.h"
Ben Murdoche69819b2013-07-17 14:56:49 +010041#include "core/dom/ScriptExecutionContext.h"
Ben Murdoch02772c62013-07-26 10:21:05 +010042#include "wtf/PassOwnPtr.h"
Ben Murdoche69819b2013-07-17 14:56:49 +010043
44namespace WebCore {
45
46#define CALLBACK_LIST(V) \
47 V(created, Created) \
Torne (Richard Coles)8abfc582013-09-12 12:10:38 +010048 V(enteredView, EnteredView) \
49 V(leftView, LeftView) \
Ben Murdoche69819b2013-07-17 14:56:49 +010050 V(attributeChanged, AttributeChanged)
51
Torne (Richard Coles)8abfc582013-09-12 12:10:38 +010052PassRefPtr<V8CustomElementLifecycleCallbacks> V8CustomElementLifecycleCallbacks::create(ScriptExecutionContext* scriptExecutionContext, v8::Handle<v8::Object> prototype, v8::Handle<v8::Function> created, v8::Handle<v8::Function> enteredView, v8::Handle<v8::Function> leftView, v8::Handle<v8::Function> attributeChanged)
Ben Murdoche69819b2013-07-17 14:56:49 +010053{
54 // A given object can only be used as a Custom Element prototype
55 // once; see customElementIsInterfacePrototypeObject
56#define SET_HIDDEN_PROPERTY(Value, Name) \
57 ASSERT(prototype->GetHiddenValue(V8HiddenPropertyName::customElement##Name()).IsEmpty()); \
58 if (!Value.IsEmpty()) \
59 prototype->SetHiddenValue(V8HiddenPropertyName::customElement##Name(), Value);
60
61 CALLBACK_LIST(SET_HIDDEN_PROPERTY)
62#undef SET_HIDDEN_PROPERTY
63
Torne (Richard Coles)8abfc582013-09-12 12:10:38 +010064 return adoptRef(new V8CustomElementLifecycleCallbacks(scriptExecutionContext, prototype, created, enteredView, leftView, attributeChanged));
Ben Murdoche69819b2013-07-17 14:56:49 +010065}
66
Torne (Richard Coles)8abfc582013-09-12 12:10:38 +010067static CustomElementLifecycleCallbacks::CallbackType flagSet(v8::Handle<v8::Function> enteredView, v8::Handle<v8::Function> leftView, v8::Handle<v8::Function> attributeChanged)
Ben Murdoche69819b2013-07-17 14:56:49 +010068{
69 // V8 Custom Elements always run created to swizzle prototypes.
70 int flags = CustomElementLifecycleCallbacks::Created;
71
Torne (Richard Coles)8abfc582013-09-12 12:10:38 +010072 if (!enteredView.IsEmpty())
73 flags |= CustomElementLifecycleCallbacks::EnteredView;
Ben Murdoche69819b2013-07-17 14:56:49 +010074
Torne (Richard Coles)8abfc582013-09-12 12:10:38 +010075 if (!leftView.IsEmpty())
76 flags |= CustomElementLifecycleCallbacks::LeftView;
Ben Murdoche69819b2013-07-17 14:56:49 +010077
78 if (!attributeChanged.IsEmpty())
79 flags |= CustomElementLifecycleCallbacks::AttributeChanged;
80
81 return CustomElementLifecycleCallbacks::CallbackType(flags);
82}
83
84template <typename T>
85static void weakCallback(v8::Isolate*, v8::Persistent<T>*, ScopedPersistent<T>* handle)
86{
87 handle->clear();
88}
89
Torne (Richard Coles)8abfc582013-09-12 12:10:38 +010090V8CustomElementLifecycleCallbacks::V8CustomElementLifecycleCallbacks(ScriptExecutionContext* scriptExecutionContext, v8::Handle<v8::Object> prototype, v8::Handle<v8::Function> created, v8::Handle<v8::Function> enteredView, v8::Handle<v8::Function> leftView, v8::Handle<v8::Function> attributeChanged)
91 : CustomElementLifecycleCallbacks(flagSet(enteredView, leftView, attributeChanged))
Ben Murdoche69819b2013-07-17 14:56:49 +010092 , ActiveDOMCallback(scriptExecutionContext)
93 , m_world(DOMWrapperWorld::current())
94 , m_prototype(prototype)
95 , m_created(created)
Torne (Richard Coles)8abfc582013-09-12 12:10:38 +010096 , m_enteredView(enteredView)
97 , m_leftView(leftView)
Ben Murdoche69819b2013-07-17 14:56:49 +010098 , m_attributeChanged(attributeChanged)
Ben Murdoch02772c62013-07-26 10:21:05 +010099 , m_owner(0)
Ben Murdoche69819b2013-07-17 14:56:49 +0100100{
101 m_prototype.makeWeak(&m_prototype, weakCallback<v8::Object>);
102
103#define MAKE_WEAK(Var, _) \
104 if (!m_##Var.isEmpty()) \
105 m_##Var.makeWeak(&m_##Var, weakCallback<v8::Function>);
106
107 CALLBACK_LIST(MAKE_WEAK)
108#undef MAKE_WEAK
109}
110
Ben Murdoch02772c62013-07-26 10:21:05 +0100111V8PerContextData* V8CustomElementLifecycleCallbacks::creationContextData()
112{
113 if (!scriptExecutionContext())
114 return 0;
115
116 v8::Handle<v8::Context> context = toV8Context(scriptExecutionContext(), m_world.get());
117 if (context.IsEmpty())
118 return 0;
119
120 return V8PerContextData::from(context);
121}
122
123V8CustomElementLifecycleCallbacks::~V8CustomElementLifecycleCallbacks()
124{
125 if (!m_owner)
126 return;
127
Torne (Richard Coles)8abfc582013-09-12 12:10:38 +0100128 v8::HandleScope handleScope(getIsolateFromScriptExecutionContext(scriptExecutionContext()));
Ben Murdoch02772c62013-07-26 10:21:05 +0100129 if (V8PerContextData* perContextData = creationContextData())
130 perContextData->clearCustomElementBinding(m_owner);
131}
132
133bool V8CustomElementLifecycleCallbacks::setBinding(CustomElementDefinition* owner, PassOwnPtr<CustomElementBinding> binding)
134{
135 ASSERT(!m_owner);
136
137 V8PerContextData* perContextData = creationContextData();
138 if (!perContextData)
139 return false;
140
141 m_owner = owner;
142
143 // Bindings retrieve the prototype when needed from per-context data.
144 perContextData->addCustomElementBinding(owner, binding);
145
146 return true;
147}
148
Ben Murdoche69819b2013-07-17 14:56:49 +0100149void V8CustomElementLifecycleCallbacks::created(Element* element)
150{
151 if (!canInvokeCallback())
152 return;
153
Ben Murdoch7757ec22013-07-23 11:17:36 +0100154 element->setCustomElementState(Element::Upgraded);
Ben Murdoche69819b2013-07-17 14:56:49 +0100155
Torne (Richard Coles)8abfc582013-09-12 12:10:38 +0100156 v8::Isolate* isolate = getIsolateFromScriptExecutionContext(scriptExecutionContext());
157 v8::HandleScope handleScope(isolate);
Ben Murdoche69819b2013-07-17 14:56:49 +0100158 v8::Handle<v8::Context> context = toV8Context(scriptExecutionContext(), m_world.get());
159 if (context.IsEmpty())
160 return;
161
162 v8::Context::Scope scope(context);
Ben Murdoche69819b2013-07-17 14:56:49 +0100163
Torne (Richard Coles)e1f1df52013-08-23 16:39:30 +0100164 v8::Handle<v8::Object> receiver = DOMDataStore::current(isolate)->get<V8Element>(element, isolate);
Ben Murdoche69819b2013-07-17 14:56:49 +0100165 if (!receiver.IsEmpty()) {
166 // Swizzle the prototype of the existing wrapper. We don't need to
167 // worry about non-existent wrappers; they will get the right
168 // prototype when wrapped.
169 v8::Handle<v8::Object> prototype = m_prototype.newLocal(isolate);
170 if (prototype.IsEmpty())
171 return;
172 receiver->SetPrototype(prototype);
173 }
174
175 v8::Handle<v8::Function> callback = m_created.newLocal(isolate);
176 if (callback.IsEmpty())
177 return;
178
179 if (receiver.IsEmpty())
180 receiver = toV8(element, context->Global(), isolate).As<v8::Object>();
181
182 ASSERT(!receiver.IsEmpty());
183
184 v8::TryCatch exceptionCatcher;
185 exceptionCatcher.SetVerbose(true);
Torne (Richard Coles)8abfc582013-09-12 12:10:38 +0100186 ScriptController::callFunctionWithInstrumentation(scriptExecutionContext(), callback, receiver, 0, 0, isolate);
Ben Murdoche69819b2013-07-17 14:56:49 +0100187}
188
Torne (Richard Coles)8abfc582013-09-12 12:10:38 +0100189void V8CustomElementLifecycleCallbacks::enteredView(Element* element)
Ben Murdoche69819b2013-07-17 14:56:49 +0100190{
Torne (Richard Coles)8abfc582013-09-12 12:10:38 +0100191 call(m_enteredView, element);
Ben Murdoche69819b2013-07-17 14:56:49 +0100192}
193
Torne (Richard Coles)8abfc582013-09-12 12:10:38 +0100194void V8CustomElementLifecycleCallbacks::leftView(Element* element)
Ben Murdoche69819b2013-07-17 14:56:49 +0100195{
Torne (Richard Coles)8abfc582013-09-12 12:10:38 +0100196 call(m_leftView, element);
Ben Murdoche69819b2013-07-17 14:56:49 +0100197}
198
199void V8CustomElementLifecycleCallbacks::attributeChanged(Element* element, const AtomicString& name, const AtomicString& oldValue, const AtomicString& newValue)
200{
201 if (!canInvokeCallback())
202 return;
203
Torne (Richard Coles)8abfc582013-09-12 12:10:38 +0100204 v8::Isolate* isolate = getIsolateFromScriptExecutionContext(scriptExecutionContext());
205 v8::HandleScope handleScope(isolate);
Ben Murdoche69819b2013-07-17 14:56:49 +0100206 v8::Handle<v8::Context> context = toV8Context(scriptExecutionContext(), m_world.get());
207 if (context.IsEmpty())
208 return;
209
210 v8::Context::Scope scope(context);
Ben Murdoche69819b2013-07-17 14:56:49 +0100211
212 v8::Handle<v8::Object> receiver = toV8(element, context->Global(), isolate).As<v8::Object>();
213 ASSERT(!receiver.IsEmpty());
214
215 v8::Handle<v8::Function> callback = m_attributeChanged.newLocal(isolate);
216 if (callback.IsEmpty())
217 return;
218
219 v8::Handle<v8::Value> argv[] = {
220 v8String(name, isolate),
221 oldValue.isNull() ? v8::Handle<v8::Value>(v8::Null()) : v8::Handle<v8::Value>(v8String(oldValue, isolate)),
222 newValue.isNull() ? v8::Handle<v8::Value>(v8::Null()) : v8::Handle<v8::Value>(v8String(newValue, isolate))
223 };
224
225 v8::TryCatch exceptionCatcher;
226 exceptionCatcher.SetVerbose(true);
Torne (Richard Coles)8abfc582013-09-12 12:10:38 +0100227 ScriptController::callFunctionWithInstrumentation(scriptExecutionContext(), callback, receiver, WTF_ARRAY_LENGTH(argv), argv, isolate);
Ben Murdoche69819b2013-07-17 14:56:49 +0100228}
229
230void V8CustomElementLifecycleCallbacks::call(const ScopedPersistent<v8::Function>& weakCallback, Element* element)
231{
232 if (!canInvokeCallback())
233 return;
234
Torne (Richard Coles)8abfc582013-09-12 12:10:38 +0100235 v8::HandleScope handleScope(getIsolateFromScriptExecutionContext(scriptExecutionContext()));
Ben Murdoche69819b2013-07-17 14:56:49 +0100236 v8::Handle<v8::Context> context = toV8Context(scriptExecutionContext(), m_world.get());
237 if (context.IsEmpty())
238 return;
239
240 v8::Context::Scope scope(context);
241 v8::Isolate* isolate = context->GetIsolate();
242
243 v8::Handle<v8::Function> callback = weakCallback.newLocal(isolate);
244 if (callback.IsEmpty())
245 return;
246
247 v8::Handle<v8::Object> receiver = toV8(element, context->Global(), isolate).As<v8::Object>();
248 ASSERT(!receiver.IsEmpty());
249
250 v8::TryCatch exceptionCatcher;
251 exceptionCatcher.SetVerbose(true);
Torne (Richard Coles)8abfc582013-09-12 12:10:38 +0100252 ScriptController::callFunctionWithInstrumentation(scriptExecutionContext(), callback, receiver, 0, 0, isolate);
Ben Murdoche69819b2013-07-17 14:56:49 +0100253}
254
255} // namespace WebCore