Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / web / 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 "public/web/WebBindings.h"
33
34 #include "bindings/core/v8/NPV8Object.h"
35 #include "bindings/core/v8/ScriptController.h"
36 #include "bindings/core/v8/V8DOMWrapper.h"
37 #include "bindings/core/v8/V8Element.h"
38 #include "bindings/core/v8/V8NPObject.h"
39 #include "bindings/core/v8/V8NPUtils.h"
40 #include "bindings/core/v8/V8Range.h"
41 #include "bindings/core/v8/custom/V8ArrayBufferCustom.h"
42 #include "bindings/core/v8/custom/V8ArrayBufferViewCustom.h"
43 #include "bindings/core/v8/npruntime_impl.h"
44 #include "bindings/core/v8/npruntime_priv.h"
45 #include "core/dom/Range.h"
46 #include "core/frame/LocalDOMWindow.h"
47 #include "core/frame/LocalFrame.h"
48 #include "public/platform/WebArrayBuffer.h"
49 #include "public/web/WebArrayBufferView.h"
50 #include "public/web/WebElement.h"
51 #include "public/web/WebRange.h"
52 #include "wtf/ArrayBufferView.h"
53
54 namespace blink {
55
56 bool WebBindings::construct(NPP npp, NPObject* object, const NPVariant* args, uint32_t argCount, NPVariant* result)
57 {
58     return _NPN_Construct(npp, object, args, argCount, result);
59 }
60
61 NPObject* WebBindings::createObject(NPP npp, NPClass* npClass)
62 {
63     return _NPN_CreateObject(npp, npClass);
64 }
65
66 bool WebBindings::enumerate(NPP npp, NPObject* object, NPIdentifier** identifier, uint32_t* identifierCount)
67 {
68     return _NPN_Enumerate(npp, object, identifier, identifierCount);
69 }
70
71 bool WebBindings::evaluate(NPP npp, NPObject* object, NPString* script, NPVariant* result)
72 {
73     return _NPN_Evaluate(npp, object, script, result);
74 }
75
76 bool WebBindings::evaluateHelper(NPP npp, bool popupsAllowed, NPObject* object, NPString* script, NPVariant* result)
77 {
78     return _NPN_EvaluateHelper(npp, popupsAllowed, object, script, result);
79 }
80
81 NPIdentifier WebBindings::getIntIdentifier(int32_t number)
82 {
83     return _NPN_GetIntIdentifier(number);
84 }
85
86 bool WebBindings::getProperty(NPP npp, NPObject* object, NPIdentifier property, NPVariant* result)
87 {
88     return _NPN_GetProperty(npp, object, property, result);
89 }
90
91 NPIdentifier WebBindings::getStringIdentifier(const NPUTF8* string)
92 {
93     return _NPN_GetStringIdentifier(string);
94 }
95
96 void WebBindings::getStringIdentifiers(const NPUTF8** names, int32_t nameCount, NPIdentifier* identifiers)
97 {
98     _NPN_GetStringIdentifiers(names, nameCount, identifiers);
99 }
100
101 bool WebBindings::hasMethod(NPP npp, NPObject* object, NPIdentifier method)
102 {
103     return _NPN_HasMethod(npp, object, method);
104 }
105
106 bool WebBindings::hasProperty(NPP npp, NPObject* object, NPIdentifier property)
107 {
108     return _NPN_HasProperty(npp, object, property);
109 }
110
111 bool WebBindings::identifierIsString(NPIdentifier identifier)
112 {
113     return _NPN_IdentifierIsString(identifier);
114 }
115
116 int32_t WebBindings::intFromIdentifier(NPIdentifier identifier)
117 {
118     return _NPN_IntFromIdentifier(identifier);
119 }
120
121 void WebBindings::initializeVariantWithStringCopy(NPVariant* variant, const NPString* value)
122 {
123     _NPN_InitializeVariantWithStringCopy(variant, value);
124 }
125
126 bool WebBindings::invoke(NPP npp, NPObject* object, NPIdentifier method, const NPVariant* args, uint32_t argCount, NPVariant* result)
127 {
128     return _NPN_Invoke(npp, object, method, args, argCount, result);
129 }
130
131 bool WebBindings::invokeDefault(NPP npp, NPObject* object, const NPVariant* args, uint32_t argCount, NPVariant* result)
132 {
133     return _NPN_InvokeDefault(npp, object, args, argCount, result);
134 }
135
136 void WebBindings::releaseObject(NPObject* object)
137 {
138     return _NPN_ReleaseObject(object);
139 }
140
141 void WebBindings::releaseVariantValue(NPVariant* variant)
142 {
143     _NPN_ReleaseVariantValue(variant);
144 }
145
146 bool WebBindings::removeProperty(NPP npp, NPObject* object, NPIdentifier identifier)
147 {
148     return _NPN_RemoveProperty(npp, object, identifier);
149 }
150
151 NPObject* WebBindings::retainObject(NPObject* object)
152 {
153     return _NPN_RetainObject(object);
154 }
155
156 void WebBindings::setException(NPObject* object, const NPUTF8* message)
157 {
158     _NPN_SetException(object, message);
159 }
160
161 bool WebBindings::setProperty(NPP npp, NPObject* object, NPIdentifier identifier, const NPVariant* value)
162 {
163     return _NPN_SetProperty(npp, object, identifier, value);
164 }
165
166 void WebBindings::registerObjectOwner(NPP)
167 {
168 }
169
170 void WebBindings::unregisterObjectOwner(NPP)
171 {
172 }
173
174 NPP WebBindings::getObjectOwner(NPObject*)
175 {
176     return 0;
177 }
178
179 void WebBindings::unregisterObject(NPObject* object)
180 {
181     _NPN_UnregisterObject(object);
182 }
183
184 void WebBindings::dropV8WrapperForObject(NPObject* object)
185 {
186     forgetV8ObjectForNPObject(object);
187 }
188
189 NPUTF8* WebBindings::utf8FromIdentifier(NPIdentifier identifier)
190 {
191     return _NPN_UTF8FromIdentifier(identifier);
192 }
193
194 void WebBindings::extractIdentifierData(const NPIdentifier& identifier, const NPUTF8*& string, int32_t& number, bool& isString)
195 {
196     PrivateIdentifier* data = static_cast<PrivateIdentifier*>(identifier);
197     if (!data) {
198         isString = false;
199         number = 0;
200         return;
201     }
202
203     isString = data->isString;
204     if (isString)
205         string = data->value.string;
206     else
207         number = data->value.number;
208 }
209
210 static bool getRangeImpl(NPObject* object, WebRange* webRange, v8::Isolate* isolate)
211 {
212     if (!object)
213         return false;
214
215     V8NPObject* v8NPObject = npObjectToV8NPObject(object);
216     if (!v8NPObject)
217         return false;
218
219     v8::HandleScope handleScope(isolate);
220     v8::Handle<v8::Object> v8Object = v8::Local<v8::Object>::New(isolate, v8NPObject->v8Object);
221     if (v8Object.IsEmpty())
222         return false;
223     if (!V8Range::wrapperTypeInfo.equals(toWrapperTypeInfo(v8Object)))
224         return false;
225
226     Range* native = V8Range::hasInstance(v8Object, isolate) ? V8Range::toImpl(v8Object) : 0;
227     if (!native)
228         return false;
229
230     *webRange = WebRange(native);
231     return true;
232 }
233
234 static bool getNodeImpl(NPObject* object, WebNode* webNode, v8::Isolate* isolate)
235 {
236     if (!object)
237         return false;
238
239     V8NPObject* v8NPObject = npObjectToV8NPObject(object);
240     if (!v8NPObject)
241         return false;
242
243     v8::HandleScope handleScope(isolate);
244     v8::Handle<v8::Object> v8Object = v8::Local<v8::Object>::New(isolate, v8NPObject->v8Object);
245     if (v8Object.IsEmpty())
246         return false;
247     Node* native = V8Node::hasInstance(v8Object, isolate) ? V8Node::toImpl(v8Object) : 0;
248     if (!native)
249         return false;
250
251     *webNode = WebNode(native);
252     return true;
253 }
254
255 static bool getElementImpl(NPObject* object, WebElement* webElement, v8::Isolate* isolate)
256 {
257     if (!object)
258         return false;
259
260     V8NPObject* v8NPObject = npObjectToV8NPObject(object);
261     if (!v8NPObject)
262         return false;
263
264     v8::HandleScope handleScope(isolate);
265     v8::Handle<v8::Object> v8Object = v8::Local<v8::Object>::New(isolate, v8NPObject->v8Object);
266     if (v8Object.IsEmpty())
267         return false;
268     Element* native = V8Element::hasInstance(v8Object, isolate) ? V8Element::toImpl(v8Object) : 0;
269     if (!native)
270         return false;
271
272     *webElement = WebElement(native);
273     return true;
274 }
275
276 static bool getArrayBufferImpl(NPObject* object, WebArrayBuffer* arrayBuffer, v8::Isolate* isolate)
277 {
278     if (!object)
279         return false;
280
281     V8NPObject* v8NPObject = npObjectToV8NPObject(object);
282     if (!v8NPObject)
283         return false;
284
285     v8::HandleScope handleScope(isolate);
286     v8::Handle<v8::Object> v8Object = v8::Local<v8::Object>::New(isolate, v8NPObject->v8Object);
287     if (v8Object.IsEmpty())
288         return false;
289     ArrayBuffer* native = V8ArrayBuffer::hasInstance(v8Object, isolate) ? V8ArrayBuffer::toImpl(v8Object) : 0;
290     if (!native)
291         return false;
292
293     *arrayBuffer = WebArrayBuffer(native);
294     return true;
295 }
296
297 static bool getArrayBufferViewImpl(NPObject* object, WebArrayBufferView* arrayBufferView, v8::Isolate* isolate)
298 {
299     if (!object)
300         return false;
301
302     V8NPObject* v8NPObject = npObjectToV8NPObject(object);
303     if (!v8NPObject)
304         return false;
305
306     v8::HandleScope handleScope(isolate);
307     v8::Handle<v8::Object> v8Object = v8::Local<v8::Object>::New(isolate, v8NPObject->v8Object);
308     if (v8Object.IsEmpty())
309         return false;
310     ArrayBufferView* native = V8ArrayBufferView::hasInstance(v8Object, isolate) ? V8ArrayBufferView::toImpl(v8Object) : 0;
311     if (!native)
312         return false;
313
314     *arrayBufferView = WebArrayBufferView(native);
315     return true;
316 }
317
318 static NPObject* makeIntArrayImpl(const WebVector<int>& data, v8::Isolate* isolate)
319 {
320     v8::HandleScope handleScope(isolate);
321     v8::Handle<v8::Array> result = v8::Array::New(isolate, data.size());
322     for (size_t i = 0; i < data.size(); ++i)
323         result->Set(i, v8::Number::New(isolate, data[i]));
324
325     LocalDOMWindow* window = currentDOMWindow(isolate);
326     return npCreateV8ScriptObject(0, result, window, isolate);
327 }
328
329 static NPObject* makeStringArrayImpl(const WebVector<WebString>& data, v8::Isolate* isolate)
330 {
331     v8::HandleScope handleScope(isolate);
332     v8::Handle<v8::Array> result = v8::Array::New(isolate, data.size());
333     for (size_t i = 0; i < data.size(); ++i)
334         result->Set(i, v8String(isolate, data[i]));
335
336     LocalDOMWindow* window = currentDOMWindow(isolate);
337     return npCreateV8ScriptObject(0, result, window, isolate);
338 }
339
340 bool WebBindings::getRange(NPObject* range, WebRange* webRange)
341 {
342     return getRangeImpl(range, webRange, v8::Isolate::GetCurrent());
343 }
344
345 bool WebBindings::getArrayBuffer(NPObject* arrayBuffer, WebArrayBuffer* webArrayBuffer)
346 {
347     return getArrayBufferImpl(arrayBuffer, webArrayBuffer, v8::Isolate::GetCurrent());
348 }
349
350 bool WebBindings::getArrayBufferView(NPObject* arrayBufferView, WebArrayBufferView* webArrayBufferView)
351 {
352     return getArrayBufferViewImpl(arrayBufferView, webArrayBufferView, v8::Isolate::GetCurrent());
353 }
354
355 bool WebBindings::getNode(NPObject* node, WebNode* webNode)
356 {
357     return getNodeImpl(node, webNode, v8::Isolate::GetCurrent());
358 }
359
360 bool WebBindings::getElement(NPObject* element, WebElement* webElement)
361 {
362     return getElementImpl(element, webElement, v8::Isolate::GetCurrent());
363 }
364
365 NPObject* WebBindings::makeIntArray(const WebVector<int>& data)
366 {
367     return makeIntArrayImpl(data, v8::Isolate::GetCurrent());
368 }
369
370 NPObject* WebBindings::makeStringArray(const WebVector<WebString>& data)
371 {
372     return makeStringArrayImpl(data, v8::Isolate::GetCurrent());
373 }
374
375 void WebBindings::pushExceptionHandler(ExceptionHandler handler, void* data)
376 {
377     blink::pushExceptionHandler(handler, data);
378 }
379
380 void WebBindings::popExceptionHandler()
381 {
382     blink::popExceptionHandler();
383 }
384
385 void WebBindings::toNPVariant(v8::Local<v8::Value> object, NPObject* root, NPVariant* result)
386 {
387     convertV8ObjectToNPVariant(object, root, result, v8::Isolate::GetCurrent());
388 }
389
390 v8::Handle<v8::Value> WebBindings::toV8Value(const NPVariant* variant)
391 {
392     v8::Isolate* isolate = v8::Isolate::GetCurrent();
393     if (variant->type == NPVariantType_Object) {
394         NPObject* object = NPVARIANT_TO_OBJECT(*variant);
395         V8NPObject* v8Object = npObjectToV8NPObject(object);
396         if (!v8Object)
397             return v8::Undefined(isolate);
398         return convertNPVariantToV8Object(variant, v8Object->rootObject->frame()->script().windowScriptNPObject(), isolate);
399     }
400     // Safe to pass 0 since we have checked the script object class to make sure the
401     // argument is a primitive v8 type.
402     return convertNPVariantToV8Object(variant, 0, isolate);
403 }
404
405 } // namespace blink