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

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

Powered by Google App Engine
This is Rietveld 408576698