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

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

Issue 2003033004: Split custom element script use and move it into bindings (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Add missing CORE_EXPORT header. Created 4 years, 6 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"
10 #include "bindings/core/v8/ExceptionState.h" 7 #include "bindings/core/v8/ExceptionState.h"
8 #include "bindings/core/v8/ScriptCustomElementDefinitionBuilder.h"
11 #include "bindings/core/v8/ScriptState.h" 9 #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" 10 #include "core/dom/Document.h"
17 #include "core/dom/ElementRegistrationOptions.h" 11 #include "core/dom/ElementRegistrationOptions.h"
18 #include "core/dom/ExceptionCode.h" 12 #include "core/dom/ExceptionCode.h"
19 #include "core/dom/custom/CustomElement.h" 13 #include "core/dom/custom/CustomElement.h"
20 #include "core/dom/custom/CustomElementDefinition.h" 14 #include "core/dom/custom/CustomElementDefinition.h"
15 #include "core/dom/custom/CustomElementDefinitionBuilder.h"
21 #include "core/dom/custom/V0CustomElementRegistrationContext.h" 16 #include "core/dom/custom/V0CustomElementRegistrationContext.h"
22 #include "core/dom/custom/V0CustomElementRegistry.h"
23 17
24 namespace blink { 18 namespace blink {
25 19
26 CustomElementsRegistry* CustomElementsRegistry::create( 20 CustomElementsRegistry* CustomElementsRegistry::create(
27 ScriptState* scriptState,
28 V0CustomElementRegistrationContext* v0) 21 V0CustomElementRegistrationContext* v0)
29 { 22 {
30 DCHECK(scriptState->world().isMainWorld()); 23 // TODO(dominicc): The window could install a new document; add a signal
24 // when a window installs a new document to notify that V0 context, too.
31 CustomElementsRegistry* registry = new CustomElementsRegistry(v0); 25 CustomElementsRegistry* registry = new CustomElementsRegistry(v0);
32 if (v0) 26 if (v0)
33 v0->setV1(registry); 27 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; 28 return registry;
46 } 29 }
47 30
48 CustomElementsRegistry::CustomElementsRegistry( 31 CustomElementsRegistry::CustomElementsRegistry(
49 const V0CustomElementRegistrationContext* v0) 32 const V0CustomElementRegistrationContext* v0)
50 : m_v0(v0) 33 : m_v0(v0)
51 { 34 {
52 } 35 }
53 36
37 DEFINE_TRACE(CustomElementsRegistry)
38 {
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 builder.checkConstructorIntrinsics();
60 v8::Isolate* isolate = scriptState->isolate(); 65 if (exceptionState.hadException())
61 v8::Local<v8::Context> context = scriptState->context(); 66 return;
62 67
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)) { 68 if (!CustomElement::isValidName(name)) {
79 exceptionState.throwDOMException( 69 exceptionState.throwDOMException(
80 SyntaxError, 70 SyntaxError,
81 "\"" + name + "\" is not a valid custom element name"); 71 "\"" + name + "\" is not a valid custom element name");
82 return; 72 return;
83 } 73 }
84 74
85 // Raise an exception if the name is already in use.
86 if (nameIsDefined(name) || v0NameIsDefined(name)) { 75 if (nameIsDefined(name) || v0NameIsDefined(name)) {
87 exceptionState.throwDOMException( 76 exceptionState.throwDOMException(
88 NotSupportedError, 77 NotSupportedError,
89 "this name has already been used with this registry"); 78 "this name has already been used with this registry");
90 return; 79 return;
91 } 80 }
92 81
93 // Raise an exception if the constructor is already registered. 82 builder.checkConstructorNotRegistered();
94 if (definitionForConstructor(scriptState, constructor)) { 83 if (exceptionState.hadException())
95 exceptionState.throwDOMException(
96 NotSupportedError,
97 "this constructor has already been used with this registry");
98 return; 84 return;
99 }
100 85
101 // TODO(dominicc): Implement steps: 86 // TODO(dominicc): Implement steps:
102 // 5: localName 87 // 5: localName
103 // 6-7: extends processing 88 // 6-7: extends processing
104 // 8-9: observed attributes caching 89 // 8-9: observed attributes caching
105 90
106 // TODO(dominicc): Add a test where the prototype getter destroys 91 // TODO(dominicc): Add a test where the prototype getter destroys
107 // the context. 92 // the context.
108 93
109 v8::TryCatch tryCatch(isolate); 94 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; 95 return;
118 } 96 if (exceptionState.hadException())
119 if (!prototypeValue->IsObject()) {
120 DCHECK(!tryCatch.HasCaught());
121 exceptionState.throwTypeError("constructor prototype is not an object");
122 return; 97 return;
123 }
124 v8::Local<v8::Object> prototype = prototypeValue.As<v8::Object>();
125 98
126 // TODO(dominicc): Implement steps: 99 // TODO(dominicc): Implement steps:
127 // 12-13: connected callback 100 // 12-13: connected callback
128 // 14-15: disconnected callback 101 // 14-15: disconnected callback
129 // 16-17: attribute changed callback 102 // 16-17: attribute changed callback
130 103
131 Id id = m_definitions.size(); 104 CustomElementDescriptor descriptor(name, name);
132 v8::Local<v8::Value> idValue = v8::Integer::NewFromUnsigned(isolate, id); 105 CustomElementDefinition* def = builder.build(descriptor);
esprehn 2016/05/26 04:50:28 definition
133 m_definitions.append(new CustomElementDefinition( 106 CHECK(!exceptionState.hadException());
134 this, 107 CHECK(def->descriptor() == descriptor);
135 id,
136 CustomElementDescriptor(name, name)));
137 // This map is stored in a hidden reference from the
138 // CustomElementsRegistry wrapper.
139 v8::Local<v8::Map> map = idMap(scriptState);
140 // The map keeps the constructor and prototypes alive.
141 v8CallOrCrash(map->Set(context, constructor, idValue));
142 v8CallOrCrash(map->Set(context, idValue, prototype));
143 m_names.add(name); 108 m_names.add(name);
144 109
145 // TODO(dominicc): Implement steps: 110 // TODO(dominicc): Implement steps:
146 // 20: when-defined promise processing 111 // 20: when-defined promise processing
147 DCHECK(!tryCatch.HasCaught() || tryCatch.HasTerminated());
148 } 112 }
149 113
150 CustomElementDefinition* CustomElementsRegistry::definitionForConstructor( 114 bool CustomElementsRegistry::v0NameIsDefined(const AtomicString& name) const
151 ScriptState* scriptState,
152 v8::Local<v8::Value> constructor)
153 { 115 {
154 Id id; 116 if (!m_v0)
155 if (!idForConstructor(scriptState, constructor, id))
156 return nullptr;
157 return m_definitions[id];
158 }
159
160 v8::Local<v8::Object> CustomElementsRegistry::prototype(
161 ScriptState* scriptState,
162 const CustomElementDefinition& def)
163 {
164 v8::Local<v8::Value> idValue =
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; 117 return false;
196 id = v8CallOrCrash(entry->Uint32Value(scriptState->context())); 118 return m_v0->nameIsDefined(name);
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 } 119 }
210 120
211 } // namespace blink 121 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698