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/ScriptCustomElementDefinitionBuilder.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/V8CustomElementsRegistry.h" | 10 #include "bindings/core/v8/V8CustomElementsRegistry.h" |
| 10 #include "bindings/core/v8/V8HiddenValue.h" | 11 #include "bindings/core/v8/V8HiddenValue.h" |
| 11 #include "wtf/Allocator.h" | 12 #include "wtf/Allocator.h" |
| 12 | 13 |
| 13 namespace blink { | 14 namespace blink { |
| 14 | 15 |
| 15 // Retrieves the custom elements constructor -> name map, creating it | 16 // Retrieves the custom elements constructor -> name map, creating it |
| 16 // if necessary. The same map is used to keep prototypes alive. | 17 // if necessary. The same map is used to keep prototypes alive. |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 67 // | 68 // |
| 68 // At a meta-level, this downcast is safe because there is | 69 // At a meta-level, this downcast is safe because there is |
| 69 // currently only one implementation of CustomElementDefinition in | 70 // currently only one implementation of CustomElementDefinition in |
| 70 // product code and that is ScriptCustomElementDefinition. But | 71 // product code and that is ScriptCustomElementDefinition. But |
| 71 // that may change in the future. | 72 // that may change in the future. |
| 72 CustomElementDefinition* definition = registry->definitionForName(name); | 73 CustomElementDefinition* definition = registry->definitionForName(name); |
| 73 CHECK(definition); | 74 CHECK(definition); |
| 74 return static_cast<ScriptCustomElementDefinition*>(definition); | 75 return static_cast<ScriptCustomElementDefinition*>(definition); |
| 75 } | 76 } |
| 76 | 77 |
| 78 static void keepAliveIfNotEmpty(v8::Local<v8::Array>& array, uint32_t index, | |
| 79 const v8::Local<v8::Object>& value, | |
| 80 ScopedPersistent<v8::Object>& persistent, | |
| 81 ScriptState* scriptState) | |
| 82 { | |
| 83 if (value.IsEmpty()) | |
| 84 return; | |
| 85 | |
| 86 v8CallOrCrash(array->Set(scriptState->context(), index, value)); | |
| 87 | |
| 88 persistent.set(scriptState->isolate(), value); | |
| 89 persistent.setPhantom(); | |
| 90 } | |
| 91 | |
| 77 ScriptCustomElementDefinition* ScriptCustomElementDefinition::create( | 92 ScriptCustomElementDefinition* ScriptCustomElementDefinition::create( |
| 78 ScriptState* scriptState, | |
| 79 CustomElementsRegistry* registry, | |
| 80 const CustomElementDescriptor& descriptor, | 93 const CustomElementDescriptor& descriptor, |
| 81 const v8::Local<v8::Object>& constructor, | 94 const ScriptCustomElementDefinitionBuilder& builder) |
| 82 const v8::Local<v8::Object>& prototype) | |
| 83 { | 95 { |
| 84 ScriptCustomElementDefinition* definition = | 96 ScriptCustomElementDefinition* definition = |
| 85 new ScriptCustomElementDefinition( | 97 new ScriptCustomElementDefinition(descriptor, builder); |
| 86 scriptState, | |
| 87 descriptor, | |
| 88 constructor, | |
| 89 prototype); | |
| 90 | 98 |
| 91 // Add a constructor -> name mapping to the registry. | 99 // Add a constructor -> name mapping to the registry. |
| 100 ScriptState* scriptState = builder.m_scriptState.get(); | |
| 92 v8::Local<v8::Value> nameValue = | 101 v8::Local<v8::Value> nameValue = |
| 93 v8String(scriptState->isolate(), descriptor.name()); | 102 v8String(scriptState->isolate(), descriptor.name()); |
| 94 v8::Local<v8::Map> map = | 103 v8::Local<v8::Map> map = |
| 95 ensureCustomElementsRegistryMap(scriptState, registry); | 104 ensureCustomElementsRegistryMap(scriptState, builder.m_registry); |
| 96 v8CallOrCrash(map->Set(scriptState->context(), constructor, nameValue)); | 105 v8CallOrCrash(map->Set(scriptState->context(), builder.m_constructor, nameVa lue)); |
| 97 // We add the prototype here to keep it alive; we make it a value | 106 |
| 98 // not a key so authors cannot return another constructor as a | 107 // We add the prototype and callbacks here to keep them alive. We use the |
| 99 // prototype to overwrite a constructor in this map. We use the | 108 // name as the key because it is unique per-registry. |
| 100 // name because it is unique per-registry. | 109 v8::Local<v8::Array> array = v8::Array::New(scriptState->isolate(), 4); |
|
yosin_UTC9
2016/06/02 01:33:37
I'll add "whenDefinedPromise" here, so JS side val
| |
| 101 v8CallOrCrash(map->Set(scriptState->context(), nameValue, prototype)); | 110 v8CallOrCrash(array->Set(scriptState->context(), 0, builder.m_prototype)); |
|
dominicc (has gone to gerrit)
2016/06/02 00:31:27
Why not use keepAliveIfNotEmpty for m_prototype to
kojii
2016/06/02 03:18:08
Done.
| |
| 111 #define V(index, name) \ | |
|
yosin_UTC9
2016/06/02 01:33:38
We should have FOR_EACH_XXX() and it should be use
kojii
2016/06/02 03:18:08
I know, but I don't like it, esp. when the list is
yosin_UTC9
2016/06/02 04:30:31
Since list is used for parameter of ctor, it might
| |
| 112 keepAliveIfNotEmpty(array, index, builder.m_##name, definition->m_##name, sc riptState) | |
| 113 V(1, connectedCallback); | |
| 114 V(2, disconnectedCallback); | |
| 115 V(3, attributeChangedCallback); | |
| 116 #undef V | |
| 117 v8CallOrCrash(map->Set(scriptState->context(), nameValue, array)); | |
| 102 | 118 |
| 103 return definition; | 119 return definition; |
| 104 } | 120 } |
| 105 | 121 |
| 106 ScriptCustomElementDefinition::ScriptCustomElementDefinition( | 122 ScriptCustomElementDefinition::ScriptCustomElementDefinition( |
| 107 ScriptState* scriptState, | |
| 108 const CustomElementDescriptor& descriptor, | 123 const CustomElementDescriptor& descriptor, |
| 109 const v8::Local<v8::Object>& constructor, | 124 const ScriptCustomElementDefinitionBuilder& builder) |
| 110 const v8::Local<v8::Object>& prototype) | |
| 111 : CustomElementDefinition(descriptor) | 125 : CustomElementDefinition(descriptor) |
| 112 , m_scriptState(scriptState) | 126 , m_scriptState(builder.m_scriptState) |
| 113 , m_constructor(scriptState->isolate(), constructor) | 127 , m_constructor(builder.m_scriptState->isolate(), builder.m_constructor) |
|
dominicc (has gone to gerrit)
2016/06/02 00:31:27
I guess you could just use m_scriptState at this p
kojii
2016/06/02 03:18:08
This was reverted, see comments in ScriptCustomEle
| |
| 114 , m_prototype(scriptState->isolate(), prototype) | 128 , m_prototype(builder.m_scriptState->isolate(), builder.m_prototype) |
| 115 { | 129 { |
| 116 // These objects are kept alive by references from the | 130 // These objects are kept alive by references from the |
| 117 // CustomElementsRegistry wrapper set up by | 131 // CustomElementsRegistry wrapper set up by |
| 118 // ScriptCustomElementDefinition::create. | 132 // ScriptCustomElementDefinition::create. |
| 119 m_constructor.setPhantom(); | 133 m_constructor.setPhantom(); |
| 120 m_prototype.setPhantom(); | 134 m_prototype.setPhantom(); |
| 135 | |
| 136 if (builder.m_observedAttributes.isEmpty()) | |
| 137 return; | |
| 138 m_observedAttributes.reserveCapacityForSize(builder.m_observedAttributes.siz e()); | |
| 139 for (const auto& attribute : builder.m_observedAttributes) | |
| 140 m_observedAttributes.add(attribute); | |
| 121 } | 141 } |
| 122 | 142 |
| 123 v8::Local<v8::Object> ScriptCustomElementDefinition::constructor() const | 143 v8::Local<v8::Object> ScriptCustomElementDefinition::constructor() const |
| 124 { | 144 { |
| 125 DCHECK(!m_constructor.isEmpty()); | 145 DCHECK(!m_constructor.isEmpty()); |
| 126 return m_constructor.newLocal(m_scriptState->isolate()); | 146 return m_constructor.newLocal(m_scriptState->isolate()); |
| 127 } | 147 } |
| 128 | 148 |
| 129 v8::Local<v8::Object> ScriptCustomElementDefinition::prototype() const | 149 v8::Local<v8::Object> ScriptCustomElementDefinition::prototype() const |
| 130 { | 150 { |
| 131 DCHECK(!m_prototype.isEmpty()); | 151 DCHECK(!m_prototype.isEmpty()); |
| 132 return m_prototype.newLocal(m_scriptState->isolate()); | 152 return m_prototype.newLocal(m_scriptState->isolate()); |
| 133 } | 153 } |
| 134 | 154 |
| 135 } // namespace blink | 155 } // namespace blink |
| OLD | NEW |