Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / Source / web / WebBindings.cpp
blobd667477bb5d52ad400be82d9c07c940beba52b72
1 /*
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
6 * met:
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.
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.
31 #include "config.h"
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"
53 namespace blink {
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*)
175 return 0;
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);
196 if (!data) {
197 isString = false;
198 number = 0;
199 return;
202 isString = data->isString;
203 if (isString)
204 string = data->value.string;
205 else
206 number = data->value.number;
209 static bool getRangeImpl(NPObject* object, WebRange* webRange, v8::Isolate* isolate)
211 if (!object)
212 return false;
214 V8NPObject* v8NPObject = npObjectToV8NPObject(object);
215 if (!v8NPObject)
216 return false;
218 v8::HandleScope handleScope(isolate);
219 v8::Local<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;
225 Range* native = V8Range::hasInstance(v8Object, isolate) ? V8Range::toImpl(v8Object) : 0;
226 if (!native)
227 return false;
229 *webRange = WebRange(native);
230 return true;
233 static bool getNodeImpl(NPObject* object, WebNode* webNode, v8::Isolate* isolate)
235 if (!object)
236 return false;
238 V8NPObject* v8NPObject = npObjectToV8NPObject(object);
239 if (!v8NPObject)
240 return false;
242 v8::HandleScope handleScope(isolate);
243 v8::Local<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;
250 *webNode = WebNode(native);
251 return true;
254 static bool getElementImpl(NPObject* object, WebElement* webElement, v8::Isolate* isolate)
256 if (!object)
257 return false;
259 V8NPObject* v8NPObject = npObjectToV8NPObject(object);
260 if (!v8NPObject)
261 return false;
263 v8::HandleScope handleScope(isolate);
264 v8::Local<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;
271 *webElement = WebElement(native);
272 return true;
275 static bool getArrayBufferImpl(NPObject* object, WebArrayBuffer* arrayBuffer, v8::Isolate* isolate)
277 if (!object)
278 return false;
280 V8NPObject* v8NPObject = npObjectToV8NPObject(object);
281 if (!v8NPObject)
282 return false;
284 v8::HandleScope handleScope(isolate);
285 v8::Local<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;
292 *arrayBuffer = WebArrayBuffer(impl);
293 return true;
296 static bool getArrayBufferViewImpl(NPObject* object, WebArrayBufferView* arrayBufferView, v8::Isolate* isolate)
298 if (!object)
299 return false;
301 V8NPObject* v8NPObject = npObjectToV8NPObject(object);
302 if (!v8NPObject)
303 return false;
305 v8::HandleScope handleScope(isolate);
306 v8::Local<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;
313 *arrayBufferView = WebArrayBufferView(impl);
314 return true;
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])))) {
323 result.Clear();
324 break;
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])))) {
338 result.Clear();
339 break;
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);
403 if (!v8Object)
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);
412 } // namespace blink