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/ScriptState.h" | 7 #include "bindings/core/v8/ScriptState.h" |
8 #include "bindings/core/v8/V8Binding.h" | 8 #include "bindings/core/v8/V8Binding.h" |
9 #include "bindings/core/v8/V8BindingMacros.h" | 9 #include "bindings/core/v8/V8BindingMacros.h" |
10 #include "bindings/core/v8/V8CustomElementsRegistry.h" | 10 #include "bindings/core/v8/V8CustomElementsRegistry.h" |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
75 // | 75 // |
76 // At a meta-level, this downcast is safe because there is | 76 // At a meta-level, this downcast is safe because there is |
77 // currently only one implementation of CustomElementDefinition in | 77 // currently only one implementation of CustomElementDefinition in |
78 // product code and that is ScriptCustomElementDefinition. But | 78 // product code and that is ScriptCustomElementDefinition. But |
79 // that may change in the future. | 79 // that may change in the future. |
80 CustomElementDefinition* definition = registry->definitionForName(name); | 80 CustomElementDefinition* definition = registry->definitionForName(name); |
81 CHECK(definition); | 81 CHECK(definition); |
82 return static_cast<ScriptCustomElementDefinition*>(definition); | 82 return static_cast<ScriptCustomElementDefinition*>(definition); |
83 } | 83 } |
84 | 84 |
85 template <typename T> | |
86 static void keepAlive(v8::Local<v8::Array>& array, uint32_t index, | 85 static void keepAlive(v8::Local<v8::Array>& array, uint32_t index, |
87 const v8::Local<T>& value, | 86 const v8::Local<v8::Object>& value, |
88 ScopedPersistent<T>& persistent, | 87 ScopedPersistent<v8::Object>& persistent, |
89 ScriptState* scriptState) | 88 ScriptState* scriptState) |
90 { | 89 { |
91 if (value.IsEmpty()) | 90 if (value.IsEmpty()) |
92 return; | 91 return; |
93 | 92 |
94 v8CallOrCrash(array->Set(scriptState->context(), index, value)); | 93 v8CallOrCrash(array->Set(scriptState->context(), index, value)); |
95 | 94 |
96 persistent.set(scriptState->isolate(), value); | 95 persistent.set(scriptState->isolate(), value); |
97 persistent.setPhantom(); | 96 persistent.setPhantom(); |
98 } | 97 } |
99 | 98 |
100 ScriptCustomElementDefinition* ScriptCustomElementDefinition::create( | 99 ScriptCustomElementDefinition* ScriptCustomElementDefinition::create( |
101 ScriptState* scriptState, | 100 ScriptState* scriptState, |
102 CustomElementsRegistry* registry, | 101 CustomElementsRegistry* registry, |
103 const CustomElementDescriptor& descriptor, | 102 const CustomElementDescriptor& descriptor, |
104 const v8::Local<v8::Object>& constructor, | 103 const v8::Local<v8::Object>& constructor, |
105 const v8::Local<v8::Object>& prototype, | 104 const v8::Local<v8::Object>& prototype, |
106 const v8::Local<v8::Function>& connectedCallback, | 105 const v8::Local<v8::Object>& connectedCallback, |
107 const v8::Local<v8::Function>& disconnectedCallback, | 106 const v8::Local<v8::Object>& disconnectedCallback, |
108 const v8::Local<v8::Function>& attributeChangedCallback, | 107 const v8::Local<v8::Object>& attributeChangedCallback, |
109 const HashSet<AtomicString>& observedAttributes) | 108 const HashSet<AtomicString>& observedAttributes) |
110 { | 109 { |
111 ScriptCustomElementDefinition* definition = | 110 ScriptCustomElementDefinition* definition = |
112 new ScriptCustomElementDefinition( | 111 new ScriptCustomElementDefinition( |
113 scriptState, | 112 scriptState, |
114 descriptor, | 113 descriptor, |
115 constructor, | 114 constructor, |
116 prototype, | 115 prototype, |
117 connectedCallback, | 116 connectedCallback, |
118 disconnectedCallback, | 117 disconnectedCallback, |
(...skipping 18 matching lines...) Expand all Loading... |
137 v8CallOrCrash(map->Set(scriptState->context(), nameValue, array)); | 136 v8CallOrCrash(map->Set(scriptState->context(), nameValue, array)); |
138 | 137 |
139 return definition; | 138 return definition; |
140 } | 139 } |
141 | 140 |
142 ScriptCustomElementDefinition::ScriptCustomElementDefinition( | 141 ScriptCustomElementDefinition::ScriptCustomElementDefinition( |
143 ScriptState* scriptState, | 142 ScriptState* scriptState, |
144 const CustomElementDescriptor& descriptor, | 143 const CustomElementDescriptor& descriptor, |
145 const v8::Local<v8::Object>& constructor, | 144 const v8::Local<v8::Object>& constructor, |
146 const v8::Local<v8::Object>& prototype, | 145 const v8::Local<v8::Object>& prototype, |
147 const v8::Local<v8::Function>& connectedCallback, | 146 const v8::Local<v8::Object>& connectedCallback, |
148 const v8::Local<v8::Function>& disconnectedCallback, | 147 const v8::Local<v8::Object>& disconnectedCallback, |
149 const v8::Local<v8::Function>& attributeChangedCallback, | 148 const v8::Local<v8::Object>& attributeChangedCallback, |
150 const HashSet<AtomicString>& observedAttributes) | 149 const HashSet<AtomicString>& observedAttributes) |
151 : CustomElementDefinition(descriptor, observedAttributes) | 150 : CustomElementDefinition(descriptor) |
152 , m_scriptState(scriptState) | 151 , m_scriptState(scriptState) |
153 , m_constructor(scriptState->isolate(), constructor) | 152 , m_constructor(scriptState->isolate(), constructor) |
| 153 , m_observedAttributes(observedAttributes) |
154 { | 154 { |
155 } | 155 } |
156 | 156 |
157 HTMLElement* ScriptCustomElementDefinition::createElementSync( | 157 HTMLElement* ScriptCustomElementDefinition::createElementSync( |
158 Document& document, const QualifiedName& tagName, | 158 Document& document, const QualifiedName& tagName, |
159 ExceptionState& exceptionState) | 159 ExceptionState& exceptionState) |
160 { | 160 { |
161 DCHECK(ScriptState::current(m_scriptState->isolate()) == m_scriptState); | 161 DCHECK(ScriptState::current(m_scriptState->isolate()) == m_scriptState); |
162 | 162 |
163 // Create an element | 163 // Create an element |
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
276 DCHECK(!m_prototype.isEmpty()); | 276 DCHECK(!m_prototype.isEmpty()); |
277 return m_prototype.newLocal(m_scriptState->isolate()); | 277 return m_prototype.newLocal(m_scriptState->isolate()); |
278 } | 278 } |
279 | 279 |
280 // CustomElementDefinition | 280 // CustomElementDefinition |
281 ScriptValue ScriptCustomElementDefinition::getConstructorForScript() | 281 ScriptValue ScriptCustomElementDefinition::getConstructorForScript() |
282 { | 282 { |
283 return ScriptValue(m_scriptState.get(), constructor()); | 283 return ScriptValue(m_scriptState.get(), constructor()); |
284 } | 284 } |
285 | 285 |
286 bool ScriptCustomElementDefinition::hasConnectedCallback() const | |
287 { | |
288 return !m_connectedCallback.isEmpty(); | |
289 } | |
290 | |
291 bool ScriptCustomElementDefinition::hasDisconnectedCallback() const | |
292 { | |
293 return !m_disconnectedCallback.isEmpty(); | |
294 } | |
295 | |
296 void ScriptCustomElementDefinition::runCallback( | |
297 v8::Local<v8::Function> callback, | |
298 Element* element, int argc, v8::Local<v8::Value> argv[]) | |
299 { | |
300 DCHECK(ScriptState::current(m_scriptState->isolate()) == m_scriptState); | |
301 v8::Isolate* isolate = m_scriptState->isolate(); | |
302 | |
303 // Invoke custom element reactions | |
304 // https://html.spec.whatwg.org/multipage/scripting.html#invoke-custom-eleme
nt-reactions | |
305 // If this throws any exception, then report the exception. | |
306 v8::TryCatch tryCatch(isolate); | |
307 tryCatch.SetVerbose(true); | |
308 | |
309 ExecutionContext* executionContext = m_scriptState->getExecutionContext(); | |
310 v8::Local<v8::Value> elementHandle = toV8(element, | |
311 m_scriptState->context()->Global(), isolate); | |
312 V8ScriptRunner::callFunction( | |
313 callback, | |
314 executionContext, | |
315 elementHandle, | |
316 argc, argv, isolate); | |
317 } | |
318 | |
319 void ScriptCustomElementDefinition::runConnectedCallback(Element* element) | |
320 { | |
321 if (!m_scriptState->contextIsValid()) | |
322 return; | |
323 ScriptState::Scope scope(m_scriptState.get()); | |
324 v8::Isolate* isolate = m_scriptState->isolate(); | |
325 runCallback(m_connectedCallback.newLocal(isolate), element); | |
326 } | |
327 | |
328 void ScriptCustomElementDefinition::runDisconnectedCallback(Element* element) | |
329 { | |
330 if (!m_scriptState->contextIsValid()) | |
331 return; | |
332 ScriptState::Scope scope(m_scriptState.get()); | |
333 v8::Isolate* isolate = m_scriptState->isolate(); | |
334 runCallback(m_disconnectedCallback.newLocal(isolate), element); | |
335 } | |
336 | |
337 void ScriptCustomElementDefinition::runAttributeChangedCallback( | |
338 Element* element, const QualifiedName& name, | |
339 const AtomicString& oldValue, const AtomicString& newValue) | |
340 { | |
341 if (!m_scriptState->contextIsValid()) | |
342 return; | |
343 ScriptState::Scope scope(m_scriptState.get()); | |
344 v8::Isolate* isolate = m_scriptState->isolate(); | |
345 const int argc = 4; | |
346 v8::Local<v8::Value> argv[argc] = { | |
347 v8String(isolate, name.localName()), | |
348 v8StringOrNull(isolate, oldValue), | |
349 v8StringOrNull(isolate, newValue), | |
350 v8String(isolate, name.namespaceURI()), | |
351 }; | |
352 runCallback(m_attributeChangedCallback.newLocal(isolate), element, | |
353 argc, argv); | |
354 } | |
355 | |
356 } // namespace blink | 286 } // namespace blink |
OLD | NEW |