1 /*
2 * Copyright (C) 2004, 2006 Apple Computer, Inc. All rights reserved.
3 * Copyright (C) 2007, 2008, 2009 Google, Inc. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27 #include "config.h"
28
29 #include "NPV8Object.h"
30
31 #if PLATFORM(CHROMIUM)
32 // TODO(andreip): upstream
33 #include "ChromiumBridge.h"
34 #endif
35 #include "DOMWindow.h"
36 #include "Frame.h"
37 #include "OwnArrayPtr.h"
38 #include "PlatformString.h"
39 #include "ScriptController.h"
40 #include "V8CustomBinding.h"
41 #include "V8GCController.h"
42 #include "V8Helpers.h"
43 #include "V8NPUtils.h"
44 #include "V8Proxy.h"
45 #if PLATFORM(CHROMIUM)
46 #include "bindings/npruntime.h"
47 #else
48 #include "bridge/npruntime.h"
49 #endif
50 #include "npruntime_impl.h"
51 #include "npruntime_priv.h"
52
53 #include <stdio.h>
54 #include <v8.h>
55 #include <wtf/StringExtras.h>
56
57 using WebCore::toV8Context;
58 using WebCore::toV8Proxy;
59 using WebCore::V8ClassIndex;
60 using WebCore::V8Custom;
61 using WebCore::V8DOMWrapper;
62 using WebCore::V8GCController;
63 using WebCore::V8Proxy;
64
65 // FIXME: Comments on why use malloc and free.
allocV8NPObject(NPP,NPClass *)66 static NPObject* allocV8NPObject(NPP, NPClass*)
67 {
68 return static_cast<NPObject*>(malloc(sizeof(V8NPObject)));
69 }
70
freeV8NPObject(NPObject * npObject)71 static void freeV8NPObject(NPObject* npObject)
72 {
73 V8NPObject* v8NpObject = reinterpret_cast<V8NPObject*>(npObject);
74 #ifndef NDEBUG
75 V8GCController::unregisterGlobalHandle(v8NpObject, v8NpObject->v8Object);
76 #endif
77 v8NpObject->v8Object.Dispose();
78 free(v8NpObject);
79 }
80
createValueListFromVariantArgs(const NPVariant * arguments,uint32_t argumentCount,NPObject * owner)81 static v8::Handle<v8::Value>* createValueListFromVariantArgs(const NPVariant* arguments, uint32_t argumentCount, NPObject* owner)
82 {
83 v8::Handle<v8::Value>* argv = new v8::Handle<v8::Value>[argumentCount];
84 for (uint32_t index = 0; index < argumentCount; index++) {
85 const NPVariant* arg = &arguments[index];
86 argv[index] = convertNPVariantToV8Object(arg, owner);
87 }
88 return argv;
89 }
90
91 // Create an identifier (null terminated utf8 char*) from the NPIdentifier.
npIdentifierToV8Identifier(NPIdentifier name)92 static v8::Local<v8::String> npIdentifierToV8Identifier(NPIdentifier name)
93 {
94 PrivateIdentifier* identifier = static_cast<PrivateIdentifier*>(name);
95 if (identifier->isString)
96 return v8::String::New(static_cast<const char*>(identifier->value.string));
97
98 char buffer[32];
99 snprintf(buffer, sizeof(buffer), "%d", identifier->value.number);
100 return v8::String::New(buffer);
101 }
102
103 static NPClass V8NPObjectClass = { NP_CLASS_STRUCT_VERSION,
104 allocV8NPObject,
105 freeV8NPObject,
106 0, 0, 0, 0, 0, 0, 0, 0, 0 };
107
108 // NPAPI's npruntime functions.
109 NPClass* npScriptObjectClass = &V8NPObjectClass;
110
npCreateV8ScriptObject(NPP npp,v8::Handle<v8::Object> object,WebCore::DOMWindow * root)111 NPObject* npCreateV8ScriptObject(NPP npp, v8::Handle<v8::Object> object, WebCore::DOMWindow* root)
112 {
113 // Check to see if this object is already wrapped.
114 if (object->InternalFieldCount() == V8Custom::kNPObjectInternalFieldCount) {
115 v8::Local<v8::Value> typeIndex = object->GetInternalField(V8Custom::kDOMWrapperTypeIndex);
116 if (typeIndex->IsNumber() && typeIndex->Uint32Value() == V8ClassIndex::NPOBJECT) {
117
118 NPObject* returnValue = V8DOMWrapper::convertToNativeObject<NPObject>(V8ClassIndex::NPOBJECT, object);
119 _NPN_RetainObject(returnValue);
120 return returnValue;
121 }
122 }
123
124 V8NPObject* v8npObject = reinterpret_cast<V8NPObject*>(_NPN_CreateObject(npp, &V8NPObjectClass));
125 v8npObject->v8Object = v8::Persistent<v8::Object>::New(object);
126 #ifndef NDEBUG
127 V8GCController::registerGlobalHandle(WebCore::NPOBJECT, v8npObject, v8npObject->v8Object);
128 #endif
129 v8npObject->rootObject = root;
130 return reinterpret_cast<NPObject*>(v8npObject);
131 }
132
_NPN_Invoke(NPP npp,NPObject * npObject,NPIdentifier methodName,const NPVariant * arguments,uint32_t argumentCount,NPVariant * result)133 bool _NPN_Invoke(NPP npp, NPObject* npObject, NPIdentifier methodName, const NPVariant* arguments, uint32_t argumentCount, NPVariant* result)
134 {
135 if (!npObject)
136 return false;
137
138 if (npObject->_class != npScriptObjectClass) {
139 if (npObject->_class->invoke)
140 return npObject->_class->invoke(npObject, methodName, arguments, argumentCount, result);
141
142 VOID_TO_NPVARIANT(*result);
143 return true;
144 }
145
146 V8NPObject* v8NpObject = reinterpret_cast<V8NPObject*>(npObject);
147
148 PrivateIdentifier* identifier = static_cast<PrivateIdentifier*>(methodName);
149 if (!identifier->isString)
150 return false;
151
152 v8::HandleScope handleScope;
153 // FIXME: should use the plugin's owner frame as the security context.
154 v8::Handle<v8::Context> context = toV8Context(npp, npObject);
155 if (context.IsEmpty())
156 return false;
157
158 v8::Context::Scope scope(context);
159
160 if (methodName == _NPN_GetStringIdentifier("eval")) {
161 if (argumentCount != 1)
162 return false;
163 if (arguments[0].type != NPVariantType_String)
164 return false;
165 return _NPN_Evaluate(npp, npObject, const_cast<NPString*>(&arguments[0].value.stringValue), result);
166 }
167
168 v8::Handle<v8::Value> functionObject = v8NpObject->v8Object->Get(v8::String::New(identifier->value.string));
169 if (functionObject.IsEmpty() || functionObject->IsNull()) {
170 NULL_TO_NPVARIANT(*result);
171 return false;
172 }
173 if (functionObject->IsUndefined()) {
174 VOID_TO_NPVARIANT(*result);
175 return false;
176 }
177
178 V8Proxy* proxy = toV8Proxy(npObject);
179 ASSERT(proxy);
180
181 // Call the function object.
182 v8::Handle<v8::Function> function = v8::Handle<v8::Function>::Cast(functionObject);
183 OwnArrayPtr<v8::Handle<v8::Value> > argv(createValueListFromVariantArgs(arguments, argumentCount, npObject));
184 v8::Local<v8::Value> resultObject = proxy->callFunction(function, v8NpObject->v8Object, argumentCount, argv.get());
185
186 // If we had an error, return false. The spec is a little unclear here, but says "Returns true if the method was
187 // successfully invoked". If we get an error return value, was that successfully invoked?
188 if (resultObject.IsEmpty())
189 return false;
190
191 convertV8ObjectToNPVariant(resultObject, npObject, result);
192 return true;
193 }
194
195 // FIXME: Fix it same as _NPN_Invoke (HandleScope and such).
_NPN_InvokeDefault(NPP npp,NPObject * npObject,const NPVariant * arguments,uint32_t argumentCount,NPVariant * result)196 bool _NPN_InvokeDefault(NPP npp, NPObject* npObject, const NPVariant* arguments, uint32_t argumentCount, NPVariant* result)
197 {
198 if (!npObject)
199 return false;
200
201 if (npObject->_class != npScriptObjectClass) {
202 if (npObject->_class->invokeDefault)
203 return npObject->_class->invokeDefault(npObject, arguments, argumentCount, result);
204
205 VOID_TO_NPVARIANT(*result);
206 return true;
207 }
208
209 V8NPObject* v8NpObject = reinterpret_cast<V8NPObject*>(npObject);
210
211 VOID_TO_NPVARIANT(*result);
212
213 v8::HandleScope handleScope;
214 v8::Handle<v8::Context> context = toV8Context(npp, npObject);
215 if (context.IsEmpty())
216 return false;
217
218 v8::Context::Scope scope(context);
219
220 // Lookup the function object and call it.
221 v8::Handle<v8::Object> functionObject(v8NpObject->v8Object);
222 if (!functionObject->IsFunction())
223 return false;
224
225 v8::Local<v8::Value> resultObject;
226 v8::Handle<v8::Function> function(v8::Function::Cast(*functionObject));
227 if (!function->IsNull()) {
228 V8Proxy* proxy = toV8Proxy(npObject);
229 ASSERT(proxy);
230
231 OwnArrayPtr<v8::Handle<v8::Value> > argv(createValueListFromVariantArgs(arguments, argumentCount, npObject));
232 resultObject = proxy->callFunction(function, functionObject, argumentCount, argv.get());
233 }
234 // If we had an error, return false. The spec is a little unclear here, but says "Returns true if the method was
235 // successfully invoked". If we get an error return value, was that successfully invoked?
236 if (resultObject.IsEmpty())
237 return false;
238
239 convertV8ObjectToNPVariant(resultObject, npObject, result);
240 return true;
241 }
242
_NPN_Evaluate(NPP npp,NPObject * npObject,NPString * npScript,NPVariant * result)243 bool _NPN_Evaluate(NPP npp, NPObject* npObject, NPString* npScript, NPVariant* result)
244 {
245 #if PLATFORM(CHROMIUM)
246 bool popupsAllowed = WebCore::ChromiumBridge::popupsAllowed(npp);
247 #else
248 // TODO(andreip): Some of the binding code is specific to Chromium
249 // and we don't want it to Android. What is the preferred way to handle this?
250 bool popupsAllowed = false;
251 #endif
252 return _NPN_EvaluateHelper(npp, popupsAllowed, npObject, npScript, result);
253 }
254
_NPN_EvaluateHelper(NPP npp,bool popupsAllowed,NPObject * npObject,NPString * npScript,NPVariant * result)255 bool _NPN_EvaluateHelper(NPP npp, bool popupsAllowed, NPObject* npObject, NPString* npScript, NPVariant* result)
256 {
257 VOID_TO_NPVARIANT(*result);
258 if (!npObject)
259 return false;
260
261 if (npObject->_class != npScriptObjectClass)
262 return false;
263
264 v8::HandleScope handleScope;
265 v8::Handle<v8::Context> context = toV8Context(npp, npObject);
266 if (context.IsEmpty())
267 return false;
268
269 V8Proxy* proxy = toV8Proxy(npObject);
270 ASSERT(proxy);
271
272 v8::Context::Scope scope(context);
273
274 WebCore::String filename;
275 if (!popupsAllowed)
276 filename = "npscript";
277
278 WebCore::String script = WebCore::String::fromUTF8(npScript->UTF8Characters, npScript->UTF8Length);
279 v8::Local<v8::Value> v8result = proxy->evaluate(WebCore::ScriptSourceCode(script, WebCore::KURL(filename)), 0);
280
281 if (v8result.IsEmpty())
282 return false;
283
284 convertV8ObjectToNPVariant(v8result, npObject, result);
285 return true;
286 }
287
_NPN_GetProperty(NPP npp,NPObject * npObject,NPIdentifier propertyName,NPVariant * result)288 bool _NPN_GetProperty(NPP npp, NPObject* npObject, NPIdentifier propertyName, NPVariant* result)
289 {
290 if (!npObject)
291 return false;
292
293 if (npObject->_class == npScriptObjectClass) {
294 V8NPObject* object = reinterpret_cast<V8NPObject*>(npObject);
295
296 v8::HandleScope handleScope;
297 v8::Handle<v8::Context> context = toV8Context(npp, npObject);
298 if (context.IsEmpty())
299 return false;
300
301 v8::Context::Scope scope(context);
302
303 v8::Handle<v8::Object> obj(object->v8Object);
304 v8::Local<v8::Value> v8result = obj->Get(npIdentifierToV8Identifier(propertyName));
305
306 convertV8ObjectToNPVariant(v8result, npObject, result);
307 return true;
308 }
309
310 if (npObject->_class->hasProperty && npObject->_class->getProperty) {
311 if (npObject->_class->hasProperty(npObject, propertyName))
312 return npObject->_class->getProperty(npObject, propertyName, result);
313 }
314
315 VOID_TO_NPVARIANT(*result);
316 return false;
317 }
318
_NPN_SetProperty(NPP npp,NPObject * npObject,NPIdentifier propertyName,const NPVariant * value)319 bool _NPN_SetProperty(NPP npp, NPObject* npObject, NPIdentifier propertyName, const NPVariant* value)
320 {
321 if (!npObject)
322 return false;
323
324 if (npObject->_class == npScriptObjectClass) {
325 V8NPObject* object = reinterpret_cast<V8NPObject*>(npObject);
326
327 v8::HandleScope handleScope;
328 v8::Handle<v8::Context> context = toV8Context(npp, npObject);
329 if (context.IsEmpty())
330 return false;
331
332 v8::Context::Scope scope(context);
333
334 v8::Handle<v8::Object> obj(object->v8Object);
335 obj->Set(npIdentifierToV8Identifier(propertyName),
336 convertNPVariantToV8Object(value, object->rootObject->frame()->script()->windowScriptNPObject()));
337 return true;
338 }
339
340 if (npObject->_class->setProperty)
341 return npObject->_class->setProperty(npObject, propertyName, value);
342
343 return false;
344 }
345
_NPN_RemoveProperty(NPP npp,NPObject * npObject,NPIdentifier propertyName)346 bool _NPN_RemoveProperty(NPP npp, NPObject* npObject, NPIdentifier propertyName)
347 {
348 if (!npObject)
349 return false;
350 if (npObject->_class != npScriptObjectClass)
351 return false;
352
353 V8NPObject* object = reinterpret_cast<V8NPObject*>(npObject);
354
355 v8::HandleScope handleScope;
356 v8::Handle<v8::Context> context = toV8Context(npp, npObject);
357 if (context.IsEmpty())
358 return false;
359 v8::Context::Scope scope(context);
360
361 v8::Handle<v8::Object> obj(object->v8Object);
362 // FIXME: Verify that setting to undefined is right.
363 obj->Set(npIdentifierToV8Identifier(propertyName), v8::Undefined());
364 return true;
365 }
366
_NPN_HasProperty(NPP npp,NPObject * npObject,NPIdentifier propertyName)367 bool _NPN_HasProperty(NPP npp, NPObject* npObject, NPIdentifier propertyName)
368 {
369 if (!npObject)
370 return false;
371
372 if (npObject->_class == npScriptObjectClass) {
373 V8NPObject* object = reinterpret_cast<V8NPObject*>(npObject);
374
375 v8::HandleScope handleScope;
376 v8::Handle<v8::Context> context = toV8Context(npp, npObject);
377 if (context.IsEmpty())
378 return false;
379 v8::Context::Scope scope(context);
380
381 v8::Handle<v8::Object> obj(object->v8Object);
382 return obj->Has(npIdentifierToV8Identifier(propertyName));
383 }
384
385 if (npObject->_class->hasProperty)
386 return npObject->_class->hasProperty(npObject, propertyName);
387 return false;
388 }
389
_NPN_HasMethod(NPP npp,NPObject * npObject,NPIdentifier methodName)390 bool _NPN_HasMethod(NPP npp, NPObject* npObject, NPIdentifier methodName)
391 {
392 if (!npObject)
393 return false;
394
395 if (npObject->_class == npScriptObjectClass) {
396 V8NPObject* object = reinterpret_cast<V8NPObject*>(npObject);
397
398 v8::HandleScope handleScope;
399 v8::Handle<v8::Context> context = toV8Context(npp, npObject);
400 if (context.IsEmpty())
401 return false;
402 v8::Context::Scope scope(context);
403
404 v8::Handle<v8::Object> obj(object->v8Object);
405 v8::Handle<v8::Value> prop = obj->Get(npIdentifierToV8Identifier(methodName));
406 return prop->IsFunction();
407 }
408
409 if (npObject->_class->hasMethod)
410 return npObject->_class->hasMethod(npObject, methodName);
411 return false;
412 }
413
_NPN_SetException(NPObject * npObject,const NPUTF8 * message)414 void _NPN_SetException(NPObject* npObject, const NPUTF8 *message)
415 {
416 if (npObject->_class != npScriptObjectClass)
417 return;
418 v8::HandleScope handleScope;
419 v8::Handle<v8::Context> context = toV8Context(0, npObject);
420 if (context.IsEmpty())
421 return;
422
423 v8::Context::Scope scope(context);
424 V8Proxy::throwError(V8Proxy::GeneralError, message);
425 }
426
_NPN_Enumerate(NPP npp,NPObject * npObject,NPIdentifier ** identifier,uint32_t * count)427 bool _NPN_Enumerate(NPP npp, NPObject* npObject, NPIdentifier** identifier, uint32_t* count)
428 {
429 if (!npObject)
430 return false;
431
432 if (npObject->_class == npScriptObjectClass) {
433 V8NPObject* object = reinterpret_cast<V8NPObject*>(npObject);
434
435 v8::HandleScope handleScope;
436 v8::Handle<v8::Context> context = toV8Context(npp, npObject);
437 if (context.IsEmpty())
438 return false;
439 v8::Context::Scope scope(context);
440
441 v8::Handle<v8::Object> obj(object->v8Object);
442
443 // FIXME: http://b/issue?id=1210340: Use a v8::Object::Keys() method when it exists, instead of evaluating javascript.
444
445 // FIXME: Figure out how to cache this helper function. Run a helper function that collects the properties
446 // on the object into an array.
447 const char enumeratorCode[] =
448 "(function (obj) {"
449 " var props = [];"
450 " for (var prop in obj) {"
451 " props[props.length] = prop;"
452 " }"
453 " return props;"
454 "});";
455 v8::Handle<v8::String> source = v8::String::New(enumeratorCode);
456 v8::Handle<v8::Script> script = v8::Script::Compile(source, 0);
457 v8::Handle<v8::Value> enumeratorObj = script->Run();
458 v8::Handle<v8::Function> enumerator = v8::Handle<v8::Function>::Cast(enumeratorObj);
459 v8::Handle<v8::Value> argv[] = { obj };
460 #if PLATFORM(ANDROID)
461 // TODO(benm): implement an arry size function on android
462 v8::Local<v8::Value> propsObj = enumerator->Call(v8::Handle<v8::Object>::Cast(enumeratorObj), 1, argv);
463 #else
464 v8::Local<v8::Value> propsObj = enumerator->Call(v8::Handle<v8::Object>::Cast(enumeratorObj), ARRAYSIZE_UNSAFE(argv), argv);
465 #endif
466 if (propsObj.IsEmpty())
467 return false;
468
469 // Convert the results into an array of NPIdentifiers.
470 v8::Handle<v8::Array> props = v8::Handle<v8::Array>::Cast(propsObj);
471 *count = props->Length();
472 *identifier = static_cast<NPIdentifier*>(malloc(sizeof(NPIdentifier*) * *count));
473 for (uint32_t i = 0; i < *count; ++i) {
474 v8::Local<v8::Value> name = props->Get(v8::Integer::New(i));
475 (*identifier)[i] = getStringIdentifier(v8::Local<v8::String>::Cast(name));
476 }
477 return true;
478 }
479
480 if (NP_CLASS_STRUCT_VERSION_HAS_ENUM(npObject->_class) && npObject->_class->enumerate)
481 return npObject->_class->enumerate(npObject, identifier, count);
482
483 return false;
484 }
485
_NPN_Construct(NPP npp,NPObject * npObject,const NPVariant * arguments,uint32_t argumentCount,NPVariant * result)486 bool _NPN_Construct(NPP npp, NPObject* npObject, const NPVariant* arguments, uint32_t argumentCount, NPVariant* result)
487 {
488 if (!npObject)
489 return false;
490
491 if (npObject->_class == npScriptObjectClass) {
492 V8NPObject* object = reinterpret_cast<V8NPObject*>(npObject);
493
494 v8::HandleScope handleScope;
495 v8::Handle<v8::Context> context = toV8Context(npp, npObject);
496 if (context.IsEmpty())
497 return false;
498 v8::Context::Scope scope(context);
499
500 // Lookup the constructor function.
501 v8::Handle<v8::Object> ctorObj(object->v8Object);
502 if (!ctorObj->IsFunction())
503 return false;
504
505 // Call the constructor.
506 v8::Local<v8::Value> resultObject;
507 v8::Handle<v8::Function> ctor(v8::Function::Cast(*ctorObj));
508 if (!ctor->IsNull()) {
509 V8Proxy* proxy = toV8Proxy(npObject);
510 ASSERT(proxy);
511
512 OwnArrayPtr<v8::Handle<v8::Value> > argv(createValueListFromVariantArgs(arguments, argumentCount, npObject));
513 resultObject = proxy->newInstance(ctor, argumentCount, argv.get());
514 }
515
516 if (resultObject.IsEmpty())
517 return false;
518
519 convertV8ObjectToNPVariant(resultObject, npObject, result);
520 return true;
521 }
522
523 if (NP_CLASS_STRUCT_VERSION_HAS_CTOR(npObject->_class) && npObject->_class->construct)
524 return npObject->_class->construct(npObject, arguments, argumentCount, result);
525
526 return false;
527 }
528