| 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 "core/dom/custom/CustomElementsRegistry.h" | 5 #include "core/dom/custom/CustomElementsRegistry.h" |
| 6 | 6 |
| 7 // TODO(dominicc): Stop including Document.h when | |
| 8 // v0CustomElementIsDefined has been removed. | |
| 9 #include "bindings/core/v8/DOMWrapperWorld.h" | |
| 10 #include "bindings/core/v8/ExceptionState.h" | 7 #include "bindings/core/v8/ExceptionState.h" |
| 11 #include "bindings/core/v8/ScriptState.h" | 8 #include "bindings/core/v8/ScriptCustomElementDefinitionBuilder.h" |
| 12 #include "bindings/core/v8/ScriptValue.h" | |
| 13 #include "bindings/core/v8/V8Binding.h" | |
| 14 #include "bindings/core/v8/V8BindingMacros.h" | |
| 15 #include "bindings/core/v8/V8HiddenValue.h" | |
| 16 #include "core/dom/Document.h" | 9 #include "core/dom/Document.h" |
| 17 #include "core/dom/ElementRegistrationOptions.h" | 10 #include "core/dom/ElementRegistrationOptions.h" |
| 18 #include "core/dom/ExceptionCode.h" | 11 #include "core/dom/ExceptionCode.h" |
| 19 #include "core/dom/custom/CustomElement.h" | 12 #include "core/dom/custom/CustomElement.h" |
| 20 #include "core/dom/custom/CustomElementDefinition.h" | 13 #include "core/dom/custom/CustomElementDefinition.h" |
| 14 #include "core/dom/custom/CustomElementDefinitionBuilder.h" |
| 21 #include "core/dom/custom/V0CustomElementRegistrationContext.h" | 15 #include "core/dom/custom/V0CustomElementRegistrationContext.h" |
| 22 #include "core/dom/custom/V0CustomElementRegistry.h" | |
| 23 | 16 |
| 24 namespace blink { | 17 namespace blink { |
| 25 | 18 |
| 26 CustomElementsRegistry* CustomElementsRegistry::create( | 19 CustomElementsRegistry* CustomElementsRegistry::create( |
| 27 ScriptState* scriptState, | |
| 28 V0CustomElementRegistrationContext* v0) | 20 V0CustomElementRegistrationContext* v0) |
| 29 { | 21 { |
| 30 DCHECK(scriptState->world().isMainWorld()); | 22 // TODO(dominicc): The window could install a new document; add a signal |
| 23 // when a window installs a new document to notify that V0 context, too. |
| 31 CustomElementsRegistry* registry = new CustomElementsRegistry(v0); | 24 CustomElementsRegistry* registry = new CustomElementsRegistry(v0); |
| 32 if (v0) | 25 if (v0) |
| 33 v0->setV1(registry); | 26 v0->setV1(registry); |
| 34 | |
| 35 v8::Isolate* isolate = scriptState->isolate(); | |
| 36 v8::Local<v8::Object> wrapper = | |
| 37 toV8(registry, scriptState).As<v8::Object>(); | |
| 38 v8::Local<v8::String> name = | |
| 39 V8HiddenValue::customElementsRegistryMap(isolate); | |
| 40 v8::Local<v8::Map> map = v8::Map::New(isolate); | |
| 41 bool didSetPrototype = | |
| 42 V8HiddenValue::setHiddenValue(scriptState, wrapper, name, map); | |
| 43 DCHECK(didSetPrototype); | |
| 44 | |
| 45 return registry; | 27 return registry; |
| 46 } | 28 } |
| 47 | 29 |
| 48 CustomElementsRegistry::CustomElementsRegistry( | 30 CustomElementsRegistry::CustomElementsRegistry( |
| 49 const V0CustomElementRegistrationContext* v0) | 31 const V0CustomElementRegistrationContext* v0) |
| 50 : m_v0(v0) | 32 : m_v0(v0) |
| 51 { | 33 { |
| 52 } | 34 } |
| 53 | 35 |
| 36 DEFINE_TRACE(CustomElementsRegistry) |
| 37 { |
| 38 visitor->trace(m_definitions); |
| 39 visitor->trace(m_v0); |
| 40 } |
| 41 |
| 42 void CustomElementsRegistry::define( |
| 43 ScriptState* scriptState, |
| 44 const AtomicString& name, |
| 45 const ScriptValue& constructor, |
| 46 const ElementRegistrationOptions& options, |
| 47 ExceptionState& exceptionState) |
| 48 { |
| 49 ScriptCustomElementDefinitionBuilder builder( |
| 50 scriptState, |
| 51 this, |
| 52 constructor, |
| 53 exceptionState); |
| 54 define(name, builder, options, exceptionState); |
| 55 } |
| 56 |
| 54 // http://w3c.github.io/webcomponents/spec/custom/#dfn-element-definition | 57 // http://w3c.github.io/webcomponents/spec/custom/#dfn-element-definition |
| 55 void CustomElementsRegistry::define(ScriptState* scriptState, | 58 void CustomElementsRegistry::define( |
| 56 const AtomicString& name, const ScriptValue& constructorScriptValue, | 59 const AtomicString& name, |
| 57 const ElementRegistrationOptions& options, ExceptionState& exceptionState) | 60 CustomElementDefinitionBuilder& builder, |
| 61 const ElementRegistrationOptions& options, |
| 62 ExceptionState& exceptionState) |
| 58 { | 63 { |
| 59 DCHECK(scriptState->world().isMainWorld()); | 64 if (!builder.checkConstructorIntrinsics()) |
| 60 v8::Isolate* isolate = scriptState->isolate(); | 65 return; |
| 61 v8::Local<v8::Context> context = scriptState->context(); | |
| 62 | 66 |
| 63 v8::Local<v8::Value> constructorValue = constructorScriptValue.v8Value(); | |
| 64 if (!constructorValue->IsFunction()) { | |
| 65 // Not even a function. | |
| 66 exceptionState.throwTypeError( | |
| 67 "constructor argument is not a constructor"); | |
| 68 return; | |
| 69 } | |
| 70 v8::Local<v8::Object> constructor = constructorValue.As<v8::Object>(); | |
| 71 if (!constructor->IsConstructor()) { | |
| 72 exceptionState.throwTypeError( | |
| 73 "constructor argument is not a constructor"); | |
| 74 return; | |
| 75 } | |
| 76 | |
| 77 // Raise an exception if the name is not valid. | |
| 78 if (!CustomElement::isValidName(name)) { | 67 if (!CustomElement::isValidName(name)) { |
| 79 exceptionState.throwDOMException( | 68 exceptionState.throwDOMException( |
| 80 SyntaxError, | 69 SyntaxError, |
| 81 "\"" + name + "\" is not a valid custom element name"); | 70 "\"" + name + "\" is not a valid custom element name"); |
| 82 return; | 71 return; |
| 83 } | 72 } |
| 84 | 73 |
| 85 // Raise an exception if the name is already in use. | |
| 86 if (nameIsDefined(name) || v0NameIsDefined(name)) { | 74 if (nameIsDefined(name) || v0NameIsDefined(name)) { |
| 87 exceptionState.throwDOMException( | 75 exceptionState.throwDOMException( |
| 88 NotSupportedError, | 76 NotSupportedError, |
| 89 "this name has already been used with this registry"); | 77 "this name has already been used with this registry"); |
| 90 return; | 78 return; |
| 91 } | 79 } |
| 92 | 80 |
| 93 // Raise an exception if the constructor is already registered. | 81 if (!builder.checkConstructorNotRegistered()) |
| 94 if (definitionForConstructor(scriptState, constructor)) { | |
| 95 exceptionState.throwDOMException( | |
| 96 NotSupportedError, | |
| 97 "this constructor has already been used with this registry"); | |
| 98 return; | 82 return; |
| 99 } | |
| 100 | 83 |
| 101 // TODO(dominicc): Implement steps: | 84 // TODO(dominicc): Implement steps: |
| 102 // 5: localName | 85 // 5: localName |
| 103 // 6-7: extends processing | 86 // 6-7: extends processing |
| 104 // 8-9: observed attributes caching | 87 // 8-9: observed attributes caching |
| 105 | 88 |
| 106 // TODO(dominicc): Add a test where the prototype getter destroys | 89 // TODO(dominicc): Add a test where the prototype getter destroys |
| 107 // the context. | 90 // the context. |
| 108 | 91 |
| 109 v8::TryCatch tryCatch(isolate); | 92 if (!builder.checkPrototype()) |
| 110 v8::Local<v8::String> prototypeString = | |
| 111 v8AtomicString(isolate, "prototype"); | |
| 112 v8::Local<v8::Value> prototypeValue; | |
| 113 if (!v8Call( | |
| 114 constructor->Get(context, prototypeString), prototypeValue)) { | |
| 115 DCHECK(tryCatch.HasCaught()); | |
| 116 tryCatch.ReThrow(); | |
| 117 return; | 93 return; |
| 118 } | |
| 119 if (!prototypeValue->IsObject()) { | |
| 120 DCHECK(!tryCatch.HasCaught()); | |
| 121 exceptionState.throwTypeError("constructor prototype is not an object"); | |
| 122 return; | |
| 123 } | |
| 124 v8::Local<v8::Object> prototype = prototypeValue.As<v8::Object>(); | |
| 125 | 94 |
| 126 // TODO(dominicc): Implement steps: | 95 // TODO(dominicc): Implement steps: |
| 127 // 12-13: connected callback | 96 // 12-13: connected callback |
| 128 // 14-15: disconnected callback | 97 // 14-15: disconnected callback |
| 129 // 16-17: attribute changed callback | 98 // 16-17: attribute changed callback |
| 130 | 99 |
| 131 Id id = m_definitions.size(); | 100 // TODO(dominicc): Add a test where retrieving the prototype |
| 132 v8::Local<v8::Value> idValue = v8::Integer::NewFromUnsigned(isolate, id); | 101 // recursively calls define with the same name. |
| 133 m_definitions.append(new CustomElementDefinition( | 102 |
| 134 this, | 103 CustomElementDescriptor descriptor(name, name); |
| 135 id, | 104 CustomElementDefinition* definition = builder.build(descriptor); |
| 136 CustomElementDescriptor(name, name))); | 105 CHECK(!exceptionState.hadException()); |
| 137 // This map is stored in a hidden reference from the | 106 CHECK(definition->descriptor() == descriptor); |
| 138 // CustomElementsRegistry wrapper. | 107 DefinitionMap::AddResult result = |
| 139 v8::Local<v8::Map> map = idMap(scriptState); | 108 m_definitions.add(descriptor.name(), definition); |
| 140 // The map keeps the constructor and prototypes alive. | 109 CHECK(result.isNewEntry); |
| 141 v8CallOrCrash(map->Set(context, constructor, idValue)); | |
| 142 v8CallOrCrash(map->Set(context, idValue, prototype)); | |
| 143 m_names.add(name); | |
| 144 | 110 |
| 145 // TODO(dominicc): Implement steps: | 111 // TODO(dominicc): Implement steps: |
| 146 // 20: when-defined promise processing | 112 // 20: when-defined promise processing |
| 147 DCHECK(!tryCatch.HasCaught() || tryCatch.HasTerminated()); | |
| 148 } | 113 } |
| 149 | 114 |
| 150 CustomElementDefinition* CustomElementsRegistry::definitionForConstructor( | 115 bool CustomElementsRegistry::v0NameIsDefined(const AtomicString& name) const |
| 151 ScriptState* scriptState, | |
| 152 v8::Local<v8::Value> constructor) | |
| 153 { | 116 { |
| 154 Id id; | 117 if (!m_v0) |
| 155 if (!idForConstructor(scriptState, constructor, id)) | 118 return false; |
| 156 return nullptr; | 119 return m_v0->nameIsDefined(name); |
| 157 return m_definitions[id]; | |
| 158 } | 120 } |
| 159 | 121 |
| 160 v8::Local<v8::Object> CustomElementsRegistry::prototype( | 122 CustomElementDefinition* CustomElementsRegistry::definitionForName( |
| 161 ScriptState* scriptState, | 123 const AtomicString& name) const |
| 162 const CustomElementDefinition& def) | |
| 163 { | 124 { |
| 164 v8::Local<v8::Value> idValue = | 125 return m_definitions.get(name); |
| 165 v8::Integer::NewFromUnsigned(scriptState->isolate(), def.id()); | |
| 166 return v8CallOrCrash( | |
| 167 idMap(scriptState)->Get(scriptState->context(), idValue)) | |
| 168 .As<v8::Object>(); | |
| 169 } | |
| 170 | |
| 171 bool CustomElementsRegistry::nameIsDefined(const AtomicString& name) const | |
| 172 { | |
| 173 return m_names.contains(name); | |
| 174 } | |
| 175 | |
| 176 v8::Local<v8::Map> CustomElementsRegistry::idMap(ScriptState* scriptState) | |
| 177 { | |
| 178 DCHECK(scriptState->world().isMainWorld()); | |
| 179 v8::Local<v8::Object> wrapper = | |
| 180 toV8(this, scriptState).As<v8::Object>(); | |
| 181 v8::Local<v8::String> name = V8HiddenValue::customElementsRegistryMap( | |
| 182 scriptState->isolate()); | |
| 183 return V8HiddenValue::getHiddenValue(scriptState, wrapper, name) | |
| 184 .As<v8::Map>(); | |
| 185 } | |
| 186 | |
| 187 bool CustomElementsRegistry::idForConstructor( | |
| 188 ScriptState* scriptState, | |
| 189 v8::Local<v8::Value> constructor, | |
| 190 Id& id) | |
| 191 { | |
| 192 v8::Local<v8::Value> entry = v8CallOrCrash( | |
| 193 idMap(scriptState)->Get(scriptState->context(), constructor)); | |
| 194 if (!entry->IsUint32()) | |
| 195 return false; | |
| 196 id = v8CallOrCrash(entry->Uint32Value(scriptState->context())); | |
| 197 return true; | |
| 198 } | |
| 199 | |
| 200 bool CustomElementsRegistry::v0NameIsDefined(const AtomicString& name) | |
| 201 { | |
| 202 return m_v0.get() && m_v0->nameIsDefined(name); | |
| 203 } | |
| 204 | |
| 205 DEFINE_TRACE(CustomElementsRegistry) | |
| 206 { | |
| 207 visitor->trace(m_definitions); | |
| 208 visitor->trace(m_v0); | |
| 209 } | 126 } |
| 210 | 127 |
| 211 } // namespace blink | 128 } // namespace blink |
| OLD | NEW |