Upstream version 10.38.222.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / bindings / core / v8 / CustomElementWrapper.cpp
1 /*
2  * Copyright (C) 2012 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/CustomElementWrapper.h"
33
34 #include "bindings/core/v8/DOMDataStore.h"
35 #include "bindings/core/v8/DOMWrapperWorld.h"
36 #include "bindings/core/v8/V8HTMLElement.h"
37 #include "bindings/core/v8/V8PerContextData.h"
38 #include "bindings/core/v8/V8SVGElement.h"
39 #include "core/V8HTMLElementWrapperFactory.h" // FIXME: should be bindings/core/v8
40 #include "core/V8SVGElementWrapperFactory.h" // FIXME: should be bindings/core/v8
41 #include "core/dom/custom/CustomElement.h"
42 #include "core/html/HTMLElement.h"
43 #include "core/html/HTMLUnknownElement.h"
44 #include "core/svg/SVGElement.h"
45
46 namespace blink {
47
48 template<typename ElementType>
49 v8::Handle<v8::Object> createDirectWrapper(ElementType*, v8::Handle<v8::Object> creationContext, v8::Isolate*);
50
51 template<>
52 v8::Handle<v8::Object> createDirectWrapper<HTMLElement>(HTMLElement* element, v8::Handle<v8::Object> creationContext, v8::Isolate* isolate)
53 {
54     return createV8HTMLDirectWrapper(element, creationContext, isolate);
55 }
56
57 template<>
58 v8::Handle<v8::Object> createDirectWrapper<SVGElement>(SVGElement* element, v8::Handle<v8::Object> creationContext, v8::Isolate* isolate)
59 {
60     return createV8SVGDirectWrapper(element, creationContext, isolate);
61 }
62
63 template<typename ElementType>
64 v8::Handle<v8::Object> createFallbackWrapper(ElementType*, v8::Handle<v8::Object> creationContext, v8::Isolate*);
65
66 template<>
67 v8::Handle<v8::Object> createFallbackWrapper<HTMLElement>(HTMLElement* element, v8::Handle<v8::Object> creationContext, v8::Isolate* isolate)
68 {
69     return createV8HTMLFallbackWrapper(toHTMLUnknownElement(element), creationContext, isolate);
70 }
71
72 template<>
73 v8::Handle<v8::Object> createFallbackWrapper<SVGElement>(SVGElement* element, v8::Handle<v8::Object> creationContext, v8::Isolate* isolate)
74 {
75     return createV8SVGFallbackWrapper(element, creationContext, isolate);
76 }
77
78 template<typename ElementType>
79 v8::Handle<v8::Object> createUpgradeCandidateWrapper(ElementType* element, v8::Handle<v8::Object> creationContext, v8::Isolate* isolate, v8::Handle<v8::Object> (*createSpecificWrapper)(ElementType* element, v8::Handle<v8::Object> creationContext, v8::Isolate*))
80 {
81     if (CustomElement::isValidName(element->localName()))
82         return createDirectWrapper(element, creationContext, isolate);
83     if (createSpecificWrapper)
84         return createSpecificWrapper(element, creationContext, isolate);
85     return createFallbackWrapper(element, creationContext, isolate);
86 }
87
88 template<typename ElementType, typename WrapperType>
89 v8::Handle<v8::Object> CustomElementWrapper<ElementType, WrapperType>::wrap(PassRefPtrWillBeRawPtr<ElementType> element, v8::Handle<v8::Object> creationContext, v8::Isolate* isolate, v8::Handle<v8::Object> (*createSpecificWrapper)(ElementType* element, v8::Handle<v8::Object> creationContext, v8::Isolate*))
90 {
91     ASSERT(DOMDataStore::getWrapper<V8Element>(element.get(), isolate).IsEmpty());
92
93     ASSERT(!creationContext.IsEmpty());
94     v8::Handle<v8::Context> context = creationContext->CreationContext();
95
96     if (!element->isUpgradedCustomElement() || DOMWrapperWorld::world(context).isIsolatedWorld())
97         return createUpgradeCandidateWrapper(element.get(), creationContext, isolate, createSpecificWrapper);
98
99     V8PerContextData* perContextData = V8PerContextData::from(context);
100     if (!perContextData)
101         return v8::Handle<v8::Object>();
102
103     CustomElementBinding* binding = perContextData->customElementBinding(element->customElementDefinition());
104     v8::Handle<v8::Object> wrapper = V8DOMWrapper::createWrapper(creationContext, binding->wrapperType(), WrapperType::toInternalPointer(element.get()), isolate);
105     if (wrapper.IsEmpty())
106         return v8::Handle<v8::Object>();
107
108     wrapper->SetPrototype(binding->prototype());
109
110     V8DOMWrapper::associateObjectWithWrapper<WrapperType>(element, binding->wrapperType(), wrapper, isolate, WrapperConfiguration::Dependent);
111     return wrapper;
112 }
113
114 template
115 class CustomElementWrapper<HTMLElement, V8HTMLElement>;
116
117 template
118 class CustomElementWrapper<SVGElement, V8SVGElement>;
119
120 } // namespace blink