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/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/V8CustomElementsRegistry.h" | 9 #include "bindings/core/v8/V8CustomElementsRegistry.h" |
| 10 #include "bindings/core/v8/V8HiddenValue.h" | 10 #include "bindings/core/v8/V8HiddenValue.h" |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 67 // | 67 // |
| 68 // At a meta-level, this downcast is safe because there is | 68 // At a meta-level, this downcast is safe because there is |
| 69 // currently only one implementation of CustomElementDefinition in | 69 // currently only one implementation of CustomElementDefinition in |
| 70 // product code and that is ScriptCustomElementDefinition. But | 70 // product code and that is ScriptCustomElementDefinition. But |
| 71 // that may change in the future. | 71 // that may change in the future. |
| 72 CustomElementDefinition* definition = registry->definitionForName(name); | 72 CustomElementDefinition* definition = registry->definitionForName(name); |
| 73 CHECK(definition); | 73 CHECK(definition); |
| 74 return static_cast<ScriptCustomElementDefinition*>(definition); | 74 return static_cast<ScriptCustomElementDefinition*>(definition); |
| 75 } | 75 } |
| 76 | 76 |
| 77 static void keepAliveIfNotEmpty(v8::Local<v8::Array>& array, uint32_t index, | |
| 78 const v8::Local<v8::Object>& value, | |
| 79 ScopedPersistent<v8::Object>& persistent, | |
| 80 ScriptState* scriptState) | |
| 81 { | |
| 82 if (value.IsEmpty()) | |
| 83 return; | |
| 84 | |
| 85 v8CallOrCrash(array->Set(scriptState->context(), index, value)); | |
| 86 | |
| 87 persistent.set(scriptState->isolate(), value); | |
| 88 persistent.setPhantom(); | |
| 89 } | |
| 90 | |
| 77 ScriptCustomElementDefinition* ScriptCustomElementDefinition::create( | 91 ScriptCustomElementDefinition* ScriptCustomElementDefinition::create( |
| 78 ScriptState* scriptState, | 92 ScriptState* scriptState, |
| 79 CustomElementsRegistry* registry, | 93 CustomElementsRegistry* registry, |
| 80 const CustomElementDescriptor& descriptor, | 94 const CustomElementDescriptor& descriptor, |
| 81 const v8::Local<v8::Object>& constructor, | 95 const v8::Local<v8::Object>& constructor, |
| 82 const v8::Local<v8::Object>& prototype) | 96 const v8::Local<v8::Object>& prototype, |
| 97 const v8::Local<v8::Object>& connectedCallback, | |
| 98 const v8::Local<v8::Object>& disconnectedCallback, | |
| 99 const v8::Local<v8::Object>& attributeChangedCallback, | |
| 100 const Vector<AtomicString>& observedAttributes) | |
| 83 { | 101 { |
| 84 ScriptCustomElementDefinition* definition = | 102 ScriptCustomElementDefinition* definition = |
| 85 new ScriptCustomElementDefinition( | 103 new ScriptCustomElementDefinition( |
| 86 scriptState, | 104 scriptState, |
| 87 descriptor, | 105 descriptor, |
| 88 constructor, | 106 constructor, |
| 89 prototype); | 107 prototype, |
| 108 connectedCallback, | |
| 109 disconnectedCallback, | |
| 110 attributeChangedCallback, | |
| 111 observedAttributes); | |
| 90 | 112 |
| 91 // Add a constructor -> name mapping to the registry. | 113 // Add a constructor -> name mapping to the registry. |
| 92 v8::Local<v8::Value> nameValue = | 114 v8::Local<v8::Value> nameValue = |
| 93 v8String(scriptState->isolate(), descriptor.name()); | 115 v8String(scriptState->isolate(), descriptor.name()); |
| 94 v8::Local<v8::Map> map = | 116 v8::Local<v8::Map> map = |
| 95 ensureCustomElementsRegistryMap(scriptState, registry); | 117 ensureCustomElementsRegistryMap(scriptState, registry); |
| 96 v8CallOrCrash(map->Set(scriptState->context(), constructor, nameValue)); | 118 v8CallOrCrash(map->Set(scriptState->context(), constructor, nameValue)); |
| 97 // We add the prototype here to keep it alive; we make it a value | 119 // We add the prototype and callbacks here to keep them alive. We use the |
| 98 // not a key so authors cannot return another constructor as a | 120 // name as the key because it is unique per-registry. |
| 99 // prototype to overwrite a constructor in this map. We use the | 121 v8::Local<v8::Array> array = v8::Array::New(scriptState->isolate(), 4); |
|
yosin_UTC9
2016/06/01 06:33:33
Could you introduce enum for array index?
Or, use
kojii
2016/06/01 07:19:27
Done, not my favorite, but I can live with it.
| |
| 100 // name because it is unique per-registry. | 122 v8CallOrCrash(array->Set(scriptState->context(), 0, prototype)); |
| 101 v8CallOrCrash(map->Set(scriptState->context(), nameValue, prototype)); | 123 keepAliveIfNotEmpty(array, 1, connectedCallback, |
| 124 definition->m_connectedCallback, scriptState); | |
| 125 keepAliveIfNotEmpty(array, 2, disconnectedCallback, | |
| 126 definition->m_disconnectedCallback, scriptState); | |
| 127 keepAliveIfNotEmpty(array, 3, attributeChangedCallback, | |
| 128 definition->m_attributeChangedCallback, scriptState); | |
| 129 v8CallOrCrash(map->Set(scriptState->context(), nameValue, array)); | |
| 102 | 130 |
| 103 return definition; | 131 return definition; |
| 104 } | 132 } |
| 105 | 133 |
| 106 ScriptCustomElementDefinition::ScriptCustomElementDefinition( | 134 ScriptCustomElementDefinition::ScriptCustomElementDefinition( |
| 107 ScriptState* scriptState, | 135 ScriptState* scriptState, |
| 108 const CustomElementDescriptor& descriptor, | 136 const CustomElementDescriptor& descriptor, |
| 109 const v8::Local<v8::Object>& constructor, | 137 const v8::Local<v8::Object>& constructor, |
| 110 const v8::Local<v8::Object>& prototype) | 138 const v8::Local<v8::Object>& prototype, |
| 139 const v8::Local<v8::Object>& connectedCallback, | |
| 140 const v8::Local<v8::Object>& disconnectedCallback, | |
| 141 const v8::Local<v8::Object>& attributeChangedCallback, | |
| 142 const Vector<AtomicString>& observedAttributes) | |
| 111 : CustomElementDefinition(descriptor) | 143 : CustomElementDefinition(descriptor) |
| 112 , m_constructor(scriptState->isolate(), constructor) | 144 , m_constructor(scriptState->isolate(), constructor) |
| 113 , m_prototype(scriptState->isolate(), prototype) | 145 , m_prototype(scriptState->isolate(), prototype) |
| 114 { | 146 { |
| 115 // These objects are kept alive by references from the | 147 // These objects are kept alive by references from the |
| 116 // CustomElementsRegistry wrapper set up by | 148 // CustomElementsRegistry wrapper set up by |
| 117 // ScriptCustomElementDefinition::create. | 149 // ScriptCustomElementDefinition::create. |
| 118 m_constructor.setPhantom(); | 150 m_constructor.setPhantom(); |
| 119 m_prototype.setPhantom(); | 151 m_prototype.setPhantom(); |
| 152 | |
| 153 if (!observedAttributes.isEmpty()) { | |
|
yosin_UTC9
2016/06/01 06:33:33
Prefer early-return style
kojii
2016/06/01 07:19:27
Done.
| |
| 154 m_observedAttributes.reserveCapacityForSize(observedAttributes.size()); | |
| 155 for (const auto& attribute : observedAttributes) | |
| 156 m_observedAttributes.add(attribute); | |
| 157 } | |
| 120 } | 158 } |
| 121 | 159 |
| 122 v8::Local<v8::Object> ScriptCustomElementDefinition::constructor( | 160 v8::Local<v8::Object> ScriptCustomElementDefinition::constructor( |
| 123 ScriptState* scriptState) const | 161 ScriptState* scriptState) const |
| 124 { | 162 { |
| 125 DCHECK(!m_constructor.isEmpty()); | 163 DCHECK(!m_constructor.isEmpty()); |
| 126 return m_constructor.newLocal(scriptState->isolate()); | 164 return m_constructor.newLocal(scriptState->isolate()); |
| 127 } | 165 } |
| 128 | 166 |
| 129 v8::Local<v8::Object> ScriptCustomElementDefinition::prototype( | 167 v8::Local<v8::Object> ScriptCustomElementDefinition::prototype( |
| 130 ScriptState* scriptState) const | 168 ScriptState* scriptState) const |
| 131 { | 169 { |
| 132 DCHECK(!m_prototype.isEmpty()); | 170 DCHECK(!m_prototype.isEmpty()); |
| 133 return m_prototype.newLocal(scriptState->isolate()); | 171 return m_prototype.newLocal(scriptState->isolate()); |
| 134 } | 172 } |
| 135 | 173 |
| 136 } // namespace blink | 174 } // namespace blink |
| OLD | NEW |