OLD | NEW |
---|---|
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "bindings/core/v8/ScriptCustomElementDefinition.h" | 5 #include "bindings/core/v8/ScriptCustomElementDefinition.h" |
6 | 6 |
7 #include "bindings/core/v8/ScriptController.h" | |
7 #include "bindings/core/v8/ScriptState.h" | 8 #include "bindings/core/v8/ScriptState.h" |
8 #include "bindings/core/v8/V8Binding.h" | 9 #include "bindings/core/v8/V8Binding.h" |
9 #include "bindings/core/v8/V8BindingMacros.h" | 10 #include "bindings/core/v8/V8BindingMacros.h" |
10 #include "bindings/core/v8/V8CustomElementsRegistry.h" | 11 #include "bindings/core/v8/V8CustomElementsRegistry.h" |
11 #include "bindings/core/v8/V8Element.h" | 12 #include "bindings/core/v8/V8Element.h" |
12 #include "bindings/core/v8/V8HiddenValue.h" | 13 #include "bindings/core/v8/V8HiddenValue.h" |
13 #include "bindings/core/v8/V8ScriptRunner.h" | 14 #include "bindings/core/v8/V8ScriptRunner.h" |
14 #include "bindings/core/v8/V8ThrowException.h" | 15 #include "bindings/core/v8/V8ThrowException.h" |
15 #include "core/dom/ExceptionCode.h" | 16 #include "core/dom/ExceptionCode.h" |
16 #include "v8.h" | 17 #include "v8.h" |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
73 // | 74 // |
74 // At a meta-level, this downcast is safe because there is | 75 // At a meta-level, this downcast is safe because there is |
75 // currently only one implementation of CustomElementDefinition in | 76 // currently only one implementation of CustomElementDefinition in |
76 // product code and that is ScriptCustomElementDefinition. But | 77 // product code and that is ScriptCustomElementDefinition. But |
77 // that may change in the future. | 78 // that may change in the future. |
78 CustomElementDefinition* definition = registry->definitionForName(name); | 79 CustomElementDefinition* definition = registry->definitionForName(name); |
79 CHECK(definition); | 80 CHECK(definition); |
80 return static_cast<ScriptCustomElementDefinition*>(definition); | 81 return static_cast<ScriptCustomElementDefinition*>(definition); |
81 } | 82 } |
82 | 83 |
84 template <typename T> | |
83 static void keepAlive(v8::Local<v8::Array>& array, uint32_t index, | 85 static void keepAlive(v8::Local<v8::Array>& array, uint32_t index, |
84 const v8::Local<v8::Object>& value, | 86 const v8::Local<T>& value, |
85 ScopedPersistent<v8::Object>& persistent, | 87 ScopedPersistent<T>& persistent, |
86 ScriptState* scriptState) | 88 ScriptState* scriptState) |
87 { | 89 { |
88 if (value.IsEmpty()) | 90 if (value.IsEmpty()) |
89 return; | 91 return; |
90 | 92 |
91 v8CallOrCrash(array->Set(scriptState->context(), index, value)); | 93 v8CallOrCrash(array->Set(scriptState->context(), index, value)); |
92 | 94 |
93 persistent.set(scriptState->isolate(), value); | 95 persistent.set(scriptState->isolate(), value); |
94 persistent.setPhantom(); | 96 persistent.setPhantom(); |
95 } | 97 } |
96 | 98 |
97 ScriptCustomElementDefinition* ScriptCustomElementDefinition::create( | 99 ScriptCustomElementDefinition* ScriptCustomElementDefinition::create( |
98 ScriptState* scriptState, | 100 ScriptState* scriptState, |
99 CustomElementsRegistry* registry, | 101 CustomElementsRegistry* registry, |
100 const CustomElementDescriptor& descriptor, | 102 const CustomElementDescriptor& descriptor, |
101 const v8::Local<v8::Object>& constructor, | 103 const v8::Local<v8::Object>& constructor, |
102 const v8::Local<v8::Object>& prototype, | 104 const v8::Local<v8::Object>& prototype, |
103 const v8::Local<v8::Object>& connectedCallback, | 105 const v8::Local<v8::Function>& connectedCallback, |
104 const v8::Local<v8::Object>& disconnectedCallback, | 106 const v8::Local<v8::Function>& disconnectedCallback, |
105 const v8::Local<v8::Object>& attributeChangedCallback, | 107 const v8::Local<v8::Function>& attributeChangedCallback, |
106 const HashSet<AtomicString>& observedAttributes) | 108 const HashSet<AtomicString>& observedAttributes) |
107 { | 109 { |
108 ScriptCustomElementDefinition* definition = | 110 ScriptCustomElementDefinition* definition = |
109 new ScriptCustomElementDefinition( | 111 new ScriptCustomElementDefinition( |
110 scriptState, | 112 scriptState, |
111 descriptor, | 113 descriptor, |
112 constructor, | 114 constructor, |
113 prototype, | 115 prototype, |
114 connectedCallback, | 116 connectedCallback, |
115 disconnectedCallback, | 117 disconnectedCallback, |
(...skipping 18 matching lines...) Expand all Loading... | |
134 v8CallOrCrash(map->Set(scriptState->context(), nameValue, array)); | 136 v8CallOrCrash(map->Set(scriptState->context(), nameValue, array)); |
135 | 137 |
136 return definition; | 138 return definition; |
137 } | 139 } |
138 | 140 |
139 ScriptCustomElementDefinition::ScriptCustomElementDefinition( | 141 ScriptCustomElementDefinition::ScriptCustomElementDefinition( |
140 ScriptState* scriptState, | 142 ScriptState* scriptState, |
141 const CustomElementDescriptor& descriptor, | 143 const CustomElementDescriptor& descriptor, |
142 const v8::Local<v8::Object>& constructor, | 144 const v8::Local<v8::Object>& constructor, |
143 const v8::Local<v8::Object>& prototype, | 145 const v8::Local<v8::Object>& prototype, |
144 const v8::Local<v8::Object>& connectedCallback, | 146 const v8::Local<v8::Function>& connectedCallback, |
145 const v8::Local<v8::Object>& disconnectedCallback, | 147 const v8::Local<v8::Function>& disconnectedCallback, |
146 const v8::Local<v8::Object>& attributeChangedCallback, | 148 const v8::Local<v8::Function>& attributeChangedCallback, |
147 const HashSet<AtomicString>& observedAttributes) | 149 const HashSet<AtomicString>& observedAttributes) |
148 : CustomElementDefinition(descriptor) | 150 : CustomElementDefinition(descriptor, observedAttributes) |
149 , m_scriptState(scriptState) | 151 , m_scriptState(scriptState) |
150 , m_constructor(scriptState->isolate(), constructor) | 152 , m_constructor(scriptState->isolate(), constructor) |
151 , m_observedAttributes(observedAttributes) | |
152 { | 153 { |
153 } | 154 } |
154 | 155 |
155 // https://html.spec.whatwg.org/multipage/scripting.html#upgrades | 156 // https://html.spec.whatwg.org/multipage/scripting.html#upgrades |
156 bool ScriptCustomElementDefinition::runConstructor(Element* element) | 157 bool ScriptCustomElementDefinition::runConstructor(Element* element) |
157 { | 158 { |
158 if (!m_scriptState->contextIsValid()) | 159 if (!m_scriptState->contextIsValid()) |
159 return false; | 160 return false; |
160 ScriptState::Scope scope(m_scriptState.get()); | 161 ScriptState::Scope scope(m_scriptState.get()); |
161 v8::Isolate* isolate = m_scriptState->isolate(); | 162 v8::Isolate* isolate = m_scriptState->isolate(); |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
202 DCHECK(!m_prototype.isEmpty()); | 203 DCHECK(!m_prototype.isEmpty()); |
203 return m_prototype.newLocal(m_scriptState->isolate()); | 204 return m_prototype.newLocal(m_scriptState->isolate()); |
204 } | 205 } |
205 | 206 |
206 // CustomElementDefinition | 207 // CustomElementDefinition |
207 ScriptValue ScriptCustomElementDefinition::getConstructorForScript() | 208 ScriptValue ScriptCustomElementDefinition::getConstructorForScript() |
208 { | 209 { |
209 return ScriptValue(m_scriptState.get(), constructor()); | 210 return ScriptValue(m_scriptState.get(), constructor()); |
210 } | 211 } |
211 | 212 |
213 bool ScriptCustomElementDefinition::hasConnectedCallback() const | |
214 { | |
215 return !m_connectedCallback.isEmpty(); | |
216 } | |
217 | |
218 bool ScriptCustomElementDefinition::hasDisconnectedCallback() const | |
219 { | |
220 return !m_disconnectedCallback.isEmpty(); | |
221 } | |
222 | |
223 void ScriptCustomElementDefinition::runCallback( | |
224 ScopedPersistent<v8::Function>& callback, | |
225 Element* element, int argc, v8::Local<v8::Value> argv[]) | |
226 { | |
227 DCHECK(ScriptState::current(m_scriptState->isolate()) == m_scriptState); | |
228 v8::Isolate* isolate = m_scriptState->isolate(); | |
229 | |
230 // Invoke custom element reactions | |
231 // https://html.spec.whatwg.org/multipage/scripting.html#invoke-custom-eleme nt-reactions | |
232 // If this throws any exception, then report the exception. | |
233 v8::TryCatch tryCatch(isolate); | |
234 tryCatch.SetVerbose(true); | |
235 | |
236 ExecutionContext* executionContext = m_scriptState->getExecutionContext(); | |
237 v8::Local<v8::Value> elementHandle = toV8(element, | |
238 m_scriptState->context()->Global(), isolate); | |
239 ScriptController::callFunction( | |
haraken
2016/06/14 11:39:24
Nit: Use V8ScriptRunner::callFunction. ScriptContr
| |
240 executionContext, | |
241 callback.newLocal(isolate), | |
242 elementHandle, | |
243 argc, argv, isolate); | |
244 } | |
245 | |
246 void ScriptCustomElementDefinition::runConnectedCallback(Element* element) | |
247 { | |
248 if (!m_scriptState->contextIsValid()) | |
249 return; | |
250 ScriptState::Scope scope(m_scriptState.get()); | |
251 runCallback(m_connectedCallback, element); | |
252 } | |
253 | |
254 void ScriptCustomElementDefinition::runDisconnectedCallback(Element* element) | |
255 { | |
256 if (!m_scriptState->contextIsValid()) | |
257 return; | |
258 ScriptState::Scope scope(m_scriptState.get()); | |
259 runCallback(m_disconnectedCallback, element); | |
260 } | |
261 | |
262 void ScriptCustomElementDefinition::runAttributeChangedCallback( | |
263 Element* element, const QualifiedName& name, | |
264 const AtomicString& oldValue, const AtomicString& newValue) | |
265 { | |
266 if (!m_scriptState->contextIsValid()) | |
267 return; | |
268 ScriptState::Scope scope(m_scriptState.get()); | |
269 v8::Isolate* isolate = m_scriptState->isolate(); | |
270 const int argc = 4; | |
271 v8::Local<v8::Value> argv[argc] = { | |
272 v8String(isolate, name.localName()), | |
273 v8StringOrNull(isolate, oldValue), | |
274 v8StringOrNull(isolate, newValue), | |
275 v8String(isolate, name.namespaceURI()), | |
276 }; | |
277 runCallback(m_attributeChangedCallback, element, argc, argv); | |
278 } | |
279 | |
212 } // namespace blink | 280 } // namespace blink |
OLD | NEW |