tizen beta release
[framework/web/webkit-efl.git] / Source / WebKit / chromium / src / WebBindings.cpp
1 /*
2  * Copyright (C) 2009 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 "WebBindings.h"
33
34 #include "npruntime_impl.h"
35 #include "npruntime_priv.h"
36
37 #if USE(V8)
38 #include "ArrayBufferView.h"
39 #include "DOMWindow.h"
40 #include "NPV8Object.h"  // for PrivateIdentifier
41 #include "Range.h"
42 #include "V8ArrayBufferView.h"
43 #include "V8BindingState.h"
44 #include "V8DOMWrapper.h"
45 #include "V8Element.h"
46 #include "V8NPUtils.h"
47 #include "V8Proxy.h"
48 #include "V8Range.h"
49 #elif USE(JSC)
50 #include "bridge/c/c_utility.h"
51 #endif
52 #include "WebArrayBufferView.h"
53 #include "WebElement.h"
54 #include "WebRange.h"
55
56 using namespace WebCore;
57
58 namespace WebKit {
59
60 bool WebBindings::construct(NPP npp, NPObject* object, const NPVariant* args, uint32_t argCount, NPVariant* result)
61 {
62     return _NPN_Construct(npp, object, args, argCount, result);
63 }
64
65 NPObject* WebBindings::createObject(NPP npp, NPClass* npClass)
66 {
67     return _NPN_CreateObject(npp, npClass);
68 }
69
70 bool WebBindings::enumerate(NPP npp, NPObject* object, NPIdentifier** identifier, uint32_t* identifierCount)
71 {
72     return _NPN_Enumerate(npp, object, identifier, identifierCount);
73 }
74
75 bool WebBindings::evaluate(NPP npp, NPObject* object, NPString* script, NPVariant* result)
76 {
77     return _NPN_Evaluate(npp, object, script, result);
78 }
79
80 bool WebBindings::evaluateHelper(NPP npp, bool popupsAllowed, NPObject* object, NPString* script, NPVariant* result)
81 {
82     return _NPN_EvaluateHelper(npp, popupsAllowed, object, script, result);
83 }
84
85 NPIdentifier WebBindings::getIntIdentifier(int32_t number)
86 {
87     return _NPN_GetIntIdentifier(number);
88 }
89
90 bool WebBindings::getProperty(NPP npp, NPObject* object, NPIdentifier property, NPVariant* result)
91 {
92     return _NPN_GetProperty(npp, object, property, result);
93 }
94
95 NPIdentifier WebBindings::getStringIdentifier(const NPUTF8* string)
96 {
97     return _NPN_GetStringIdentifier(string);
98 }
99
100 void WebBindings::getStringIdentifiers(const NPUTF8** names, int32_t nameCount, NPIdentifier* identifiers)
101 {
102     _NPN_GetStringIdentifiers(names, nameCount, identifiers);
103 }
104
105 bool WebBindings::hasMethod(NPP npp, NPObject* object, NPIdentifier method)
106 {
107     return _NPN_HasMethod(npp, object, method);
108 }
109
110 bool WebBindings::hasProperty(NPP npp, NPObject* object, NPIdentifier property)
111 {
112     return _NPN_HasProperty(npp, object, property);
113 }
114
115 bool WebBindings::identifierIsString(NPIdentifier identifier)
116 {
117     return _NPN_IdentifierIsString(identifier);
118 }
119
120 int32_t WebBindings::intFromIdentifier(NPIdentifier identifier)
121 {
122     return _NPN_IntFromIdentifier(identifier);
123 }
124
125 void WebBindings::initializeVariantWithStringCopy(NPVariant* variant, const NPString* value)
126 {
127 #if USE(V8)
128     _NPN_InitializeVariantWithStringCopy(variant, value);
129 #else
130     NPN_InitializeVariantWithStringCopy(variant, value);
131 #endif
132 }
133
134 bool WebBindings::invoke(NPP npp, NPObject* object, NPIdentifier method, const NPVariant* args, uint32_t argCount, NPVariant* result)
135 {
136     return _NPN_Invoke(npp, object, method, args, argCount, result);
137 }
138
139 bool WebBindings::invokeDefault(NPP npp, NPObject* object, const NPVariant* args, uint32_t argCount, NPVariant* result)
140 {
141     return _NPN_InvokeDefault(npp, object, args, argCount, result);
142 }
143
144 void WebBindings::releaseObject(NPObject* object)
145 {
146     return _NPN_ReleaseObject(object);
147 }
148
149 void WebBindings::releaseVariantValue(NPVariant* variant)
150 {
151     _NPN_ReleaseVariantValue(variant);
152 }
153
154 bool WebBindings::removeProperty(NPP npp, NPObject* object, NPIdentifier identifier)
155 {
156     return _NPN_RemoveProperty(npp, object, identifier);
157 }
158
159 NPObject* WebBindings::retainObject(NPObject* object)
160 {
161     return _NPN_RetainObject(object);
162 }
163
164 void WebBindings::setException(NPObject* object, const NPUTF8* message)
165 {
166     _NPN_SetException(object, message);
167 }
168
169 bool WebBindings::setProperty(NPP npp, NPObject* object, NPIdentifier identifier, const NPVariant* value)
170 {
171     return _NPN_SetProperty(npp, object, identifier, value);
172 }
173
174 void WebBindings::unregisterObject(NPObject* object)
175 {
176 #if USE(V8)
177     _NPN_UnregisterObject(object);
178 #endif
179 }
180
181 NPUTF8* WebBindings::utf8FromIdentifier(NPIdentifier identifier)
182 {
183     return _NPN_UTF8FromIdentifier(identifier);
184 }
185
186 void WebBindings::extractIdentifierData(const NPIdentifier& identifier, const NPUTF8*& string, int32_t& number, bool& isString)
187 {
188     PrivateIdentifier* data = static_cast<PrivateIdentifier*>(identifier);
189     if (!data) {
190         isString = false;
191         number = 0;
192         return;
193     }
194
195     isString = data->isString;
196     if (isString)
197         string = data->value.string;
198     else
199         number = data->value.number;
200 }
201
202 #if USE(V8)
203
204 static bool getRangeImpl(NPObject* object, WebRange* webRange)
205 {
206     if (!object || (object->_class != npScriptObjectClass))
207         return false;
208
209     V8NPObject* v8NPObject = reinterpret_cast<V8NPObject*>(object);
210     v8::Handle<v8::Object> v8Object(v8NPObject->v8Object);
211     if (!V8Range::info.equals(V8DOMWrapper::domWrapperType(v8Object)))
212         return false;
213
214     Range* native = V8Range::HasInstance(v8Object) ? V8Range::toNative(v8Object) : 0;
215     if (!native)
216         return false;
217
218     *webRange = WebRange(native);
219     return true;
220 }
221
222 static bool getElementImpl(NPObject* object, WebElement* webElement)
223 {
224     if (!object || (object->_class != npScriptObjectClass))
225         return false;
226
227     V8NPObject* v8NPObject = reinterpret_cast<V8NPObject*>(object);
228     v8::Handle<v8::Object> v8Object(v8NPObject->v8Object);
229     Element* native = V8Element::HasInstance(v8Object) ? V8Element::toNative(v8Object) : 0;
230     if (!native)
231         return false;
232
233     *webElement = WebElement(native);
234     return true;
235 }
236
237 static bool getArrayBufferViewImpl(NPObject* object, WebArrayBufferView* arrayBufferView)
238 {
239     if (!object || (object->_class != npScriptObjectClass))
240         return false;
241
242     V8NPObject* v8NPObject = reinterpret_cast<V8NPObject*>(object);
243     v8::Handle<v8::Object> v8Object(v8NPObject->v8Object);
244     ArrayBufferView* native = V8ArrayBufferView::HasInstance(v8Object) ? V8ArrayBufferView::toNative(v8Object) : 0;
245     if (!native)
246         return false;
247
248     *arrayBufferView = WebArrayBufferView(native);
249     return true;
250 }
251
252 static NPObject* makeIntArrayImpl(const WebVector<int>& data)
253 {
254     v8::HandleScope handleScope;
255     v8::Handle<v8::Array> result = v8::Array::New(data.size());
256     for (size_t i = 0; i < data.size(); ++i)
257         result->Set(i, v8::Number::New(data[i]));
258
259     DOMWindow* window = V8Proxy::retrieveWindow(V8Proxy::currentContext());
260     return npCreateV8ScriptObject(0, result, window);
261 }
262
263 static NPObject* makeStringArrayImpl(const WebVector<WebString>& data)
264 {
265     v8::HandleScope handleScope;
266     v8::Handle<v8::Array> result = v8::Array::New(data.size());
267     for (size_t i = 0; i < data.size(); ++i)
268         result->Set(i, data[i].data() ? v8::String::New(reinterpret_cast<const uint16_t*>((data[i].data())), data[i].length()) : v8::String::New(""));
269
270     DOMWindow* window = V8Proxy::retrieveWindow(V8Proxy::currentContext());
271     return npCreateV8ScriptObject(0, result, window);
272 }
273
274 #endif
275
276 bool WebBindings::getRange(NPObject* range, WebRange* webRange)
277 {
278 #if USE(V8)
279     return getRangeImpl(range, webRange);
280 #else
281     // Not supported on other ports (JSC, etc).
282     return false;
283 #endif
284 }
285
286 bool WebBindings::getArrayBufferView(NPObject* arrayBufferView, WebArrayBufferView* webArrayBufferView)
287 {
288 #if USE(V8)
289     return getArrayBufferViewImpl(arrayBufferView, webArrayBufferView);
290 #else
291     // Not supported on other ports (JSC, etc).
292     return false;
293 #endif
294 }
295
296 bool WebBindings::getElement(NPObject* element, WebElement* webElement)
297 {
298 #if USE(V8)
299     return getElementImpl(element, webElement);
300 #else
301     // Not supported on other ports (JSC, etc.).
302     return false;
303 #endif
304 }
305
306 NPObject* WebBindings::makeIntArray(const WebVector<int>& data)
307 {
308 #if USE(V8)
309     return makeIntArrayImpl(data);
310 #else
311     // Not supported on other ports (JSC, etc.).
312     return 0;
313 #endif
314 }
315
316 NPObject* WebBindings::makeStringArray(const WebVector<WebString>& data)
317 {
318 #if USE(V8)
319     return makeStringArrayImpl(data);
320 #else
321     // Not supported on other ports (JSC, etc.).
322     return 0;
323 #endif
324 }
325
326 void WebBindings::pushExceptionHandler(ExceptionHandler handler, void* data)
327 {
328     WebCore::pushExceptionHandler(handler, data);
329 }
330
331 void WebBindings::popExceptionHandler()
332 {
333     WebCore::popExceptionHandler();
334 }
335
336 #if WEBKIT_USING_V8
337 void WebBindings::toNPVariant(v8::Local<v8::Value> object, NPObject* root, NPVariant* result)
338 {
339     WebCore::convertV8ObjectToNPVariant(object, root, result);
340 }
341
342 v8::Handle<v8::Value> WebBindings::toV8Value(const NPVariant* variant)
343 {
344     if (variant->type == NPVariantType_Object) {
345         NPObject* object = NPVARIANT_TO_OBJECT(*variant);
346         if (object->_class != npScriptObjectClass)
347             return v8::Undefined();
348         V8NPObject* v8Object = reinterpret_cast<V8NPObject*>(object);
349         return convertNPVariantToV8Object(variant, v8Object->rootObject->frame()->script()->windowScriptNPObject());
350     }
351     // Safe to pass 0 since we have checked the script object class to make sure the
352     // argument is a primitive v8 type.
353     return convertNPVariantToV8Object(variant, 0);
354 }
355 #endif
356
357 } // namespace WebKit