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 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
99 } | 99 } |
100 | 100 |
101 ScriptCustomElementDefinition* ScriptCustomElementDefinition::create( | 101 ScriptCustomElementDefinition* ScriptCustomElementDefinition::create( |
102 ScriptState* scriptState, | 102 ScriptState* scriptState, |
103 CustomElementsRegistry* registry, | 103 CustomElementsRegistry* registry, |
104 const CustomElementDescriptor& descriptor, | 104 const CustomElementDescriptor& descriptor, |
105 const v8::Local<v8::Object>& constructor, | 105 const v8::Local<v8::Object>& constructor, |
106 const v8::Local<v8::Object>& prototype, | 106 const v8::Local<v8::Object>& prototype, |
107 const v8::Local<v8::Function>& connectedCallback, | 107 const v8::Local<v8::Function>& connectedCallback, |
108 const v8::Local<v8::Function>& disconnectedCallback, | 108 const v8::Local<v8::Function>& disconnectedCallback, |
| 109 const v8::Local<v8::Function>& adoptedCallback, |
109 const v8::Local<v8::Function>& attributeChangedCallback, | 110 const v8::Local<v8::Function>& attributeChangedCallback, |
110 const HashSet<AtomicString>& observedAttributes) | 111 const HashSet<AtomicString>& observedAttributes) |
111 { | 112 { |
112 ScriptCustomElementDefinition* definition = | 113 ScriptCustomElementDefinition* definition = |
113 new ScriptCustomElementDefinition( | 114 new ScriptCustomElementDefinition( |
114 scriptState, | 115 scriptState, |
115 descriptor, | 116 descriptor, |
116 constructor, | 117 constructor, |
117 prototype, | 118 prototype, |
118 connectedCallback, | 119 connectedCallback, |
119 disconnectedCallback, | 120 disconnectedCallback, |
| 121 adoptedCallback, |
120 attributeChangedCallback, | 122 attributeChangedCallback, |
121 observedAttributes); | 123 observedAttributes); |
122 | 124 |
123 // Add a constructor -> name mapping to the registry. | 125 // Add a constructor -> name mapping to the registry. |
124 v8::Local<v8::Value> nameValue = | 126 v8::Local<v8::Value> nameValue = |
125 v8String(scriptState->isolate(), descriptor.name()); | 127 v8String(scriptState->isolate(), descriptor.name()); |
126 v8::Local<v8::Map> map = | 128 v8::Local<v8::Map> map = |
127 ensureCustomElementsRegistryMap(scriptState, registry); | 129 ensureCustomElementsRegistryMap(scriptState, registry); |
128 map->Set(scriptState->context(), constructor, nameValue).ToLocalChecked(); | 130 map->Set(scriptState->context(), constructor, nameValue).ToLocalChecked(); |
129 definition->m_constructor.setPhantom(); | 131 definition->m_constructor.setPhantom(); |
130 | 132 |
131 // We add the prototype and callbacks here to keep them alive. We use the | 133 // We add the prototype and callbacks here to keep them alive. We use the |
132 // name as the key because it is unique per-registry. | 134 // name as the key because it is unique per-registry. |
133 v8::Local<v8::Array> array = v8::Array::New(scriptState->isolate(), 4); | 135 v8::Local<v8::Array> array = v8::Array::New(scriptState->isolate(), 5); |
134 keepAlive(array, 0, prototype, definition->m_prototype, scriptState); | 136 keepAlive(array, 0, prototype, definition->m_prototype, scriptState); |
135 keepAlive(array, 1, connectedCallback, definition->m_connectedCallback, scri
ptState); | 137 keepAlive(array, 1, connectedCallback, definition->m_connectedCallback, scri
ptState); |
136 keepAlive(array, 2, disconnectedCallback, definition->m_disconnectedCallback
, scriptState); | 138 keepAlive(array, 2, disconnectedCallback, definition->m_disconnectedCallback
, scriptState); |
137 keepAlive(array, 3, attributeChangedCallback, definition->m_attributeChanged
Callback, scriptState); | 139 keepAlive(array, 3, adoptedCallback, definition->m_adoptedCallback, scriptSt
ate); |
| 140 keepAlive(array, 4, attributeChangedCallback, definition->m_attributeChanged
Callback, scriptState); |
138 map->Set(scriptState->context(), nameValue, array).ToLocalChecked(); | 141 map->Set(scriptState->context(), nameValue, array).ToLocalChecked(); |
139 | 142 |
140 return definition; | 143 return definition; |
141 } | 144 } |
142 | 145 |
143 ScriptCustomElementDefinition::ScriptCustomElementDefinition( | 146 ScriptCustomElementDefinition::ScriptCustomElementDefinition( |
144 ScriptState* scriptState, | 147 ScriptState* scriptState, |
145 const CustomElementDescriptor& descriptor, | 148 const CustomElementDescriptor& descriptor, |
146 const v8::Local<v8::Object>& constructor, | 149 const v8::Local<v8::Object>& constructor, |
147 const v8::Local<v8::Object>& prototype, | 150 const v8::Local<v8::Object>& prototype, |
148 const v8::Local<v8::Function>& connectedCallback, | 151 const v8::Local<v8::Function>& connectedCallback, |
149 const v8::Local<v8::Function>& disconnectedCallback, | 152 const v8::Local<v8::Function>& disconnectedCallback, |
| 153 const v8::Local<v8::Function>& adoptedCallback, |
150 const v8::Local<v8::Function>& attributeChangedCallback, | 154 const v8::Local<v8::Function>& attributeChangedCallback, |
151 const HashSet<AtomicString>& observedAttributes) | 155 const HashSet<AtomicString>& observedAttributes) |
152 : CustomElementDefinition(descriptor, observedAttributes) | 156 : CustomElementDefinition(descriptor, observedAttributes) |
153 , m_scriptState(scriptState) | 157 , m_scriptState(scriptState) |
154 , m_constructor(scriptState->isolate(), constructor) | 158 , m_constructor(scriptState->isolate(), constructor) |
155 { | 159 { |
156 } | 160 } |
157 | 161 |
158 HTMLElement* ScriptCustomElementDefinition::createElementSync( | 162 HTMLElement* ScriptCustomElementDefinition::createElementSync( |
159 Document& document, const QualifiedName& tagName, | 163 Document& document, const QualifiedName& tagName, |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
223 // catch it. The side effect is to report the error. | 227 // catch it. The side effect is to report the error. |
224 v8::TryCatch tryCatch(isolate); | 228 v8::TryCatch tryCatch(isolate); |
225 tryCatch.SetVerbose(true); | 229 tryCatch.SetVerbose(true); |
226 | 230 |
227 Element* result = runConstructor(); | 231 Element* result = runConstructor(); |
228 | 232 |
229 // To report exception thrown from runConstructor() | 233 // To report exception thrown from runConstructor() |
230 if (tryCatch.HasCaught()) | 234 if (tryCatch.HasCaught()) |
231 return false; | 235 return false; |
232 | 236 |
233 // To report InvalidStateError Exception, when the constructor returns some
differnt object | 237 // To report InvalidStateError Exception, when the constructor returns some
different object |
234 if (result != element) { | 238 if (result != element) { |
235 const String& message = "custom element constructors must call super() f
irst and must " | 239 const String& message = "custom element constructors must call super() f
irst and must " |
236 "not return a different object"; | 240 "not return a different object"; |
237 | |
238 std::unique_ptr<SourceLocation> location = SourceLocation::fromFunction(
constructor().As<v8::Function>()); | 241 std::unique_ptr<SourceLocation> location = SourceLocation::fromFunction(
constructor().As<v8::Function>()); |
239 v8::Local<v8::Value> exception = V8ThrowException::createDOMException( | 242 v8::Local<v8::Value> exception = V8ThrowException::createDOMException( |
240 m_scriptState->isolate(), | 243 m_scriptState->isolate(), |
241 InvalidStateError, | 244 InvalidStateError, |
242 message, | 245 message, |
243 constructor()); | 246 constructor()); |
244 fireErrorEvent(m_scriptState.get(), message, exception, std::move(locati
on)); | 247 fireErrorEvent(m_scriptState.get(), message, exception, std::move(locati
on)); |
245 return false; | 248 return false; |
246 } | 249 } |
247 | 250 |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
295 bool ScriptCustomElementDefinition::hasConnectedCallback() const | 298 bool ScriptCustomElementDefinition::hasConnectedCallback() const |
296 { | 299 { |
297 return !m_connectedCallback.isEmpty(); | 300 return !m_connectedCallback.isEmpty(); |
298 } | 301 } |
299 | 302 |
300 bool ScriptCustomElementDefinition::hasDisconnectedCallback() const | 303 bool ScriptCustomElementDefinition::hasDisconnectedCallback() const |
301 { | 304 { |
302 return !m_disconnectedCallback.isEmpty(); | 305 return !m_disconnectedCallback.isEmpty(); |
303 } | 306 } |
304 | 307 |
| 308 bool ScriptCustomElementDefinition::hasAdoptedCallback() const |
| 309 { |
| 310 return !m_adoptedCallback.isEmpty(); |
| 311 } |
| 312 |
305 void ScriptCustomElementDefinition::runCallback( | 313 void ScriptCustomElementDefinition::runCallback( |
306 v8::Local<v8::Function> callback, | 314 v8::Local<v8::Function> callback, |
307 Element* element, int argc, v8::Local<v8::Value> argv[]) | 315 Element* element, int argc, v8::Local<v8::Value> argv[]) |
308 { | 316 { |
309 DCHECK(ScriptState::current(m_scriptState->isolate()) == m_scriptState); | 317 DCHECK(ScriptState::current(m_scriptState->isolate()) == m_scriptState); |
310 v8::Isolate* isolate = m_scriptState->isolate(); | 318 v8::Isolate* isolate = m_scriptState->isolate(); |
311 | 319 |
312 // Invoke custom element reactions | 320 // Invoke custom element reactions |
313 // https://html.spec.whatwg.org/multipage/scripting.html#invoke-custom-eleme
nt-reactions | 321 // https://html.spec.whatwg.org/multipage/scripting.html#invoke-custom-eleme
nt-reactions |
314 // If this throws any exception, then report the exception. | 322 // If this throws any exception, then report the exception. |
(...skipping 21 matching lines...) Expand all Loading... |
336 | 344 |
337 void ScriptCustomElementDefinition::runDisconnectedCallback(Element* element) | 345 void ScriptCustomElementDefinition::runDisconnectedCallback(Element* element) |
338 { | 346 { |
339 if (!m_scriptState->contextIsValid()) | 347 if (!m_scriptState->contextIsValid()) |
340 return; | 348 return; |
341 ScriptState::Scope scope(m_scriptState.get()); | 349 ScriptState::Scope scope(m_scriptState.get()); |
342 v8::Isolate* isolate = m_scriptState->isolate(); | 350 v8::Isolate* isolate = m_scriptState->isolate(); |
343 runCallback(m_disconnectedCallback.newLocal(isolate), element); | 351 runCallback(m_disconnectedCallback.newLocal(isolate), element); |
344 } | 352 } |
345 | 353 |
| 354 void ScriptCustomElementDefinition::runAdoptedCallback(Element* element) |
| 355 { |
| 356 if (!m_scriptState->contextIsValid()) |
| 357 return; |
| 358 ScriptState::Scope scope(m_scriptState.get()); |
| 359 v8::Isolate* isolate = m_scriptState->isolate(); |
| 360 runCallback(m_adoptedCallback.newLocal(isolate), element); |
| 361 } |
| 362 |
346 void ScriptCustomElementDefinition::runAttributeChangedCallback( | 363 void ScriptCustomElementDefinition::runAttributeChangedCallback( |
347 Element* element, const QualifiedName& name, | 364 Element* element, const QualifiedName& name, |
348 const AtomicString& oldValue, const AtomicString& newValue) | 365 const AtomicString& oldValue, const AtomicString& newValue) |
349 { | 366 { |
350 if (!m_scriptState->contextIsValid()) | 367 if (!m_scriptState->contextIsValid()) |
351 return; | 368 return; |
352 ScriptState::Scope scope(m_scriptState.get()); | 369 ScriptState::Scope scope(m_scriptState.get()); |
353 v8::Isolate* isolate = m_scriptState->isolate(); | 370 v8::Isolate* isolate = m_scriptState->isolate(); |
354 const int argc = 4; | 371 const int argc = 4; |
355 v8::Local<v8::Value> argv[argc] = { | 372 v8::Local<v8::Value> argv[argc] = { |
356 v8String(isolate, name.localName()), | 373 v8String(isolate, name.localName()), |
357 v8StringOrNull(isolate, oldValue), | 374 v8StringOrNull(isolate, oldValue), |
358 v8StringOrNull(isolate, newValue), | 375 v8StringOrNull(isolate, newValue), |
359 v8String(isolate, name.namespaceURI()), | 376 v8String(isolate, name.namespaceURI()), |
360 }; | 377 }; |
361 runCallback(m_attributeChangedCallback.newLocal(isolate), element, | 378 runCallback(m_attributeChangedCallback.newLocal(isolate), element, |
362 argc, argv); | 379 argc, argv); |
363 } | 380 } |
364 | 381 |
365 } // namespace blink | 382 } // namespace blink |
OLD | NEW |