Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(205)

Side by Side Diff: third_party/WebKit/Source/core/dom/custom/CustomElementsRegistry.cpp

Issue 1952893003: Implement custom element construction and some 'define' checks (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Remove defunct stuff. Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 "bindings/core/v8/V8BindingMacros.h"
15 #include "bindings/core/v8/V8HiddenValue.h"
16 #include "core/dom/Document.h"
9 #include "core/dom/ElementRegistrationOptions.h" 17 #include "core/dom/ElementRegistrationOptions.h"
18 #include "core/dom/ExceptionCode.h"
19 #include "core/dom/custom/CustomElement.h"
20 #include "core/dom/custom/CustomElementDefinition.h"
21 #include "core/dom/custom/V0CustomElementRegistrationContext.h"
22 #include "core/dom/custom/V0CustomElementRegistry.h"
10 23
11 namespace blink { 24 namespace blink {
12 25
13 CustomElementsRegistry::CustomElementsRegistry() 26 CustomElementsRegistry* CustomElementsRegistry::create(
27 ScriptState* scriptState,
28 V0CustomElementRegistrationContext* v0)
29 {
30 DCHECK(scriptState->world().isMainWorld());
31 CustomElementsRegistry* registry = new CustomElementsRegistry(v0);
32 if (v0)
33 v0->setV1(registry);
34
35 v8::Isolate* isolate = scriptState->isolate();
36 v8::Local<v8::Object> wrapper =
37 toV8(registry, scriptState->context()->Global(), isolate)
38 .As<v8::Object>();
haraken 2016/05/12 08:19:41 Add DCHECK(!wrapper.IsEmpty()).
39 v8::Local<v8::String> name =
40 V8HiddenValue::customElementsRegistryMap(isolate);
41 v8::Local<v8::Map> map = v8::Map::New(isolate);
42 DCHECK(V8HiddenValue::setHiddenValue(scriptState, wrapper, name, map));
43
44 return registry;
45 }
46
47 CustomElementsRegistry::CustomElementsRegistry(
48 const V0CustomElementRegistrationContext* v0)
49 : m_v0(v0)
14 { 50 {
15 } 51 }
16 52
53 // http://w3c.github.io/webcomponents/spec/custom/#dfn-element-definition
17 void CustomElementsRegistry::define(ScriptState* scriptState, 54 void CustomElementsRegistry::define(ScriptState* scriptState,
18 const AtomicString& name, const ScriptValue& constructor, 55 const AtomicString& name, const ScriptValue& constructorScriptValue,
19 const ElementRegistrationOptions& options, ExceptionState& exceptionState) 56 const ElementRegistrationOptions& options, ExceptionState& exceptionState)
20 { 57 {
21 // TODO(kojii): Element definition process not implemented yet. 58 DCHECK(scriptState->world().isMainWorld());
22 // http://w3c.github.io/webcomponents/spec/custom/#dfn-element-definition 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 = constructorScriptValue.v8Value();
66 if (!constructorValue->IsFunction()) {
67 exceptionState.throwTypeError(
68 "constructor argument is not a constructor");
69 return;
70 }
71 v8::Local<v8::Object> constructor = constructorValue.As<v8::Object>();
72
73 // Raise an exception if the name is not valid.
74 if (!CustomElement::isValidName(name)) {
75 exceptionState.throwDOMException(
haraken 2016/05/12 08:19:41 Do you want to throw a DOM exception, not JS's syn
76 SyntaxError,
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) || v0NameIsDefined(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(scriptState, constructor)) {
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::Local<v8::String> prototypeString =
104 v8AtomicString(isolate, "prototype");
105 v8::Local<v8::Value> prototypeValue;
106 if (!v8Call(
107 constructor->Get(context, prototypeString), prototypeValue)) {
108 DCHECK(tryCatch.HasCaught());
109 tryCatch.ReThrow();
110 return;
111 }
112 if (!prototypeValue->IsObject()) {
113 DCHECK(!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 Id id = m_definitions.size();
125 v8::Local<v8::Value> idValue = v8::Integer::NewFromUnsigned(isolate, id);
126 m_definitions.append(new CustomElementDefinition(this, id, name));
127 v8::Local<v8::Map> map = idMap(scriptState);
128 v8CallOrCrash(map->Set(context, constructor, idValue));
129 v8CallOrCrash(map->Set(context, idValue, prototype));
haraken 2016/05/12 08:19:41 Hmm, would you help me understand why you cannot s
130 m_names.add(name);
131
132 // TODO(dominicc): Implement steps:
133 // 20: when-defined promise processing
134 DCHECK(!tryCatch.HasCaught() || tryCatch.HasTerminated());
135 }
136
137 CustomElementDefinition* CustomElementsRegistry::definitionForConstructor(
138 ScriptState* scriptState,
139 v8::Local<v8::Value> constructor)
140 {
141 Id id;
142 if (!idForConstructor(scriptState, constructor, id))
143 return nullptr;
144 return m_definitions[id];
145 }
146
147 v8::Local<v8::Object> CustomElementsRegistry::prototype(
148 ScriptState* scriptState,
149 const CustomElementDefinition& def)
150 {
151 v8::Local<v8::Value> idValue =
152 v8::Integer::NewFromUnsigned(scriptState->isolate(), def.id());
153 return v8CallOrCrash(
154 idMap(scriptState)->Get(scriptState->context(), idValue))
155 .As<v8::Object>();
156 }
157
158 bool CustomElementsRegistry::nameIsDefined(const AtomicString& name) const
159 {
160 return m_names.contains(name);
161 }
162
163 v8::Local<v8::Map> CustomElementsRegistry::idMap(ScriptState* scriptState)
164 {
165 v8::Local<v8::String> name = V8HiddenValue::customElementsRegistryMap(
166 scriptState->isolate());
167 return V8HiddenValue::getHiddenValueFromMainWorldWrapper(
haraken 2016/05/12 08:19:41 getHiddenValueFromMainWorldWrapper => getHiddenVal
168 scriptState,
169 this,
170 name).As<v8::Map>();
171 }
172
173 bool CustomElementsRegistry::idForConstructor(
174 ScriptState* scriptState,
175 v8::Local<v8::Value> constructor,
176 Id& id)
177 {
178 v8::Local<v8::Value> entry = v8CallOrCrash(
179 idMap(scriptState)->Get(scriptState->context(), constructor));
180 if (!entry->IsUint32())
181 return false;
182 id = v8CallOrCrash(entry->Uint32Value(scriptState->context()));
183 return true;
184 }
185
186 bool CustomElementsRegistry::v0NameIsDefined(const AtomicString& name)
187 {
188 return m_v0.get() && m_v0->nameIsDefined(name);
23 } 189 }
24 190
25 DEFINE_TRACE(CustomElementsRegistry) 191 DEFINE_TRACE(CustomElementsRegistry)
26 { 192 {
193 visitor->trace(m_definitions);
194 visitor->trace(m_v0);
27 } 195 }
28 196
29 } // namespace blink 197 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698