Upstream version 9.38.198.0
[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::toNative(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::toNative(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     if (instance->Set(property, value))
105         v8SetReturnValue(info, value);
106 }
107 } // namespace
108
109 void V8HTMLAppletElement::namedPropertyGetterCustom(v8::Local<v8::String> name, const v8::PropertyCallbackInfo<v8::Value>& info)
110 {
111     getScriptableObjectProperty<V8HTMLAppletElement>(name, info);
112 }
113
114 void V8HTMLEmbedElement::namedPropertyGetterCustom(v8::Local<v8::String> name, const v8::PropertyCallbackInfo<v8::Value>& info)
115 {
116     getScriptableObjectProperty<V8HTMLEmbedElement>(name, info);
117 }
118
119 void V8HTMLObjectElement::namedPropertyGetterCustom(v8::Local<v8::String> name, const v8::PropertyCallbackInfo<v8::Value>& info)
120 {
121     getScriptableObjectProperty<V8HTMLObjectElement>(name, info);
122 }
123
124 void V8HTMLAppletElement::namedPropertySetterCustom(v8::Local<v8::String> name, v8::Local<v8::Value> value, const v8::PropertyCallbackInfo<v8::Value>& info)
125 {
126     setScriptableObjectProperty<V8HTMLAppletElement>(name, value, info);
127 }
128
129 void V8HTMLEmbedElement::namedPropertySetterCustom(v8::Local<v8::String> name, v8::Local<v8::Value> value, const v8::PropertyCallbackInfo<v8::Value>& info)
130 {
131     setScriptableObjectProperty<V8HTMLEmbedElement>(name, value, info);
132 }
133
134 void V8HTMLObjectElement::namedPropertySetterCustom(v8::Local<v8::String> name, v8::Local<v8::Value> value, const v8::PropertyCallbackInfo<v8::Value>& info)
135 {
136     setScriptableObjectProperty<V8HTMLObjectElement>(name, value, info);
137 }
138
139 void V8HTMLAppletElement::indexedPropertyGetterCustom(uint32_t index, const v8::PropertyCallbackInfo<v8::Value>& info)
140 {
141     getScriptableObjectProperty<V8HTMLAppletElement>(index, info);
142 }
143
144 void V8HTMLEmbedElement::indexedPropertyGetterCustom(uint32_t index, const v8::PropertyCallbackInfo<v8::Value>& info)
145 {
146     getScriptableObjectProperty<V8HTMLEmbedElement>(index, info);
147 }
148
149 void V8HTMLObjectElement::indexedPropertyGetterCustom(uint32_t index, const v8::PropertyCallbackInfo<v8::Value>& info)
150 {
151     getScriptableObjectProperty<V8HTMLObjectElement>(index, info);
152 }
153
154 void V8HTMLAppletElement::indexedPropertySetterCustom(uint32_t index, v8::Local<v8::Value> value, const v8::PropertyCallbackInfo<v8::Value>& info)
155 {
156     setScriptableObjectProperty<V8HTMLAppletElement>(index, value, info);
157 }
158
159 void V8HTMLEmbedElement::indexedPropertySetterCustom(uint32_t index, v8::Local<v8::Value> value, const v8::PropertyCallbackInfo<v8::Value>& info)
160 {
161     setScriptableObjectProperty<V8HTMLEmbedElement>(index, value, info);
162 }
163
164 void V8HTMLObjectElement::indexedPropertySetterCustom(uint32_t index, v8::Local<v8::Value> value, const v8::PropertyCallbackInfo<v8::Value>& info)
165 {
166     setScriptableObjectProperty<V8HTMLObjectElement>(index, value, info);
167 }
168
169 namespace {
170
171 template <typename ElementType>
172 void invokeOnScriptableObject(const v8::FunctionCallbackInfo<v8::Value>& info)
173 {
174     HTMLPlugInElement* impl = ElementType::toNative(info.Holder());
175     RefPtr<SharedPersistent<v8::Object> > wrapper = impl->pluginWrapper();
176     if (!wrapper)
177         return;
178
179     v8::Local<v8::Object> instance = wrapper->newLocal(info.GetIsolate());
180     if (instance.IsEmpty())
181         return;
182
183     WTF::OwnPtr<v8::Handle<v8::Value>[] > arguments = adoptArrayPtr(new v8::Handle<v8::Value>[info.Length()]);
184     for (int i = 0; i < info.Length(); ++i)
185         arguments[i] = info[i];
186
187     TONATIVE_VOID(v8::Local<v8::Value>, retVal, instance->CallAsFunction(info.This(), info.Length(), arguments.get()));
188     v8SetReturnValue(info, retVal);
189 }
190
191 } // namespace
192
193 void V8HTMLAppletElement::legacyCallCustom(const v8::FunctionCallbackInfo<v8::Value>& info)
194 {
195     invokeOnScriptableObject<V8HTMLAppletElement>(info);
196     UseCounter::count(V8HTMLAppletElement::toNative(info.Holder())->document(), UseCounter::HTMLAppletElementLegacyCall);
197 }
198
199 void V8HTMLEmbedElement::legacyCallCustom(const v8::FunctionCallbackInfo<v8::Value>& info)
200 {
201     invokeOnScriptableObject<V8HTMLEmbedElement>(info);
202     UseCounter::count(V8HTMLEmbedElement::toNative(info.Holder())->document(), UseCounter::HTMLEmbedElementLegacyCall);
203 }
204
205 void V8HTMLObjectElement::legacyCallCustom(const v8::FunctionCallbackInfo<v8::Value>& info)
206 {
207     invokeOnScriptableObject<V8HTMLObjectElement>(info);
208     UseCounter::count(V8HTMLObjectElement::toNative(info.Holder())->document(), UseCounter::HTMLObjectElementLegacyCall);
209 }
210
211 } // namespace blink