Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(771)

Side by Side Diff: third_party/WebKit/Source/bindings/core/v8/V8CustomElementLifecycleCallbacks.cpp

Issue 1914923002: Rename all existing custom element classes as V0 (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: CustomElementV0 -> V0CustomElement Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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 "bindings/core/v8/V8CustomElementLifecycleCallbacks.h"
32
33 #include "bindings/core/v8/CustomElementBinding.h"
34 #include "bindings/core/v8/DOMDataStore.h"
35 #include "bindings/core/v8/ScriptController.h"
36 #include "bindings/core/v8/V8Binding.h"
37 #include "bindings/core/v8/V8Element.h"
38 #include "bindings/core/v8/V8HiddenValue.h"
39 #include "bindings/core/v8/V8PerContextData.h"
40 #include "core/dom/ExecutionContext.h"
41 #include "wtf/PassOwnPtr.h"
42
43 namespace blink {
44
45 #define CALLBACK_LIST(V) \
46 V(created, CreatedCallback) \
47 V(attached, AttachedCallback) \
48 V(detached, DetachedCallback) \
49 V(attributeChanged, AttributeChangedCallback)
50
51 V8CustomElementLifecycleCallbacks* V8CustomElementLifecycleCallbacks::create(Scr iptState* scriptState, v8::Local<v8::Object> prototype, v8::MaybeLocal<v8::Funct ion> created, v8::MaybeLocal<v8::Function> attached, v8::MaybeLocal<v8::Function > detached, v8::MaybeLocal<v8::Function> attributeChanged)
52 {
53 v8::Isolate* isolate = scriptState->isolate();
54 // A given object can only be used as a Custom Element prototype
55 // once; see customElementIsInterfacePrototypeObject
56 #define SET_HIDDEN_VALUE(Value, Name) \
57 ASSERT(V8HiddenValue::getHiddenValue(scriptState, prototype, V8HiddenValue:: customElement##Name(isolate)).IsEmpty()); \
58 if (!Value.IsEmpty()) \
59 V8HiddenValue::setHiddenValue(scriptState, prototype, V8HiddenValue::cus tomElement##Name(isolate), Value.ToLocalChecked());
60
61 CALLBACK_LIST(SET_HIDDEN_VALUE)
62 #undef SET_HIDDEN_VALUE
63
64 return new V8CustomElementLifecycleCallbacks(scriptState, prototype, created , attached, detached, attributeChanged);
65 }
66
67 static CustomElementLifecycleCallbacks::CallbackType flagSet(v8::MaybeLocal<v8:: Function> attached, v8::MaybeLocal<v8::Function> detached, v8::MaybeLocal<v8::Fu nction> attributeChanged)
68 {
69 // V8 Custom Elements always run created to swizzle prototypes.
70 int flags = CustomElementLifecycleCallbacks::CreatedCallback;
71
72 if (!attached.IsEmpty())
73 flags |= CustomElementLifecycleCallbacks::AttachedCallback;
74
75 if (!detached.IsEmpty())
76 flags |= CustomElementLifecycleCallbacks::DetachedCallback;
77
78 if (!attributeChanged.IsEmpty())
79 flags |= CustomElementLifecycleCallbacks::AttributeChangedCallback;
80
81 return CustomElementLifecycleCallbacks::CallbackType(flags);
82 }
83
84 template <typename T>
85 static void weakCallback(const v8::WeakCallbackInfo<ScopedPersistent<T>>& data)
86 {
87 data.GetParameter()->clear();
88 }
89
90 V8CustomElementLifecycleCallbacks::V8CustomElementLifecycleCallbacks(ScriptState * scriptState, v8::Local<v8::Object> prototype, v8::MaybeLocal<v8::Function> cre ated, v8::MaybeLocal<v8::Function> attached, v8::MaybeLocal<v8::Function> detach ed, v8::MaybeLocal<v8::Function> attributeChanged)
91 : CustomElementLifecycleCallbacks(flagSet(attached, detached, attributeChang ed))
92 , ContextLifecycleObserver(scriptState->getExecutionContext())
93 , m_scriptState(scriptState)
94 , m_prototype(scriptState->isolate(), prototype)
95 , m_created(scriptState->isolate(), created)
96 , m_attached(scriptState->isolate(), attached)
97 , m_detached(scriptState->isolate(), detached)
98 , m_attributeChanged(scriptState->isolate(), attributeChanged)
99 {
100 m_prototype.setWeak(&m_prototype, weakCallback<v8::Object>);
101
102 #define MAKE_WEAK(Var, _) \
103 if (!m_##Var.isEmpty()) \
104 m_##Var.setWeak(&m_##Var, weakCallback<v8::Function>);
105
106 CALLBACK_LIST(MAKE_WEAK)
107 #undef MAKE_WEAK
108 }
109
110 V8PerContextData* V8CustomElementLifecycleCallbacks::creationContextData()
111 {
112 if (!getExecutionContext())
113 return 0;
114
115 v8::Local<v8::Context> context = m_scriptState->context();
116 if (context.IsEmpty())
117 return 0;
118
119 return V8PerContextData::from(context);
120 }
121
122 V8CustomElementLifecycleCallbacks::~V8CustomElementLifecycleCallbacks()
123 {
124 }
125
126 bool V8CustomElementLifecycleCallbacks::setBinding(PassOwnPtr<CustomElementBindi ng> binding)
127 {
128 V8PerContextData* perContextData = creationContextData();
129 if (!perContextData)
130 return false;
131
132 // The context is responsible for keeping the prototype
133 // alive. This in turn keeps callbacks alive through hidden
134 // references; see CALLBACK_LIST(SET_HIDDEN_VALUE).
135 perContextData->addCustomElementBinding(std::move(binding));
136 return true;
137 }
138
139 void V8CustomElementLifecycleCallbacks::created(Element* element)
140 {
141 // FIXME: callbacks while paused should be queued up for execution to
142 // continue then be delivered in order rather than delivered immediately.
143 // Bug 329665 tracks similar behavior for other synchronous events.
144 if (!getExecutionContext() || getExecutionContext()->activeDOMObjectsAreStop ped())
145 return;
146
147 if (!m_scriptState->contextIsValid())
148 return;
149
150 element->setCustomElementState(Element::Upgraded);
151
152 ScriptState::Scope scope(m_scriptState.get());
153 v8::Isolate* isolate = m_scriptState->isolate();
154 v8::Local<v8::Context> context = m_scriptState->context();
155 v8::Local<v8::Value> receiverValue = toV8(element, context->Global(), isolat e);
156 if (receiverValue.IsEmpty())
157 return;
158 v8::Local<v8::Object> receiver = receiverValue.As<v8::Object>();
159
160 // Swizzle the prototype of the wrapper.
161 v8::Local<v8::Object> prototype = m_prototype.newLocal(isolate);
162 if (prototype.IsEmpty())
163 return;
164 if (!v8CallBoolean(receiver->SetPrototype(context, prototype)))
165 return;
166
167 v8::Local<v8::Function> callback = m_created.newLocal(isolate);
168 if (callback.IsEmpty())
169 return;
170
171 v8::TryCatch exceptionCatcher(isolate);
172 exceptionCatcher.SetVerbose(true);
173 ScriptController::callFunction(getExecutionContext(), callback, receiver, 0, 0, isolate);
174 }
175
176 void V8CustomElementLifecycleCallbacks::attached(Element* element)
177 {
178 call(m_attached, element);
179 }
180
181 void V8CustomElementLifecycleCallbacks::detached(Element* element)
182 {
183 call(m_detached, element);
184 }
185
186 void V8CustomElementLifecycleCallbacks::attributeChanged(Element* element, const AtomicString& name, const AtomicString& oldValue, const AtomicString& newValue)
187 {
188 // FIXME: callbacks while paused should be queued up for execution to
189 // continue then be delivered in order rather than delivered immediately.
190 // Bug 329665 tracks similar behavior for other synchronous events.
191 if (!getExecutionContext() || getExecutionContext()->activeDOMObjectsAreStop ped())
192 return;
193
194 if (!m_scriptState->contextIsValid())
195 return;
196 ScriptState::Scope scope(m_scriptState.get());
197 v8::Isolate* isolate = m_scriptState->isolate();
198 v8::Local<v8::Context> context = m_scriptState->context();
199 v8::Local<v8::Value> receiver = toV8(element, context->Global(), isolate);
200 if (receiver.IsEmpty())
201 return;
202
203 v8::Local<v8::Function> callback = m_attributeChanged.newLocal(isolate);
204 if (callback.IsEmpty())
205 return;
206
207 v8::Local<v8::Value> argv[] = {
208 v8String(isolate, name),
209 oldValue.isNull() ? v8::Local<v8::Value>(v8::Null(isolate)) : v8::Local< v8::Value>(v8String(isolate, oldValue)),
210 newValue.isNull() ? v8::Local<v8::Value>(v8::Null(isolate)) : v8::Local< v8::Value>(v8String(isolate, newValue))
211 };
212
213 v8::TryCatch exceptionCatcher(isolate);
214 exceptionCatcher.SetVerbose(true);
215 ScriptController::callFunction(getExecutionContext(), callback, receiver, WT F_ARRAY_LENGTH(argv), argv, isolate);
216 }
217
218 void V8CustomElementLifecycleCallbacks::call(const ScopedPersistent<v8::Function >& weakCallback, Element* element)
219 {
220 // FIXME: callbacks while paused should be queued up for execution to
221 // continue then be delivered in order rather than delivered immediately.
222 // Bug 329665 tracks similar behavior for other synchronous events.
223 if (!getExecutionContext() || getExecutionContext()->activeDOMObjectsAreStop ped())
224 return;
225
226 if (!m_scriptState->contextIsValid())
227 return;
228 ScriptState::Scope scope(m_scriptState.get());
229 v8::Isolate* isolate = m_scriptState->isolate();
230 v8::Local<v8::Context> context = m_scriptState->context();
231 v8::Local<v8::Function> callback = weakCallback.newLocal(isolate);
232 if (callback.IsEmpty())
233 return;
234
235 v8::Local<v8::Value> receiver = toV8(element, context->Global(), isolate);
236 if (receiver.IsEmpty())
237 return;
238
239 v8::TryCatch exceptionCatcher(isolate);
240 exceptionCatcher.SetVerbose(true);
241 ScriptController::callFunction(getExecutionContext(), callback, receiver, 0, 0, isolate);
242 }
243
244 DEFINE_TRACE(V8CustomElementLifecycleCallbacks)
245 {
246 CustomElementLifecycleCallbacks::trace(visitor);
247 ContextLifecycleObserver::trace(visitor);
248 }
249
250 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698