| 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" |
| 7 #include "bindings/core/v8/ExceptionState.h" | 10 #include "bindings/core/v8/ExceptionState.h" |
| 8 #include "bindings/core/v8/ScriptState.h" | 11 #include "bindings/core/v8/ScriptState.h" |
| 12 #include "bindings/core/v8/ScriptValue.h" |
| 13 #include "bindings/core/v8/V8Binding.h" |
| 14 #include "core/dom/Document.h" |
| 9 #include "core/dom/ElementRegistrationOptions.h" | 15 #include "core/dom/ElementRegistrationOptions.h" |
| 16 #include "core/dom/ExceptionCode.h" |
| 17 #include "core/dom/custom/CustomElement.h" |
| 18 #include "core/dom/custom/CustomElementDefinition.h" |
| 19 #include "core/dom/custom/V0CustomElementRegistrationContext.h" |
| 20 #include "core/dom/custom/V0CustomElementRegistry.h" |
| 10 | 21 |
| 11 namespace blink { | 22 namespace blink { |
| 12 | 23 |
| 13 CustomElementsRegistry::CustomElementsRegistry() | 24 CustomElementsRegistry* CustomElementsRegistry::create( |
| 25 ScriptState* scriptState) |
| 26 { |
| 27 if (!scriptState->contextIsValid()) |
| 28 return nullptr; |
| 29 DCHECK(scriptState->world().isMainWorld()); |
| 30 return new CustomElementsRegistry(scriptState->isolate()); |
| 31 } |
| 32 |
| 33 CustomElementsRegistry::CustomElementsRegistry(v8::Isolate* isolate) |
| 34 : m_constructorIdMap(isolate, v8::NativeWeakMap::New(isolate)) |
| 14 { | 35 { |
| 15 } | 36 } |
| 16 | 37 |
| 38 static bool v0CustomElementIsDefined( |
| 39 ScriptState* scriptState, |
| 40 const AtomicString& name) |
| 41 { |
| 42 Document* doc = toDocument(scriptState->getExecutionContext()); |
| 43 V0CustomElementRegistrationContext* v0Context = doc->registrationContext(); |
| 44 return v0Context && v0Context->registry().nameIsDefined(name); |
| 45 } |
| 46 |
| 47 // http://w3c.github.io/webcomponents/spec/custom/#dfn-element-definition |
| 17 void CustomElementsRegistry::define(ScriptState* scriptState, | 48 void CustomElementsRegistry::define(ScriptState* scriptState, |
| 18 const AtomicString& name, const ScriptValue& constructor, | 49 const AtomicString& name, const ScriptValue& constructor, |
| 19 const ElementRegistrationOptions& options, ExceptionState& exceptionState) | 50 const ElementRegistrationOptions& options, ExceptionState& exceptionState) |
| 20 { | 51 { |
| 21 // TODO(kojii): Element definition process not implemented yet. | 52 if (!scriptState->world().isMainWorld()) { |
| 22 // http://w3c.github.io/webcomponents/spec/custom/#dfn-element-definition | 53 exceptionState.throwDOMException( |
| 54 NotSupportedError, |
| 55 "defining custom elements from extensions is not supported"); |
| 56 return; |
| 57 } |
| 58 v8::Isolate* isolate = scriptState->isolate(); |
| 59 v8::Local<v8::Context> context = scriptState->context(); |
| 60 |
| 61 // TODO(dominicc): Make this check for constructors and not just |
| 62 // functions when |
| 63 // https://bugs.chromium.org/p/v8/issues/detail?id=4993 is fixed. |
| 64 v8::Local<v8::Value> constructorValue = constructor.v8Value(); |
| 65 if (!constructorValue->IsFunction()) { |
| 66 exceptionState.throwTypeError( |
| 67 "constructor argument is not a constructor"); |
| 68 return; |
| 69 } |
| 70 v8::Local<v8::Object> constructorObject = |
| 71 constructorValue.As<v8::Object>(); |
| 72 |
| 73 // Raise an exception if the name is not valid. |
| 74 if (!CustomElement::isValidName(name)) { |
| 75 exceptionState.throwSyntaxError( |
| 76 "\"" + name + "\" is not a valid custom element name"); |
| 77 return; |
| 78 } |
| 79 |
| 80 // Raise an exception if the name is already in use. |
| 81 if (nameIsDefined(name) || v0CustomElementIsDefined(scriptState, name)) { |
| 82 exceptionState.throwDOMException( |
| 83 NotSupportedError, |
| 84 "this name has already been used with this registry"); |
| 85 return; |
| 86 } |
| 87 |
| 88 // Raise an exception if the constructor is already registered. |
| 89 if (definitionForConstructor(scriptState, constructorObject)) { |
| 90 exceptionState.throwDOMException( |
| 91 NotSupportedError, |
| 92 "this constructor has already been used with this registry"); |
| 93 return; |
| 94 } |
| 95 |
| 96 // TODO(dominicc): Implement steps: |
| 97 // 5: localName |
| 98 // 6-7: extends processing |
| 99 // 8-9: observed attributes caching |
| 100 |
| 101 v8::TryCatch tryCatch(isolate); |
| 102 v8::Local<v8::String> prototypeString = |
| 103 v8AtomicString(isolate, "prototype"); |
| 104 v8::Local<v8::Value> prototypeValue; |
| 105 if (!v8Call( |
| 106 constructorObject->Get(context, prototypeString), prototypeValue)) { |
| 107 DCHECK(tryCatch.HasCaught()); |
| 108 tryCatch.ReThrow(); |
| 109 return; |
| 110 } |
| 111 if (!prototypeValue->IsObject()) { |
| 112 DCHECK(!tryCatch.HasCaught()); |
| 113 exceptionState.throwTypeError("constructor prototype is not an object"); |
| 114 return; |
| 115 } |
| 116 v8::Local<v8::Object> prototype = prototypeValue.As<v8::Object>(); |
| 117 |
| 118 // TODO(dominicc): Implement steps: |
| 119 // 12-13: connected callback |
| 120 // 14-15: disconnected callback |
| 121 // 16-17: attribute changed callback |
| 122 |
| 123 size_t id = m_definitions.size(); |
| 124 m_definitions.append(new CustomElementDefinition(name, isolate, prototype)); |
| 125 m_constructorIdMap.Get(isolate)->Set( |
| 126 constructorObject, |
| 127 v8::Integer::NewFromUnsigned(isolate, id)); |
| 128 m_names.add(name); |
| 129 |
| 130 // TODO(dominicc): Implement steps: |
| 131 // 20: when-defined promise processing |
| 132 DCHECK(!tryCatch.HasCaught() || tryCatch.HasTerminated()); |
| 133 } |
| 134 |
| 135 CustomElementDefinition* CustomElementsRegistry::definitionForConstructor( |
| 136 ScriptState* scriptState, |
| 137 v8::Handle<v8::Value> constructor) |
| 138 { |
| 139 v8::Local<v8::Value> entry = |
| 140 m_constructorIdMap.Get(scriptState->isolate())->Get(constructor); |
| 141 if (!entry->IsUint32()) |
| 142 return nullptr; |
| 143 uint32_t id = v8CallOrCrash(entry->Uint32Value(scriptState->context())); |
| 144 return m_definitions[id]; |
| 145 } |
| 146 |
| 147 bool CustomElementsRegistry::nameIsDefined(const AtomicString& name) const |
| 148 { |
| 149 return m_names.contains(name); |
| 150 } |
| 151 |
| 152 void CustomElementsRegistry::setWrapperReferences( |
| 153 v8::Isolate* isolate, |
| 154 const v8::Persistent<v8::Object>& wrapper) const |
| 155 { |
| 156 for (const auto& def : m_definitions) { |
| 157 def->setReference(isolate, wrapper); |
| 158 } |
| 23 } | 159 } |
| 24 | 160 |
| 25 DEFINE_TRACE(CustomElementsRegistry) | 161 DEFINE_TRACE(CustomElementsRegistry) |
| 26 { | 162 { |
| 163 visitor->trace(m_definitions); |
| 27 } | 164 } |
| 28 | 165 |
| 29 } // namespace blink | 166 } // namespace blink |
| OLD | NEW |