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

Side by Side Diff: third_party/WebKit/Source/bindings/core/v8/CustomElementConstructorBuilder.cpp

Issue 1914923002: Rename all existing custom element classes as V0 (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: CustomElementV0 -> V0CustomElement Created 4 years, 8 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
(Empty)
1 /*
2 * Copyright (C) 2013 Google Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 *
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 #include "bindings/core/v8/CustomElementConstructorBuilder.h"
32
33 #include "bindings/core/v8/CustomElementBinding.h"
34 #include "bindings/core/v8/DOMWrapperWorld.h"
35 #include "bindings/core/v8/ExceptionState.h"
36 #include "bindings/core/v8/V8Binding.h"
37 #include "bindings/core/v8/V8Document.h"
38 #include "bindings/core/v8/V8HTMLElement.h"
39 #include "bindings/core/v8/V8HiddenValue.h"
40 #include "bindings/core/v8/V8PerContextData.h"
41 #include "bindings/core/v8/V8SVGElement.h"
42 #include "core/HTMLNames.h"
43 #include "core/SVGNames.h"
44 #include "core/dom/Document.h"
45 #include "core/dom/ElementRegistrationOptions.h"
46 #include "core/dom/custom/CustomElementDefinition.h"
47 #include "core/dom/custom/CustomElementDescriptor.h"
48 #include "core/dom/custom/CustomElementException.h"
49 #include "core/dom/custom/CustomElementProcessingStack.h"
50 #include "wtf/Assertions.h"
51
52 namespace blink {
53
54 static void constructCustomElement(const v8::FunctionCallbackInfo<v8::Value>&);
55
56 CustomElementConstructorBuilder::CustomElementConstructorBuilder(ScriptState* sc riptState, const ElementRegistrationOptions& options)
57 : m_scriptState(scriptState)
58 , m_options(options)
59 {
60 ASSERT(m_scriptState->context() == m_scriptState->isolate()->GetCurrentConte xt());
61 }
62
63 bool CustomElementConstructorBuilder::isFeatureAllowed() const
64 {
65 return m_scriptState->world().isMainWorld();
66 }
67
68 bool CustomElementConstructorBuilder::validateOptions(const AtomicString& type, QualifiedName& tagName, ExceptionState& exceptionState)
69 {
70 ASSERT(m_prototype.IsEmpty());
71
72 v8::TryCatch tryCatch(m_scriptState->isolate());
73
74 if (!m_scriptState->perContextData()) {
75 // FIXME: This should generate an InvalidContext exception at a later po int.
76 CustomElementException::throwException(CustomElementException::ContextDe stroyedCheckingPrototype, type, exceptionState);
77 tryCatch.ReThrow();
78 return false;
79 }
80
81 if (m_options.hasPrototype()) {
82 ASSERT(m_options.prototype().isObject());
83 m_prototype = m_options.prototype().v8Value().As<v8::Object>();
84 } else {
85 m_prototype = v8::Object::New(m_scriptState->isolate());
86 v8::Local<v8::Object> basePrototype = m_scriptState->perContextData()->p rototypeForType(&V8HTMLElement::wrapperTypeInfo);
87 if (!basePrototype.IsEmpty()) {
88 if (!v8CallBoolean(m_prototype->SetPrototype(m_scriptState->context( ), basePrototype)))
89 return false;
90 }
91 }
92
93 AtomicString namespaceURI = HTMLNames::xhtmlNamespaceURI;
94 if (hasValidPrototypeChainFor(&V8SVGElement::wrapperTypeInfo))
95 namespaceURI = SVGNames::svgNamespaceURI;
96
97 ASSERT(!tryCatch.HasCaught());
98
99 AtomicString localName;
100
101 if (m_options.hasExtends()) {
102 localName = AtomicString(m_options.extends().lower());
103
104 if (!Document::isValidName(localName)) {
105 CustomElementException::throwException(CustomElementException::Exten dsIsInvalidName, type, exceptionState);
106 tryCatch.ReThrow();
107 return false;
108 }
109 if (CustomElement::isValidName(localName)) {
110 CustomElementException::throwException(CustomElementException::Exten dsIsCustomElementName, type, exceptionState);
111 tryCatch.ReThrow();
112 return false;
113 }
114 } else {
115 if (namespaceURI == SVGNames::svgNamespaceURI) {
116 CustomElementException::throwException(CustomElementException::Exten dsIsInvalidName, type, exceptionState);
117 tryCatch.ReThrow();
118 return false;
119 }
120 localName = type;
121 }
122
123 ASSERT(!tryCatch.HasCaught());
124 tagName = QualifiedName(nullAtom, localName, namespaceURI);
125 return true;
126 }
127
128 CustomElementLifecycleCallbacks* CustomElementConstructorBuilder::createCallback s()
129 {
130 ASSERT(!m_prototype.IsEmpty());
131
132 v8::TryCatch exceptionCatcher(m_scriptState->isolate());
133 exceptionCatcher.SetVerbose(true);
134
135 v8::MaybeLocal<v8::Function> created = retrieveCallback("createdCallback");
136 v8::MaybeLocal<v8::Function> attached = retrieveCallback("attachedCallback") ;
137 v8::MaybeLocal<v8::Function> detached = retrieveCallback("detachedCallback") ;
138 v8::MaybeLocal<v8::Function> attributeChanged = retrieveCallback("attributeC hangedCallback");
139
140 m_callbacks = V8CustomElementLifecycleCallbacks::create(m_scriptState.get(), m_prototype, created, attached, detached, attributeChanged);
141 return m_callbacks.get();
142 }
143
144 v8::MaybeLocal<v8::Function> CustomElementConstructorBuilder::retrieveCallback(c onst char* name)
145 {
146 v8::Local<v8::Value> value;
147 if (!m_prototype->Get(m_scriptState->context(), v8String(m_scriptState->isol ate(), name)).ToLocal(&value) || !value->IsFunction())
148 return v8::MaybeLocal<v8::Function>();
149 return v8::MaybeLocal<v8::Function>(value.As<v8::Function>());
150 }
151
152 bool CustomElementConstructorBuilder::createConstructor(Document* document, Cust omElementDefinition* definition, ExceptionState& exceptionState)
153 {
154 ASSERT(!m_prototype.IsEmpty());
155 ASSERT(m_constructor.IsEmpty());
156 ASSERT(document);
157
158 v8::Isolate* isolate = m_scriptState->isolate();
159 v8::Local<v8::Context> context = m_scriptState->context();
160
161 if (!prototypeIsValid(definition->descriptor().type(), exceptionState))
162 return false;
163
164 const CustomElementDescriptor& descriptor = definition->descriptor();
165
166 v8::Local<v8::String> v8TagName = v8String(isolate, descriptor.localName());
167 v8::Local<v8::Value> v8Type;
168 if (descriptor.isTypeExtension())
169 v8Type = v8String(isolate, descriptor.type());
170 else
171 v8Type = v8::Null(isolate);
172
173 v8::Local<v8::Object> data = v8::Object::New(isolate);
174 V8HiddenValue::setHiddenValue(m_scriptState.get(), data, V8HiddenValue::cust omElementDocument(isolate), toV8(document, context->Global(), isolate));
175 V8HiddenValue::setHiddenValue(m_scriptState.get(), data, V8HiddenValue::cust omElementNamespaceURI(isolate), v8String(isolate, descriptor.namespaceURI()));
176 V8HiddenValue::setHiddenValue(m_scriptState.get(), data, V8HiddenValue::cust omElementTagName(isolate), v8TagName);
177 V8HiddenValue::setHiddenValue(m_scriptState.get(), data, V8HiddenValue::cust omElementType(isolate), v8Type);
178
179 v8::Local<v8::FunctionTemplate> constructorTemplate = v8::FunctionTemplate:: New(isolate);
180 constructorTemplate->SetCallHandler(constructCustomElement, data);
181 if (!constructorTemplate->GetFunction(context).ToLocal(&m_constructor)) {
182 CustomElementException::throwException(CustomElementException::ContextDe stroyedRegisteringDefinition, definition->descriptor().type(), exceptionState);
183 return false;
184 }
185
186 m_constructor->SetName(v8Type->IsNull() ? v8TagName : v8Type.As<v8::String>( ));
187
188 v8::Local<v8::String> prototypeKey = v8String(isolate, "prototype");
189 if (!v8CallBoolean(m_constructor->HasOwnProperty(context, prototypeKey)))
190 return false;
191 // This sets the property *value*; calling Set is safe because
192 // "prototype" is a non-configurable data property so there can be
193 // no side effects.
194 if (!v8CallBoolean(m_constructor->Set(context, prototypeKey, m_prototype)))
195 return false;
196 // This *configures* the property. DefineOwnProperty of a function's
197 // "prototype" does not affect the value, but can reconfigure the
198 // property.
199 if (!v8CallBoolean(m_constructor->DefineOwnProperty(context, prototypeKey, m _prototype, v8::PropertyAttribute(v8::ReadOnly | v8::DontEnum | v8::DontDelete)) ))
200 return false;
201
202 v8::Local<v8::String> constructorKey = v8String(isolate, "constructor");
203 v8::Local<v8::Value> constructorPrototype;
204 if (!m_prototype->Get(context, constructorKey).ToLocal(&constructorPrototype ))
205 return false;
206
207 if (!v8CallBoolean(m_constructor->SetPrototype(context, constructorPrototype )))
208 return false;
209
210 V8HiddenValue::setHiddenValue(m_scriptState.get(), m_prototype, V8HiddenValu e::customElementIsInterfacePrototypeObject(isolate), v8::True(isolate));
211 if (!v8CallBoolean(m_prototype->DefineOwnProperty(context, v8String(isolate, "constructor"), m_constructor, v8::DontEnum)))
212 return false;
213
214 return true;
215 }
216
217 bool CustomElementConstructorBuilder::prototypeIsValid(const AtomicString& type, ExceptionState& exceptionState) const
218 {
219 if (m_prototype->InternalFieldCount() || !V8HiddenValue::getHiddenValue(m_sc riptState.get(), m_prototype, V8HiddenValue::customElementIsInterfacePrototypeOb ject(m_scriptState->isolate())).IsEmpty()) {
220 CustomElementException::throwException(CustomElementException::Prototype InUse, type, exceptionState);
221 return false;
222 }
223
224 v8::PropertyAttribute propertyAttribute;
225 if (!v8Call(m_prototype->GetPropertyAttributes(m_scriptState->context(), v8S tring(m_scriptState->isolate(), "constructor")), propertyAttribute) || (property Attribute & v8::DontDelete)) {
226 CustomElementException::throwException(CustomElementException::Construct orPropertyNotConfigurable, type, exceptionState);
227 return false;
228 }
229
230 return true;
231 }
232
233 bool CustomElementConstructorBuilder::didRegisterDefinition() const
234 {
235 ASSERT(!m_constructor.IsEmpty());
236
237 return m_callbacks->setBinding(CustomElementBinding::create(m_scriptState->i solate(), m_prototype));
238 }
239
240 ScriptValue CustomElementConstructorBuilder::bindingsReturnValue() const
241 {
242 return ScriptValue(m_scriptState.get(), m_constructor);
243 }
244
245 bool CustomElementConstructorBuilder::hasValidPrototypeChainFor(const WrapperTyp eInfo* type) const
246 {
247 v8::Local<v8::Object> elementPrototype = m_scriptState->perContextData()->pr ototypeForType(type);
248 if (elementPrototype.IsEmpty())
249 return false;
250
251 v8::Local<v8::Value> chain = m_prototype;
252 while (!chain.IsEmpty() && chain->IsObject()) {
253 if (chain == elementPrototype)
254 return true;
255 chain = chain.As<v8::Object>()->GetPrototype();
256 }
257
258 return false;
259 }
260
261 static void constructCustomElement(const v8::FunctionCallbackInfo<v8::Value>& in fo)
262 {
263 v8::Isolate* isolate = info.GetIsolate();
264
265 if (!info.IsConstructCall()) {
266 V8ThrowException::throwTypeError(isolate, "DOM object constructor cannot be called as a function.");
267 return;
268 }
269
270 if (info.Length() > 0) {
271 V8ThrowException::throwTypeError(isolate, "This constructor should be ca lled without arguments.");
272 return;
273 }
274
275 ScriptState* scriptState = ScriptState::current(isolate);
276 v8::Local<v8::Object> data = v8::Local<v8::Object>::Cast(info.Data());
277 Document* document = V8Document::toImpl(V8HiddenValue::getHiddenValue(script State, data, V8HiddenValue::customElementDocument(isolate)).As<v8::Object>());
278 TOSTRING_VOID(V8StringResource<>, namespaceURI, V8HiddenValue::getHiddenValu e(scriptState, data, V8HiddenValue::customElementNamespaceURI(isolate)));
279 TOSTRING_VOID(V8StringResource<>, tagName, V8HiddenValue::getHiddenValue(scr iptState, data, V8HiddenValue::customElementTagName(isolate)));
280 v8::Local<v8::Value> maybeType = V8HiddenValue::getHiddenValue(scriptState, data, V8HiddenValue::customElementType(isolate));
281 TOSTRING_VOID(V8StringResource<>, type, maybeType);
282
283 ExceptionState exceptionState(ExceptionState::ConstructionContext, "CustomEl ement", info.Holder(), info.GetIsolate());
284 CustomElementProcessingStack::CallbackDeliveryScope deliveryScope;
285 Element* element = document->createElementNS(namespaceURI, tagName, maybeTyp e->IsNull() ? nullAtom : type, exceptionState);
286 if (exceptionState.throwIfNeeded())
287 return;
288 v8SetReturnValueFast(info, element, document);
289 }
290
291 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698