2 * Copyright (C) 2013 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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
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.
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.
32 #include "bindings/v8/V8CustomElementLifecycleCallbacks.h"
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"
46 #define CALLBACK_LIST(V) \
48 V(attached, Attached) \
49 V(detached, Detached) \
50 V(attributeChanged, AttributeChanged)
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)
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);
62 CALLBACK_LIST(SET_HIDDEN_PROPERTY)
63 #undef SET_HIDDEN_PROPERTY
65 return adoptRef(new V8CustomElementLifecycleCallbacks(executionContext, prototype, created, attached, detached, attributeChanged));
68 static CustomElementLifecycleCallbacks::CallbackType flagSet(v8::Handle<v8::Function> attached, v8::Handle<v8::Function> detached, v8::Handle<v8::Function> attributeChanged)
70 // V8 Custom Elements always run created to swizzle prototypes.
71 int flags = CustomElementLifecycleCallbacks::Created;
73 if (!attached.IsEmpty())
74 flags |= CustomElementLifecycleCallbacks::Attached;
76 if (!detached.IsEmpty())
77 flags |= CustomElementLifecycleCallbacks::Detached;
79 if (!attributeChanged.IsEmpty())
80 flags |= CustomElementLifecycleCallbacks::AttributeChanged;
82 return CustomElementLifecycleCallbacks::CallbackType(flags);
86 static void weakCallback(const v8::WeakCallbackData<T, ScopedPersistent<T> >& data)
88 data.GetParameter()->clear();
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)
95 , m_world(DOMWrapperWorld::current())
96 , m_prototype(toIsolate(executionContext), prototype)
97 , m_created(toIsolate(executionContext), created)
98 , m_attached(toIsolate(executionContext), attached)
99 , m_detached(toIsolate(executionContext), detached)
100 , m_attributeChanged(toIsolate(executionContext), attributeChanged)
102 m_prototype.setWeak(&m_prototype, weakCallback<v8::Object>);
104 #define MAKE_WEAK(Var, _) \
105 if (!m_##Var.isEmpty()) \
106 m_##Var.setWeak(&m_##Var, weakCallback<v8::Function>);
108 CALLBACK_LIST(MAKE_WEAK)
112 V8PerContextData* V8CustomElementLifecycleCallbacks::creationContextData()
114 if (!executionContext())
117 v8::Handle<v8::Context> context = toV8Context(executionContext(), m_world.get());
118 if (context.IsEmpty())
121 return V8PerContextData::from(context);
124 V8CustomElementLifecycleCallbacks::~V8CustomElementLifecycleCallbacks()
129 v8::HandleScope handleScope(toIsolate(executionContext()));
130 if (V8PerContextData* perContextData = creationContextData())
131 perContextData->clearCustomElementBinding(m_owner);
134 bool V8CustomElementLifecycleCallbacks::setBinding(CustomElementDefinition* owner, PassOwnPtr<CustomElementBinding> binding)
138 V8PerContextData* perContextData = creationContextData();
144 // Bindings retrieve the prototype when needed from per-context data.
145 perContextData->addCustomElementBinding(owner, binding);
150 void V8CustomElementLifecycleCallbacks::created(Element* element)
152 // FIXME: callbacks while paused should be queued up for execution to
153 // continue then be delivered in order rather than delivered immediately.
154 // Bug 329665 tracks similar behavior for other synchronous events.
155 if (!executionContext() || executionContext()->activeDOMObjectsAreStopped())
158 element->setCustomElementState(Element::Upgraded);
160 v8::Isolate* isolate = toIsolate(executionContext());
161 v8::HandleScope handleScope(isolate);
162 v8::Handle<v8::Context> context = toV8Context(executionContext(), m_world.get());
163 if (context.IsEmpty())
166 v8::Context::Scope scope(context);
168 v8::Handle<v8::Object> receiver = DOMDataStore::current(isolate).get<V8Element>(element, 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(isolate);
174 if (prototype.IsEmpty())
176 receiver->SetPrototype(prototype);
179 v8::Handle<v8::Function> callback = m_created.newLocal(isolate);
180 if (callback.IsEmpty())
183 if (receiver.IsEmpty())
184 receiver = toV8(element, context->Global(), isolate).As<v8::Object>();
186 ASSERT(!receiver.IsEmpty());
188 InspectorInstrumentation::willExecuteCustomElementCallback(element);
190 v8::TryCatch exceptionCatcher;
191 exceptionCatcher.SetVerbose(true);
192 ScriptController::callFunction(executionContext(), callback, receiver, 0, 0, isolate);
195 void V8CustomElementLifecycleCallbacks::attached(Element* element)
197 call(m_attached, element);
200 void V8CustomElementLifecycleCallbacks::detached(Element* element)
202 call(m_detached, element);
205 void V8CustomElementLifecycleCallbacks::attributeChanged(Element* element, const AtomicString& name, const AtomicString& oldValue, const AtomicString& newValue)
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())
213 v8::Isolate* isolate = toIsolate(executionContext());
214 v8::HandleScope handleScope(isolate);
215 v8::Handle<v8::Context> context = toV8Context(executionContext(), m_world.get());
216 if (context.IsEmpty())
219 v8::Context::Scope scope(context);
221 v8::Handle<v8::Object> receiver = toV8(element, context->Global(), isolate).As<v8::Object>();
222 ASSERT(!receiver.IsEmpty());
224 v8::Handle<v8::Function> callback = m_attributeChanged.newLocal(isolate);
225 if (callback.IsEmpty())
228 v8::Handle<v8::Value> argv[] = {
229 v8String(isolate, name),
230 oldValue.isNull() ? v8::Handle<v8::Value>(v8::Null(isolate)) : v8::Handle<v8::Value>(v8String(isolate, oldValue)),
231 newValue.isNull() ? v8::Handle<v8::Value>(v8::Null(isolate)) : v8::Handle<v8::Value>(v8String(isolate, newValue))
234 InspectorInstrumentation::willExecuteCustomElementCallback(element);
236 v8::TryCatch exceptionCatcher;
237 exceptionCatcher.SetVerbose(true);
238 ScriptController::callFunction(executionContext(), callback, receiver, WTF_ARRAY_LENGTH(argv), argv, isolate);
241 void V8CustomElementLifecycleCallbacks::call(const ScopedPersistent<v8::Function>& weakCallback, Element* element)
243 // FIXME: callbacks while paused should be queued up for execution to
244 // continue then be delivered in order rather than delivered immediately.
245 // Bug 329665 tracks similar behavior for other synchronous events.
246 if (!executionContext() || executionContext()->activeDOMObjectsAreStopped())
249 v8::HandleScope handleScope(toIsolate(executionContext()));
250 v8::Handle<v8::Context> context = toV8Context(executionContext(), m_world.get());
251 if (context.IsEmpty())
254 v8::Context::Scope scope(context);
255 v8::Isolate* isolate = context->GetIsolate();
257 v8::Handle<v8::Function> callback = weakCallback.newLocal(isolate);
258 if (callback.IsEmpty())
261 v8::Handle<v8::Object> receiver = toV8(element, context->Global(), isolate).As<v8::Object>();
262 ASSERT(!receiver.IsEmpty());
264 InspectorInstrumentation::willExecuteCustomElementCallback(element);
266 v8::TryCatch exceptionCatcher;
267 exceptionCatcher.SetVerbose(true);
268 ScriptController::callFunction(executionContext(), callback, receiver, 0, 0, isolate);
271 } // namespace WebCore