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