Chromium Code Reviews| 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) |
| 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 { |
| 154 m_observedAttributes = observedAttributes; | |
|
dominicc (has gone to gerrit)
2016/06/14 09:19:45
We should make the CustomElementDefinition take ob
| |
| 153 } | 155 } |
| 154 | 156 |
| 155 // https://html.spec.whatwg.org/multipage/scripting.html#upgrades | 157 // https://html.spec.whatwg.org/multipage/scripting.html#upgrades |
| 156 bool ScriptCustomElementDefinition::runConstructor(Element* element) | 158 bool ScriptCustomElementDefinition::runConstructor(Element* element) |
| 157 { | 159 { |
| 158 if (!m_scriptState->contextIsValid()) | 160 if (!m_scriptState->contextIsValid()) |
| 159 return false; | 161 return false; |
| 160 ScriptState::Scope scope(m_scriptState.get()); | 162 ScriptState::Scope scope(m_scriptState.get()); |
| 161 v8::Isolate* isolate = m_scriptState->isolate(); | 163 v8::Isolate* isolate = m_scriptState->isolate(); |
| 162 | 164 |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 202 DCHECK(!m_prototype.isEmpty()); | 204 DCHECK(!m_prototype.isEmpty()); |
| 203 return m_prototype.newLocal(m_scriptState->isolate()); | 205 return m_prototype.newLocal(m_scriptState->isolate()); |
| 204 } | 206 } |
| 205 | 207 |
| 206 // CustomElementDefinition | 208 // CustomElementDefinition |
| 207 ScriptValue ScriptCustomElementDefinition::getConstructorForScript() | 209 ScriptValue ScriptCustomElementDefinition::getConstructorForScript() |
| 208 { | 210 { |
| 209 return ScriptValue(m_scriptState.get(), constructor()); | 211 return ScriptValue(m_scriptState.get(), constructor()); |
| 210 } | 212 } |
| 211 | 213 |
| 214 bool ScriptCustomElementDefinition::hasConnectedCallback() const | |
| 215 { | |
| 216 return !m_connectedCallback.isEmpty(); | |
| 217 } | |
| 218 | |
| 219 bool ScriptCustomElementDefinition::hasDisconnectedCallback() const | |
| 220 { | |
| 221 return !m_disconnectedCallback.isEmpty(); | |
| 222 } | |
| 223 | |
| 224 void ScriptCustomElementDefinition::runCallback( | |
| 225 ScopedPersistent<v8::Function>& callback, | |
| 226 Element* element, int argc, v8::Local<v8::Value> argv[]) | |
| 227 { | |
| 228 if (!m_scriptState->contextIsValid()) | |
| 229 return; | |
| 230 ScriptState::Scope scope(m_scriptState.get()); | |
| 231 v8::Isolate* isolate = m_scriptState->isolate(); | |
| 232 | |
| 233 // Invoke custom element reactions | |
| 234 // https://html.spec.whatwg.org/multipage/scripting.html#invoke-custom-eleme nt-reactions | |
| 235 // If this throws any exception, then report the exception. | |
| 236 v8::TryCatch tryCatch(isolate); | |
| 237 tryCatch.SetVerbose(true); | |
| 238 | |
| 239 ExecutionContext* executionContext = m_scriptState->getExecutionContext(); | |
| 240 v8::Local<v8::Value> elementHandle = toV8(element, | |
| 241 m_scriptState->context()->Global(), isolate); | |
| 242 ScriptController::callFunction( | |
| 243 executionContext, | |
| 244 callback.newLocal(isolate), | |
| 245 elementHandle, | |
| 246 argc, argv, isolate); | |
| 247 } | |
| 248 | |
| 249 void ScriptCustomElementDefinition::runConnectedCallback(Element* element) | |
| 250 { | |
| 251 runCallback(m_connectedCallback, element); | |
| 252 } | |
| 253 | |
| 254 void ScriptCustomElementDefinition::runDisconnectedCallback(Element* element) | |
| 255 { | |
| 256 runCallback(m_disconnectedCallback, element); | |
| 257 } | |
| 258 | |
| 259 static v8::Local<v8::Value> v8StringOrNull(v8::Isolate* isolate, | |
|
dominicc (has gone to gerrit)
2016/06/14 09:19:45
This is a nice little helper; maybe this should li
| |
| 260 const AtomicString& value) | |
| 261 { | |
| 262 if (value.isNull()) | |
| 263 return v8::Null(isolate); | |
| 264 return v8String(isolate, value); | |
| 265 } | |
| 266 | |
| 267 void ScriptCustomElementDefinition::runAttributeChangedCallback( | |
| 268 Element* element, const QualifiedName& name, | |
| 269 const AtomicString& oldValue, const AtomicString& newValue) | |
| 270 { | |
| 271 v8::Isolate* isolate = m_scriptState->isolate(); | |
|
dominicc (has gone to gerrit)
2016/06/14 09:19:45
This needs to set up the ScriptState::Scope just l
| |
| 272 const int argc = 4; | |
| 273 v8::Local<v8::Value> argv[argc] = { | |
| 274 v8String(isolate, name.localName()), | |
| 275 v8StringOrNull(isolate, oldValue), | |
| 276 v8StringOrNull(isolate, newValue), | |
| 277 v8String(isolate, name.namespaceURI()), | |
| 278 }; | |
| 279 runCallback(m_attributeChangedCallback, element, argc, argv); | |
| 280 } | |
| 281 | |
| 212 } // namespace blink | 282 } // namespace blink |
| OLD | NEW |