2007-03-22 10:30:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 04:12:37 -07:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-04-07 17:16:19 -07:00
|
|
|
#include "base/basictypes.h"
|
|
|
|
|
2011-10-12 12:20:52 -07:00
|
|
|
#include "jsfriendapi.h"
|
2012-07-13 01:55:14 -07:00
|
|
|
#include "jswrapper.h"
|
2010-03-03 21:02:55 -08:00
|
|
|
|
2011-05-21 06:28:54 -07:00
|
|
|
#include "nsIInterfaceRequestorUtils.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsJSNPRuntime.h"
|
2008-09-15 05:45:01 -07:00
|
|
|
#include "nsNPAPIPlugin.h"
|
|
|
|
#include "nsNPAPIPluginInstance.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsIScriptGlobalObject.h"
|
|
|
|
#include "nsIScriptContext.h"
|
2008-01-20 21:34:17 -08:00
|
|
|
#include "nsDOMJSUtils.h"
|
2014-07-12 00:43:08 -07:00
|
|
|
#include "nsJSUtils.h"
|
2013-05-22 09:05:26 -07:00
|
|
|
#include "nsCxPusher.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsIDocument.h"
|
|
|
|
#include "nsIJSRuntimeService.h"
|
|
|
|
#include "nsIXPConnect.h"
|
|
|
|
#include "nsIDOMElement.h"
|
|
|
|
#include "prmem.h"
|
|
|
|
#include "nsIContent.h"
|
2012-10-02 19:43:18 -07:00
|
|
|
#include "nsPluginInstanceOwner.h"
|
2013-11-20 08:38:10 -08:00
|
|
|
#include "nsWrapperCacheInlines.h"
|
2013-11-19 14:53:36 -08:00
|
|
|
#include "js/HashTable.h"
|
|
|
|
#include "mozilla/HashFunctions.h"
|
2014-07-16 05:49:02 -07:00
|
|
|
#include "mozilla/dom/ScriptSettings.h"
|
2013-11-19 14:53:36 -08:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-05-17 18:48:34 -07:00
|
|
|
#define NPRUNTIME_JSCLASS_NAME "NPObject JS wrapper class"
|
|
|
|
|
2009-12-01 13:27:13 -08:00
|
|
|
using namespace mozilla::plugins::parent;
|
2012-03-12 15:53:18 -07:00
|
|
|
using namespace mozilla;
|
2009-12-01 13:27:13 -08:00
|
|
|
|
2010-04-07 17:16:19 -07:00
|
|
|
#include "mozilla/plugins/PluginScriptableObjectParent.h"
|
|
|
|
using mozilla::plugins::PluginScriptableObjectParent;
|
|
|
|
using mozilla::plugins::ParentNPObject;
|
|
|
|
|
2013-11-19 14:53:36 -08:00
|
|
|
struct JSObjWrapperHasher : public js::DefaultHasher<nsJSObjWrapperKey>
|
|
|
|
{
|
|
|
|
typedef nsJSObjWrapperKey Key;
|
|
|
|
typedef Key Lookup;
|
|
|
|
|
|
|
|
static uint32_t hash(const Lookup &l) {
|
|
|
|
return HashGeneric(l.mJSObj, l.mNpp);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void rekey(Key &k, const Key& newKey) {
|
|
|
|
MOZ_ASSERT(k.mNpp == newKey.mNpp);
|
|
|
|
k.mJSObj = newKey.mJSObj;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// Hash of JSObject wrappers that wraps JSObjects as NPObjects. There
|
|
|
|
// will be one wrapper per JSObject per plugin instance, i.e. if two
|
|
|
|
// plugins access the JSObject x, two wrappers for x will be
|
|
|
|
// created. This is needed to be able to properly drop the wrappers
|
|
|
|
// when a plugin is torn down in case there's a leak in the plugin (we
|
|
|
|
// don't want to leak the world just because a plugin leaks an
|
|
|
|
// NPObject).
|
2013-11-19 14:53:36 -08:00
|
|
|
typedef js::HashMap<nsJSObjWrapperKey,
|
|
|
|
nsJSObjWrapper*,
|
|
|
|
JSObjWrapperHasher,
|
|
|
|
js::SystemAllocPolicy> JSObjWrapperTable;
|
|
|
|
static JSObjWrapperTable sJSObjWrappers;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-27 02:35:13 -07:00
|
|
|
// Whether it's safe to iterate sJSObjWrappers. Set to true when sJSObjWrappers
|
|
|
|
// has been initialized and is not currently being enumerated.
|
|
|
|
static bool sJSObjWrappersAccessible = false;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// Hash of NPObject wrappers that wrap NPObjects as JSObjects.
|
|
|
|
static PLDHashTable sNPObjWrappers;
|
|
|
|
|
|
|
|
// Global wrapper count. This includes JSObject wrappers *and*
|
|
|
|
// NPObject wrappers. When this count goes to zero, there are no more
|
|
|
|
// wrappers and we can kill off hash tables etc.
|
2012-08-22 08:56:38 -07:00
|
|
|
static int32_t sWrapperCount;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// The JSRuntime. Used to unroot JSObjects when no JSContext is
|
|
|
|
// reachable.
|
|
|
|
static JSRuntime *sJSRuntime;
|
|
|
|
|
2009-11-17 11:51:46 -08:00
|
|
|
static nsTArray<NPObject*>* sDelayedReleases;
|
2007-11-06 09:56:03 -08:00
|
|
|
|
2010-04-07 17:16:19 -07:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
inline bool
|
|
|
|
NPObjectIsOutOfProcessProxy(NPObject *obj)
|
|
|
|
{
|
|
|
|
return obj->_class == PluginScriptableObjectParent::GetClass();
|
|
|
|
}
|
|
|
|
|
|
|
|
} // anonymous namespace
|
|
|
|
|
2007-11-06 09:56:03 -08:00
|
|
|
// Helper class that reports any JS exceptions that were thrown while
|
|
|
|
// the plugin executed JS.
|
|
|
|
|
|
|
|
class AutoJSExceptionReporter
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
AutoJSExceptionReporter(JSContext *cx)
|
|
|
|
: mCx(cx)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
~AutoJSExceptionReporter()
|
|
|
|
{
|
|
|
|
JS_ReportPendingException(mCx);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
JSContext *mCx;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
NPClass nsJSObjWrapper::sJSObjWrapperNPClass =
|
|
|
|
{
|
|
|
|
NP_CLASS_STRUCT_VERSION,
|
|
|
|
nsJSObjWrapper::NP_Allocate,
|
|
|
|
nsJSObjWrapper::NP_Deallocate,
|
|
|
|
nsJSObjWrapper::NP_Invalidate,
|
|
|
|
nsJSObjWrapper::NP_HasMethod,
|
|
|
|
nsJSObjWrapper::NP_Invoke,
|
|
|
|
nsJSObjWrapper::NP_InvokeDefault,
|
|
|
|
nsJSObjWrapper::NP_HasProperty,
|
|
|
|
nsJSObjWrapper::NP_GetProperty,
|
|
|
|
nsJSObjWrapper::NP_SetProperty,
|
|
|
|
nsJSObjWrapper::NP_RemoveProperty,
|
2007-10-09 18:24:28 -07:00
|
|
|
nsJSObjWrapper::NP_Enumerate,
|
|
|
|
nsJSObjWrapper::NP_Construct
|
2007-03-22 10:30:00 -07:00
|
|
|
};
|
|
|
|
|
2013-08-08 15:53:04 -07:00
|
|
|
static bool
|
2013-06-21 06:12:46 -07:00
|
|
|
NPObjWrapper_AddProperty(JSContext *cx, JS::Handle<JSObject*> obj, JS::Handle<jsid> id, JS::MutableHandle<JS::Value> vp);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-08-08 15:53:04 -07:00
|
|
|
static bool
|
|
|
|
NPObjWrapper_DelProperty(JSContext *cx, JS::Handle<JSObject*> obj, JS::Handle<jsid> id, bool *succeeded);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-08-08 15:53:04 -07:00
|
|
|
static bool
|
|
|
|
NPObjWrapper_SetProperty(JSContext *cx, JS::Handle<JSObject*> obj, JS::Handle<jsid> id, bool strict,
|
2013-06-19 03:32:27 -07:00
|
|
|
JS::MutableHandle<JS::Value> vp);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-08-08 15:53:04 -07:00
|
|
|
static bool
|
2013-06-21 06:12:46 -07:00
|
|
|
NPObjWrapper_GetProperty(JSContext *cx, JS::Handle<JSObject*> obj, JS::Handle<jsid> id, JS::MutableHandle<JS::Value> vp);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-08-08 15:53:04 -07:00
|
|
|
static bool
|
2013-06-21 06:12:46 -07:00
|
|
|
NPObjWrapper_newEnumerate(JSContext *cx, JS::Handle<JSObject*> obj, JSIterateOp enum_op,
|
2013-04-02 16:05:37 -07:00
|
|
|
JS::Value *statep, jsid *idp);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-08-08 15:53:04 -07:00
|
|
|
static bool
|
2014-04-25 14:11:02 -07:00
|
|
|
NPObjWrapper_NewResolve(JSContext *cx, JS::Handle<JSObject*> obj, JS::Handle<jsid> id,
|
2013-04-16 16:20:44 -07:00
|
|
|
JS::MutableHandle<JSObject*> objp);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-08-08 15:53:04 -07:00
|
|
|
static bool
|
2013-06-21 06:12:46 -07:00
|
|
|
NPObjWrapper_Convert(JSContext *cx, JS::Handle<JSObject*> obj, JSType type, JS::MutableHandle<JS::Value> vp);
|
2007-10-09 18:24:28 -07:00
|
|
|
|
2008-09-06 15:21:43 -07:00
|
|
|
static void
|
2012-03-19 07:34:55 -07:00
|
|
|
NPObjWrapper_Finalize(JSFreeOp *fop, JSObject *obj);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-08-02 00:41:57 -07:00
|
|
|
static bool
|
2013-04-02 16:05:37 -07:00
|
|
|
NPObjWrapper_Call(JSContext *cx, unsigned argc, JS::Value *vp);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-08-02 00:41:57 -07:00
|
|
|
static bool
|
2013-04-02 16:05:37 -07:00
|
|
|
NPObjWrapper_Construct(JSContext *cx, unsigned argc, JS::Value *vp);
|
2007-10-09 18:24:28 -07:00
|
|
|
|
2013-08-08 15:53:04 -07:00
|
|
|
static bool
|
2014-04-16 01:47:53 -07:00
|
|
|
CreateNPObjectMember(NPP npp, JSContext *cx, JSObject *obj, NPObject* npobj,
|
|
|
|
JS::Handle<jsid> id, NPVariant* getPropertyResult,
|
|
|
|
JS::MutableHandle<JS::Value> vp);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-09-11 05:49:05 -07:00
|
|
|
const JSClass sNPObjectJSWrapperClass =
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2007-08-28 17:28:32 -07:00
|
|
|
NPRUNTIME_JSCLASS_NAME,
|
2012-02-17 14:35:20 -08:00
|
|
|
JSCLASS_HAS_PRIVATE | JSCLASS_IMPLEMENTS_BARRIERS | JSCLASS_NEW_RESOLVE | JSCLASS_NEW_ENUMERATE,
|
2012-05-07 12:44:08 -07:00
|
|
|
NPObjWrapper_AddProperty,
|
|
|
|
NPObjWrapper_DelProperty,
|
|
|
|
NPObjWrapper_GetProperty,
|
|
|
|
NPObjWrapper_SetProperty,
|
2007-04-17 16:10:20 -07:00
|
|
|
(JSEnumerateOp)NPObjWrapper_newEnumerate,
|
2012-05-07 12:44:08 -07:00
|
|
|
(JSResolveOp)NPObjWrapper_NewResolve,
|
|
|
|
NPObjWrapper_Convert,
|
|
|
|
NPObjWrapper_Finalize,
|
|
|
|
NPObjWrapper_Call,
|
2012-07-30 07:20:58 -07:00
|
|
|
nullptr, /* hasInstance */
|
2012-02-13 00:02:21 -08:00
|
|
|
NPObjWrapper_Construct
|
2007-03-22 10:30:00 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
typedef struct NPObjectMemberPrivate {
|
2013-07-23 02:58:27 -07:00
|
|
|
JS::Heap<JSObject *> npobjWrapper;
|
|
|
|
JS::Heap<JS::Value> fieldValue;
|
|
|
|
JS::Heap<jsid> methodName;
|
2007-03-22 10:30:00 -07:00
|
|
|
NPP npp;
|
|
|
|
} NPObjectMemberPrivate;
|
|
|
|
|
2013-08-08 15:53:04 -07:00
|
|
|
static bool
|
2013-06-21 06:12:46 -07:00
|
|
|
NPObjectMember_Convert(JSContext *cx, JS::Handle<JSObject*> obj, JSType type, JS::MutableHandle<JS::Value> vp);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2008-09-06 15:21:43 -07:00
|
|
|
static void
|
2012-03-19 07:34:55 -07:00
|
|
|
NPObjectMember_Finalize(JSFreeOp *fop, JSObject *obj);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-08-02 00:41:57 -07:00
|
|
|
static bool
|
2013-04-02 16:05:37 -07:00
|
|
|
NPObjectMember_Call(JSContext *cx, unsigned argc, JS::Value *vp);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-03-05 14:50:11 -08:00
|
|
|
static void
|
|
|
|
NPObjectMember_Trace(JSTracer *trc, JSObject *obj);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-09-11 05:49:05 -07:00
|
|
|
static const JSClass sNPObjectMemberClass =
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-02-28 10:44:04 -08:00
|
|
|
"NPObject Ambiguous Member class", JSCLASS_HAS_PRIVATE | JSCLASS_IMPLEMENTS_BARRIERS,
|
2013-04-05 21:22:55 -07:00
|
|
|
JS_PropertyStub, JS_DeletePropertyStub,
|
2011-02-09 11:31:40 -08:00
|
|
|
JS_PropertyStub, JS_StrictPropertyStub, JS_EnumerateStub,
|
2007-03-22 10:30:00 -07:00
|
|
|
JS_ResolveStub, NPObjectMember_Convert,
|
2014-01-24 16:08:26 -08:00
|
|
|
NPObjectMember_Finalize, NPObjectMember_Call,
|
2012-07-30 07:20:58 -07:00
|
|
|
nullptr, nullptr, NPObjectMember_Trace
|
2007-03-22 10:30:00 -07:00
|
|
|
};
|
|
|
|
|
2009-11-17 11:51:46 -08:00
|
|
|
static void
|
|
|
|
OnWrapperDestroyed();
|
|
|
|
|
2012-02-29 12:23:38 -08:00
|
|
|
static void
|
2013-07-09 07:28:15 -07:00
|
|
|
DelayedReleaseGCCallback(JSGCStatus status)
|
2009-11-17 11:51:46 -08:00
|
|
|
{
|
|
|
|
if (JSGC_END == status) {
|
2010-02-01 12:35:48 -08:00
|
|
|
// Take ownership of sDelayedReleases and null it out now. The
|
|
|
|
// _releaseobject call below can reenter GC and double-free these objects.
|
|
|
|
nsAutoPtr<nsTArray<NPObject*> > delayedReleases(sDelayedReleases);
|
2012-07-30 07:20:58 -07:00
|
|
|
sDelayedReleases = nullptr;
|
2010-02-01 12:35:48 -08:00
|
|
|
|
|
|
|
if (delayedReleases) {
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t i = 0; i < delayedReleases->Length(); ++i) {
|
2010-02-01 12:35:48 -08:00
|
|
|
NPObject* obj = (*delayedReleases)[i];
|
2009-11-17 11:51:46 -08:00
|
|
|
if (obj)
|
|
|
|
_releaseobject(obj);
|
|
|
|
OnWrapperDestroyed();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
static void
|
|
|
|
OnWrapperCreated()
|
|
|
|
{
|
|
|
|
if (sWrapperCount++ == 0) {
|
|
|
|
static const char rtsvc_id[] = "@mozilla.org/js/xpc/RuntimeService;1";
|
|
|
|
nsCOMPtr<nsIJSRuntimeService> rtsvc(do_GetService(rtsvc_id));
|
|
|
|
if (!rtsvc)
|
|
|
|
return;
|
|
|
|
|
|
|
|
rtsvc->GetRuntime(&sJSRuntime);
|
2012-07-30 07:20:58 -07:00
|
|
|
NS_ASSERTION(sJSRuntime != nullptr, "no JSRuntime?!");
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-11-17 11:51:46 -08:00
|
|
|
// Register our GC callback to perform delayed destruction of finalized
|
|
|
|
// NPObjects. Leave this callback around and don't ever unregister it.
|
|
|
|
rtsvc->RegisterGCCallback(DelayedReleaseGCCallback);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
OnWrapperDestroyed()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(sWrapperCount, "Whaaa, unbalanced created/destroyed calls!");
|
|
|
|
|
|
|
|
if (--sWrapperCount == 0) {
|
2014-05-27 02:35:13 -07:00
|
|
|
if (sJSObjWrappersAccessible) {
|
2013-11-19 14:53:36 -08:00
|
|
|
MOZ_ASSERT(sJSObjWrappers.count() == 0);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// No more wrappers, and our hash was initialized. Finish the
|
|
|
|
// hash to prevent leaking it.
|
2013-11-19 14:53:36 -08:00
|
|
|
sJSObjWrappers.finish();
|
2014-05-27 02:35:13 -07:00
|
|
|
sJSObjWrappersAccessible = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (sNPObjWrappers.ops) {
|
2013-11-19 14:53:36 -08:00
|
|
|
MOZ_ASSERT(sNPObjWrappers.entryCount == 0);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// No more wrappers, and our hash was initialized. Finish the
|
|
|
|
// hash to prevent leaking it.
|
|
|
|
PL_DHashTableFinish(&sNPObjWrappers);
|
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
sNPObjWrappers.ops = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// No more need for this.
|
2012-07-30 07:20:58 -07:00
|
|
|
sJSRuntime = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-06-14 12:49:48 -07:00
|
|
|
namespace mozilla {
|
|
|
|
namespace plugins {
|
|
|
|
namespace parent {
|
|
|
|
|
2014-07-16 05:49:02 -07:00
|
|
|
static nsIGlobalObject*
|
|
|
|
GetGlobalObject(NPP npp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-07-30 07:20:58 -07:00
|
|
|
NS_ENSURE_TRUE(npp, nullptr);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2008-09-15 05:45:01 -07:00
|
|
|
nsNPAPIPluginInstance *inst = (nsNPAPIPluginInstance *)npp->ndata;
|
2012-07-30 07:20:58 -07:00
|
|
|
NS_ENSURE_TRUE(inst, nullptr);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-10-02 19:43:18 -07:00
|
|
|
nsRefPtr<nsPluginInstanceOwner> owner = inst->GetOwner();
|
2012-07-30 07:20:58 -07:00
|
|
|
NS_ENSURE_TRUE(owner, nullptr);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nsCOMPtr<nsIDocument> doc;
|
|
|
|
owner->GetDocument(getter_AddRefs(doc));
|
2012-07-30 07:20:58 -07:00
|
|
|
NS_ENSURE_TRUE(doc, nullptr);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-07-16 05:49:02 -07:00
|
|
|
return doc->GetScopeObject();
|
|
|
|
}
|
|
|
|
|
|
|
|
JSContext *
|
|
|
|
GetJSContext(NPP npp)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIScriptGlobalObject> sgo = do_QueryInterface(GetGlobalObject(npp));
|
2012-07-30 07:20:58 -07:00
|
|
|
NS_ENSURE_TRUE(sgo, nullptr);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nsIScriptContext *scx = sgo->GetContext();
|
2012-07-30 07:20:58 -07:00
|
|
|
NS_ENSURE_TRUE(scx, nullptr);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-09-18 02:22:17 -07:00
|
|
|
return scx->GetNativeContext();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2011-06-14 12:49:48 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
static NPP
|
|
|
|
LookupNPP(NPObject *npobj);
|
|
|
|
|
|
|
|
|
2013-04-02 16:05:37 -07:00
|
|
|
static JS::Value
|
2007-03-22 10:30:00 -07:00
|
|
|
NPVariantToJSVal(NPP npp, JSContext *cx, const NPVariant *variant)
|
|
|
|
{
|
|
|
|
switch (variant->type) {
|
|
|
|
case NPVariantType_Void :
|
|
|
|
return JSVAL_VOID;
|
|
|
|
case NPVariantType_Null :
|
|
|
|
return JSVAL_NULL;
|
|
|
|
case NPVariantType_Bool :
|
|
|
|
return BOOLEAN_TO_JSVAL(NPVARIANT_TO_BOOLEAN(*variant));
|
|
|
|
case NPVariantType_Int32 :
|
2007-12-03 16:33:48 -08:00
|
|
|
{
|
|
|
|
// Don't use INT_TO_JSVAL directly to prevent bugs when dealing
|
2013-04-02 16:05:37 -07:00
|
|
|
// with ints larger than what fits in a integer JS::Value.
|
2012-08-01 17:59:47 -07:00
|
|
|
return ::JS_NumberValue(NPVARIANT_TO_INT32(*variant));
|
2007-12-03 16:33:48 -08:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
case NPVariantType_Double :
|
|
|
|
{
|
2012-08-01 17:59:47 -07:00
|
|
|
return ::JS_NumberValue(NPVARIANT_TO_DOUBLE(*variant));
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
case NPVariantType_String :
|
|
|
|
{
|
|
|
|
const NPString *s = &NPVARIANT_TO_STRING(*variant);
|
2008-12-16 20:48:42 -08:00
|
|
|
NS_ConvertUTF8toUTF16 utf16String(s->UTF8Characters, s->UTF8Length);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
JSString *str =
|
2013-12-04 04:19:38 -08:00
|
|
|
::JS_NewUCStringCopyN(cx, utf16String.get(), utf16String.Length());
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (str) {
|
|
|
|
return STRING_TO_JSVAL(str);
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case NPVariantType_Object:
|
|
|
|
{
|
|
|
|
if (npp) {
|
|
|
|
JSObject *obj =
|
|
|
|
nsNPObjWrapper::GetNewOrUsed(npp, cx, NPVARIANT_TO_OBJECT(*variant));
|
|
|
|
|
|
|
|
if (obj) {
|
|
|
|
return OBJECT_TO_JSVAL(obj);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_ERROR("Error wrapping NPObject!");
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
NS_ERROR("Unknown NPVariant type!");
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_ERROR("Unable to convert NPVariant to jsval!");
|
|
|
|
|
|
|
|
return JSVAL_VOID;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2013-04-02 16:05:37 -07:00
|
|
|
JSValToNPVariant(NPP npp, JSContext *cx, JS::Value val, NPVariant *variant)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
NS_ASSERTION(npp, "Must have an NPP to wrap a jsval!");
|
|
|
|
|
2014-04-27 20:27:54 -07:00
|
|
|
if (val.isPrimitive()) {
|
2007-03-22 10:30:00 -07:00
|
|
|
if (val == JSVAL_VOID) {
|
|
|
|
VOID_TO_NPVARIANT(*variant);
|
2014-04-27 19:30:51 -07:00
|
|
|
} else if (val.isNull()) {
|
2007-03-22 10:30:00 -07:00
|
|
|
NULL_TO_NPVARIANT(*variant);
|
2014-04-27 19:44:05 -07:00
|
|
|
} else if (val.isBoolean()) {
|
2014-04-27 19:34:12 -07:00
|
|
|
BOOLEAN_TO_NPVARIANT(val.toBoolean(), *variant);
|
2014-04-27 19:47:02 -07:00
|
|
|
} else if (val.isInt32()) {
|
2014-04-27 19:55:08 -07:00
|
|
|
INT32_TO_NPVARIANT(val.toInt32(), *variant);
|
2014-04-27 19:35:40 -07:00
|
|
|
} else if (val.isDouble()) {
|
2014-04-27 19:38:31 -07:00
|
|
|
double d = val.toDouble();
|
2012-03-01 18:54:01 -08:00
|
|
|
int i;
|
2010-08-17 18:40:16 -07:00
|
|
|
if (JS_DoubleIsInt32(d, &i)) {
|
|
|
|
INT32_TO_NPVARIANT(i, *variant);
|
|
|
|
} else {
|
|
|
|
DOUBLE_TO_NPVARIANT(d, *variant);
|
|
|
|
}
|
2014-04-27 19:41:01 -07:00
|
|
|
} else if (val.isString()) {
|
2014-04-27 19:45:46 -07:00
|
|
|
JSString *jsstr = val.toString();
|
2010-12-03 00:24:17 -08:00
|
|
|
|
2014-07-12 00:43:08 -07:00
|
|
|
nsAutoJSString str;
|
|
|
|
if (!str.init(cx, jsstr)) {
|
|
|
|
return false;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t len;
|
2007-03-22 10:30:00 -07:00
|
|
|
char *p = ToNewUTF8String(str, &len);
|
|
|
|
|
|
|
|
if (!p) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
STRINGN_TO_NPVARIANT(p, len, *variant);
|
|
|
|
} else {
|
|
|
|
NS_ERROR("Unknown primitive type!");
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-07-13 01:55:14 -07:00
|
|
|
// The reflected plugin object may be in another compartment if the plugin
|
|
|
|
// element has since been adopted into a new document. We don't bother
|
|
|
|
// transplanting the plugin objects, and just do a unwrap with security
|
|
|
|
// checks if we encounter one of them as an argument. If the unwrap fails,
|
2012-11-14 09:56:26 -08:00
|
|
|
// we run with the original wrapped object, since sometimes there are
|
|
|
|
// legitimate cases where a security wrapper ends up here (for example,
|
|
|
|
// Location objects, which are _always_ behind security wrappers).
|
2013-05-04 00:52:57 -07:00
|
|
|
JS::Rooted<JSObject*> obj(cx, val.toObjectOrNull());
|
2013-04-11 11:50:18 -07:00
|
|
|
obj = js::CheckedUnwrap(obj);
|
2012-07-13 01:55:14 -07:00
|
|
|
if (!obj) {
|
2014-04-27 19:58:52 -07:00
|
|
|
obj = val.toObjectOrNull();
|
2012-07-13 01:55:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NPObject *npobj = nsJSObjWrapper::GetNewOrUsed(npp, cx, obj);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!npobj) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pass over ownership of npobj to *variant
|
|
|
|
OBJECT_TO_NPVARIANT(npobj, *variant);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
ThrowJSException(JSContext *cx, const char *message)
|
|
|
|
{
|
|
|
|
const char *ex = PeekException();
|
|
|
|
|
|
|
|
if (ex) {
|
|
|
|
nsAutoString ucex;
|
|
|
|
|
|
|
|
if (message) {
|
|
|
|
AppendASCIItoUTF16(message, ucex);
|
|
|
|
|
|
|
|
AppendASCIItoUTF16(" [plugin exception: ", ucex);
|
|
|
|
}
|
|
|
|
|
|
|
|
AppendUTF8toUTF16(ex, ucex);
|
|
|
|
|
|
|
|
if (message) {
|
|
|
|
AppendASCIItoUTF16("].", ucex);
|
|
|
|
}
|
|
|
|
|
2013-12-04 04:19:38 -08:00
|
|
|
JSString *str = ::JS_NewUCStringCopyN(cx, ucex.get(), ucex.Length());
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (str) {
|
2013-11-11 00:04:41 -08:00
|
|
|
JS::Rooted<JS::Value> exn(cx, JS::StringValue(str));
|
2013-09-19 00:54:01 -07:00
|
|
|
::JS_SetPendingException(cx, exn);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
PopException();
|
|
|
|
} else {
|
|
|
|
::JS_ReportError(cx, message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-08 15:53:04 -07:00
|
|
|
static bool
|
2007-03-22 10:30:00 -07:00
|
|
|
ReportExceptionIfPending(JSContext *cx)
|
|
|
|
{
|
|
|
|
const char *ex = PeekException();
|
|
|
|
|
|
|
|
if (!ex) {
|
2013-08-06 23:59:54 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
ThrowJSException(cx, nullptr);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-08-06 23:59:54 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsJSObjWrapper::nsJSObjWrapper(NPP npp)
|
2014-04-16 01:47:53 -07:00
|
|
|
: mJSObj(GetJSContext(npp), nullptr), mNpp(npp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2009-11-12 14:16:54 -08:00
|
|
|
MOZ_COUNT_CTOR(nsJSObjWrapper);
|
2007-03-22 10:30:00 -07:00
|
|
|
OnWrapperCreated();
|
|
|
|
}
|
|
|
|
|
|
|
|
nsJSObjWrapper::~nsJSObjWrapper()
|
|
|
|
{
|
2009-11-12 14:16:54 -08:00
|
|
|
MOZ_COUNT_DTOR(nsJSObjWrapper);
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// Invalidate first, since it relies on sJSRuntime and sJSObjWrappers.
|
|
|
|
NP_Invalidate(this);
|
|
|
|
|
|
|
|
OnWrapperDestroyed();
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
NPObject *
|
|
|
|
nsJSObjWrapper::NP_Allocate(NPP npp, NPClass *aClass)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(aClass == &sJSObjWrapperNPClass,
|
|
|
|
"Huh, wrong class passed to NP_Allocate()!!!");
|
|
|
|
|
|
|
|
return new nsJSObjWrapper(npp);
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
void
|
|
|
|
nsJSObjWrapper::NP_Deallocate(NPObject *npobj)
|
|
|
|
{
|
|
|
|
// nsJSObjWrapper::~nsJSObjWrapper() will call NP_Invalidate().
|
|
|
|
delete (nsJSObjWrapper *)npobj;
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
void
|
|
|
|
nsJSObjWrapper::NP_Invalidate(NPObject *npobj)
|
|
|
|
{
|
|
|
|
nsJSObjWrapper *jsnpobj = (nsJSObjWrapper *)npobj;
|
|
|
|
|
|
|
|
if (jsnpobj && jsnpobj->mJSObj) {
|
|
|
|
|
2014-05-27 02:35:13 -07:00
|
|
|
if (sJSObjWrappersAccessible) {
|
|
|
|
// Remove the wrapper from the hash
|
|
|
|
nsJSObjWrapperKey key(jsnpobj->mJSObj, jsnpobj->mNpp);
|
|
|
|
JSObjWrapperTable::Ptr ptr = sJSObjWrappers.lookup(key);
|
|
|
|
MOZ_ASSERT(ptr.found());
|
|
|
|
sJSObjWrappers.remove(ptr);
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-27 02:35:13 -07:00
|
|
|
// Forget our reference to the JSObject.
|
|
|
|
jsnpobj->mJSObj = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-08 15:53:04 -07:00
|
|
|
static bool
|
2014-01-31 01:55:20 -08:00
|
|
|
GetProperty(JSContext *cx, JSObject *objArg, NPIdentifier npid, JS::MutableHandle<JS::Value> rval)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-01-31 01:55:20 -08:00
|
|
|
NS_ASSERTION(NPIdentifierIsInt(npid) || NPIdentifierIsString(npid),
|
2010-12-03 00:24:17 -08:00
|
|
|
"id must be either string or int!\n");
|
2014-01-31 01:55:20 -08:00
|
|
|
JS::Rooted<JSObject *> obj(cx, objArg);
|
|
|
|
JS::Rooted<jsid> id(cx, NPIdentifierToJSId(npid));
|
|
|
|
return ::JS_GetPropertyById(cx, obj, id, rval);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
bool
|
2010-07-14 23:19:36 -07:00
|
|
|
nsJSObjWrapper::NP_HasMethod(NPObject *npobj, NPIdentifier id)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
NPP npp = NPPStack::Peek();
|
2014-07-16 05:49:02 -07:00
|
|
|
dom::AutoJSAPI jsapi;
|
|
|
|
if (NS_WARN_IF(!jsapi.InitWithLegacyErrorReporting(GetGlobalObject(npp)))) {
|
2011-09-29 23:02:59 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2014-07-16 05:49:02 -07:00
|
|
|
JSContext *cx = jsapi.cx();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (!npobj) {
|
|
|
|
ThrowJSException(cx,
|
|
|
|
"Null npobj in nsJSObjWrapper::NP_HasMethod!");
|
|
|
|
|
2011-09-29 23:02:59 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsJSObjWrapper *npjsobj = (nsJSObjWrapper *)npobj;
|
2007-11-06 09:56:03 -08:00
|
|
|
|
2012-08-21 18:42:53 -07:00
|
|
|
JSAutoCompartment ac(cx, npjsobj->mJSObj);
|
2010-10-10 15:50:08 -07:00
|
|
|
|
2007-11-06 09:56:03 -08:00
|
|
|
AutoJSExceptionReporter reporter(cx);
|
|
|
|
|
2013-05-04 00:52:57 -07:00
|
|
|
JS::Rooted<JS::Value> v(cx);
|
2013-08-08 15:53:04 -07:00
|
|
|
bool ok = GetProperty(cx, npjsobj->mJSObj, id, &v);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-04-27 20:27:54 -07:00
|
|
|
return ok && !v.isPrimitive() &&
|
2014-04-27 19:58:52 -07:00
|
|
|
::JS_ObjectIsFunction(cx, v.toObjectOrNull());
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
doInvoke(NPObject *npobj, NPIdentifier method, const NPVariant *args,
|
2011-09-28 23:19:26 -07:00
|
|
|
uint32_t argCount, bool ctorCall, NPVariant *result)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
NPP npp = NPPStack::Peek();
|
|
|
|
JSContext *cx = GetJSContext(npp);
|
|
|
|
|
|
|
|
if (!cx) {
|
2011-09-29 23:02:59 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!npobj || !result) {
|
|
|
|
ThrowJSException(cx, "Null npobj, or result in doInvoke!");
|
|
|
|
|
2011-09-29 23:02:59 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize *result
|
|
|
|
VOID_TO_NPVARIANT(*result);
|
|
|
|
|
|
|
|
nsJSObjWrapper *npjsobj = (nsJSObjWrapper *)npobj;
|
|
|
|
|
2013-04-18 08:36:04 -07:00
|
|
|
nsCxPusher pusher;
|
|
|
|
pusher.Push(cx);
|
2014-02-13 07:33:04 -08:00
|
|
|
JS::Rooted<JSObject*> jsobj(cx, npjsobj->mJSObj);
|
|
|
|
JSAutoCompartment ac(cx, jsobj);
|
2013-06-20 11:05:34 -07:00
|
|
|
JS::Rooted<JS::Value> fv(cx);
|
2010-10-10 15:50:08 -07:00
|
|
|
|
2007-11-06 09:56:03 -08:00
|
|
|
AutoJSExceptionReporter reporter(cx);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-07-14 23:19:36 -07:00
|
|
|
if (method != NPIdentifier_VOID) {
|
2014-02-13 07:33:04 -08:00
|
|
|
if (!GetProperty(cx, jsobj, method, &fv) ||
|
2007-03-22 10:30:00 -07:00
|
|
|
::JS_TypeOfValue(cx, fv) != JSTYPE_FUNCTION) {
|
2011-09-29 23:02:59 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
} else {
|
2014-02-13 07:33:04 -08:00
|
|
|
fv.setObject(*jsobj);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2014-01-29 09:31:09 -08:00
|
|
|
// Convert args
|
|
|
|
JS::AutoValueVector jsargs(cx);
|
|
|
|
if (!jsargs.reserve(argCount)) {
|
2007-03-22 10:30:00 -07:00
|
|
|
::JS_ReportOutOfMemory(cx);
|
2011-09-29 23:02:59 -07:00
|
|
|
return false;
|
2014-01-29 09:31:09 -08:00
|
|
|
}
|
|
|
|
for (uint32_t i = 0; i < argCount; ++i) {
|
|
|
|
jsargs.infallibleAppend(NPVariantToJSVal(npp, cx, args + i));
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2013-05-04 00:52:57 -07:00
|
|
|
JS::Rooted<JS::Value> v(cx);
|
2014-01-29 09:31:09 -08:00
|
|
|
bool ok = false;
|
2007-10-09 18:24:28 -07:00
|
|
|
|
2014-01-29 09:31:09 -08:00
|
|
|
if (ctorCall) {
|
|
|
|
JSObject *newObj =
|
2014-03-20 02:32:37 -07:00
|
|
|
::JS_New(cx, jsobj, jsargs);
|
2010-03-29 19:47:40 -07:00
|
|
|
|
2014-01-29 09:31:09 -08:00
|
|
|
if (newObj) {
|
|
|
|
v.setObject(*newObj);
|
|
|
|
ok = true;
|
2010-03-29 19:47:40 -07:00
|
|
|
}
|
2014-01-29 09:31:09 -08:00
|
|
|
} else {
|
2014-02-13 07:33:04 -08:00
|
|
|
ok = ::JS_CallFunctionValue(cx, jsobj, fv, jsargs, &v);
|
2010-03-29 19:47:40 -07:00
|
|
|
}
|
2010-03-26 18:01:54 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
if (ok)
|
|
|
|
ok = JSValToNPVariant(npp, cx, v, result);
|
|
|
|
|
2013-08-06 23:59:54 -07:00
|
|
|
return ok;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
bool
|
|
|
|
nsJSObjWrapper::NP_Invoke(NPObject *npobj, NPIdentifier method,
|
|
|
|
const NPVariant *args, uint32_t argCount,
|
|
|
|
NPVariant *result)
|
|
|
|
{
|
2010-07-14 23:19:36 -07:00
|
|
|
if (method == NPIdentifier_VOID) {
|
2011-09-29 23:02:59 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2011-09-29 23:02:59 -07:00
|
|
|
return doInvoke(npobj, method, args, argCount, false, result);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
bool
|
|
|
|
nsJSObjWrapper::NP_InvokeDefault(NPObject *npobj, const NPVariant *args,
|
|
|
|
uint32_t argCount, NPVariant *result)
|
|
|
|
{
|
2011-09-29 23:02:59 -07:00
|
|
|
return doInvoke(npobj, NPIdentifier_VOID, args, argCount, false,
|
2007-10-09 18:24:28 -07:00
|
|
|
result);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
bool
|
2014-01-22 03:28:06 -08:00
|
|
|
nsJSObjWrapper::NP_HasProperty(NPObject *npobj, NPIdentifier npid)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
NPP npp = NPPStack::Peek();
|
|
|
|
JSContext *cx = GetJSContext(npp);
|
|
|
|
|
|
|
|
if (!cx) {
|
2011-09-29 23:02:59 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!npobj) {
|
|
|
|
ThrowJSException(cx,
|
|
|
|
"Null npobj in nsJSObjWrapper::NP_HasProperty!");
|
|
|
|
|
2011-09-29 23:02:59 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsJSObjWrapper *npjsobj = (nsJSObjWrapper *)npobj;
|
2013-08-08 15:53:04 -07:00
|
|
|
bool found, ok = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-04-18 08:36:04 -07:00
|
|
|
nsCxPusher pusher;
|
|
|
|
pusher.Push(cx);
|
2007-11-06 09:56:03 -08:00
|
|
|
AutoJSExceptionReporter reporter(cx);
|
2014-01-22 03:28:06 -08:00
|
|
|
JS::Rooted<JSObject*> jsobj(cx, npjsobj->mJSObj);
|
|
|
|
JSAutoCompartment ac(cx, jsobj);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-01-22 03:28:06 -08:00
|
|
|
NS_ASSERTION(NPIdentifierIsInt(npid) || NPIdentifierIsString(npid),
|
2010-12-03 00:24:17 -08:00
|
|
|
"id must be either string or int!\n");
|
2014-01-22 03:28:06 -08:00
|
|
|
JS::Rooted<jsid> id(cx, NPIdentifierToJSId(npid));
|
|
|
|
ok = ::JS_HasPropertyById(cx, jsobj, id, &found);
|
2007-03-22 10:30:00 -07:00
|
|
|
return ok && found;
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
bool
|
2010-07-14 23:19:36 -07:00
|
|
|
nsJSObjWrapper::NP_GetProperty(NPObject *npobj, NPIdentifier id,
|
2007-03-22 10:30:00 -07:00
|
|
|
NPVariant *result)
|
|
|
|
{
|
|
|
|
NPP npp = NPPStack::Peek();
|
|
|
|
JSContext *cx = GetJSContext(npp);
|
|
|
|
|
|
|
|
if (!cx) {
|
2011-09-29 23:02:59 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!npobj) {
|
|
|
|
ThrowJSException(cx,
|
|
|
|
"Null npobj in nsJSObjWrapper::NP_GetProperty!");
|
|
|
|
|
2011-09-29 23:02:59 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsJSObjWrapper *npjsobj = (nsJSObjWrapper *)npobj;
|
|
|
|
|
2013-04-18 08:36:04 -07:00
|
|
|
nsCxPusher pusher;
|
|
|
|
pusher.Push(cx);
|
2007-11-06 09:56:03 -08:00
|
|
|
AutoJSExceptionReporter reporter(cx);
|
2012-08-21 18:42:53 -07:00
|
|
|
JSAutoCompartment ac(cx, npjsobj->mJSObj);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-05-04 00:52:57 -07:00
|
|
|
JS::Rooted<JS::Value> v(cx);
|
2013-07-26 02:00:38 -07:00
|
|
|
return (GetProperty(cx, npjsobj->mJSObj, id, &v) &&
|
2007-03-22 10:30:00 -07:00
|
|
|
JSValToNPVariant(npp, cx, v, result));
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
bool
|
2014-01-25 01:29:12 -08:00
|
|
|
nsJSObjWrapper::NP_SetProperty(NPObject *npobj, NPIdentifier npid,
|
2007-03-22 10:30:00 -07:00
|
|
|
const NPVariant *value)
|
|
|
|
{
|
|
|
|
NPP npp = NPPStack::Peek();
|
|
|
|
JSContext *cx = GetJSContext(npp);
|
|
|
|
|
|
|
|
if (!cx) {
|
2011-09-29 23:02:59 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!npobj) {
|
|
|
|
ThrowJSException(cx,
|
|
|
|
"Null npobj in nsJSObjWrapper::NP_SetProperty!");
|
|
|
|
|
2011-09-29 23:02:59 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsJSObjWrapper *npjsobj = (nsJSObjWrapper *)npobj;
|
2013-08-06 23:59:54 -07:00
|
|
|
bool ok = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-04-18 08:36:04 -07:00
|
|
|
nsCxPusher pusher;
|
|
|
|
pusher.Push(cx);
|
2007-11-06 09:56:03 -08:00
|
|
|
AutoJSExceptionReporter reporter(cx);
|
2014-01-25 01:29:12 -08:00
|
|
|
JS::Rooted<JSObject*> jsObj(cx, npjsobj->mJSObj);
|
|
|
|
JSAutoCompartment ac(cx, jsObj);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-04-26 21:08:15 -07:00
|
|
|
JS::Rooted<JS::Value> v(cx, NPVariantToJSVal(npp, cx, value));
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-01-25 01:29:12 -08:00
|
|
|
NS_ASSERTION(NPIdentifierIsInt(npid) || NPIdentifierIsString(npid),
|
2010-12-03 00:24:17 -08:00
|
|
|
"id must be either string or int!\n");
|
2014-01-25 01:29:12 -08:00
|
|
|
JS::Rooted<jsid> id(cx, NPIdentifierToJSId(npid));
|
|
|
|
ok = ::JS_SetPropertyById(cx, jsObj, id, v);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-08-06 23:59:54 -07:00
|
|
|
return ok;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
bool
|
2014-01-22 03:28:06 -08:00
|
|
|
nsJSObjWrapper::NP_RemoveProperty(NPObject *npobj, NPIdentifier npid)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
NPP npp = NPPStack::Peek();
|
|
|
|
JSContext *cx = GetJSContext(npp);
|
|
|
|
|
|
|
|
if (!cx) {
|
2011-09-29 23:02:59 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!npobj) {
|
|
|
|
ThrowJSException(cx,
|
|
|
|
"Null npobj in nsJSObjWrapper::NP_RemoveProperty!");
|
|
|
|
|
2011-09-29 23:02:59 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsJSObjWrapper *npjsobj = (nsJSObjWrapper *)npobj;
|
2013-08-08 15:53:04 -07:00
|
|
|
bool ok = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-04-18 08:36:04 -07:00
|
|
|
nsCxPusher pusher;
|
|
|
|
pusher.Push(cx);
|
2007-11-06 09:56:03 -08:00
|
|
|
AutoJSExceptionReporter reporter(cx);
|
2013-08-05 06:01:53 -07:00
|
|
|
bool deleted = false;
|
2014-01-22 03:28:06 -08:00
|
|
|
JS::Rooted<JSObject*> obj(cx, npjsobj->mJSObj);
|
|
|
|
JSAutoCompartment ac(cx, obj);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-01-22 03:28:06 -08:00
|
|
|
NS_ASSERTION(NPIdentifierIsInt(npid) || NPIdentifierIsString(npid),
|
2010-12-03 00:24:17 -08:00
|
|
|
"id must be either string or int!\n");
|
2014-01-22 03:28:06 -08:00
|
|
|
JS::Rooted<jsid> id(cx, NPIdentifierToJSId(npid));
|
|
|
|
ok = ::JS_DeletePropertyById2(cx, obj, id, &deleted);
|
2013-08-05 06:01:53 -07:00
|
|
|
if (ok && deleted) {
|
2010-12-03 00:24:17 -08:00
|
|
|
// FIXME: See bug 425823, we shouldn't need to do this, and once
|
|
|
|
// that bug is fixed we can remove this code.
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-08-08 15:53:04 -07:00
|
|
|
bool hasProp;
|
2014-01-22 03:28:06 -08:00
|
|
|
ok = ::JS_HasPropertyById(cx, obj, id, &hasProp);
|
2008-03-28 15:29:14 -07:00
|
|
|
|
2010-12-03 00:24:17 -08:00
|
|
|
if (ok && hasProp) {
|
|
|
|
// The property might have been deleted, but it got
|
|
|
|
// re-resolved, so no, it's not really deleted.
|
2008-03-28 15:29:14 -07:00
|
|
|
|
2013-08-05 06:01:53 -07:00
|
|
|
deleted = false;
|
2008-03-28 15:29:14 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2013-08-05 06:01:53 -07:00
|
|
|
return ok && deleted;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
//static
|
|
|
|
bool
|
2010-07-14 23:19:36 -07:00
|
|
|
nsJSObjWrapper::NP_Enumerate(NPObject *npobj, NPIdentifier **idarray,
|
2007-03-22 10:30:00 -07:00
|
|
|
uint32_t *count)
|
|
|
|
{
|
|
|
|
NPP npp = NPPStack::Peek();
|
|
|
|
JSContext *cx = GetJSContext(npp);
|
|
|
|
|
2010-07-14 23:19:36 -07:00
|
|
|
*idarray = 0;
|
2007-03-22 10:30:00 -07:00
|
|
|
*count = 0;
|
|
|
|
|
|
|
|
if (!cx) {
|
2011-09-29 23:02:59 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!npobj) {
|
|
|
|
ThrowJSException(cx,
|
|
|
|
"Null npobj in nsJSObjWrapper::NP_Enumerate!");
|
|
|
|
|
2011-09-29 23:02:59 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsJSObjWrapper *npjsobj = (nsJSObjWrapper *)npobj;
|
|
|
|
|
2013-04-18 08:36:04 -07:00
|
|
|
nsCxPusher pusher;
|
|
|
|
pusher.Push(cx);
|
2007-11-06 09:56:03 -08:00
|
|
|
AutoJSExceptionReporter reporter(cx);
|
2014-04-02 07:28:03 -07:00
|
|
|
JS::Rooted<JSObject*> jsobj(cx, npjsobj->mJSObj);
|
|
|
|
JSAutoCompartment ac(cx, jsobj);
|
2007-09-29 23:19:52 -07:00
|
|
|
|
2014-04-02 07:28:03 -07:00
|
|
|
JS::AutoIdArray ida(cx, JS_Enumerate(cx, jsobj));
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!ida) {
|
2011-09-29 23:02:59 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2012-01-11 00:23:09 -08:00
|
|
|
*count = ida.length();
|
2010-07-14 23:19:36 -07:00
|
|
|
*idarray = (NPIdentifier *)PR_Malloc(*count * sizeof(NPIdentifier));
|
|
|
|
if (!*idarray) {
|
2007-03-22 10:30:00 -07:00
|
|
|
ThrowJSException(cx, "Memory allocation failed for NPIdentifier!");
|
2011-09-29 23:02:59 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t i = 0; i < *count; i++) {
|
2013-05-04 00:52:57 -07:00
|
|
|
JS::Rooted<JS::Value> v(cx);
|
2014-01-17 10:08:51 -08:00
|
|
|
if (!JS_IdToValue(cx, ida[i], &v)) {
|
2010-07-14 23:19:36 -07:00
|
|
|
PR_Free(*idarray);
|
2011-09-29 23:02:59 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2010-07-14 23:19:36 -07:00
|
|
|
NPIdentifier id;
|
2013-09-16 18:33:40 -07:00
|
|
|
if (v.isString()) {
|
|
|
|
JS::Rooted<JSString*> str(cx, v.toString());
|
|
|
|
str = JS_InternJSString(cx, str);
|
2010-12-03 00:24:17 -08:00
|
|
|
if (!str) {
|
2012-01-11 00:23:09 -08:00
|
|
|
PR_Free(*idarray);
|
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2011-05-17 12:15:12 -07:00
|
|
|
id = StringToNPIdentifier(cx, str);
|
2007-03-22 10:30:00 -07:00
|
|
|
} else {
|
2014-04-27 19:47:02 -07:00
|
|
|
NS_ASSERTION(v.isInt32(),
|
2007-03-22 10:30:00 -07:00
|
|
|
"The element in ida must be either string or int!\n");
|
2014-04-27 19:55:08 -07:00
|
|
|
id = IntToNPIdentifier(v.toInt32());
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2010-07-14 23:19:36 -07:00
|
|
|
(*idarray)[i] = id;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2011-09-29 23:02:59 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2007-10-09 18:24:28 -07:00
|
|
|
//static
|
|
|
|
bool
|
|
|
|
nsJSObjWrapper::NP_Construct(NPObject *npobj, const NPVariant *args,
|
|
|
|
uint32_t argCount, NPVariant *result)
|
|
|
|
{
|
2011-09-29 23:02:59 -07:00
|
|
|
return doInvoke(npobj, NPIdentifier_VOID, args, argCount, true, result);
|
2007-10-09 18:24:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-11-20 08:38:10 -08:00
|
|
|
|
2013-11-19 14:53:36 -08:00
|
|
|
/*
|
|
|
|
* This function is called during minor GCs for each key in the sJSObjWrappers
|
|
|
|
* table that has been moved.
|
2014-01-21 02:44:38 -08:00
|
|
|
*
|
|
|
|
* Note that the wrapper may be dead at this point, and even the table may have
|
|
|
|
* been finalized if all wrappers have died.
|
2013-11-19 14:53:36 -08:00
|
|
|
*/
|
|
|
|
static void
|
2014-01-21 02:44:39 -08:00
|
|
|
JSObjWrapperKeyMarkCallback(JSTracer *trc, JSObject *obj, void *data) {
|
2014-01-21 02:44:38 -08:00
|
|
|
NPP npp = static_cast<NPP>(data);
|
2014-05-27 02:35:13 -07:00
|
|
|
MOZ_ASSERT(sJSObjWrappersAccessible);
|
2014-01-21 02:44:38 -08:00
|
|
|
if (!sJSObjWrappers.initialized())
|
|
|
|
return;
|
|
|
|
|
2013-11-19 14:53:36 -08:00
|
|
|
JSObject *prior = obj;
|
|
|
|
nsJSObjWrapperKey oldKey(prior, npp);
|
2014-01-21 02:44:38 -08:00
|
|
|
JSObjWrapperTable::Ptr p = sJSObjWrappers.lookup(oldKey);
|
|
|
|
if (!p)
|
|
|
|
return;
|
|
|
|
|
|
|
|
JS_CallObjectTracer(trc, &obj, "sJSObjWrappers key object");
|
2013-11-19 14:53:36 -08:00
|
|
|
nsJSObjWrapperKey newKey(obj, npp);
|
|
|
|
sJSObjWrappers.rekeyIfMoved(oldKey, newKey);
|
2013-11-20 08:38:10 -08:00
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// Look up or create an NPObject that wraps the JSObject obj.
|
|
|
|
|
|
|
|
// static
|
|
|
|
NPObject *
|
2013-05-14 16:25:32 -07:00
|
|
|
nsJSObjWrapper::GetNewOrUsed(NPP npp, JSContext *cx, JS::Handle<JSObject*> obj)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
if (!npp) {
|
|
|
|
NS_ERROR("Null NPP passed to nsJSObjWrapper::GetNewOrUsed()!");
|
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!cx) {
|
|
|
|
cx = GetJSContext(npp);
|
|
|
|
|
|
|
|
if (!cx) {
|
2010-10-10 15:50:08 -07:00
|
|
|
NS_ERROR("Unable to find a JSContext in nsJSObjWrapper::GetNewOrUsed()!");
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-10-10 15:50:08 -07:00
|
|
|
// No need to enter the right compartment here as we only get the
|
|
|
|
// class and private from the JSObject, neither of which cares about
|
|
|
|
// compartments.
|
|
|
|
|
2013-09-11 05:49:05 -07:00
|
|
|
const JSClass *clazz = JS_GetClass(obj);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (clazz == &sNPObjectJSWrapperClass) {
|
|
|
|
// obj is one of our own, its private data is the NPObject we're
|
|
|
|
// looking for.
|
|
|
|
|
2012-02-05 12:07:23 -08:00
|
|
|
NPObject *npobj = (NPObject *)::JS_GetPrivate(obj);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-07-27 02:15:43 -07:00
|
|
|
// If the private is null, that means that the object has already been torn
|
|
|
|
// down, possible because the owning plugin was destroyed (there can be
|
|
|
|
// multiple plugins, so the fact that it was destroyed does not prevent one
|
|
|
|
// of its dead JS objects from being passed to another plugin). There's not
|
|
|
|
// much use in wrapping such a dead object, so we just return null, causing
|
|
|
|
// us to throw.
|
|
|
|
if (!npobj)
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2012-07-27 02:15:43 -07:00
|
|
|
|
2009-12-02 09:02:41 -08:00
|
|
|
if (LookupNPP(npobj) == npp)
|
|
|
|
return _retainobject(npobj);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2013-11-19 14:53:36 -08:00
|
|
|
if (!sJSObjWrappers.initialized()) {
|
2007-03-22 10:30:00 -07:00
|
|
|
// No hash yet (or any more), initialize it.
|
2013-11-19 14:53:36 -08:00
|
|
|
if (!sJSObjWrappers.init(16)) {
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_ERROR("Error initializing PLDHashTable!");
|
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2014-05-27 02:35:13 -07:00
|
|
|
sJSObjWrappersAccessible = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2014-05-27 02:35:13 -07:00
|
|
|
MOZ_ASSERT(sJSObjWrappersAccessible);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-12-06 13:52:13 -08:00
|
|
|
JSObjWrapperTable::Ptr p = sJSObjWrappers.lookupForAdd(nsJSObjWrapperKey(obj, npp));
|
|
|
|
if (p) {
|
2013-11-25 17:35:09 -08:00
|
|
|
MOZ_ASSERT(p->value());
|
2007-03-22 10:30:00 -07:00
|
|
|
// Found a live nsJSObjWrapper, return it.
|
|
|
|
|
2013-11-25 17:35:09 -08:00
|
|
|
return _retainobject(p->value());
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// No existing nsJSObjWrapper, create one.
|
|
|
|
|
|
|
|
nsJSObjWrapper *wrapper =
|
|
|
|
(nsJSObjWrapper *)_createobject(npp, &sJSObjWrapperNPClass);
|
|
|
|
|
|
|
|
if (!wrapper) {
|
2013-11-19 14:53:36 -08:00
|
|
|
// Out of memory, entry not yet added to table.
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2014-04-16 01:47:53 -07:00
|
|
|
// Assign mJSObj, rooting the JSObject. Its lifetime is now tied to that of
|
|
|
|
// the NPObject.
|
2007-03-22 10:30:00 -07:00
|
|
|
wrapper->mJSObj = obj;
|
|
|
|
|
2013-12-06 13:52:13 -08:00
|
|
|
nsJSObjWrapperKey key(obj, npp);
|
|
|
|
if (!sJSObjWrappers.putNew(key, wrapper)) {
|
2013-11-19 14:53:36 -08:00
|
|
|
// Out of memory, free the wrapper we created.
|
|
|
|
_releaseobject(wrapper);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-11-19 14:53:36 -08:00
|
|
|
// Add postbarrier for the hashtable key
|
2014-01-21 02:44:38 -08:00
|
|
|
JS_StoreObjectPostBarrierCallback(cx, JSObjWrapperKeyMarkCallback, obj, wrapper->mNpp);
|
2013-11-19 14:53:36 -08:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
return wrapper;
|
|
|
|
}
|
|
|
|
|
2012-07-13 01:55:14 -07:00
|
|
|
// Climb the prototype chain, unwrapping as necessary until we find an NP object
|
|
|
|
// wrapper.
|
|
|
|
//
|
2012-07-20 00:17:43 -07:00
|
|
|
// Because this function unwraps, its return value must be wrapped for the cx
|
|
|
|
// compartment for callers that plan to hold onto the result or do anything
|
|
|
|
// substantial with it.
|
2012-07-13 01:55:14 -07:00
|
|
|
static JSObject *
|
2013-05-14 16:25:32 -07:00
|
|
|
GetNPObjectWrapper(JSContext *cx, JSObject *aObj, bool wrapResult = true)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2013-05-14 16:25:32 -07:00
|
|
|
JS::Rooted<JSObject*> obj(cx, aObj);
|
2013-04-11 11:50:18 -07:00
|
|
|
while (obj && (obj = js::CheckedUnwrap(obj))) {
|
2012-07-20 00:17:43 -07:00
|
|
|
if (JS_GetClass(obj) == &sNPObjectJSWrapperClass) {
|
2013-10-15 17:02:23 -07:00
|
|
|
if (wrapResult && !JS_WrapObject(cx, &obj)) {
|
2013-10-23 13:34:30 -07:00
|
|
|
return nullptr;
|
2012-07-20 00:17:43 -07:00
|
|
|
}
|
2012-07-13 01:55:14 -07:00
|
|
|
return obj;
|
2012-07-20 00:17:43 -07:00
|
|
|
}
|
2013-07-31 09:20:33 -07:00
|
|
|
if (!::JS_GetPrototype(cx, obj, &obj)) {
|
2013-10-23 13:34:30 -07:00
|
|
|
return nullptr;
|
2012-09-03 16:42:17 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2013-10-23 13:34:30 -07:00
|
|
|
return nullptr;
|
2012-07-13 01:55:14 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-07-13 01:55:14 -07:00
|
|
|
static NPObject *
|
|
|
|
GetNPObject(JSContext *cx, JSObject *obj)
|
|
|
|
{
|
2012-07-20 00:17:43 -07:00
|
|
|
obj = GetNPObjectWrapper(cx, obj, /* wrapResult = */ false);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!obj) {
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2012-02-05 12:07:23 -08:00
|
|
|
return (NPObject *)::JS_GetPrivate(obj);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2009-05-08 06:55:04 -07:00
|
|
|
|
|
|
|
// Does not actually add a property because this is always followed by a
|
|
|
|
// SetProperty call.
|
2013-08-08 15:53:04 -07:00
|
|
|
static bool
|
2013-06-21 06:12:46 -07:00
|
|
|
NPObjWrapper_AddProperty(JSContext *cx, JS::Handle<JSObject*> obj, JS::Handle<jsid> id, JS::MutableHandle<JS::Value> vp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-07-13 01:55:14 -07:00
|
|
|
NPObject *npobj = GetNPObject(cx, obj);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (!npobj || !npobj->_class || !npobj->_class->hasProperty ||
|
|
|
|
!npobj->_class->hasMethod) {
|
|
|
|
ThrowJSException(cx, "Bad NPObject as private data!");
|
|
|
|
|
2013-08-06 23:59:54 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2010-04-07 17:16:19 -07:00
|
|
|
if (NPObjectIsOutOfProcessProxy(npobj)) {
|
2013-08-06 23:59:54 -07:00
|
|
|
return true;
|
2010-04-07 17:16:19 -07:00
|
|
|
}
|
|
|
|
|
2009-04-07 14:20:57 -07:00
|
|
|
PluginDestructionGuard pdg(LookupNPP(npobj));
|
|
|
|
|
2010-07-14 23:19:36 -07:00
|
|
|
NPIdentifier identifier = JSIdToNPIdentifier(id);
|
2013-08-08 15:53:04 -07:00
|
|
|
bool hasProperty = npobj->_class->hasProperty(npobj, identifier);
|
2009-05-08 06:55:04 -07:00
|
|
|
if (!ReportExceptionIfPending(cx))
|
2013-08-06 23:59:54 -07:00
|
|
|
return false;
|
2009-05-08 06:55:04 -07:00
|
|
|
|
|
|
|
if (hasProperty)
|
2013-08-06 23:59:54 -07:00
|
|
|
return true;
|
2009-05-08 06:55:04 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// We must permit methods here since JS_DefineUCFunction() will add
|
|
|
|
// the function as a property
|
2013-08-08 15:53:04 -07:00
|
|
|
bool hasMethod = npobj->_class->hasMethod(npobj, identifier);
|
2009-05-08 06:55:04 -07:00
|
|
|
if (!ReportExceptionIfPending(cx))
|
2013-08-06 23:59:54 -07:00
|
|
|
return false;
|
2009-05-08 06:55:04 -07:00
|
|
|
|
|
|
|
if (!hasMethod) {
|
|
|
|
ThrowJSException(cx, "Trying to add unsupported property on NPObject!");
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-08-06 23:59:54 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2013-08-06 23:59:54 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2013-08-08 15:53:04 -07:00
|
|
|
static bool
|
|
|
|
NPObjWrapper_DelProperty(JSContext *cx, JS::Handle<JSObject*> obj, JS::Handle<jsid> id, bool *succeeded)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-07-13 01:55:14 -07:00
|
|
|
NPObject *npobj = GetNPObject(cx, obj);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-01-16 09:14:43 -08:00
|
|
|
if (!npobj || !npobj->_class || !npobj->_class->hasProperty ||
|
|
|
|
!npobj->_class->removeProperty) {
|
2007-03-22 10:30:00 -07:00
|
|
|
ThrowJSException(cx, "Bad NPObject as private data!");
|
|
|
|
|
2013-08-06 23:59:54 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2009-04-07 14:20:57 -07:00
|
|
|
PluginDestructionGuard pdg(LookupNPP(npobj));
|
|
|
|
|
2010-07-14 23:19:36 -07:00
|
|
|
NPIdentifier identifier = JSIdToNPIdentifier(id);
|
|
|
|
|
2010-04-07 17:16:19 -07:00
|
|
|
if (!NPObjectIsOutOfProcessProxy(npobj)) {
|
2013-08-08 15:53:04 -07:00
|
|
|
bool hasProperty = npobj->_class->hasProperty(npobj, identifier);
|
2010-04-07 17:16:19 -07:00
|
|
|
if (!ReportExceptionIfPending(cx))
|
2013-08-06 23:59:54 -07:00
|
|
|
return false;
|
2009-05-08 06:55:04 -07:00
|
|
|
|
2013-04-05 21:22:55 -07:00
|
|
|
if (!hasProperty) {
|
|
|
|
*succeeded = true;
|
2013-08-06 23:59:54 -07:00
|
|
|
return true;
|
2013-04-05 21:22:55 -07:00
|
|
|
}
|
2010-04-07 17:16:19 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-07-14 23:19:36 -07:00
|
|
|
if (!npobj->_class->removeProperty(npobj, identifier))
|
2013-04-05 21:22:55 -07:00
|
|
|
*succeeded = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
return ReportExceptionIfPending(cx);
|
|
|
|
}
|
|
|
|
|
2013-08-08 15:53:04 -07:00
|
|
|
static bool
|
|
|
|
NPObjWrapper_SetProperty(JSContext *cx, JS::Handle<JSObject*> obj, JS::Handle<jsid> id, bool strict,
|
2013-06-19 03:32:27 -07:00
|
|
|
JS::MutableHandle<JS::Value> vp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-07-13 01:55:14 -07:00
|
|
|
NPObject *npobj = GetNPObject(cx, obj);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (!npobj || !npobj->_class || !npobj->_class->hasProperty ||
|
|
|
|
!npobj->_class->setProperty) {
|
|
|
|
ThrowJSException(cx, "Bad NPObject as private data!");
|
|
|
|
|
2013-08-06 23:59:54 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Find out what plugin (NPP) is the owner of the object we're
|
|
|
|
// manipulating, and make it own any JSObject wrappers created here.
|
|
|
|
NPP npp = LookupNPP(npobj);
|
|
|
|
|
|
|
|
if (!npp) {
|
|
|
|
ThrowJSException(cx, "No NPP found for NPObject!");
|
|
|
|
|
2013-08-06 23:59:54 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2009-04-07 14:20:57 -07:00
|
|
|
PluginDestructionGuard pdg(npp);
|
|
|
|
|
2010-07-14 23:19:36 -07:00
|
|
|
NPIdentifier identifier = JSIdToNPIdentifier(id);
|
|
|
|
|
2010-04-07 17:16:19 -07:00
|
|
|
if (!NPObjectIsOutOfProcessProxy(npobj)) {
|
2013-08-08 15:53:04 -07:00
|
|
|
bool hasProperty = npobj->_class->hasProperty(npobj, identifier);
|
2010-04-07 17:16:19 -07:00
|
|
|
if (!ReportExceptionIfPending(cx))
|
2013-08-06 23:59:54 -07:00
|
|
|
return false;
|
2009-05-08 06:55:04 -07:00
|
|
|
|
2010-04-07 17:16:19 -07:00
|
|
|
if (!hasProperty) {
|
|
|
|
ThrowJSException(cx, "Trying to set unsupported property on NPObject!");
|
2009-04-07 14:20:57 -07:00
|
|
|
|
2013-08-06 23:59:54 -07:00
|
|
|
return false;
|
2010-04-07 17:16:19 -07:00
|
|
|
}
|
2009-04-07 14:20:57 -07:00
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
NPVariant npv;
|
2012-07-30 04:19:09 -07:00
|
|
|
if (!JSValToNPVariant(npp, cx, vp, &npv)) {
|
2007-03-22 10:30:00 -07:00
|
|
|
ThrowJSException(cx, "Error converting jsval to NPVariant!");
|
|
|
|
|
2013-08-06 23:59:54 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2013-08-08 15:53:04 -07:00
|
|
|
bool ok = npobj->_class->setProperty(npobj, identifier, &npv);
|
2009-05-08 06:55:04 -07:00
|
|
|
_releasevariantvalue(&npv); // Release the variant
|
|
|
|
if (!ReportExceptionIfPending(cx))
|
2013-08-06 23:59:54 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (!ok) {
|
2009-05-08 06:55:04 -07:00
|
|
|
ThrowJSException(cx, "Error setting property on NPObject!");
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-08-06 23:59:54 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2013-08-06 23:59:54 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2013-08-08 15:53:04 -07:00
|
|
|
static bool
|
2013-06-21 06:12:46 -07:00
|
|
|
NPObjWrapper_GetProperty(JSContext *cx, JS::Handle<JSObject*> obj, JS::Handle<jsid> id, JS::MutableHandle<JS::Value> vp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-07-13 01:55:14 -07:00
|
|
|
NPObject *npobj = GetNPObject(cx, obj);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (!npobj || !npobj->_class || !npobj->_class->hasProperty ||
|
|
|
|
!npobj->_class->hasMethod || !npobj->_class->getProperty) {
|
|
|
|
ThrowJSException(cx, "Bad NPObject as private data!");
|
|
|
|
|
2013-08-06 23:59:54 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2009-04-07 14:20:57 -07:00
|
|
|
// Find out what plugin (NPP) is the owner of the object we're
|
|
|
|
// manipulating, and make it own any JSObject wrappers created here.
|
|
|
|
NPP npp = LookupNPP(npobj);
|
|
|
|
if (!npp) {
|
|
|
|
ThrowJSException(cx, "No NPP found for NPObject!");
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-08-06 23:59:54 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2009-04-07 14:20:57 -07:00
|
|
|
PluginDestructionGuard pdg(npp);
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool hasProperty, hasMethod;
|
2010-04-07 17:16:19 -07:00
|
|
|
|
|
|
|
NPVariant npv;
|
|
|
|
VOID_TO_NPVARIANT(npv);
|
|
|
|
|
2010-07-14 23:19:36 -07:00
|
|
|
NPIdentifier identifier = JSIdToNPIdentifier(id);
|
|
|
|
|
2010-04-07 17:16:19 -07:00
|
|
|
if (NPObjectIsOutOfProcessProxy(npobj)) {
|
|
|
|
PluginScriptableObjectParent* actor =
|
|
|
|
static_cast<ParentNPObject*>(npobj)->parent;
|
2010-05-17 15:18:48 -07:00
|
|
|
|
|
|
|
// actor may be null if the plugin crashed.
|
|
|
|
if (!actor)
|
2013-08-06 23:59:54 -07:00
|
|
|
return false;
|
2010-05-17 15:18:48 -07:00
|
|
|
|
2013-08-08 15:53:04 -07:00
|
|
|
bool success = actor->GetPropertyHelper(identifier, &hasProperty,
|
|
|
|
&hasMethod, &npv);
|
2010-04-07 17:16:19 -07:00
|
|
|
if (!ReportExceptionIfPending(cx)) {
|
|
|
|
if (success)
|
|
|
|
_releasevariantvalue(&npv);
|
2013-08-06 23:59:54 -07:00
|
|
|
return false;
|
2010-04-07 17:16:19 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (success) {
|
|
|
|
// We return NPObject Member class here to support ambiguous members.
|
|
|
|
if (hasProperty && hasMethod)
|
2014-04-16 01:47:53 -07:00
|
|
|
return CreateNPObjectMember(npp, cx, obj, npobj, id, &npv, vp);
|
2010-04-07 17:16:19 -07:00
|
|
|
|
|
|
|
if (hasProperty) {
|
2012-07-30 04:19:09 -07:00
|
|
|
vp.set(NPVariantToJSVal(npp, cx, &npv));
|
2010-04-07 17:16:19 -07:00
|
|
|
_releasevariantvalue(&npv);
|
|
|
|
|
|
|
|
if (!ReportExceptionIfPending(cx))
|
2013-08-06 23:59:54 -07:00
|
|
|
return false;
|
2010-04-07 17:16:19 -07:00
|
|
|
}
|
|
|
|
}
|
2013-08-06 23:59:54 -07:00
|
|
|
return true;
|
2010-04-07 17:16:19 -07:00
|
|
|
}
|
|
|
|
|
2010-07-14 23:19:36 -07:00
|
|
|
hasProperty = npobj->_class->hasProperty(npobj, identifier);
|
2009-05-08 06:55:04 -07:00
|
|
|
if (!ReportExceptionIfPending(cx))
|
2013-08-06 23:59:54 -07:00
|
|
|
return false;
|
2009-05-08 06:55:04 -07:00
|
|
|
|
2010-07-14 23:19:36 -07:00
|
|
|
hasMethod = npobj->_class->hasMethod(npobj, identifier);
|
2009-05-08 06:55:04 -07:00
|
|
|
if (!ReportExceptionIfPending(cx))
|
2013-08-06 23:59:54 -07:00
|
|
|
return false;
|
2009-04-07 14:20:57 -07:00
|
|
|
|
2009-05-08 06:55:04 -07:00
|
|
|
// We return NPObject Member class here to support ambiguous members.
|
2007-03-22 10:30:00 -07:00
|
|
|
if (hasProperty && hasMethod)
|
2014-04-16 01:47:53 -07:00
|
|
|
return CreateNPObjectMember(npp, cx, obj, npobj, id, nullptr, vp);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (hasProperty) {
|
2010-07-14 23:19:36 -07:00
|
|
|
if (npobj->_class->getProperty(npobj, identifier, &npv))
|
2012-07-30 04:19:09 -07:00
|
|
|
vp.set(NPVariantToJSVal(npp, cx, &npv));
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
_releasevariantvalue(&npv);
|
|
|
|
|
2009-05-08 06:55:04 -07:00
|
|
|
if (!ReportExceptionIfPending(cx))
|
2013-08-06 23:59:54 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2013-08-06 23:59:54 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2013-08-08 15:53:04 -07:00
|
|
|
static bool
|
2013-05-14 16:25:32 -07:00
|
|
|
CallNPMethodInternal(JSContext *cx, JS::Handle<JSObject*> obj, unsigned argc,
|
2013-04-02 16:05:37 -07:00
|
|
|
JS::Value *argv, JS::Value *rval, bool ctorCall)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-07-13 01:55:14 -07:00
|
|
|
NPObject *npobj = GetNPObject(cx, obj);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-02-17 10:30:54 -08:00
|
|
|
if (!npobj || !npobj->_class) {
|
2007-03-22 10:30:00 -07:00
|
|
|
ThrowJSException(cx, "Bad NPObject as private data!");
|
|
|
|
|
2013-08-06 23:59:54 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Find out what plugin (NPP) is the owner of the object we're
|
|
|
|
// manipulating, and make it own any JSObject wrappers created here.
|
|
|
|
NPP npp = LookupNPP(npobj);
|
|
|
|
|
|
|
|
if (!npp) {
|
|
|
|
ThrowJSException(cx, "Error finding NPP for NPObject!");
|
|
|
|
|
2013-08-06 23:59:54 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2009-04-07 14:20:57 -07:00
|
|
|
PluginDestructionGuard pdg(npp);
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
NPVariant npargs_buf[8];
|
|
|
|
NPVariant *npargs = npargs_buf;
|
|
|
|
|
|
|
|
if (argc > (sizeof(npargs_buf) / sizeof(NPVariant))) {
|
|
|
|
// Our stack buffer isn't large enough to hold all arguments,
|
|
|
|
// malloc a buffer.
|
|
|
|
npargs = (NPVariant *)PR_Malloc(argc * sizeof(NPVariant));
|
|
|
|
|
|
|
|
if (!npargs) {
|
|
|
|
ThrowJSException(cx, "Out of memory!");
|
|
|
|
|
2013-08-06 23:59:54 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Convert arguments
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t i;
|
2007-03-22 10:30:00 -07:00
|
|
|
for (i = 0; i < argc; ++i) {
|
|
|
|
if (!JSValToNPVariant(npp, cx, argv[i], npargs + i)) {
|
|
|
|
ThrowJSException(cx, "Error converting jsvals to NPVariants!");
|
|
|
|
|
|
|
|
if (npargs != npargs_buf) {
|
|
|
|
PR_Free(npargs);
|
|
|
|
}
|
|
|
|
|
2013-08-06 23:59:54 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NPVariant v;
|
|
|
|
VOID_TO_NPVARIANT(v);
|
|
|
|
|
2014-04-27 19:58:52 -07:00
|
|
|
JSObject *funobj = argv[-2].toObjectOrNull();
|
2013-08-08 15:53:04 -07:00
|
|
|
bool ok;
|
2007-10-09 18:24:28 -07:00
|
|
|
const char *msg = "Error calling method on NPObject!";
|
|
|
|
|
|
|
|
if (ctorCall) {
|
|
|
|
// construct a new NPObject based on the NPClass in npobj. Fail if
|
|
|
|
// no construct method is available.
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-10-09 18:24:28 -07:00
|
|
|
if (NP_CLASS_STRUCT_VERSION_HAS_CTOR(npobj->_class) &&
|
|
|
|
npobj->_class->construct) {
|
|
|
|
ok = npobj->_class->construct(npobj, npargs, argc, &v);
|
|
|
|
} else {
|
2013-08-06 23:59:54 -07:00
|
|
|
ok = false;
|
2007-10-09 18:24:28 -07:00
|
|
|
|
|
|
|
msg = "Attempt to construct object from class with no constructor.";
|
|
|
|
}
|
|
|
|
} else if (funobj != obj) {
|
2007-03-22 10:30:00 -07:00
|
|
|
// A obj.function() style call is made, get the method name from
|
|
|
|
// the function object.
|
|
|
|
|
2007-10-09 18:24:28 -07:00
|
|
|
if (npobj->_class->invoke) {
|
2011-10-12 09:48:46 -07:00
|
|
|
JSFunction *fun = ::JS_GetObjectFunction(funobj);
|
2013-09-16 18:33:40 -07:00
|
|
|
JS::Rooted<JSString*> funId(cx, ::JS_GetFunctionId(fun));
|
|
|
|
JSString *name = ::JS_InternJSString(cx, funId);
|
2011-05-17 12:15:12 -07:00
|
|
|
NPIdentifier id = StringToNPIdentifier(cx, name);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-07-14 23:19:36 -07:00
|
|
|
ok = npobj->_class->invoke(npobj, id, npargs, argc, &v);
|
2007-10-09 18:24:28 -07:00
|
|
|
} else {
|
2013-08-06 23:59:54 -07:00
|
|
|
ok = false;
|
2007-10-09 18:24:28 -07:00
|
|
|
|
|
|
|
msg = "Attempt to call a method on object with no invoke method.";
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
} else {
|
2007-10-09 18:24:28 -07:00
|
|
|
if (npobj->_class->invokeDefault) {
|
|
|
|
// obj is a callable object that is being called, no method name
|
|
|
|
// available then. Invoke the default method.
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-10-09 18:24:28 -07:00
|
|
|
ok = npobj->_class->invokeDefault(npobj, npargs, argc, &v);
|
|
|
|
} else {
|
2013-08-06 23:59:54 -07:00
|
|
|
ok = false;
|
2007-10-09 18:24:28 -07:00
|
|
|
|
|
|
|
msg = "Attempt to call a default method on object with no "
|
|
|
|
"invokeDefault method.";
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Release arguments.
|
|
|
|
for (i = 0; i < argc; ++i) {
|
|
|
|
_releasevariantvalue(npargs + i);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (npargs != npargs_buf) {
|
|
|
|
PR_Free(npargs);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ok) {
|
2013-08-06 23:59:54 -07:00
|
|
|
// ReportExceptionIfPending returns a return value, which is true
|
2009-06-25 17:29:58 -07:00
|
|
|
// if no exception was thrown. In that case, throw our own.
|
|
|
|
if (ReportExceptionIfPending(cx))
|
|
|
|
ThrowJSException(cx, msg);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-08-06 23:59:54 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
*rval = NPVariantToJSVal(npp, cx, &v);
|
|
|
|
|
|
|
|
// *rval now owns the value, release our reference.
|
|
|
|
_releasevariantvalue(&v);
|
|
|
|
|
|
|
|
return ReportExceptionIfPending(cx);
|
|
|
|
}
|
|
|
|
|
2013-08-02 00:41:57 -07:00
|
|
|
static bool
|
2013-04-02 16:05:37 -07:00
|
|
|
CallNPMethod(JSContext *cx, unsigned argc, JS::Value *vp)
|
2007-10-09 18:24:28 -07:00
|
|
|
{
|
2014-03-19 10:05:04 -07:00
|
|
|
JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
|
2013-05-14 16:25:32 -07:00
|
|
|
JS::Rooted<JSObject*> obj(cx, JS_THIS_OBJECT(cx, vp));
|
2010-08-16 12:35:04 -07:00
|
|
|
if (!obj)
|
2013-08-02 00:41:57 -07:00
|
|
|
return false;
|
2010-08-16 12:35:04 -07:00
|
|
|
|
2014-03-19 10:05:04 -07:00
|
|
|
return CallNPMethodInternal(cx, obj, args.length(), args.array(), vp, false);
|
2007-10-09 18:24:28 -07:00
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
struct NPObjectEnumerateState {
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t index;
|
|
|
|
uint32_t length;
|
2007-03-22 10:30:00 -07:00
|
|
|
NPIdentifier *value;
|
|
|
|
};
|
|
|
|
|
2013-08-08 15:53:04 -07:00
|
|
|
static bool
|
2013-06-21 06:12:46 -07:00
|
|
|
NPObjWrapper_newEnumerate(JSContext *cx, JS::Handle<JSObject*> obj, JSIterateOp enum_op,
|
2013-04-02 16:05:37 -07:00
|
|
|
JS::Value *statep, jsid *idp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-07-13 01:55:14 -07:00
|
|
|
NPObject *npobj = GetNPObject(cx, obj);
|
2007-03-22 10:30:00 -07:00
|
|
|
NPIdentifier *enum_value;
|
|
|
|
uint32_t length;
|
|
|
|
NPObjectEnumerateState *state;
|
|
|
|
|
|
|
|
if (!npobj || !npobj->_class) {
|
|
|
|
ThrowJSException(cx, "Bad NPObject as private data!");
|
2013-08-06 23:59:54 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2009-04-07 14:20:57 -07:00
|
|
|
PluginDestructionGuard pdg(LookupNPP(npobj));
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_ASSERTION(statep, "Must have a statep to enumerate!");
|
|
|
|
|
|
|
|
switch(enum_op) {
|
|
|
|
case JSENUMERATE_INIT:
|
2009-09-24 14:33:14 -07:00
|
|
|
case JSENUMERATE_INIT_ALL:
|
2007-03-22 10:30:00 -07:00
|
|
|
state = new NPObjectEnumerateState();
|
|
|
|
if (!state) {
|
|
|
|
ThrowJSException(cx, "Memory allocation failed for "
|
|
|
|
"NPObjectEnumerateState!");
|
|
|
|
|
2013-08-06 23:59:54 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!NP_CLASS_STRUCT_VERSION_HAS_ENUM(npobj->_class) ||
|
|
|
|
!npobj->_class->enumerate) {
|
|
|
|
enum_value = 0;
|
|
|
|
length = 0;
|
|
|
|
} else if (!npobj->_class->enumerate(npobj, &enum_value, &length)) {
|
|
|
|
delete state;
|
|
|
|
|
2009-06-25 17:29:58 -07:00
|
|
|
if (ReportExceptionIfPending(cx)) {
|
2013-08-06 23:59:54 -07:00
|
|
|
// ReportExceptionIfPending returns a return value, which is true
|
2009-06-25 17:29:58 -07:00
|
|
|
// if no exception was thrown. In that case, throw our own.
|
|
|
|
ThrowJSException(cx, "Error enumerating properties on scriptable "
|
|
|
|
"plugin object");
|
|
|
|
}
|
|
|
|
|
2013-08-06 23:59:54 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
state->value = enum_value;
|
|
|
|
state->length = length;
|
|
|
|
state->index = 0;
|
|
|
|
*statep = PRIVATE_TO_JSVAL(state);
|
|
|
|
if (idp) {
|
2010-07-14 23:19:36 -07:00
|
|
|
*idp = INT_TO_JSID(length);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case JSENUMERATE_NEXT:
|
2014-04-28 16:01:31 -07:00
|
|
|
state = (NPObjectEnumerateState *)statep->toPrivate();
|
2007-03-22 10:30:00 -07:00
|
|
|
enum_value = state->value;
|
|
|
|
length = state->length;
|
|
|
|
if (state->index != length) {
|
2010-07-14 23:19:36 -07:00
|
|
|
*idp = NPIdentifierToJSId(enum_value[state->index++]);
|
2013-08-06 23:59:54 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// FALL THROUGH
|
|
|
|
|
|
|
|
case JSENUMERATE_DESTROY:
|
2014-04-28 16:01:31 -07:00
|
|
|
state = (NPObjectEnumerateState *)statep->toPrivate();
|
2007-03-22 10:30:00 -07:00
|
|
|
if (state->value)
|
|
|
|
PR_Free(state->value);
|
|
|
|
delete state;
|
|
|
|
*statep = JSVAL_NULL;
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-08-06 23:59:54 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2013-08-08 15:53:04 -07:00
|
|
|
static bool
|
2014-04-25 14:11:02 -07:00
|
|
|
NPObjWrapper_NewResolve(JSContext *cx, JS::Handle<JSObject*> obj, JS::Handle<jsid> id,
|
2013-04-16 16:20:44 -07:00
|
|
|
JS::MutableHandle<JSObject*> objp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-07-13 01:55:14 -07:00
|
|
|
NPObject *npobj = GetNPObject(cx, obj);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (!npobj || !npobj->_class || !npobj->_class->hasProperty ||
|
|
|
|
!npobj->_class->hasMethod) {
|
|
|
|
ThrowJSException(cx, "Bad NPObject as private data!");
|
|
|
|
|
2013-08-06 23:59:54 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2009-04-07 14:20:57 -07:00
|
|
|
PluginDestructionGuard pdg(LookupNPP(npobj));
|
|
|
|
|
2010-07-14 23:19:36 -07:00
|
|
|
NPIdentifier identifier = JSIdToNPIdentifier(id);
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool hasProperty = npobj->_class->hasProperty(npobj, identifier);
|
2009-06-25 17:29:58 -07:00
|
|
|
if (!ReportExceptionIfPending(cx))
|
2013-08-06 23:59:54 -07:00
|
|
|
return false;
|
2009-06-25 17:29:58 -07:00
|
|
|
|
|
|
|
if (hasProperty) {
|
2010-12-03 00:24:17 -08:00
|
|
|
NS_ASSERTION(JSID_IS_STRING(id) || JSID_IS_INT(id),
|
|
|
|
"id must be either string or int!\n");
|
2014-04-30 02:10:33 -07:00
|
|
|
if (!::JS_DefinePropertyById(cx, obj, id, JS::UndefinedHandleValue,
|
|
|
|
JSPROP_ENUMERATE | JSPROP_SHARED)) {
|
2013-08-06 23:59:54 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2012-07-04 11:12:16 -07:00
|
|
|
objp.set(obj);
|
2009-06-25 17:29:58 -07:00
|
|
|
|
2013-08-06 23:59:54 -07:00
|
|
|
return true;
|
2009-06-25 17:29:58 -07:00
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool hasMethod = npobj->_class->hasMethod(npobj, identifier);
|
2009-06-25 17:29:58 -07:00
|
|
|
if (!ReportExceptionIfPending(cx))
|
2013-08-06 23:59:54 -07:00
|
|
|
return false;
|
2009-06-25 17:29:58 -07:00
|
|
|
|
|
|
|
if (hasMethod) {
|
2010-12-03 00:24:17 -08:00
|
|
|
NS_ASSERTION(JSID_IS_STRING(id) || JSID_IS_INT(id),
|
|
|
|
"id must be either string or int!\n");
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-12-03 00:24:17 -08:00
|
|
|
JSFunction *fnc = ::JS_DefineFunctionById(cx, obj, id, CallNPMethod, 0,
|
|
|
|
JSPROP_ENUMERATE);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-07-04 11:12:16 -07:00
|
|
|
objp.set(obj);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
return fnc != nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2009-06-25 17:29:58 -07:00
|
|
|
// no property or method
|
2013-08-06 23:59:54 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2013-08-08 15:53:04 -07:00
|
|
|
static bool
|
2013-06-21 06:12:46 -07:00
|
|
|
NPObjWrapper_Convert(JSContext *cx, JS::Handle<JSObject*> obj, JSType hint, JS::MutableHandle<JS::Value> vp)
|
2007-10-09 18:24:28 -07:00
|
|
|
{
|
2014-02-17 22:09:06 -08:00
|
|
|
MOZ_ASSERT(hint == JSTYPE_NUMBER || hint == JSTYPE_STRING || hint == JSTYPE_VOID);
|
2011-10-14 16:27:50 -07:00
|
|
|
|
|
|
|
// Plugins do not simply use JS_ConvertStub, and the default [[DefaultValue]]
|
|
|
|
// behavior, because that behavior involves calling toString or valueOf on
|
|
|
|
// objects which weren't designed to accommodate this. Usually this wouldn't
|
|
|
|
// be a problem, because the absence of either property, or the presence of
|
|
|
|
// either property with a value that isn't callable, will cause that property
|
|
|
|
// to simply be ignored. But there is a problem in one specific case: Java,
|
|
|
|
// specifically java.lang.Integer. The Integer class has static valueOf
|
|
|
|
// methods, none of which are nullary, so the JS-reflected method will behave
|
|
|
|
// poorly when called with no arguments. We work around this problem by
|
|
|
|
// giving plugins a [[DefaultValue]] which uses only toString and not valueOf.
|
|
|
|
|
2013-05-04 00:52:57 -07:00
|
|
|
JS::Rooted<JS::Value> v(cx, JSVAL_VOID);
|
2013-07-26 02:00:38 -07:00
|
|
|
if (!JS_GetProperty(cx, obj, "toString", &v))
|
2011-10-14 16:27:50 -07:00
|
|
|
return false;
|
2014-04-27 20:27:54 -07:00
|
|
|
if (!v.isPrimitive() && JS_ObjectIsCallable(cx, v.toObjectOrNull())) {
|
2014-02-12 23:38:36 -08:00
|
|
|
if (!JS_CallFunctionValue(cx, obj, v, JS::HandleValueArray::empty(), vp))
|
2011-10-14 16:27:50 -07:00
|
|
|
return false;
|
2014-04-27 20:27:54 -07:00
|
|
|
if (vp.isPrimitive())
|
2011-10-14 16:27:50 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-10-23 13:34:30 -07:00
|
|
|
JS_ReportErrorNumber(cx, js_GetErrorMessage, nullptr, JSMSG_CANT_CONVERT_TO,
|
2012-02-03 16:54:57 -08:00
|
|
|
JS_GetClass(obj)->name,
|
2011-10-14 16:27:50 -07:00
|
|
|
hint == JSTYPE_VOID
|
|
|
|
? "primitive type"
|
|
|
|
: hint == JSTYPE_NUMBER
|
|
|
|
? "number"
|
|
|
|
: "string");
|
|
|
|
return false;
|
2007-10-09 18:24:28 -07:00
|
|
|
}
|
|
|
|
|
2008-09-06 15:21:43 -07:00
|
|
|
static void
|
2012-03-19 07:34:55 -07:00
|
|
|
NPObjWrapper_Finalize(JSFreeOp *fop, JSObject *obj)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-02-05 12:07:23 -08:00
|
|
|
NPObject *npobj = (NPObject *)::JS_GetPrivate(obj);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (npobj) {
|
|
|
|
if (sNPObjWrappers.ops) {
|
|
|
|
PL_DHashTableOperate(&sNPObjWrappers, npobj, PL_DHASH_REMOVE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-11-17 11:51:46 -08:00
|
|
|
if (!sDelayedReleases)
|
|
|
|
sDelayedReleases = new nsTArray<NPObject*>;
|
|
|
|
sDelayedReleases->AppendElement(npobj);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2013-08-02 00:41:57 -07:00
|
|
|
static bool
|
2013-04-02 16:05:37 -07:00
|
|
|
NPObjWrapper_Call(JSContext *cx, unsigned argc, JS::Value *vp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-03-19 10:05:04 -07:00
|
|
|
JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
|
|
|
|
JS::Rooted<JSObject*> obj(cx, &args.callee());
|
|
|
|
return CallNPMethodInternal(cx, obj, args.length(), args.array(), vp, false);
|
2007-10-09 18:24:28 -07:00
|
|
|
}
|
|
|
|
|
2013-08-02 00:41:57 -07:00
|
|
|
static bool
|
2013-04-02 16:05:37 -07:00
|
|
|
NPObjWrapper_Construct(JSContext *cx, unsigned argc, JS::Value *vp)
|
2007-10-09 18:24:28 -07:00
|
|
|
{
|
2014-03-19 10:05:04 -07:00
|
|
|
JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
|
|
|
|
JS::Rooted<JSObject*> obj(cx, &args.callee());
|
|
|
|
return CallNPMethodInternal(cx, obj, args.length(), args.array(), vp, true);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
class NPObjWrapperHashEntry : public PLDHashEntryHdr
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
NPObject *mNPObj; // Must be the first member for the PLDHash stubs to work
|
|
|
|
JSObject *mJSObj;
|
|
|
|
NPP mNpp;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2007-08-09 15:22:26 -07:00
|
|
|
// An NPObject is going away, make sure we null out the JS object's
|
|
|
|
// private data in case this is an NPObject that came from a plugin
|
|
|
|
// and it's destroyed prematurely.
|
|
|
|
|
|
|
|
// static
|
|
|
|
void
|
|
|
|
nsNPObjWrapper::OnDestroy(NPObject *npobj)
|
|
|
|
{
|
|
|
|
if (!npobj) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (npobj->_class == &nsJSObjWrapper::sJSObjWrapperNPClass) {
|
|
|
|
// npobj is one of our own, no private data to clean up here.
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!sNPObjWrappers.ops) {
|
|
|
|
// No hash yet (or any more), no used wrappers available.
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-08-27 16:55:47 -07:00
|
|
|
NPObjWrapperHashEntry *entry = static_cast<NPObjWrapperHashEntry *>
|
|
|
|
(PL_DHashTableOperate(&sNPObjWrappers, npobj, PL_DHASH_LOOKUP));
|
2007-08-09 15:22:26 -07:00
|
|
|
|
|
|
|
if (PL_DHASH_ENTRY_IS_BUSY(entry) && entry->mJSObj) {
|
|
|
|
// Found a live NPObject wrapper, null out its JSObjects' private
|
|
|
|
// data.
|
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
::JS_SetPrivate(entry->mJSObj, nullptr);
|
2007-08-09 15:22:26 -07:00
|
|
|
|
|
|
|
// Remove the npobj from the hash now that it went away.
|
|
|
|
PL_DHashTableRawRemove(&sNPObjWrappers, entry);
|
|
|
|
|
2012-07-13 01:56:38 -07:00
|
|
|
// The finalize hook will call OnWrapperDestroyed().
|
2007-08-09 15:22:26 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// Look up or create a JSObject that wraps the NPObject npobj.
|
|
|
|
|
|
|
|
// static
|
|
|
|
JSObject *
|
|
|
|
nsNPObjWrapper::GetNewOrUsed(NPP npp, JSContext *cx, NPObject *npobj)
|
|
|
|
{
|
|
|
|
if (!npobj) {
|
|
|
|
NS_ERROR("Null NPObject passed to nsNPObjWrapper::GetNewOrUsed()!");
|
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (npobj->_class == &nsJSObjWrapper::sJSObjWrapperNPClass) {
|
|
|
|
// npobj is one of our own, return its existing JSObject.
|
|
|
|
|
2013-05-04 00:52:57 -07:00
|
|
|
JS::Rooted<JSObject*> obj(cx, ((nsJSObjWrapper *)npobj)->mJSObj);
|
2013-10-15 17:02:23 -07:00
|
|
|
if (!JS_WrapObject(cx, &obj)) {
|
2013-10-23 13:34:30 -07:00
|
|
|
return nullptr;
|
2012-08-03 12:41:49 -07:00
|
|
|
}
|
|
|
|
return obj;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!npp) {
|
|
|
|
NS_ERROR("No npp passed to nsNPObjWrapper::GetNewOrUsed()!");
|
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!sNPObjWrappers.ops) {
|
|
|
|
// No hash yet (or any more), initialize it.
|
2014-02-27 10:04:09 -08:00
|
|
|
PL_DHashTableInit(&sNPObjWrappers, PL_DHashGetStubOps(), nullptr,
|
|
|
|
sizeof(NPObjWrapperHashEntry), 16);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2007-08-27 16:55:47 -07:00
|
|
|
NPObjWrapperHashEntry *entry = static_cast<NPObjWrapperHashEntry *>
|
|
|
|
(PL_DHashTableOperate(&sNPObjWrappers, npobj, PL_DHASH_ADD));
|
|
|
|
|
2007-04-17 16:10:20 -07:00
|
|
|
if (!entry) {
|
|
|
|
// Out of memory
|
|
|
|
JS_ReportOutOfMemory(cx);
|
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2007-04-17 16:10:20 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (PL_DHASH_ENTRY_IS_BUSY(entry) && entry->mJSObj) {
|
2012-07-13 01:55:14 -07:00
|
|
|
// Found a live NPObject wrapper. It may not be in the same compartment
|
|
|
|
// as cx, so we need to wrap it before returning it.
|
2013-05-04 00:52:57 -07:00
|
|
|
JS::Rooted<JSObject*> obj(cx, entry->mJSObj);
|
2013-10-15 17:02:23 -07:00
|
|
|
if (!JS_WrapObject(cx, &obj)) {
|
2013-10-23 13:34:30 -07:00
|
|
|
return nullptr;
|
2012-07-13 01:55:14 -07:00
|
|
|
}
|
|
|
|
return obj;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
entry->mNPObj = npobj;
|
|
|
|
entry->mNpp = npp;
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t generation = sNPObjWrappers.generation;
|
2008-07-23 09:49:29 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// No existing JSObject, create one.
|
|
|
|
|
2014-01-16 09:48:58 -08:00
|
|
|
JS::Rooted<JSObject*> obj(cx, ::JS_NewObject(cx, &sNPObjectJSWrapperClass, JS::NullPtr(),
|
|
|
|
JS::NullPtr()));
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2008-07-23 09:49:29 -07:00
|
|
|
if (generation != sNPObjWrappers.generation) {
|
|
|
|
// Reload entry if the JS_NewObject call caused a GC and reallocated
|
|
|
|
// the table (see bug 445229). This is guaranteed to succeed.
|
|
|
|
|
|
|
|
entry = static_cast<NPObjWrapperHashEntry *>
|
|
|
|
(PL_DHashTableOperate(&sNPObjWrappers, npobj, PL_DHASH_LOOKUP));
|
|
|
|
NS_ASSERTION(entry && PL_DHASH_ENTRY_IS_BUSY(entry),
|
|
|
|
"Hashtable didn't find what we just added?");
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!obj) {
|
|
|
|
// OOM? Remove the stale entry from the hash.
|
|
|
|
|
2007-08-27 16:55:47 -07:00
|
|
|
PL_DHashTableRawRemove(&sNPObjWrappers, entry);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
OnWrapperCreated();
|
|
|
|
|
|
|
|
entry->mJSObj = obj;
|
|
|
|
|
2012-02-05 12:07:23 -08:00
|
|
|
::JS_SetPrivate(obj, npobj);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// The new JSObject now holds on to npobj
|
|
|
|
_retainobject(npobj);
|
|
|
|
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-08-06 10:19:55 -07:00
|
|
|
// Struct for passing an NPP and a JSContext to
|
|
|
|
// NPObjWrapperPluginDestroyedCallback
|
|
|
|
struct NppAndCx
|
|
|
|
{
|
|
|
|
NPP npp;
|
|
|
|
JSContext *cx;
|
|
|
|
};
|
|
|
|
|
2008-10-10 08:04:34 -07:00
|
|
|
static PLDHashOperator
|
2007-03-22 10:30:00 -07:00
|
|
|
NPObjWrapperPluginDestroyedCallback(PLDHashTable *table, PLDHashEntryHdr *hdr,
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t number, void *arg)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
NPObjWrapperHashEntry *entry = (NPObjWrapperHashEntry *)hdr;
|
2007-08-06 10:19:55 -07:00
|
|
|
NppAndCx *nppcx = reinterpret_cast<NppAndCx *>(arg);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-08-06 10:19:55 -07:00
|
|
|
if (entry->mNpp == nppcx->npp) {
|
2008-08-18 10:06:16 -07:00
|
|
|
// Prevent invalidate() and deallocate() from touching the hash
|
|
|
|
// we're enumerating.
|
|
|
|
const PLDHashTableOps *ops = table->ops;
|
2012-07-30 07:20:58 -07:00
|
|
|
table->ops = nullptr;
|
2008-08-18 10:06:16 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
NPObject *npobj = entry->mNPObj;
|
|
|
|
|
|
|
|
if (npobj->_class && npobj->_class->invalidate) {
|
|
|
|
npobj->_class->invalidate(npobj);
|
|
|
|
}
|
|
|
|
|
2009-11-12 14:16:54 -08:00
|
|
|
#ifdef NS_BUILD_REFCNT_LOGGING
|
|
|
|
{
|
|
|
|
int32_t refCnt = npobj->referenceCount;
|
|
|
|
while (refCnt) {
|
|
|
|
--refCnt;
|
|
|
|
NS_LOG_RELEASE(npobj, refCnt, "BrowserNPObject");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// Force deallocation of plugin objects since the plugin they came
|
|
|
|
// from is being torn down.
|
|
|
|
if (npobj->_class && npobj->_class->deallocate) {
|
|
|
|
npobj->_class->deallocate(npobj);
|
|
|
|
} else {
|
|
|
|
PR_Free(npobj);
|
|
|
|
}
|
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
::JS_SetPrivate(entry->mJSObj, nullptr);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-11-19 14:53:36 -08:00
|
|
|
table->ops = ops;
|
2008-08-18 10:06:16 -07:00
|
|
|
|
2012-10-01 10:53:35 -07:00
|
|
|
if (sDelayedReleases && sDelayedReleases->RemoveElement(npobj)) {
|
|
|
|
OnWrapperDestroyed();
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
return PL_DHASH_REMOVE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return PL_DHASH_NEXT;
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
void
|
|
|
|
nsJSNPRuntime::OnPluginDestroy(NPP npp)
|
|
|
|
{
|
2014-05-27 02:35:13 -07:00
|
|
|
if (sJSObjWrappersAccessible) {
|
|
|
|
|
|
|
|
// Prevent modification of sJSObjWrappers table if we go reentrant.
|
|
|
|
sJSObjWrappersAccessible = false;
|
|
|
|
|
2013-11-19 14:53:36 -08:00
|
|
|
for (JSObjWrapperTable::Enum e(sJSObjWrappers); !e.empty(); e.popFront()) {
|
2013-11-25 17:35:09 -08:00
|
|
|
nsJSObjWrapper *npobj = e.front().value();
|
2013-11-19 14:53:36 -08:00
|
|
|
MOZ_ASSERT(npobj->_class == &nsJSObjWrapper::sJSObjWrapperNPClass);
|
|
|
|
if (npobj->mNpp == npp) {
|
2014-05-27 02:35:13 -07:00
|
|
|
if (npobj->_class && npobj->_class->invalidate) {
|
|
|
|
npobj->_class->invalidate(npobj);
|
|
|
|
}
|
|
|
|
|
2013-11-19 14:53:36 -08:00
|
|
|
_releaseobject(npobj);
|
2014-05-27 02:35:13 -07:00
|
|
|
|
2013-11-19 14:53:36 -08:00
|
|
|
e.removeFront();
|
|
|
|
}
|
|
|
|
}
|
2014-05-27 02:35:13 -07:00
|
|
|
|
|
|
|
sJSObjWrappersAccessible = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2007-08-06 10:19:55 -07:00
|
|
|
// Use the safe JSContext here as we're not always able to find the
|
|
|
|
// JSContext associated with the NPP any more.
|
2013-05-06 06:04:17 -07:00
|
|
|
AutoSafeJSContext cx;
|
2007-03-22 10:30:00 -07:00
|
|
|
if (sNPObjWrappers.ops) {
|
2007-08-06 10:19:55 -07:00
|
|
|
NppAndCx nppcx = { npp, cx };
|
2007-03-22 10:30:00 -07:00
|
|
|
PL_DHashTableEnumerate(&sNPObjWrappers,
|
2007-08-06 10:19:55 -07:00
|
|
|
NPObjWrapperPluginDestroyedCallback, &nppcx);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Find the NPP for a NPObject.
|
|
|
|
static NPP
|
|
|
|
LookupNPP(NPObject *npobj)
|
|
|
|
{
|
|
|
|
if (npobj->_class == &nsJSObjWrapper::sJSObjWrapperNPClass) {
|
2009-12-02 09:02:41 -08:00
|
|
|
nsJSObjWrapper* o = static_cast<nsJSObjWrapper*>(npobj);
|
|
|
|
return o->mNpp;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2007-08-27 16:55:47 -07:00
|
|
|
NPObjWrapperHashEntry *entry = static_cast<NPObjWrapperHashEntry *>
|
|
|
|
(PL_DHashTableOperate(&sNPObjWrappers, npobj, PL_DHASH_ADD));
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (PL_DHASH_ENTRY_IS_FREE(entry)) {
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_ASSERTION(entry->mNpp, "Live NPObject entry w/o an NPP!");
|
|
|
|
|
|
|
|
return entry->mNpp;
|
|
|
|
}
|
|
|
|
|
2014-04-16 01:47:53 -07:00
|
|
|
static bool
|
2010-04-07 17:16:19 -07:00
|
|
|
CreateNPObjectMember(NPP npp, JSContext *cx, JSObject *obj, NPObject* npobj,
|
2014-04-16 01:47:53 -07:00
|
|
|
JS::Handle<jsid> id, NPVariant* getPropertyResult,
|
|
|
|
JS::MutableHandle<JS::Value> vp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
if (!npobj || !npobj->_class || !npobj->_class->getProperty ||
|
|
|
|
!npobj->_class->invoke) {
|
|
|
|
ThrowJSException(cx, "Bad NPObject");
|
|
|
|
|
2013-08-06 23:59:54 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NPObjectMemberPrivate *memberPrivate =
|
|
|
|
(NPObjectMemberPrivate *)PR_Malloc(sizeof(NPObjectMemberPrivate));
|
|
|
|
if (!memberPrivate)
|
2013-08-06 23:59:54 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// Make sure to clear all members in case something fails here
|
|
|
|
// during initialization.
|
|
|
|
memset(memberPrivate, 0, sizeof(NPObjectMemberPrivate));
|
|
|
|
|
2014-01-16 09:48:58 -08:00
|
|
|
JSObject *memobj = ::JS_NewObject(cx, &sNPObjectMemberClass, JS::NullPtr(), JS::NullPtr());
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!memobj) {
|
|
|
|
PR_Free(memberPrivate);
|
2013-08-06 23:59:54 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2007-04-17 16:10:20 -07:00
|
|
|
|
2014-04-16 01:47:53 -07:00
|
|
|
vp.setObject(*memobj);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-02-05 12:07:23 -08:00
|
|
|
::JS_SetPrivate(memobj, (void *)memberPrivate);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-07-14 23:19:36 -07:00
|
|
|
NPIdentifier identifier = JSIdToNPIdentifier(id);
|
|
|
|
|
2013-05-04 00:52:57 -07:00
|
|
|
JS::Rooted<JS::Value> fieldValue(cx);
|
2007-03-22 10:30:00 -07:00
|
|
|
NPVariant npv;
|
2009-06-25 17:29:58 -07:00
|
|
|
|
2010-04-07 17:16:19 -07:00
|
|
|
if (getPropertyResult) {
|
|
|
|
// Plugin has already handed us the value we want here.
|
|
|
|
npv = *getPropertyResult;
|
2009-06-25 17:29:58 -07:00
|
|
|
}
|
2010-04-07 17:16:19 -07:00
|
|
|
else {
|
|
|
|
VOID_TO_NPVARIANT(npv);
|
2010-04-07 13:55:10 -07:00
|
|
|
|
2010-07-14 23:19:36 -07:00
|
|
|
NPBool hasProperty = npobj->_class->getProperty(npobj, identifier,
|
2010-04-07 17:16:19 -07:00
|
|
|
&npv);
|
2014-04-16 01:47:53 -07:00
|
|
|
if (!ReportExceptionIfPending(cx) || !hasProperty) {
|
2013-08-06 23:59:54 -07:00
|
|
|
return false;
|
2010-04-07 17:16:19 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
fieldValue = NPVariantToJSVal(npp, cx, &npv);
|
|
|
|
|
|
|
|
// npobjWrapper is the JSObject through which we make sure we don't
|
|
|
|
// outlive the underlying NPObject, so make sure it points to the
|
|
|
|
// real JSObject wrapper for the NPObject.
|
2012-07-13 01:55:14 -07:00
|
|
|
obj = GetNPObjectWrapper(cx, obj);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
memberPrivate->npobjWrapper = obj;
|
|
|
|
|
|
|
|
memberPrivate->fieldValue = fieldValue;
|
2013-07-23 02:58:27 -07:00
|
|
|
memberPrivate->methodName = id;
|
2007-03-22 10:30:00 -07:00
|
|
|
memberPrivate->npp = npp;
|
2007-04-17 16:10:20 -07:00
|
|
|
|
2013-08-06 23:59:54 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2013-08-08 15:53:04 -07:00
|
|
|
static bool
|
2013-06-21 06:12:46 -07:00
|
|
|
NPObjectMember_Convert(JSContext *cx, JS::Handle<JSObject*> obj, JSType type, JS::MutableHandle<JS::Value> vp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
NPObjectMemberPrivate *memberPrivate =
|
|
|
|
(NPObjectMemberPrivate *)::JS_GetInstancePrivate(cx, obj,
|
|
|
|
&sNPObjectMemberClass,
|
2012-07-30 07:20:58 -07:00
|
|
|
nullptr);
|
2009-08-12 02:49:53 -07:00
|
|
|
if (!memberPrivate) {
|
|
|
|
NS_ERROR("no Ambiguous Member Private data!");
|
2013-08-06 23:59:54 -07:00
|
|
|
return false;
|
2009-08-12 02:49:53 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case JSTYPE_VOID:
|
|
|
|
case JSTYPE_STRING:
|
|
|
|
case JSTYPE_NUMBER:
|
2012-07-30 04:19:09 -07:00
|
|
|
vp.set(memberPrivate->fieldValue);
|
2014-01-14 12:41:22 -08:00
|
|
|
if (vp.isObject()) {
|
|
|
|
JS::Rooted<JSObject*> objVal(cx, &vp.toObject());
|
|
|
|
return JS_DefaultValue(cx, objVal, type, vp);
|
2011-10-14 16:27:50 -07:00
|
|
|
}
|
2013-08-06 23:59:54 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
case JSTYPE_BOOLEAN:
|
|
|
|
case JSTYPE_OBJECT:
|
2012-07-30 04:19:09 -07:00
|
|
|
vp.set(memberPrivate->fieldValue);
|
2013-08-06 23:59:54 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
case JSTYPE_FUNCTION:
|
|
|
|
// Leave this to NPObjectMember_Call.
|
2013-08-06 23:59:54 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
default:
|
|
|
|
NS_ERROR("illegal operation on JSObject prototype object");
|
2013-08-06 23:59:54 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-09-06 15:21:43 -07:00
|
|
|
static void
|
2012-03-19 07:34:55 -07:00
|
|
|
NPObjectMember_Finalize(JSFreeOp *fop, JSObject *obj)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
NPObjectMemberPrivate *memberPrivate;
|
|
|
|
|
2012-02-05 12:07:23 -08:00
|
|
|
memberPrivate = (NPObjectMemberPrivate *)::JS_GetPrivate(obj);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!memberPrivate)
|
|
|
|
return;
|
|
|
|
|
|
|
|
PR_Free(memberPrivate);
|
|
|
|
}
|
|
|
|
|
2013-08-02 00:41:57 -07:00
|
|
|
static bool
|
2013-04-02 16:05:37 -07:00
|
|
|
NPObjectMember_Call(JSContext *cx, unsigned argc, JS::Value *vp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-03-19 10:05:04 -07:00
|
|
|
JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
|
|
|
|
JS::Rooted<JSObject*> memobj(cx, &args.callee());
|
2013-08-02 00:41:57 -07:00
|
|
|
NS_ENSURE_TRUE(memobj, false);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
NPObjectMemberPrivate *memberPrivate =
|
|
|
|
(NPObjectMemberPrivate *)::JS_GetInstancePrivate(cx, memobj,
|
|
|
|
&sNPObjectMemberClass,
|
2014-03-20 02:32:37 -07:00
|
|
|
&args);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!memberPrivate || !memberPrivate->npobjWrapper)
|
2013-08-02 00:41:57 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-07-13 01:55:14 -07:00
|
|
|
NPObject *npobj = GetNPObject(cx, memberPrivate->npobjWrapper);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!npobj) {
|
|
|
|
ThrowJSException(cx, "Call on invalid member object");
|
|
|
|
|
2013-08-02 00:41:57 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NPVariant npargs_buf[8];
|
|
|
|
NPVariant *npargs = npargs_buf;
|
|
|
|
|
2014-03-19 10:05:04 -07:00
|
|
|
if (args.length() > (sizeof(npargs_buf) / sizeof(NPVariant))) {
|
2007-03-22 10:30:00 -07:00
|
|
|
// Our stack buffer isn't large enough to hold all arguments,
|
|
|
|
// malloc a buffer.
|
2014-03-19 10:05:04 -07:00
|
|
|
npargs = (NPVariant *)PR_Malloc(args.length() * sizeof(NPVariant));
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (!npargs) {
|
|
|
|
ThrowJSException(cx, "Out of memory!");
|
|
|
|
|
2013-08-02 00:41:57 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Convert arguments
|
2014-03-19 10:05:04 -07:00
|
|
|
for (uint32_t i = 0; i < args.length(); ++i) {
|
|
|
|
if (!JSValToNPVariant(memberPrivate->npp, cx, args[i], npargs + i)) {
|
2007-03-22 10:30:00 -07:00
|
|
|
ThrowJSException(cx, "Error converting jsvals to NPVariants!");
|
|
|
|
|
|
|
|
if (npargs != npargs_buf) {
|
|
|
|
PR_Free(npargs);
|
|
|
|
}
|
|
|
|
|
2013-08-02 00:41:57 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
2007-04-17 16:10:20 -07:00
|
|
|
|
2010-07-14 23:19:36 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
NPVariant npv;
|
2014-03-19 10:05:04 -07:00
|
|
|
bool ok = npobj->_class->invoke(npobj,
|
|
|
|
JSIdToNPIdentifier(memberPrivate->methodName),
|
|
|
|
npargs, args.length(), &npv);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// Release arguments.
|
2014-03-19 10:05:04 -07:00
|
|
|
for (uint32_t i = 0; i < args.length(); ++i) {
|
2007-03-22 10:30:00 -07:00
|
|
|
_releasevariantvalue(npargs + i);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (npargs != npargs_buf) {
|
|
|
|
PR_Free(npargs);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ok) {
|
2013-08-06 23:59:54 -07:00
|
|
|
// ReportExceptionIfPending returns a return value, which is true
|
2009-06-25 17:29:58 -07:00
|
|
|
// if no exception was thrown. In that case, throw our own.
|
|
|
|
if (ReportExceptionIfPending(cx))
|
|
|
|
ThrowJSException(cx, "Error calling method on NPObject!");
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-08-02 00:41:57 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2014-03-19 10:05:04 -07:00
|
|
|
args.rval().set(NPVariantToJSVal(memberPrivate->npp, cx, &npv));
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-08-16 12:35:04 -07:00
|
|
|
// *vp now owns the value, release our reference.
|
2007-03-22 10:30:00 -07:00
|
|
|
_releasevariantvalue(&npv);
|
|
|
|
|
|
|
|
return ReportExceptionIfPending(cx);
|
|
|
|
}
|
|
|
|
|
2011-03-05 14:50:11 -08:00
|
|
|
static void
|
|
|
|
NPObjectMember_Trace(JSTracer *trc, JSObject *obj)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
NPObjectMemberPrivate *memberPrivate =
|
2012-02-05 12:07:23 -08:00
|
|
|
(NPObjectMemberPrivate *)::JS_GetPrivate(obj);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!memberPrivate)
|
2011-03-05 14:50:11 -08:00
|
|
|
return;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-05-06 08:24:05 -07:00
|
|
|
// Our NPIdentifier is not always interned, so we must root it explicitly.
|
2013-07-23 02:58:27 -07:00
|
|
|
JS_CallHeapIdTracer(trc, &memberPrivate->methodName, "NPObjectMemberPrivate.methodName");
|
2011-05-06 08:24:05 -07:00
|
|
|
|
2014-04-27 20:27:54 -07:00
|
|
|
if (!memberPrivate->fieldValue.isPrimitive()) {
|
2013-07-23 02:58:27 -07:00
|
|
|
JS_CallHeapValueTracer(trc, &memberPrivate->fieldValue,
|
|
|
|
"NPObject Member => fieldValue");
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// There's no strong reference from our private data to the
|
|
|
|
// NPObject, so make sure to mark the NPObject wrapper to keep the
|
|
|
|
// NPObject alive as long as this NPObjectMember is alive.
|
|
|
|
if (memberPrivate->npobjWrapper) {
|
2013-07-23 02:58:27 -07:00
|
|
|
JS_CallHeapObjectTracer(trc, &memberPrivate->npobjWrapper,
|
|
|
|
"NPObject Member => npobjWrapper");
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|