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> |
85 static void keepAlive(v8::Local<v8::Array>& array, uint32_t index, | 86 static void keepAlive(v8::Local<v8::Array>& array, uint32_t index, |
86 const v8::Local<v8::Object>& value, | 87 const v8::Local<T>& value, |
87 ScopedPersistent<v8::Object>& persistent, | 88 ScopedPersistent<T>& persistent, |
88 ScriptState* scriptState) | 89 ScriptState* scriptState) |
89 { | 90 { |
90 if (value.IsEmpty()) | 91 if (value.IsEmpty()) |
91 return; | 92 return; |
92 | 93 |
93 v8CallOrCrash(array->Set(scriptState->context(), index, value)); | 94 v8CallOrCrash(array->Set(scriptState->context(), index, value)); |
94 | 95 |
95 persistent.set(scriptState->isolate(), value); | 96 persistent.set(scriptState->isolate(), value); |
96 persistent.setPhantom(); | 97 persistent.setPhantom(); |
97 } | 98 } |
98 | 99 |
99 ScriptCustomElementDefinition* ScriptCustomElementDefinition::create( | 100 ScriptCustomElementDefinition* ScriptCustomElementDefinition::create( |
100 ScriptState* scriptState, | 101 ScriptState* scriptState, |
101 CustomElementsRegistry* registry, | 102 CustomElementsRegistry* registry, |
102 const CustomElementDescriptor& descriptor, | 103 const CustomElementDescriptor& descriptor, |
103 const v8::Local<v8::Object>& constructor, | 104 const v8::Local<v8::Object>& constructor, |
104 const v8::Local<v8::Object>& prototype, | 105 const v8::Local<v8::Object>& prototype, |
105 const v8::Local<v8::Object>& connectedCallback, | 106 const v8::Local<v8::Function>& connectedCallback, |
106 const v8::Local<v8::Object>& disconnectedCallback, | 107 const v8::Local<v8::Function>& disconnectedCallback, |
107 const v8::Local<v8::Object>& attributeChangedCallback, | 108 const v8::Local<v8::Function>& attributeChangedCallback, |
108 const HashSet<AtomicString>& observedAttributes) | 109 const HashSet<AtomicString>& observedAttributes) |
109 { | 110 { |
110 ScriptCustomElementDefinition* definition = | 111 ScriptCustomElementDefinition* definition = |
111 new ScriptCustomElementDefinition( | 112 new ScriptCustomElementDefinition( |
112 scriptState, | 113 scriptState, |
113 descriptor, | 114 descriptor, |
114 constructor, | 115 constructor, |
115 prototype, | 116 prototype, |
116 connectedCallback, | 117 connectedCallback, |
117 disconnectedCallback, | 118 disconnectedCallback, |
(...skipping 18 matching lines...) Expand all Loading... |
136 v8CallOrCrash(map->Set(scriptState->context(), nameValue, array)); | 137 v8CallOrCrash(map->Set(scriptState->context(), nameValue, array)); |
137 | 138 |
138 return definition; | 139 return definition; |
139 } | 140 } |
140 | 141 |
141 ScriptCustomElementDefinition::ScriptCustomElementDefinition( | 142 ScriptCustomElementDefinition::ScriptCustomElementDefinition( |
142 ScriptState* scriptState, | 143 ScriptState* scriptState, |
143 const CustomElementDescriptor& descriptor, | 144 const CustomElementDescriptor& descriptor, |
144 const v8::Local<v8::Object>& constructor, | 145 const v8::Local<v8::Object>& constructor, |
145 const v8::Local<v8::Object>& prototype, | 146 const v8::Local<v8::Object>& prototype, |
146 const v8::Local<v8::Object>& connectedCallback, | 147 const v8::Local<v8::Function>& connectedCallback, |
147 const v8::Local<v8::Object>& disconnectedCallback, | 148 const v8::Local<v8::Function>& disconnectedCallback, |
148 const v8::Local<v8::Object>& attributeChangedCallback, | 149 const v8::Local<v8::Function>& attributeChangedCallback, |
149 const HashSet<AtomicString>& observedAttributes) | 150 const HashSet<AtomicString>& observedAttributes) |
150 : CustomElementDefinition(descriptor) | 151 : CustomElementDefinition(descriptor, observedAttributes) |
151 , m_scriptState(scriptState) | 152 , m_scriptState(scriptState) |
152 , m_constructor(scriptState->isolate(), constructor) | 153 , 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 |
286 } // namespace blink | 356 } // namespace blink |
OLD | NEW |