Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / bindings / v8 / V8CustomElementLifecycleCallbacks.cpp
1 /*
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"
35 #include "bindings/v8/CustomElementBinding.h"
36 #include "bindings/v8/DOMDataStore.h"
37 #include "bindings/v8/ScriptController.h"
38 #include "bindings/v8/V8Binding.h"
39 #include "bindings/v8/V8PerContextData.h"
40 #include "core/dom/ExecutionContext.h"
41 #include "core/inspector/InspectorInstrumentation.h"
42 #include "wtf/PassOwnPtr.h"
43
44 namespace WebCore {
45
46 #define CALLBACK_LIST(V)                  \
47     V(created, Created)                   \
48     V(attached, Attached)           \
49     V(detached, Detached)                 \
50     V(attributeChanged, AttributeChanged)
51
52 PassRefPtr<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)
53 {
54     v8::Isolate* isolate = toIsolate(executionContext);
55     // A given object can only be used as a Custom Element prototype
56     // once; see customElementIsInterfacePrototypeObject
57 #define SET_HIDDEN_PROPERTY(Value, Name) \
58     ASSERT(getHiddenValue(isolate, prototype, "customElement" #Name).IsEmpty()); \
59     if (!Value.IsEmpty()) \
60         setHiddenValue(isolate, prototype, "customElement" #Name, Value);
61
62     CALLBACK_LIST(SET_HIDDEN_PROPERTY)
63 #undef SET_HIDDEN_PROPERTY
64
65     return adoptRef(new V8CustomElementLifecycleCallbacks(executionContext, prototype, created, attached, detached, attributeChanged));
66 }
67
68 static CustomElementLifecycleCallbacks::CallbackType flagSet(v8::Handle<v8::Function> attached, v8::Handle<v8::Function> detached, v8::Handle<v8::Function> attributeChanged)
69 {
70     // V8 Custom Elements always run created to swizzle prototypes.
71     int flags = CustomElementLifecycleCallbacks::Created;
72
73     if (!attached.IsEmpty())
74         flags |= CustomElementLifecycleCallbacks::Attached;
75
76     if (!detached.IsEmpty())
77         flags |= CustomElementLifecycleCallbacks::Detached;
78
79     if (!attributeChanged.IsEmpty())
80         flags |= CustomElementLifecycleCallbacks::AttributeChanged;
81
82     return CustomElementLifecycleCallbacks::CallbackType(flags);
83 }
84
85 template <typename T>
86 static void weakCallback(const v8::WeakCallbackData<T, ScopedPersistent<T> >& data)
87 {
88     data.GetParameter()->clear();
89 }
90
91 V8CustomElementLifecycleCallbacks::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)
92     : CustomElementLifecycleCallbacks(flagSet(attached, detached, attributeChanged))
93     , ContextLifecycleObserver(executionContext)
94     , m_isolate(toIsolate(executionContext))
95     , m_owner(0)
96     , m_world(DOMWrapperWorld::current(m_isolate))
97     , m_prototype(m_isolate, prototype)
98     , m_created(m_isolate, created)
99     , m_attached(m_isolate, attached)
100     , m_detached(m_isolate, detached)
101     , m_attributeChanged(m_isolate, attributeChanged)
102 {
103     m_prototype.setWeak(&m_prototype, weakCallback<v8::Object>);
104
105 #define MAKE_WEAK(Var, _) \
106     if (!m_##Var.isEmpty()) \
107         m_##Var.setWeak(&m_##Var, weakCallback<v8::Function>);
108
109     CALLBACK_LIST(MAKE_WEAK)
110 #undef MAKE_WEAK
111 }
112
113 V8PerContextData* V8CustomElementLifecycleCallbacks::creationContextData()
114 {
115     if (!executionContext())
116         return 0;
117
118     v8::Handle<v8::Context> context = toV8Context(executionContext(), m_world.get());
119     if (context.IsEmpty())
120         return 0;
121
122     return V8PerContextData::from(context);
123 }
124
125 V8CustomElementLifecycleCallbacks::~V8CustomElementLifecycleCallbacks()
126 {
127     if (!m_owner)
128         return;
129
130     v8::HandleScope handleScope(m_isolate);
131     if (V8PerContextData* perContextData = creationContextData())
132         perContextData->clearCustomElementBinding(m_owner);
133 }
134
135 bool 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
151 void V8CustomElementLifecycleCallbacks::created(Element* element)
152 {
153     // 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())
157         return;
158
159     element->setCustomElementState(Element::Upgraded);
160
161     v8::HandleScope handleScope(m_isolate);
162     v8::Handle<v8::Context> context = toV8Context(executionContext(), m_world.get());
163     if (context.IsEmpty())
164         return;
165
166     v8::Context::Scope scope(context);
167
168     v8::Handle<v8::Object> receiver = DOMDataStore::current(m_isolate).get<V8Element>(element, m_isolate);
169     if (!receiver.IsEmpty()) {
170         // Swizzle the prototype of the existing wrapper. We don't need to
171         // worry about non-existent wrappers; they will get the right
172         // prototype when wrapped.
173         v8::Handle<v8::Object> prototype = m_prototype.newLocal(m_isolate);
174         if (prototype.IsEmpty())
175             return;
176         receiver->SetPrototype(prototype);
177     }
178
179     v8::Handle<v8::Function> callback = m_created.newLocal(m_isolate);
180     if (callback.IsEmpty())
181         return;
182
183     if (receiver.IsEmpty())
184         receiver = toV8(element, context->Global(), m_isolate).As<v8::Object>();
185
186     ASSERT(!receiver.IsEmpty());
187
188     InspectorInstrumentation::willExecuteCustomElementCallback(element);
189
190     v8::TryCatch exceptionCatcher;
191     exceptionCatcher.SetVerbose(true);
192     ScriptController::callFunction(executionContext(), callback, receiver, 0, 0, m_isolate);
193 }
194
195 void V8CustomElementLifecycleCallbacks::attached(Element* element)
196 {
197     call(m_attached, element);
198 }
199
200 void V8CustomElementLifecycleCallbacks::detached(Element* element)
201 {
202     call(m_detached, element);
203 }
204
205 void V8CustomElementLifecycleCallbacks::attributeChanged(Element* element, const AtomicString& name, const AtomicString& oldValue, const AtomicString& newValue)
206 {
207     // FIXME: callbacks while paused should be queued up for execution to
208     // continue then be delivered in order rather than delivered immediately.
209     // Bug 329665 tracks similar behavior for other synchronous events.
210     if (!executionContext() || executionContext()->activeDOMObjectsAreStopped())
211         return;
212
213     v8::HandleScope handleScope(m_isolate);
214     v8::Handle<v8::Context> context = toV8Context(executionContext(), m_world.get());
215     if (context.IsEmpty())
216         return;
217
218     v8::Context::Scope scope(context);
219
220     v8::Handle<v8::Object> receiver = toV8(element, context->Global(), m_isolate).As<v8::Object>();
221     ASSERT(!receiver.IsEmpty());
222
223     v8::Handle<v8::Function> callback = m_attributeChanged.newLocal(m_isolate);
224     if (callback.IsEmpty())
225         return;
226
227     v8::Handle<v8::Value> argv[] = {
228         v8String(m_isolate, name),
229         oldValue.isNull() ? v8::Handle<v8::Value>(v8::Null(m_isolate)) : v8::Handle<v8::Value>(v8String(m_isolate, oldValue)),
230         newValue.isNull() ? v8::Handle<v8::Value>(v8::Null(m_isolate)) : v8::Handle<v8::Value>(v8String(m_isolate, newValue))
231     };
232
233     InspectorInstrumentation::willExecuteCustomElementCallback(element);
234
235     v8::TryCatch exceptionCatcher;
236     exceptionCatcher.SetVerbose(true);
237     ScriptController::callFunction(executionContext(), callback, receiver, WTF_ARRAY_LENGTH(argv), argv, m_isolate);
238 }
239
240 void V8CustomElementLifecycleCallbacks::call(const ScopedPersistent<v8::Function>& weakCallback, Element* element)
241 {
242     // FIXME: callbacks while paused should be queued up for execution to
243     // continue then be delivered in order rather than delivered immediately.
244     // Bug 329665 tracks similar behavior for other synchronous events.
245     if (!executionContext() || executionContext()->activeDOMObjectsAreStopped())
246         return;
247
248     v8::HandleScope handleScope(m_isolate);
249     v8::Handle<v8::Context> context = toV8Context(executionContext(), m_world.get());
250     if (context.IsEmpty())
251         return;
252
253     v8::Context::Scope scope(context);
254
255     v8::Handle<v8::Function> callback = weakCallback.newLocal(m_isolate);
256     if (callback.IsEmpty())
257         return;
258
259     v8::Handle<v8::Object> receiver = toV8(element, context->Global(), m_isolate).As<v8::Object>();
260     ASSERT(!receiver.IsEmpty());
261
262     InspectorInstrumentation::willExecuteCustomElementCallback(element);
263
264     v8::TryCatch exceptionCatcher;
265     exceptionCatcher.SetVerbose(true);
266     ScriptController::callFunction(executionContext(), callback, receiver, 0, 0, m_isolate);
267 }
268
269 } // namespace WebCore