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