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 keepAlive(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(); | |
haraken
2016/06/06 01:39:15
Why do you need to create a phantom handle? I thin
kojii
2016/06/06 11:12:34
This is for perf to retrieve the value without goi
| |
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 keepAlive(array, 0, prototype, definition->m_prototype, scriptState); | |
125 keepAlive(array, 1, connectedCallback, definition->m_connectedCallback, scri ptState); | |
126 keepAlive(array, 2, disconnectedCallback, definition->m_disconnectedCallback , scriptState); | |
127 keepAlive(array, 3, attributeChangedCallback, definition->m_attributeChanged Callback, scriptState); | |
128 v8CallOrCrash(map->Set(scriptState->context(), nameValue, array)); | |
102 | 129 |
103 return definition; | 130 return definition; |
104 } | 131 } |
105 | 132 |
106 ScriptCustomElementDefinition::ScriptCustomElementDefinition( | 133 ScriptCustomElementDefinition::ScriptCustomElementDefinition( |
107 ScriptState* scriptState, | 134 ScriptState* scriptState, |
108 const CustomElementDescriptor& descriptor, | 135 const CustomElementDescriptor& descriptor, |
109 const v8::Local<v8::Object>& constructor, | 136 const v8::Local<v8::Object>& constructor, |
110 const v8::Local<v8::Object>& prototype) | 137 const v8::Local<v8::Object>& prototype, |
138 const v8::Local<v8::Object>& connectedCallback, | |
139 const v8::Local<v8::Object>& disconnectedCallback, | |
140 const v8::Local<v8::Object>& attributeChangedCallback, | |
141 const HashSet<AtomicString>& observedAttributes) | |
111 : CustomElementDefinition(descriptor) | 142 : CustomElementDefinition(descriptor) |
112 , m_scriptState(scriptState) | 143 , m_scriptState(scriptState) |
113 , m_constructor(scriptState->isolate(), constructor) | 144 , m_constructor(scriptState->isolate(), constructor) |
114 , m_prototype(scriptState->isolate(), prototype) | 145 , m_observedAttributes(observedAttributes) |
115 { | 146 { |
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 } | 147 } |
122 | 148 |
123 v8::Local<v8::Object> ScriptCustomElementDefinition::constructor() const | 149 v8::Local<v8::Object> ScriptCustomElementDefinition::constructor() const |
124 { | 150 { |
125 DCHECK(!m_constructor.isEmpty()); | 151 DCHECK(!m_constructor.isEmpty()); |
126 return m_constructor.newLocal(m_scriptState->isolate()); | 152 return m_constructor.newLocal(m_scriptState->isolate()); |
127 } | 153 } |
128 | 154 |
129 v8::Local<v8::Object> ScriptCustomElementDefinition::prototype() const | 155 v8::Local<v8::Object> ScriptCustomElementDefinition::prototype() const |
130 { | 156 { |
131 DCHECK(!m_prototype.isEmpty()); | 157 DCHECK(!m_prototype.isEmpty()); |
132 return m_prototype.newLocal(m_scriptState->isolate()); | 158 return m_prototype.newLocal(m_scriptState->isolate()); |
133 } | 159 } |
134 | 160 |
135 } // namespace blink | 161 } // namespace blink |
OLD | NEW |