• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "config.h"
32 #include "bindings/v8/CustomElementConstructorBuilder.h"
33 
34 #include "HTMLNames.h"
35 #include "SVGNames.h"
36 #include "V8Document.h"
37 #include "V8HTMLElementWrapperFactory.h"
38 #include "V8SVGElementWrapperFactory.h"
39 #include "bindings/v8/CustomElementBinding.h"
40 #include "bindings/v8/DOMWrapperWorld.h"
41 #include "bindings/v8/Dictionary.h"
42 #include "bindings/v8/ExceptionState.h"
43 #include "bindings/v8/ScriptState.h"
44 #include "bindings/v8/UnsafePersistent.h"
45 #include "bindings/v8/V8Binding.h"
46 #include "bindings/v8/V8HiddenPropertyName.h"
47 #include "bindings/v8/V8PerContextData.h"
48 #include "core/dom/Document.h"
49 #include "core/dom/custom/CustomElementCallbackDispatcher.h"
50 #include "core/dom/custom/CustomElementDefinition.h"
51 #include "core/dom/custom/CustomElementDescriptor.h"
52 #include "core/dom/custom/CustomElementException.h"
53 #include "wtf/Assertions.h"
54 
55 namespace WebCore {
56 
57 static void constructCustomElement(const v8::FunctionCallbackInfo<v8::Value>&);
58 
CustomElementConstructorBuilder(ScriptState * state,const Dictionary * options)59 CustomElementConstructorBuilder::CustomElementConstructorBuilder(ScriptState* state, const Dictionary* options)
60     : m_context(state->context())
61     , m_options(options)
62     , m_wrapperType(0)
63 {
64     ASSERT(m_context == v8::Isolate::GetCurrent()->GetCurrentContext());
65 }
66 
isFeatureAllowed() const67 bool CustomElementConstructorBuilder::isFeatureAllowed() const
68 {
69     // Check that we are in the main world
70     return !DOMWrapperWorld::isolatedWorld(m_context);
71 }
72 
validateOptions(const AtomicString & type,QualifiedName & tagName,ExceptionState & exceptionState)73 bool CustomElementConstructorBuilder::validateOptions(const AtomicString& type, QualifiedName& tagName, ExceptionState& exceptionState)
74 {
75     ASSERT(m_prototype.IsEmpty());
76 
77     ScriptValue prototypeScriptValue;
78     if (m_options->get("prototype", prototypeScriptValue) && !prototypeScriptValue.isNull()) {
79         if (!prototypeScriptValue.isObject()) {
80             CustomElementException::throwException(CustomElementException::PrototypeNotAnObject, type, exceptionState);
81             return false;
82         }
83         m_prototype = prototypeScriptValue.v8Value().As<v8::Object>();
84     } else {
85         m_prototype = v8::Object::New();
86         v8::Local<v8::Object> basePrototype = V8PerContextData::from(m_context)->prototypeForType(&V8HTMLElement::wrapperTypeInfo);
87         if (!basePrototype.IsEmpty())
88             m_prototype->SetPrototype(basePrototype);
89     }
90 
91     String extends;
92     bool extendsProvidedAndNonNull = m_options->get("extends", extends);
93 
94     if (!V8PerContextData::from(m_context)) {
95         // FIXME: This should generate an InvalidContext exception at a later point.
96         CustomElementException::throwException(CustomElementException::ContextDestroyedCheckingPrototype, type, exceptionState);
97         return false;
98     }
99 
100     AtomicString namespaceURI = HTMLNames::xhtmlNamespaceURI;
101     if (hasValidPrototypeChainFor(&V8SVGElement::wrapperTypeInfo))
102         namespaceURI = SVGNames::svgNamespaceURI;
103 
104     AtomicString localName;
105 
106     if (extendsProvidedAndNonNull) {
107         localName = extends.lower();
108 
109         if (!Document::isValidName(localName)) {
110             CustomElementException::throwException(CustomElementException::ExtendsIsInvalidName, type, exceptionState);
111             return false;
112         }
113         if (CustomElement::isValidName(localName)) {
114             CustomElementException::throwException(CustomElementException::ExtendsIsCustomElementName, type, exceptionState);
115             return false;
116         }
117     } else {
118         if (namespaceURI == SVGNames::svgNamespaceURI) {
119             CustomElementException::throwException(CustomElementException::ExtendsIsInvalidName, type, exceptionState);
120             return false;
121         }
122         localName = type;
123     }
124 
125     if (!extendsProvidedAndNonNull)
126         m_wrapperType = &V8HTMLElement::wrapperTypeInfo;
127     else if (namespaceURI == HTMLNames::xhtmlNamespaceURI)
128         m_wrapperType = findWrapperTypeForHTMLTagName(localName);
129     else
130         m_wrapperType = findWrapperTypeForSVGTagName(localName);
131 
132     ASSERT(m_wrapperType);
133     tagName = QualifiedName(nullAtom, localName, namespaceURI);
134     return m_wrapperType;
135 }
136 
createCallbacks()137 PassRefPtr<CustomElementLifecycleCallbacks> CustomElementConstructorBuilder::createCallbacks()
138 {
139     ASSERT(!m_prototype.IsEmpty());
140 
141     RefPtr<ExecutionContext> executionContext(toExecutionContext(m_context));
142 
143     v8::TryCatch exceptionCatcher;
144     exceptionCatcher.SetVerbose(true);
145 
146     v8::Isolate* isolate = v8::Isolate::GetCurrent();
147     v8::Handle<v8::Function> created = retrieveCallback(isolate, "createdCallback");
148     v8::Handle<v8::Function> attached = retrieveCallback(isolate, "attachedCallback");
149     v8::Handle<v8::Function> detached = retrieveCallback(isolate, "detachedCallback");
150     v8::Handle<v8::Function> attributeChanged = retrieveCallback(isolate, "attributeChangedCallback");
151 
152     m_callbacks = V8CustomElementLifecycleCallbacks::create(executionContext.get(), m_prototype, created, attached, detached, attributeChanged);
153     return m_callbacks.get();
154 }
155 
retrieveCallback(v8::Isolate * isolate,const char * name)156 v8::Handle<v8::Function> CustomElementConstructorBuilder::retrieveCallback(v8::Isolate* isolate, const char* name)
157 {
158     v8::Handle<v8::Value> value = m_prototype->Get(v8String(isolate, name));
159     if (value.IsEmpty() || !value->IsFunction())
160         return v8::Handle<v8::Function>();
161     return value.As<v8::Function>();
162 }
163 
createConstructor(Document * document,CustomElementDefinition * definition,ExceptionState & exceptionState)164 bool CustomElementConstructorBuilder::createConstructor(Document* document, CustomElementDefinition* definition, ExceptionState& exceptionState)
165 {
166     ASSERT(!m_prototype.IsEmpty());
167     ASSERT(m_constructor.IsEmpty());
168     ASSERT(document);
169 
170     v8::Isolate* isolate = m_context->GetIsolate();
171 
172     if (!prototypeIsValid(definition->descriptor().type(), exceptionState))
173         return false;
174 
175     v8::Local<v8::FunctionTemplate> constructorTemplate = v8::FunctionTemplate::New(isolate);
176     constructorTemplate->SetCallHandler(constructCustomElement);
177     m_constructor = constructorTemplate->GetFunction();
178     if (m_constructor.IsEmpty()) {
179         CustomElementException::throwException(CustomElementException::ContextDestroyedRegisteringDefinition, definition->descriptor().type(), exceptionState);
180         return false;
181     }
182 
183     const CustomElementDescriptor& descriptor = definition->descriptor();
184 
185     v8::Handle<v8::String> v8TagName = v8String(isolate, descriptor.localName());
186     v8::Handle<v8::Value> v8Type;
187     if (descriptor.isTypeExtension())
188         v8Type = v8String(isolate, descriptor.type());
189     else
190         v8Type = v8::Null(isolate);
191 
192     m_constructor->SetName(v8Type->IsNull() ? v8TagName : v8Type.As<v8::String>());
193 
194     V8HiddenPropertyName::setNamedHiddenReference(m_constructor, "customElementDocument", toV8(document, m_context->Global(), isolate));
195     V8HiddenPropertyName::setNamedHiddenReference(m_constructor, "customElementNamespaceURI", v8String(isolate, descriptor.namespaceURI()));
196     V8HiddenPropertyName::setNamedHiddenReference(m_constructor, "customElementTagName", v8TagName);
197     V8HiddenPropertyName::setNamedHiddenReference(m_constructor, "customElementType", v8Type);
198 
199     v8::Handle<v8::String> prototypeKey = v8String(isolate, "prototype");
200     ASSERT(m_constructor->HasOwnProperty(prototypeKey));
201     // This sets the property *value*; calling Set is safe because
202     // "prototype" is a non-configurable data property so there can be
203     // no side effects.
204     m_constructor->Set(prototypeKey, m_prototype);
205     // This *configures* the property. ForceSet of a function's
206     // "prototype" does not affect the value, but can reconfigure the
207     // property.
208     m_constructor->ForceSet(prototypeKey, m_prototype, v8::PropertyAttribute(v8::ReadOnly | v8::DontEnum | v8::DontDelete));
209 
210     V8HiddenPropertyName::setNamedHiddenReference(m_prototype, "customElementIsInterfacePrototypeObject", v8::True(isolate));
211     m_prototype->ForceSet(v8String(isolate, "constructor"), m_constructor, v8::DontEnum);
212 
213     return true;
214 }
215 
prototypeIsValid(const AtomicString & type,ExceptionState & exceptionState) const216 bool CustomElementConstructorBuilder::prototypeIsValid(const AtomicString& type, ExceptionState& exceptionState) const
217 {
218     if (m_prototype->InternalFieldCount() || !m_prototype->GetHiddenValue(V8HiddenPropertyName::customElementIsInterfacePrototypeObject(m_context->GetIsolate())).IsEmpty()) {
219         CustomElementException::throwException(CustomElementException::PrototypeInUse, type, exceptionState);
220         return false;
221     }
222 
223     if (m_prototype->GetPropertyAttributes(v8String(m_context->GetIsolate(), "constructor")) & v8::DontDelete) {
224         CustomElementException::throwException(CustomElementException::ConstructorPropertyNotConfigurable, type, exceptionState);
225         return false;
226     }
227 
228     return true;
229 }
230 
didRegisterDefinition(CustomElementDefinition * definition) const231 bool CustomElementConstructorBuilder::didRegisterDefinition(CustomElementDefinition* definition) const
232 {
233     ASSERT(!m_constructor.IsEmpty());
234 
235     return m_callbacks->setBinding(definition, CustomElementBinding::create(m_context->GetIsolate(), m_prototype, m_wrapperType));
236 }
237 
bindingsReturnValue() const238 ScriptValue CustomElementConstructorBuilder::bindingsReturnValue() const
239 {
240     return ScriptValue(m_constructor, m_context->GetIsolate());
241 }
242 
hasValidPrototypeChainFor(const WrapperTypeInfo * type) const243 bool CustomElementConstructorBuilder::hasValidPrototypeChainFor(const WrapperTypeInfo* type) const
244 {
245     v8::Handle<v8::Object> elementPrototype = V8PerContextData::from(m_context)->prototypeForType(type);
246     if (elementPrototype.IsEmpty())
247         return false;
248 
249     v8::Handle<v8::Value> chain = m_prototype;
250     while (!chain.IsEmpty() && chain->IsObject()) {
251         if (chain == elementPrototype)
252             return true;
253         chain = chain.As<v8::Object>()->GetPrototype();
254     }
255 
256     return false;
257 }
258 
constructCustomElement(const v8::FunctionCallbackInfo<v8::Value> & info)259 static void constructCustomElement(const v8::FunctionCallbackInfo<v8::Value>& info)
260 {
261     v8::Isolate* isolate = info.GetIsolate();
262 
263     if (!info.IsConstructCall()) {
264         throwTypeError("DOM object constructor cannot be called as a function.", isolate);
265         return;
266     }
267 
268     if (info.Length() > 0) {
269         throwUninformativeAndGenericTypeError(isolate);
270         return;
271     }
272 
273     Document* document = V8Document::toNative(info.Callee()->GetHiddenValue(V8HiddenPropertyName::customElementDocument(isolate)).As<v8::Object>());
274     V8TRYCATCH_FOR_V8STRINGRESOURCE_VOID(V8StringResource<>, namespaceURI, info.Callee()->GetHiddenValue(V8HiddenPropertyName::customElementNamespaceURI(isolate)));
275     V8TRYCATCH_FOR_V8STRINGRESOURCE_VOID(V8StringResource<>, tagName, info.Callee()->GetHiddenValue(V8HiddenPropertyName::customElementTagName(isolate)));
276     v8::Handle<v8::Value> maybeType = info.Callee()->GetHiddenValue(V8HiddenPropertyName::customElementType(isolate));
277     V8TRYCATCH_FOR_V8STRINGRESOURCE_VOID(V8StringResource<>, type, maybeType);
278 
279     ExceptionState exceptionState(ExceptionState::ConstructionContext, "CustomElement", info.Holder(), info.GetIsolate());
280     CustomElementCallbackDispatcher::CallbackDeliveryScope deliveryScope;
281     RefPtr<Element> element = document->createElementNS(namespaceURI, tagName, maybeType->IsNull() ? nullAtom : type, exceptionState);
282     if (exceptionState.throwIfNeeded())
283         return;
284     v8SetReturnValueFast(info, element.release(), document);
285 }
286 
287 } // namespace WebCore
288