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 HashSet<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 definition->m_constructor.setPhantom(); |
| 98 // not a key so authors cannot return another constructor as a | 120 |
| 99 // prototype to overwrite a constructor in this map. We use the | 121 // We add the prototype and callbacks here to keep them alive. We use the |
| 100 // name because it is unique per-registry. | 122 // name as the key because it is unique per-registry. |
| 101 v8CallOrCrash(map->Set(scriptState->context(), nameValue, prototype)); | 123 v8::Local<v8::Array> array = v8::Array::New(scriptState->isolate(), 4); |
| 124 #define V(index, name) \ | |
|
yosin_UTC9
2016/06/02 04:30:31
No need to use macro, it is hard to read.
Name |V
kojii
2016/06/02 04:47:13
Done, reverted the macro as per your suggestion.
| |
| 125 keepAliveIfNotEmpty(array, index, name, definition->m_##name, scriptState) | |
| 126 V(0, prototype); | |
| 127 V(1, connectedCallback); | |
| 128 V(2, disconnectedCallback); | |
| 129 V(3, attributeChangedCallback); | |
| 130 #undef V | |
| 131 v8CallOrCrash(map->Set(scriptState->context(), nameValue, array)); | |
| 102 | 132 |
| 103 return definition; | 133 return definition; |
| 104 } | 134 } |
| 105 | 135 |
| 106 ScriptCustomElementDefinition::ScriptCustomElementDefinition( | 136 ScriptCustomElementDefinition::ScriptCustomElementDefinition( |
| 107 ScriptState* scriptState, | 137 ScriptState* scriptState, |
| 108 const CustomElementDescriptor& descriptor, | 138 const CustomElementDescriptor& descriptor, |
| 109 const v8::Local<v8::Object>& constructor, | 139 const v8::Local<v8::Object>& constructor, |
| 110 const v8::Local<v8::Object>& prototype) | 140 const v8::Local<v8::Object>& prototype, |
| 141 const v8::Local<v8::Object>& connectedCallback, | |
| 142 const v8::Local<v8::Object>& disconnectedCallback, | |
| 143 const v8::Local<v8::Object>& attributeChangedCallback, | |
| 144 const HashSet<AtomicString>& observedAttributes) | |
| 111 : CustomElementDefinition(descriptor) | 145 : CustomElementDefinition(descriptor) |
| 112 , m_scriptState(scriptState) | 146 , m_scriptState(scriptState) |
| 113 , m_constructor(scriptState->isolate(), constructor) | 147 , m_constructor(scriptState->isolate(), constructor) |
| 114 , m_prototype(scriptState->isolate(), prototype) | 148 , m_observedAttributes(observedAttributes) |
| 115 { | 149 { |
| 116 // These objects are kept alive by references from the | |
| 117 // CustomElementsRegistry wrapper set up by | |
| 118 // ScriptCustomElementDefinition::create. | |
| 119 m_constructor.setPhantom(); | |
| 120 m_prototype.setPhantom(); | |
| 121 } | 150 } |
| 122 | 151 |
| 123 v8::Local<v8::Object> ScriptCustomElementDefinition::constructor() const | 152 v8::Local<v8::Object> ScriptCustomElementDefinition::constructor() const |
| 124 { | 153 { |
| 125 DCHECK(!m_constructor.isEmpty()); | 154 DCHECK(!m_constructor.isEmpty()); |
| 126 return m_constructor.newLocal(m_scriptState->isolate()); | 155 return m_constructor.newLocal(m_scriptState->isolate()); |
| 127 } | 156 } |
| 128 | 157 |
| 129 v8::Local<v8::Object> ScriptCustomElementDefinition::prototype() const | 158 v8::Local<v8::Object> ScriptCustomElementDefinition::prototype() const |
| 130 { | 159 { |
| 131 DCHECK(!m_prototype.isEmpty()); | 160 DCHECK(!m_prototype.isEmpty()); |
| 132 return m_prototype.newLocal(m_scriptState->isolate()); | 161 return m_prototype.newLocal(m_scriptState->isolate()); |
| 133 } | 162 } |
| 134 | 163 |
| 135 } // namespace blink | 164 } // namespace blink |
| OLD | NEW |