2730cce547b0ac730412b6008953a5dc2597d63d
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / bindings / core / v8 / custom / V8HTMLPlugInElementCustom.cpp
1 /*
2 * Copyright (C) 2009 Google Inc. All rights reserved.
3 * Copyright (C) 2014 Opera Software ASA. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met:
8 *
9 *     * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *     * Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following disclaimer
13 * in the documentation and/or other materials provided with the
14 * distribution.
15 *     * Neither the name of Google Inc. nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #include "config.h"
33
34 #include "bindings/core/v8/NPV8Object.h"
35 #include "bindings/core/v8/SharedPersistent.h"
36 #include "bindings/core/v8/V8Binding.h"
37 #include "bindings/core/v8/V8HTMLAppletElement.h"
38 #include "bindings/core/v8/V8HTMLEmbedElement.h"
39 #include "bindings/core/v8/V8HTMLObjectElement.h"
40 #include "bindings/core/v8/V8NPObject.h"
41 #include "core/frame/UseCounter.h"
42 #include "wtf/OwnPtr.h"
43 #include "wtf/PassOwnPtr.h"
44
45 namespace blink {
46
47 namespace {
48
49 template <typename ElementType, typename PropertyType>
50 void getScriptableObjectProperty(PropertyType property, const v8::PropertyCallbackInfo<v8::Value>& info)
51 {
52     HTMLPlugInElement* impl = ElementType::toImpl(info.Holder());
53     RefPtr<SharedPersistent<v8::Object> > wrapper = impl->pluginWrapper();
54     if (!wrapper)
55         return;
56
57     v8::Local<v8::Object> instance = wrapper->newLocal(info.GetIsolate());
58     if (instance.IsEmpty())
59         return;
60
61     TONATIVE_VOID(v8::Local<v8::Value>, value, instance->Get(property));
62
63     // We quit here to allow the binding code to look up general HTMLObjectElement properties
64     // if they are not overriden by plugin.
65     if (value->IsUndefined())
66         return;
67
68     v8SetReturnValue(info, value);
69 }
70
71 namespace {
72 void callNpObjectSetter(v8::Local<v8::Object> self, v8::Local<v8::String> name, v8::Local<v8::Value> value, const v8::PropertyCallbackInfo<v8::Value>& info)
73 {
74     npObjectSetNamedProperty(self, name, value, info);
75 }
76
77 void callNpObjectSetter(v8::Local<v8::Object> self, uint32_t index, v8::Local<v8::Value> value, const v8::PropertyCallbackInfo<v8::Value>& info)
78 {
79     npObjectSetIndexedProperty(self, index, value, info);
80 }
81 }
82
83 template <typename ElementType, typename PropertyType>
84 void setScriptableObjectProperty(PropertyType property, v8::Local<v8::Value> value, const v8::PropertyCallbackInfo<v8::Value>& info)
85 {
86     HTMLPlugInElement* impl = ElementType::toImpl(info.Holder());
87     RefPtr<SharedPersistent<v8::Object> > wrapper = impl->pluginWrapper();
88     if (!wrapper)
89         return;
90
91     v8::Local<v8::Object> instance = wrapper->newLocal(info.GetIsolate());
92     if (instance.IsEmpty())
93         return;
94
95     // We need to directly call setter on NPObject to be able to detect
96     // situation where NPObject notifies it does not possess the property
97     // to be able to lookup standard DOM property.
98     // This information is lost when retrieving it through v8::Object.
99     if (isWrappedNPObject(instance)) {
100         callNpObjectSetter(instance, property, value, info);
101         return;
102     }
103 }
104 } // namespace
105
106 void V8HTMLAppletElement::namedPropertyGetterCustom(v8::Local<v8::String> name, const v8::PropertyCallbackInfo<v8::Value>& info)
107 {
108     getScriptableObjectProperty<V8HTMLAppletElement>(name, info);
109 }
110
111 void V8HTMLEmbedElement::namedPropertyGetterCustom(v8::Local<v8::String> name, const v8::PropertyCallbackInfo<v8::Value>& info)
112 {
113     getScriptableObjectProperty<V8HTMLEmbedElement>(name, info);
114 }
115
116 void V8HTMLObjectElement::namedPropertyGetterCustom(v8::Local<v8::String> name, const v8::PropertyCallbackInfo<v8::Value>& info)
117 {
118     getScriptableObjectProperty<V8HTMLObjectElement>(name, info);
119 }
120
121 void V8HTMLAppletElement::namedPropertySetterCustom(v8::Local<v8::String> name, v8::Local<v8::Value> value, const v8::PropertyCallbackInfo<v8::Value>& info)
122 {
123     setScriptableObjectProperty<V8HTMLAppletElement>(name, value, info);
124 }
125
126 void V8HTMLEmbedElement::namedPropertySetterCustom(v8::Local<v8::String> name, v8::Local<v8::Value> value, const v8::PropertyCallbackInfo<v8::Value>& info)
127 {
128     setScriptableObjectProperty<V8HTMLEmbedElement>(name, value, info);
129 }
130
131 void V8HTMLObjectElement::namedPropertySetterCustom(v8::Local<v8::String> name, v8::Local<v8::Value> value, const v8::PropertyCallbackInfo<v8::Value>& info)
132 {
133     setScriptableObjectProperty<V8HTMLObjectElement>(name, value, info);
134 }
135
136 void V8HTMLAppletElement::indexedPropertyGetterCustom(uint32_t index, const v8::PropertyCallbackInfo<v8::Value>& info)
137 {
138     getScriptableObjectProperty<V8HTMLAppletElement>(index, info);
139 }
140
141 void V8HTMLEmbedElement::indexedPropertyGetterCustom(uint32_t index, const v8::PropertyCallbackInfo<v8::Value>& info)
142 {
143     getScriptableObjectProperty<V8HTMLEmbedElement>(index, info);
144 }
145
146 void V8HTMLObjectElement::indexedPropertyGetterCustom(uint32_t index, const v8::PropertyCallbackInfo<v8::Value>& info)
147 {
148     getScriptableObjectProperty<V8HTMLObjectElement>(index, info);
149 }
150
151 void V8HTMLAppletElement::indexedPropertySetterCustom(uint32_t index, v8::Local<v8::Value> value, const v8::PropertyCallbackInfo<v8::Value>& info)
152 {
153     setScriptableObjectProperty<V8HTMLAppletElement>(index, value, info);
154 }
155
156 void V8HTMLEmbedElement::indexedPropertySetterCustom(uint32_t index, v8::Local<v8::Value> value, const v8::PropertyCallbackInfo<v8::Value>& info)
157 {
158     setScriptableObjectProperty<V8HTMLEmbedElement>(index, value, info);
159 }
160
161 void V8HTMLObjectElement::indexedPropertySetterCustom(uint32_t index, v8::Local<v8::Value> value, const v8::PropertyCallbackInfo<v8::Value>& info)
162 {
163     setScriptableObjectProperty<V8HTMLObjectElement>(index, value, info);
164 }
165
166 namespace {
167
168 template <typename ElementType>
169 void invokeOnScriptableObject(const v8::FunctionCallbackInfo<v8::Value>& info)
170 {
171     HTMLPlugInElement* impl = ElementType::toImpl(info.Holder());
172     RefPtr<SharedPersistent<v8::Object> > wrapper = impl->pluginWrapper();
173     if (!wrapper)
174         return;
175
176     v8::Local<v8::Object> instance = wrapper->newLocal(info.GetIsolate());
177     if (instance.IsEmpty())
178         return;
179
180     WTF::OwnPtr<v8::Handle<v8::Value>[] > arguments = adoptArrayPtr(new v8::Handle<v8::Value>[info.Length()]);
181     for (int i = 0; i < info.Length(); ++i)
182         arguments[i] = info[i];
183
184     TONATIVE_VOID(v8::Local<v8::Value>, retVal, instance->CallAsFunction(info.This(), info.Length(), arguments.get()));
185     v8SetReturnValue(info, retVal);
186 }
187
188 } // namespace
189
190 void V8HTMLAppletElement::legacyCallCustom(const v8::FunctionCallbackInfo<v8::Value>& info)
191 {
192     invokeOnScriptableObject<V8HTMLAppletElement>(info);
193     UseCounter::count(V8HTMLAppletElement::toImpl(info.Holder())->document(), UseCounter::HTMLAppletElementLegacyCall);
194 }
195
196 void V8HTMLEmbedElement::legacyCallCustom(const v8::FunctionCallbackInfo<v8::Value>& info)
197 {
198     invokeOnScriptableObject<V8HTMLEmbedElement>(info);
199     UseCounter::count(V8HTMLEmbedElement::toImpl(info.Holder())->document(), UseCounter::HTMLEmbedElementLegacyCall);
200 }
201
202 void V8HTMLObjectElement::legacyCallCustom(const v8::FunctionCallbackInfo<v8::Value>& info)
203 {
204     invokeOnScriptableObject<V8HTMLObjectElement>(info);
205     UseCounter::count(V8HTMLObjectElement::toImpl(info.Holder())->document(), UseCounter::HTMLObjectElementLegacyCall);
206 }
207
208 } // namespace blink