blob: 1b138c66f4749dc92fd5662a54dea5cdf56feaca [file] [log] [blame]
Ben Murdoche69819b2013-07-17 14:56:49 +01001/*
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
Ben Murdoche69819b2013-07-17 14:56:49 +010034#include "V8Element.h"
35#include "V8Range.h"
Ben Murdoche69819b2013-07-17 14:56:49 +010036#include "WebArrayBufferView.h"
37#include "WebElement.h"
38#include "WebRange.h"
39#include "bindings/v8/NPV8Object.h" // for PrivateIdentifier
40#include "bindings/v8/ScriptController.h"
41#include "bindings/v8/V8DOMWrapper.h"
42#include "bindings/v8/V8NPUtils.h"
43#include "bindings/v8/custom/V8ArrayBufferCustom.h"
Ben Murdoch3c9e4ae2013-08-12 14:20:44 +010044#include "bindings/v8/custom/V8ArrayBufferViewCustom.h"
Ben Murdoche69819b2013-07-17 14:56:49 +010045#include "bindings/v8/npruntime_impl.h"
46#include "bindings/v8/npruntime_priv.h"
47#include "core/dom/Range.h"
Torne (Richard Coles)1e202182013-10-18 15:46:42 +010048#include "core/frame/DOMWindow.h"
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +000049#include "core/frame/LocalFrame.h"
Ben Murdoch0019e4e2013-07-18 11:57:54 +010050#include "public/platform/WebArrayBuffer.h"
Ben Murdoche69819b2013-07-17 14:56:49 +010051#include "wtf/ArrayBufferView.h"
52
53using namespace WebCore;
54
Torne (Richard Coles)51b29062013-11-28 11:56:03 +000055namespace blink {
Ben Murdoche69819b2013-07-17 14:56:49 +010056
57bool WebBindings::construct(NPP npp, NPObject* object, const NPVariant* args, uint32_t argCount, NPVariant* result)
58{
59 return _NPN_Construct(npp, object, args, argCount, result);
60}
61
62NPObject* WebBindings::createObject(NPP npp, NPClass* npClass)
63{
64 return _NPN_CreateObject(npp, npClass);
65}
66
67bool WebBindings::enumerate(NPP npp, NPObject* object, NPIdentifier** identifier, uint32_t* identifierCount)
68{
69 return _NPN_Enumerate(npp, object, identifier, identifierCount);
70}
71
72bool WebBindings::evaluate(NPP npp, NPObject* object, NPString* script, NPVariant* result)
73{
74 return _NPN_Evaluate(npp, object, script, result);
75}
76
77bool WebBindings::evaluateHelper(NPP npp, bool popupsAllowed, NPObject* object, NPString* script, NPVariant* result)
78{
79 return _NPN_EvaluateHelper(npp, popupsAllowed, object, script, result);
80}
81
82NPIdentifier WebBindings::getIntIdentifier(int32_t number)
83{
84 return _NPN_GetIntIdentifier(number);
85}
86
87bool WebBindings::getProperty(NPP npp, NPObject* object, NPIdentifier property, NPVariant* result)
88{
89 return _NPN_GetProperty(npp, object, property, result);
90}
91
92NPIdentifier WebBindings::getStringIdentifier(const NPUTF8* string)
93{
94 return _NPN_GetStringIdentifier(string);
95}
96
97void WebBindings::getStringIdentifiers(const NPUTF8** names, int32_t nameCount, NPIdentifier* identifiers)
98{
99 _NPN_GetStringIdentifiers(names, nameCount, identifiers);
100}
101
102bool WebBindings::hasMethod(NPP npp, NPObject* object, NPIdentifier method)
103{
104 return _NPN_HasMethod(npp, object, method);
105}
106
107bool WebBindings::hasProperty(NPP npp, NPObject* object, NPIdentifier property)
108{
109 return _NPN_HasProperty(npp, object, property);
110}
111
112bool WebBindings::identifierIsString(NPIdentifier identifier)
113{
114 return _NPN_IdentifierIsString(identifier);
115}
116
117int32_t WebBindings::intFromIdentifier(NPIdentifier identifier)
118{
119 return _NPN_IntFromIdentifier(identifier);
120}
121
122void WebBindings::initializeVariantWithStringCopy(NPVariant* variant, const NPString* value)
123{
124 _NPN_InitializeVariantWithStringCopy(variant, value);
125}
126
127bool WebBindings::invoke(NPP npp, NPObject* object, NPIdentifier method, const NPVariant* args, uint32_t argCount, NPVariant* result)
128{
129 return _NPN_Invoke(npp, object, method, args, argCount, result);
130}
131
132bool WebBindings::invokeDefault(NPP npp, NPObject* object, const NPVariant* args, uint32_t argCount, NPVariant* result)
133{
134 return _NPN_InvokeDefault(npp, object, args, argCount, result);
135}
136
137void WebBindings::releaseObject(NPObject* object)
138{
139 return _NPN_ReleaseObject(object);
140}
141
142void WebBindings::releaseVariantValue(NPVariant* variant)
143{
144 _NPN_ReleaseVariantValue(variant);
145}
146
147bool WebBindings::removeProperty(NPP npp, NPObject* object, NPIdentifier identifier)
148{
149 return _NPN_RemoveProperty(npp, object, identifier);
150}
151
152NPObject* WebBindings::retainObject(NPObject* object)
153{
154 return _NPN_RetainObject(object);
155}
156
157void WebBindings::setException(NPObject* object, const NPUTF8* message)
158{
159 _NPN_SetException(object, message);
160}
161
162bool WebBindings::setProperty(NPP npp, NPObject* object, NPIdentifier identifier, const NPVariant* value)
163{
164 return _NPN_SetProperty(npp, object, identifier, value);
165}
166
167void WebBindings::registerObjectOwner(NPP)
168{
169}
170
171void WebBindings::unregisterObjectOwner(NPP)
172{
173}
174
175NPP WebBindings::getObjectOwner(NPObject*)
176{
177 return 0;
178}
179
180void WebBindings::unregisterObject(NPObject* object)
181{
182 _NPN_UnregisterObject(object);
183}
184
185NPUTF8* WebBindings::utf8FromIdentifier(NPIdentifier identifier)
186{
187 return _NPN_UTF8FromIdentifier(identifier);
188}
189
190void WebBindings::extractIdentifierData(const NPIdentifier& identifier, const NPUTF8*& string, int32_t& number, bool& isString)
191{
192 PrivateIdentifier* data = static_cast<PrivateIdentifier*>(identifier);
193 if (!data) {
194 isString = false;
195 number = 0;
196 return;
197 }
198
199 isString = data->isString;
200 if (isString)
201 string = data->value.string;
202 else
203 number = data->value.number;
204}
205
206static bool getRangeImpl(NPObject* object, WebRange* webRange, v8::Isolate* isolate)
207{
208 if (!object)
209 return false;
210
211 V8NPObject* v8NPObject = npObjectToV8NPObject(object);
212 if (!v8NPObject)
213 return false;
214
215 v8::HandleScope handleScope(isolate);
216 v8::Handle<v8::Object> v8Object = v8::Local<v8::Object>::New(isolate, v8NPObject->v8Object);
217 if (v8Object.IsEmpty())
218 return false;
Torne (Richard Coles)f79f16f2013-10-31 11:16:44 +0000219 if (!V8Range::wrapperTypeInfo.equals(toWrapperTypeInfo(v8Object)))
Ben Murdoche69819b2013-07-17 14:56:49 +0100220 return false;
221
Torne (Richard Coles)09380292014-02-21 12:17:33 +0000222 Range* native = V8Range::hasInstance(v8Object, isolate) ? V8Range::toNative(v8Object) : 0;
Ben Murdoche69819b2013-07-17 14:56:49 +0100223 if (!native)
224 return false;
225
226 *webRange = WebRange(native);
227 return true;
228}
229
230static bool getNodeImpl(NPObject* object, WebNode* webNode, v8::Isolate* isolate)
231{
232 if (!object)
233 return false;
234
235 V8NPObject* v8NPObject = npObjectToV8NPObject(object);
236 if (!v8NPObject)
237 return false;
238
239 v8::HandleScope handleScope(isolate);
240 v8::Handle<v8::Object> v8Object = v8::Local<v8::Object>::New(isolate, v8NPObject->v8Object);
241 if (v8Object.IsEmpty())
242 return false;
Torne (Richard Coles)09380292014-02-21 12:17:33 +0000243 Node* native = V8Node::hasInstance(v8Object, isolate) ? V8Node::toNative(v8Object) : 0;
Ben Murdoche69819b2013-07-17 14:56:49 +0100244 if (!native)
245 return false;
246
247 *webNode = WebNode(native);
248 return true;
249}
250
251static bool getElementImpl(NPObject* object, WebElement* webElement, v8::Isolate* isolate)
252{
253 if (!object)
254 return false;
255
256 V8NPObject* v8NPObject = npObjectToV8NPObject(object);
257 if (!v8NPObject)
258 return false;
259
260 v8::HandleScope handleScope(isolate);
261 v8::Handle<v8::Object> v8Object = v8::Local<v8::Object>::New(isolate, v8NPObject->v8Object);
262 if (v8Object.IsEmpty())
263 return false;
Torne (Richard Coles)09380292014-02-21 12:17:33 +0000264 Element* native = V8Element::hasInstance(v8Object, isolate) ? V8Element::toNative(v8Object) : 0;
Ben Murdoche69819b2013-07-17 14:56:49 +0100265 if (!native)
266 return false;
267
268 *webElement = WebElement(native);
269 return true;
270}
271
272static bool getArrayBufferImpl(NPObject* object, WebArrayBuffer* arrayBuffer, v8::Isolate* isolate)
273{
274 if (!object)
275 return false;
276
277 V8NPObject* v8NPObject = npObjectToV8NPObject(object);
278 if (!v8NPObject)
279 return false;
280
281 v8::HandleScope handleScope(isolate);
282 v8::Handle<v8::Object> v8Object = v8::Local<v8::Object>::New(isolate, v8NPObject->v8Object);
283 if (v8Object.IsEmpty())
284 return false;
Torne (Richard Coles)09380292014-02-21 12:17:33 +0000285 ArrayBuffer* native = V8ArrayBuffer::hasInstance(v8Object, isolate) ? V8ArrayBuffer::toNative(v8Object) : 0;
Ben Murdoche69819b2013-07-17 14:56:49 +0100286 if (!native)
287 return false;
288
289 *arrayBuffer = WebArrayBuffer(native);
290 return true;
291}
292
293static bool getArrayBufferViewImpl(NPObject* object, WebArrayBufferView* arrayBufferView, v8::Isolate* isolate)
294{
295 if (!object)
296 return false;
297
298 V8NPObject* v8NPObject = npObjectToV8NPObject(object);
299 if (!v8NPObject)
300 return false;
301
302 v8::HandleScope handleScope(isolate);
303 v8::Handle<v8::Object> v8Object = v8::Local<v8::Object>::New(isolate, v8NPObject->v8Object);
304 if (v8Object.IsEmpty())
305 return false;
Torne (Richard Coles)09380292014-02-21 12:17:33 +0000306 ArrayBufferView* native = V8ArrayBufferView::hasInstance(v8Object, isolate) ? V8ArrayBufferView::toNative(v8Object) : 0;
Ben Murdoche69819b2013-07-17 14:56:49 +0100307 if (!native)
308 return false;
309
310 *arrayBufferView = WebArrayBufferView(native);
311 return true;
312}
313
Torne (Richard Coles)8abfc582013-09-12 12:10:38 +0100314static NPObject* makeIntArrayImpl(const WebVector<int>& data, v8::Isolate* isolate)
Ben Murdoche69819b2013-07-17 14:56:49 +0100315{
Torne (Richard Coles)8abfc582013-09-12 12:10:38 +0100316 v8::HandleScope handleScope(isolate);
Torne (Richard Coles)a854de02013-12-18 16:25:25 +0000317 v8::Handle<v8::Array> result = v8::Array::New(isolate, data.size());
Ben Murdoche69819b2013-07-17 14:56:49 +0100318 for (size_t i = 0; i < data.size(); ++i)
Torne (Richard Coles)8abfc582013-09-12 12:10:38 +0100319 result->Set(i, v8::Number::New(isolate, data[i]));
Ben Murdoche69819b2013-07-17 14:56:49 +0100320
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +0000321 DOMWindow* window = currentDOMWindow(isolate);
Torne (Richard Coles)9bbd2f52013-09-19 22:37:05 +0100322 return npCreateV8ScriptObject(0, result, window, isolate);
Ben Murdoche69819b2013-07-17 14:56:49 +0100323}
324
Torne (Richard Coles)8abfc582013-09-12 12:10:38 +0100325static NPObject* makeStringArrayImpl(const WebVector<WebString>& data, v8::Isolate* isolate)
Ben Murdoche69819b2013-07-17 14:56:49 +0100326{
Torne (Richard Coles)8abfc582013-09-12 12:10:38 +0100327 v8::HandleScope handleScope(isolate);
Torne (Richard Coles)a854de02013-12-18 16:25:25 +0000328 v8::Handle<v8::Array> result = v8::Array::New(isolate, data.size());
Ben Murdoche69819b2013-07-17 14:56:49 +0100329 for (size_t i = 0; i < data.size(); ++i)
Torne (Richard Coles)a854de02013-12-18 16:25:25 +0000330 result->Set(i, v8String(isolate, data[i]));
Ben Murdoche69819b2013-07-17 14:56:49 +0100331
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +0000332 DOMWindow* window = currentDOMWindow(isolate);
Torne (Richard Coles)9bbd2f52013-09-19 22:37:05 +0100333 return npCreateV8ScriptObject(0, result, window, isolate);
Ben Murdoche69819b2013-07-17 14:56:49 +0100334}
335
336bool WebBindings::getRange(NPObject* range, WebRange* webRange)
337{
338 return getRangeImpl(range, webRange, v8::Isolate::GetCurrent());
339}
340
341bool WebBindings::getArrayBuffer(NPObject* arrayBuffer, WebArrayBuffer* webArrayBuffer)
342{
343 return getArrayBufferImpl(arrayBuffer, webArrayBuffer, v8::Isolate::GetCurrent());
344}
345
346bool WebBindings::getArrayBufferView(NPObject* arrayBufferView, WebArrayBufferView* webArrayBufferView)
347{
348 return getArrayBufferViewImpl(arrayBufferView, webArrayBufferView, v8::Isolate::GetCurrent());
349}
350
351bool WebBindings::getNode(NPObject* node, WebNode* webNode)
352{
353 return getNodeImpl(node, webNode, v8::Isolate::GetCurrent());
354}
355
356bool WebBindings::getElement(NPObject* element, WebElement* webElement)
357{
358 return getElementImpl(element, webElement, v8::Isolate::GetCurrent());
359}
360
361NPObject* WebBindings::makeIntArray(const WebVector<int>& data)
362{
Torne (Richard Coles)8abfc582013-09-12 12:10:38 +0100363 return makeIntArrayImpl(data, v8::Isolate::GetCurrent());
Ben Murdoche69819b2013-07-17 14:56:49 +0100364}
365
366NPObject* WebBindings::makeStringArray(const WebVector<WebString>& data)
367{
Torne (Richard Coles)8abfc582013-09-12 12:10:38 +0100368 return makeStringArrayImpl(data, v8::Isolate::GetCurrent());
Ben Murdoche69819b2013-07-17 14:56:49 +0100369}
370
371void WebBindings::pushExceptionHandler(ExceptionHandler handler, void* data)
372{
373 WebCore::pushExceptionHandler(handler, data);
374}
375
376void WebBindings::popExceptionHandler()
377{
378 WebCore::popExceptionHandler();
379}
380
381void WebBindings::toNPVariant(v8::Local<v8::Value> object, NPObject* root, NPVariant* result)
382{
Torne (Richard Coles)9bbd2f52013-09-19 22:37:05 +0100383 WebCore::convertV8ObjectToNPVariant(object, root, result, v8::Isolate::GetCurrent());
Ben Murdoche69819b2013-07-17 14:56:49 +0100384}
385
386v8::Handle<v8::Value> WebBindings::toV8Value(const NPVariant* variant)
387{
388 v8::Isolate* isolate = v8::Isolate::GetCurrent();
389 if (variant->type == NPVariantType_Object) {
390 NPObject* object = NPVARIANT_TO_OBJECT(*variant);
391 V8NPObject* v8Object = npObjectToV8NPObject(object);
392 if (!v8Object)
Torne (Richard Coles)9bbd2f52013-09-19 22:37:05 +0100393 return v8::Undefined(isolate);
Torne (Richard Coles)f79f16f2013-10-31 11:16:44 +0000394 return convertNPVariantToV8Object(variant, v8Object->rootObject->frame()->script().windowScriptNPObject(), isolate);
Ben Murdoche69819b2013-07-17 14:56:49 +0100395 }
396 // Safe to pass 0 since we have checked the script object class to make sure the
397 // argument is a primitive v8 type.
398 return convertNPVariantToV8Object(variant, 0, isolate);
399}
400
Torne (Richard Coles)51b29062013-11-28 11:56:03 +0000401} // namespace blink