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