| 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 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 73 // | 73 // |
| 74 // At a meta-level, this downcast is safe because there is | 74 // At a meta-level, this downcast is safe because there is |
| 75 // currently only one implementation of CustomElementDefinition in | 75 // currently only one implementation of CustomElementDefinition in |
| 76 // product code and that is ScriptCustomElementDefinition. But | 76 // product code and that is ScriptCustomElementDefinition. But |
| 77 // that may change in the future. | 77 // that may change in the future. |
| 78 CustomElementDefinition* definition = registry->definitionForName(name); | 78 CustomElementDefinition* definition = registry->definitionForName(name); |
| 79 CHECK(definition); | 79 CHECK(definition); |
| 80 return static_cast<ScriptCustomElementDefinition*>(definition); | 80 return static_cast<ScriptCustomElementDefinition*>(definition); |
| 81 } | 81 } |
| 82 | 82 |
| 83 static void keepAlive(v8::Local<v8::Array>& array, uint32_t index, |
| 84 const v8::Local<v8::Object>& value, |
| 85 ScopedPersistent<v8::Object>& persistent, |
| 86 ScriptState* scriptState) |
| 87 { |
| 88 if (value.IsEmpty()) |
| 89 return; |
| 90 |
| 91 v8CallOrCrash(array->Set(scriptState->context(), index, value)); |
| 92 |
| 93 persistent.set(scriptState->isolate(), value); |
| 94 persistent.setPhantom(); |
| 95 } |
| 96 |
| 83 ScriptCustomElementDefinition* ScriptCustomElementDefinition::create( | 97 ScriptCustomElementDefinition* ScriptCustomElementDefinition::create( |
| 84 ScriptState* scriptState, | 98 ScriptState* scriptState, |
| 85 CustomElementsRegistry* registry, | 99 CustomElementsRegistry* registry, |
| 86 const CustomElementDescriptor& descriptor, | 100 const CustomElementDescriptor& descriptor, |
| 87 const v8::Local<v8::Object>& constructor, | 101 const v8::Local<v8::Object>& constructor, |
| 88 const v8::Local<v8::Object>& prototype) | 102 const v8::Local<v8::Object>& prototype, |
| 103 const v8::Local<v8::Object>& connectedCallback, |
| 104 const v8::Local<v8::Object>& disconnectedCallback, |
| 105 const v8::Local<v8::Object>& attributeChangedCallback, |
| 106 const HashSet<AtomicString>& observedAttributes) |
| 89 { | 107 { |
| 90 ScriptCustomElementDefinition* definition = | 108 ScriptCustomElementDefinition* definition = |
| 91 new ScriptCustomElementDefinition( | 109 new ScriptCustomElementDefinition( |
| 92 scriptState, | 110 scriptState, |
| 93 descriptor, | 111 descriptor, |
| 94 constructor, | 112 constructor, |
| 95 prototype); | 113 prototype, |
| 114 connectedCallback, |
| 115 disconnectedCallback, |
| 116 attributeChangedCallback, |
| 117 observedAttributes); |
| 96 | 118 |
| 97 // Add a constructor -> name mapping to the registry. | 119 // Add a constructor -> name mapping to the registry. |
| 98 v8::Local<v8::Value> nameValue = | 120 v8::Local<v8::Value> nameValue = |
| 99 v8String(scriptState->isolate(), descriptor.name()); | 121 v8String(scriptState->isolate(), descriptor.name()); |
| 100 v8::Local<v8::Map> map = | 122 v8::Local<v8::Map> map = |
| 101 ensureCustomElementsRegistryMap(scriptState, registry); | 123 ensureCustomElementsRegistryMap(scriptState, registry); |
| 102 v8CallOrCrash(map->Set(scriptState->context(), constructor, nameValue)); | 124 v8CallOrCrash(map->Set(scriptState->context(), constructor, nameValue)); |
| 103 // We add the prototype here to keep it alive; we make it a value | 125 definition->m_constructor.setPhantom(); |
| 104 // not a key so authors cannot return another constructor as a | 126 |
| 105 // prototype to overwrite a constructor in this map. We use the | 127 // We add the prototype and callbacks here to keep them alive. We use the |
| 106 // name because it is unique per-registry. | 128 // name as the key because it is unique per-registry. |
| 107 v8CallOrCrash(map->Set(scriptState->context(), nameValue, prototype)); | 129 v8::Local<v8::Array> array = v8::Array::New(scriptState->isolate(), 4); |
| 130 keepAlive(array, 0, prototype, definition->m_prototype, scriptState); |
| 131 keepAlive(array, 1, connectedCallback, definition->m_connectedCallback, scri
ptState); |
| 132 keepAlive(array, 2, disconnectedCallback, definition->m_disconnectedCallback
, scriptState); |
| 133 keepAlive(array, 3, attributeChangedCallback, definition->m_attributeChanged
Callback, scriptState); |
| 134 v8CallOrCrash(map->Set(scriptState->context(), nameValue, array)); |
| 108 | 135 |
| 109 return definition; | 136 return definition; |
| 110 } | 137 } |
| 111 | 138 |
| 112 ScriptCustomElementDefinition::ScriptCustomElementDefinition( | 139 ScriptCustomElementDefinition::ScriptCustomElementDefinition( |
| 113 ScriptState* scriptState, | 140 ScriptState* scriptState, |
| 114 const CustomElementDescriptor& descriptor, | 141 const CustomElementDescriptor& descriptor, |
| 115 const v8::Local<v8::Object>& constructor, | 142 const v8::Local<v8::Object>& constructor, |
| 116 const v8::Local<v8::Object>& prototype) | 143 const v8::Local<v8::Object>& prototype, |
| 144 const v8::Local<v8::Object>& connectedCallback, |
| 145 const v8::Local<v8::Object>& disconnectedCallback, |
| 146 const v8::Local<v8::Object>& attributeChangedCallback, |
| 147 const HashSet<AtomicString>& observedAttributes) |
| 117 : CustomElementDefinition(descriptor) | 148 : CustomElementDefinition(descriptor) |
| 118 , m_scriptState(scriptState) | 149 , m_scriptState(scriptState) |
| 119 , m_constructor(scriptState->isolate(), constructor) | 150 , m_constructor(scriptState->isolate(), constructor) |
| 120 , m_prototype(scriptState->isolate(), prototype) | 151 , m_observedAttributes(observedAttributes) |
| 121 { | 152 { |
| 122 // These objects are kept alive by references from the | |
| 123 // CustomElementsRegistry wrapper set up by | |
| 124 // ScriptCustomElementDefinition::create. | |
| 125 m_constructor.setPhantom(); | |
| 126 m_prototype.setPhantom(); | |
| 127 } | 153 } |
| 128 | 154 |
| 129 // https://html.spec.whatwg.org/multipage/scripting.html#upgrades | 155 // https://html.spec.whatwg.org/multipage/scripting.html#upgrades |
| 130 bool ScriptCustomElementDefinition::runConstructor(Element* element) | 156 bool ScriptCustomElementDefinition::runConstructor(Element* element) |
| 131 { | 157 { |
| 132 if (!m_scriptState->contextIsValid()) | 158 if (!m_scriptState->contextIsValid()) |
| 133 return false; | 159 return false; |
| 134 ScriptState::Scope scope(m_scriptState.get()); | 160 ScriptState::Scope scope(m_scriptState.get()); |
| 135 v8::Isolate* isolate = m_scriptState->isolate(); | 161 v8::Isolate* isolate = m_scriptState->isolate(); |
| 136 | 162 |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 177 return m_prototype.newLocal(m_scriptState->isolate()); | 203 return m_prototype.newLocal(m_scriptState->isolate()); |
| 178 } | 204 } |
| 179 | 205 |
| 180 // CustomElementDefinition | 206 // CustomElementDefinition |
| 181 ScriptValue ScriptCustomElementDefinition::getConstructorForScript() | 207 ScriptValue ScriptCustomElementDefinition::getConstructorForScript() |
| 182 { | 208 { |
| 183 return ScriptValue(m_scriptState.get(), constructor()); | 209 return ScriptValue(m_scriptState.get(), constructor()); |
| 184 } | 210 } |
| 185 | 211 |
| 186 } // namespace blink | 212 } // namespace blink |
| OLD | NEW |