2 * Copyright (C) 2009 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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
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.
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.
32 #include "public/web/WebBindings.h"
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/web/WebArrayBuffer.h"
49 #include "public/web/WebArrayBufferView.h"
50 #include "public/web/WebElement.h"
51 #include "public/web/WebRange.h"
55 bool WebBindings::construct(NPP npp
, NPObject
* object
, const NPVariant
* args
, uint32_t argCount
, NPVariant
* result
)
57 return _NPN_Construct(npp
, object
, args
, argCount
, result
);
60 NPObject
* WebBindings::createObject(NPP npp
, NPClass
* npClass
)
62 return _NPN_CreateObject(npp
, npClass
);
65 bool WebBindings::enumerate(NPP npp
, NPObject
* object
, NPIdentifier
** identifier
, uint32_t* identifierCount
)
67 return _NPN_Enumerate(npp
, object
, identifier
, identifierCount
);
70 bool WebBindings::evaluate(NPP npp
, NPObject
* object
, NPString
* script
, NPVariant
* result
)
72 return _NPN_Evaluate(npp
, object
, script
, result
);
75 bool WebBindings::evaluateHelper(NPP npp
, bool popupsAllowed
, NPObject
* object
, NPString
* script
, NPVariant
* result
)
77 return _NPN_EvaluateHelper(npp
, popupsAllowed
, object
, script
, result
);
80 NPIdentifier
WebBindings::getIntIdentifier(int32_t number
)
82 return _NPN_GetIntIdentifier(number
);
85 bool WebBindings::getProperty(NPP npp
, NPObject
* object
, NPIdentifier property
, NPVariant
* result
)
87 return _NPN_GetProperty(npp
, object
, property
, result
);
90 NPIdentifier
WebBindings::getStringIdentifier(const NPUTF8
* string
)
92 return _NPN_GetStringIdentifier(string
);
95 void WebBindings::getStringIdentifiers(const NPUTF8
** names
, int32_t nameCount
, NPIdentifier
* identifiers
)
97 _NPN_GetStringIdentifiers(names
, nameCount
, identifiers
);
100 bool WebBindings::hasMethod(NPP npp
, NPObject
* object
, NPIdentifier method
)
102 return _NPN_HasMethod(npp
, object
, method
);
105 bool WebBindings::hasProperty(NPP npp
, NPObject
* object
, NPIdentifier property
)
107 return _NPN_HasProperty(npp
, object
, property
);
110 bool WebBindings::identifierIsString(NPIdentifier identifier
)
112 return _NPN_IdentifierIsString(identifier
);
115 int32_t WebBindings::intFromIdentifier(NPIdentifier identifier
)
117 return _NPN_IntFromIdentifier(identifier
);
120 void WebBindings::initializeVariantWithStringCopy(NPVariant
* variant
, const NPString
* value
)
122 _NPN_InitializeVariantWithStringCopy(variant
, value
);
125 bool WebBindings::invoke(NPP npp
, NPObject
* object
, NPIdentifier method
, const NPVariant
* args
, uint32_t argCount
, NPVariant
* result
)
127 return _NPN_Invoke(npp
, object
, method
, args
, argCount
, result
);
130 bool WebBindings::invokeDefault(NPP npp
, NPObject
* object
, const NPVariant
* args
, uint32_t argCount
, NPVariant
* result
)
132 return _NPN_InvokeDefault(npp
, object
, args
, argCount
, result
);
135 void WebBindings::releaseObject(NPObject
* object
)
137 return _NPN_ReleaseObject(object
);
140 void WebBindings::releaseVariantValue(NPVariant
* variant
)
142 _NPN_ReleaseVariantValue(variant
);
145 bool WebBindings::removeProperty(NPP npp
, NPObject
* object
, NPIdentifier identifier
)
147 return _NPN_RemoveProperty(npp
, object
, identifier
);
150 NPObject
* WebBindings::retainObject(NPObject
* object
)
152 return _NPN_RetainObject(object
);
155 void WebBindings::setException(NPObject
* object
, const NPUTF8
* message
)
157 _NPN_SetException(object
, message
);
160 bool WebBindings::setProperty(NPP npp
, NPObject
* object
, NPIdentifier identifier
, const NPVariant
* value
)
162 return _NPN_SetProperty(npp
, object
, identifier
, value
);
165 void WebBindings::registerObjectOwner(NPP
)
169 void WebBindings::unregisterObjectOwner(NPP
)
173 NPP
WebBindings::getObjectOwner(NPObject
*)
178 void WebBindings::unregisterObject(NPObject
* object
)
180 _NPN_UnregisterObject(object
);
183 void WebBindings::dropV8WrapperForObject(NPObject
* object
)
185 forgetV8ObjectForNPObject(object
);
188 NPUTF8
* WebBindings::utf8FromIdentifier(NPIdentifier identifier
)
190 return _NPN_UTF8FromIdentifier(identifier
);
193 void WebBindings::extractIdentifierData(const NPIdentifier
& identifier
, const NPUTF8
*& string
, int32_t& number
, bool& isString
)
195 PrivateIdentifier
* data
= static_cast<PrivateIdentifier
*>(identifier
);
202 isString
= data
->isString
;
204 string
= data
->value
.string
;
206 number
= data
->value
.number
;
209 static bool getRangeImpl(NPObject
* object
, WebRange
* webRange
, v8::Isolate
* isolate
)
214 V8NPObject
* v8NPObject
= npObjectToV8NPObject(object
);
218 v8::HandleScope
handleScope(isolate
);
219 v8::Local
<v8::Object
> v8Object
= v8::Local
<v8::Object
>::New(isolate
, v8NPObject
->v8Object
);
220 if (v8Object
.IsEmpty())
222 if (!V8Range::wrapperTypeInfo
.equals(toWrapperTypeInfo(v8Object
)))
225 Range
* native
= V8Range::hasInstance(v8Object
, isolate
) ? V8Range::toImpl(v8Object
) : 0;
229 *webRange
= WebRange(native
);
233 static bool getNodeImpl(NPObject
* object
, WebNode
* webNode
, v8::Isolate
* isolate
)
238 V8NPObject
* v8NPObject
= npObjectToV8NPObject(object
);
242 v8::HandleScope
handleScope(isolate
);
243 v8::Local
<v8::Object
> v8Object
= v8::Local
<v8::Object
>::New(isolate
, v8NPObject
->v8Object
);
244 if (v8Object
.IsEmpty())
246 Node
* native
= V8Node::hasInstance(v8Object
, isolate
) ? V8Node::toImpl(v8Object
) : 0;
250 *webNode
= WebNode(native
);
254 static bool getElementImpl(NPObject
* object
, WebElement
* webElement
, v8::Isolate
* isolate
)
259 V8NPObject
* v8NPObject
= npObjectToV8NPObject(object
);
263 v8::HandleScope
handleScope(isolate
);
264 v8::Local
<v8::Object
> v8Object
= v8::Local
<v8::Object
>::New(isolate
, v8NPObject
->v8Object
);
265 if (v8Object
.IsEmpty())
267 Element
* native
= V8Element::hasInstance(v8Object
, isolate
) ? V8Element::toImpl(v8Object
) : 0;
271 *webElement
= WebElement(native
);
275 static bool getArrayBufferImpl(NPObject
* object
, WebArrayBuffer
* arrayBuffer
, v8::Isolate
* isolate
)
280 V8NPObject
* v8NPObject
= npObjectToV8NPObject(object
);
284 v8::HandleScope
handleScope(isolate
);
285 v8::Local
<v8::Object
> v8Object
= v8::Local
<v8::Object
>::New(isolate
, v8NPObject
->v8Object
);
286 if (v8Object
.IsEmpty())
288 DOMArrayBuffer
* impl
= V8ArrayBuffer::hasInstance(v8Object
, isolate
) ? V8ArrayBuffer::toImpl(v8Object
) : 0;
292 *arrayBuffer
= WebArrayBuffer(impl
);
296 static bool getArrayBufferViewImpl(NPObject
* object
, WebArrayBufferView
* arrayBufferView
, v8::Isolate
* isolate
)
301 V8NPObject
* v8NPObject
= npObjectToV8NPObject(object
);
305 v8::HandleScope
handleScope(isolate
);
306 v8::Local
<v8::Object
> v8Object
= v8::Local
<v8::Object
>::New(isolate
, v8NPObject
->v8Object
);
307 if (v8Object
.IsEmpty())
309 DOMArrayBufferView
* impl
= V8ArrayBufferView::hasInstance(v8Object
, isolate
) ? V8ArrayBufferView::toImpl(v8Object
) : 0;
313 *arrayBufferView
= WebArrayBufferView(impl
);
317 static NPObject
* makeIntArrayImpl(const WebVector
<int>& data
, v8::Isolate
* isolate
)
319 v8::HandleScope
handleScope(isolate
);
320 v8::Local
<v8::Array
> result
= v8::Array::New(isolate
, data
.size());
321 for (size_t i
= 0; i
< data
.size(); ++i
) {
322 if (!v8CallBoolean(result
->Set(isolate
->GetCurrentContext(), v8::Integer::New(isolate
, i
), v8::Number::New(isolate
, data
[i
])))) {
328 LocalDOMWindow
* window
= currentDOMWindow(isolate
);
329 return npCreateV8ScriptObject(isolate
, 0, result
, window
);
332 static NPObject
* makeStringArrayImpl(const WebVector
<WebString
>& data
, v8::Isolate
* isolate
)
334 v8::HandleScope
handleScope(isolate
);
335 v8::Local
<v8::Array
> result
= v8::Array::New(isolate
, data
.size());
336 for (size_t i
= 0; i
< data
.size(); ++i
) {
337 if (!v8CallBoolean(result
->Set(isolate
->GetCurrentContext(), v8::Integer::New(isolate
, i
), v8String(isolate
, data
[i
])))) {
343 LocalDOMWindow
* window
= currentDOMWindow(isolate
);
344 return npCreateV8ScriptObject(isolate
, 0, result
, window
);
347 bool WebBindings::getRange(NPObject
* range
, WebRange
* webRange
)
349 return getRangeImpl(range
, webRange
, v8::Isolate::GetCurrent());
352 bool WebBindings::getArrayBuffer(NPObject
* arrayBuffer
, WebArrayBuffer
* webArrayBuffer
)
354 return getArrayBufferImpl(arrayBuffer
, webArrayBuffer
, v8::Isolate::GetCurrent());
357 bool WebBindings::getArrayBufferView(NPObject
* arrayBufferView
, WebArrayBufferView
* webArrayBufferView
)
359 return getArrayBufferViewImpl(arrayBufferView
, webArrayBufferView
, v8::Isolate::GetCurrent());
362 bool WebBindings::getNode(NPObject
* node
, WebNode
* webNode
)
364 return getNodeImpl(node
, webNode
, v8::Isolate::GetCurrent());
367 bool WebBindings::getElement(NPObject
* element
, WebElement
* webElement
)
369 return getElementImpl(element
, webElement
, v8::Isolate::GetCurrent());
372 NPObject
* WebBindings::makeIntArray(const WebVector
<int>& data
)
374 return makeIntArrayImpl(data
, v8::Isolate::GetCurrent());
377 NPObject
* WebBindings::makeStringArray(const WebVector
<WebString
>& data
)
379 return makeStringArrayImpl(data
, v8::Isolate::GetCurrent());
382 void WebBindings::pushExceptionHandler(ExceptionHandler handler
, void* data
)
384 blink::pushExceptionHandler(handler
, data
);
387 void WebBindings::popExceptionHandler()
389 blink::popExceptionHandler();
392 void WebBindings::toNPVariant(v8::Local
<v8::Value
> object
, NPObject
* root
, NPVariant
* result
)
394 convertV8ObjectToNPVariant(v8::Isolate::GetCurrent(), object
, root
, result
);
397 v8::Local
<v8::Value
> WebBindings::toV8Value(const NPVariant
* variant
)
399 v8::Isolate
* isolate
= v8::Isolate::GetCurrent();
400 if (variant
->type
== NPVariantType_Object
) {
401 NPObject
* object
= NPVARIANT_TO_OBJECT(*variant
);
402 V8NPObject
* v8Object
= npObjectToV8NPObject(object
);
404 return v8::Undefined(isolate
);
405 return convertNPVariantToV8Object(isolate
, variant
, v8Object
->rootObject
->frame()->script().windowScriptNPObject());
407 // Safe to pass 0 since we have checked the script object class to make sure the
408 // argument is a primitive v8 type.
409 return convertNPVariantToV8Object(isolate
, variant
, 0);