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