Backed out changeset 0a641abef602 (bug 934421)

This commit is contained in:
Ed Morley 2013-11-20 16:38:10 +00:00
parent 19c89623dc
commit 8aec0cea8e
2 changed files with 112 additions and 96 deletions

View File

@ -23,10 +23,8 @@
#include "prmem.h"
#include "nsIContent.h"
#include "nsPluginInstanceOwner.h"
#include "nsWrapperCacheInlines.h"
#include "js/HashTable.h"
#include "mozilla/HashFunctions.h"
#include "nsWrapperCacheInlines.h"
#define NPRUNTIME_JSCLASS_NAME "NPObject JS wrapper class"
@ -37,21 +35,6 @@ using namespace mozilla;
using mozilla::plugins::PluginScriptableObjectParent;
using mozilla::plugins::ParentNPObject;
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;
}
};
// 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
@ -59,11 +42,7 @@ struct JSObjWrapperHasher : public js::DefaultHasher<nsJSObjWrapperKey>
// 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).
typedef js::HashMap<nsJSObjWrapperKey,
nsJSObjWrapper*,
JSObjWrapperHasher,
js::SystemAllocPolicy> JSObjWrapperTable;
static JSObjWrapperTable sJSObjWrappers;
static PLDHashTable sJSObjWrappers;
// Hash of NPObject wrappers that wrap NPObjects as JSObjects.
static PLDHashTable sNPObjWrappers;
@ -258,16 +237,18 @@ OnWrapperDestroyed()
NS_ASSERTION(sWrapperCount, "Whaaa, unbalanced created/destroyed calls!");
if (--sWrapperCount == 0) {
if (sJSObjWrappers.initialized()) {
MOZ_ASSERT(sJSObjWrappers.count() == 0);
if (sJSObjWrappers.ops) {
NS_ASSERTION(sJSObjWrappers.entryCount == 0, "Uh, hash not empty?");
// No more wrappers, and our hash was initialized. Finish the
// hash to prevent leaking it.
sJSObjWrappers.finish();
PL_DHashTableFinish(&sJSObjWrappers);
sJSObjWrappers.ops = nullptr;
}
if (sNPObjWrappers.ops) {
MOZ_ASSERT(sNPObjWrappers.entryCount == 0);
NS_ASSERTION(sNPObjWrappers.entryCount == 0, "Uh, hash not empty?");
// No more wrappers, and our hash was initialized. Finish the
// hash to prevent leaking it.
@ -499,8 +480,9 @@ ReportExceptionIfPending(JSContext *cx)
return false;
}
nsJSObjWrapper::nsJSObjWrapper(NPP npp)
: mNpp(npp)
: nsJSObjWrapperKey(nullptr, npp)
{
MOZ_COUNT_CTOR(nsJSObjWrapper);
OnWrapperCreated();
@ -516,15 +498,6 @@ nsJSObjWrapper::~nsJSObjWrapper()
OnWrapperDestroyed();
}
void
nsJSObjWrapper::ClearJSObject() {
// Unroot the object's JSObject
JS_RemoveObjectRootRT(sJSRuntime, &mJSObj);
// Forget our reference to the JSObject.
mJSObj = nullptr;
}
// static
NPObject *
nsJSObjWrapper::NP_Allocate(NPP npp, NPClass *aClass)
@ -550,13 +523,18 @@ nsJSObjWrapper::NP_Invalidate(NPObject *npobj)
nsJSObjWrapper *jsnpobj = (nsJSObjWrapper *)npobj;
if (jsnpobj && jsnpobj->mJSObj) {
// Unroot the object's JSObject
JS_RemoveObjectRootRT(sJSRuntime, &jsnpobj->mJSObj);
// Remove the wrapper from the hash
MOZ_ASSERT(sJSObjWrappers.initialized());
nsJSObjWrapperKey key(jsnpobj->mJSObj, jsnpobj->mNpp);
sJSObjWrappers.remove(key);
if (sJSObjWrappers.ops) {
// Remove the wrapper from the hash
jsnpobj->ClearJSObject();
nsJSObjWrapperKey key(jsnpobj->mJSObj, jsnpobj->mNpp);
PL_DHashTableOperate(&sJSObjWrappers, &key, PL_DHASH_REMOVE);
}
// Forget our reference to the JSObject.
jsnpobj->mJSObj = nullptr;
}
}
@ -937,23 +915,34 @@ nsJSObjWrapper::NP_Construct(NPObject *npobj, const NPVariant *args,
}
class JSObjWrapperHashEntry : public PLDHashEntryHdr
{
public:
nsJSObjWrapper *mJSObjWrapper;
};
/*
* This function is called during minor GCs for each key in the sJSObjWrappers
* table that has been moved.
*/
static void
JSObjWrapperKeyMarkCallback(JSTracer *trc, void *key, void *data) {
JSObject *obj = static_cast<JSObject*>(key);
nsJSObjWrapper* wrapper = static_cast<nsJSObjWrapper*>(data);
JSObject *prior = obj;
JS_CallObjectTracer(trc, &obj, "sJSObjWrappers key object");
NPP npp = wrapper->mNpp;
nsJSObjWrapperKey oldKey(prior, npp);
nsJSObjWrapperKey newKey(obj, npp);
sJSObjWrappers.rekeyIfMoved(oldKey, newKey);
static PLDHashNumber
JSObjWrapperHash(PLDHashTable *table, const void *key)
{
const nsJSObjWrapperKey *e = static_cast<const nsJSObjWrapperKey *>(key);
return HashGeneric(e->mJSObj, e->mNpp);
}
static bool
JSObjWrapperHashMatchEntry(PLDHashTable *table, const PLDHashEntryHdr *entry,
const void *key)
{
const nsJSObjWrapperKey *objWrapperKey =
static_cast<const nsJSObjWrapperKey *>(key);
const JSObjWrapperHashEntry *e =
static_cast<const JSObjWrapperHashEntry *>(entry);
return (e->mJSObjWrapper->mJSObj == objWrapperKey->mJSObj &&
e->mJSObjWrapper->mNpp == objWrapperKey->mNpp);
}
// Look up or create an NPObject that wraps the JSObject obj.
// static
@ -1001,9 +990,22 @@ nsJSObjWrapper::GetNewOrUsed(NPP npp, JSContext *cx, JS::Handle<JSObject*> obj)
return _retainobject(npobj);
}
if (!sJSObjWrappers.initialized()) {
if (!sJSObjWrappers.ops) {
// No hash yet (or any more), initialize it.
if (!sJSObjWrappers.init(16)) {
static const PLDHashTableOps ops =
{
PL_DHashAllocTable,
PL_DHashFreeTable,
JSObjWrapperHash,
JSObjWrapperHashMatchEntry,
PL_DHashMoveEntryStub,
PL_DHashClearEntryStub,
PL_DHashFinalizeStub
};
if (!PL_DHashTableInit(&sJSObjWrappers, &ops, nullptr,
sizeof(JSObjWrapperHashEntry), 16)) {
NS_ERROR("Error initializing PLDHashTable!");
return nullptr;
@ -1012,13 +1014,18 @@ nsJSObjWrapper::GetNewOrUsed(NPP npp, JSContext *cx, JS::Handle<JSObject*> obj)
nsJSObjWrapperKey key(obj, npp);
JSObjWrapperTable::AddPtr p = sJSObjWrappers.lookupForAdd(key);
JSObjWrapperHashEntry *entry = static_cast<JSObjWrapperHashEntry *>
(PL_DHashTableOperate(&sJSObjWrappers, &key, PL_DHASH_ADD));
if (p/* && p->value*/) {
MOZ_ASSERT(p->value);
if (!entry) {
// Out of memory.
return nullptr;
}
if (PL_DHASH_ENTRY_IS_BUSY(entry) && entry->mJSObjWrapper) {
// Found a live nsJSObjWrapper, return it.
return _retainobject(p->value);
return _retainobject(entry->mJSObjWrapper);
}
// No existing nsJSObjWrapper, create one.
@ -1027,17 +1034,16 @@ nsJSObjWrapper::GetNewOrUsed(NPP npp, JSContext *cx, JS::Handle<JSObject*> obj)
(nsJSObjWrapper *)_createobject(npp, &sJSObjWrapperNPClass);
if (!wrapper) {
// Out of memory, entry not yet added to table.
// OOM? Remove the stale entry from the hash.
PL_DHashTableRawRemove(&sJSObjWrappers, entry);
return nullptr;
}
wrapper->mJSObj = obj;
if (!sJSObjWrappers.add(p, key, wrapper)) {
// Out of memory, free the wrapper we created.
_releaseobject(wrapper);
return nullptr;
}
entry->mJSObjWrapper = wrapper;
NS_ASSERTION(wrapper->mNpp == npp, "nsJSObjWrapper::mNpp not initialized!");
@ -1046,15 +1052,13 @@ nsJSObjWrapper::GetNewOrUsed(NPP npp, JSContext *cx, JS::Handle<JSObject*> obj)
if (!::JS_AddNamedObjectRoot(cx, &wrapper->mJSObj, "nsJSObjWrapper::mJSObject")) {
NS_ERROR("Failed to root JSObject!");
sJSObjWrappers.remove(key);
_releaseobject(wrapper);
PL_DHashTableRawRemove(&sJSObjWrappers, entry);
return nullptr;
}
// Add postbarrier for the hashtable key
JS_StoreObjectPostBarrierCallback(cx, JSObjWrapperKeyMarkCallback, obj, wrapper);
return wrapper;
}
@ -1808,6 +1812,35 @@ nsNPObjWrapper::GetNewOrUsed(NPP npp, JSContext *cx, NPObject *npobj)
}
// PLDHashTable enumeration callbacks for destruction code.
static PLDHashOperator
JSObjWrapperPluginDestroyedCallback(PLDHashTable *table, PLDHashEntryHdr *hdr,
uint32_t number, void *arg)
{
JSObjWrapperHashEntry *entry = (JSObjWrapperHashEntry *)hdr;
nsJSObjWrapper *npobj = entry->mJSObjWrapper;
if (npobj->mNpp == arg) {
// Prevent invalidate() and _releaseobject() from touching the hash
// we're enumerating.
const PLDHashTableOps *ops = table->ops;
table->ops = nullptr;
if (npobj->_class && npobj->_class->invalidate) {
npobj->_class->invalidate(npobj);
}
_releaseobject(npobj);
table->ops = ops;
return PL_DHASH_REMOVE;
}
return PL_DHASH_NEXT;
}
// Struct for passing an NPP and a JSContext to
// NPObjWrapperPluginDestroyedCallback
struct NppAndCx
@ -1855,7 +1888,7 @@ NPObjWrapperPluginDestroyedCallback(PLDHashTable *table, PLDHashEntryHdr *hdr,
::JS_SetPrivate(entry->mJSObj, nullptr);
table->ops = ops;
table->ops = ops;
if (sDelayedReleases && sDelayedReleases->RemoveElement(npobj)) {
OnWrapperDestroyed();
@ -1871,16 +1904,9 @@ NPObjWrapperPluginDestroyedCallback(PLDHashTable *table, PLDHashEntryHdr *hdr,
void
nsJSNPRuntime::OnPluginDestroy(NPP npp)
{
if (sJSObjWrappers.initialized()) {
for (JSObjWrapperTable::Enum e(sJSObjWrappers); !e.empty(); e.popFront()) {
nsJSObjWrapper *npobj = e.front().value;
MOZ_ASSERT(npobj->_class == &nsJSObjWrapper::sJSObjWrapperNPClass);
if (npobj->mNpp == npp) {
npobj->ClearJSObject();
_releaseobject(npobj);
e.removeFront();
}
}
if (sJSObjWrappers.ops) {
PL_DHashTableEnumerate(&sJSObjWrappers,
JSObjWrapperPluginDestroyedCallback, npp);
}
// Use the safe JSContext here as we're not always able to find the

View File

@ -25,30 +25,20 @@ public:
{
}
bool operator==(const nsJSObjWrapperKey& other) const {
return mJSObj == other.mJSObj && mNpp == other.mNpp;
}
bool operator!=(const nsJSObjWrapperKey& other) const {
return !(*this == other);
}
JSObject *mJSObj;
JSObject * mJSObj;
const NPP mNpp;
};
extern const JSClass sNPObjectJSWrapperClass;
class nsJSObjWrapper : public NPObject
class nsJSObjWrapper : public NPObject,
public nsJSObjWrapperKey
{
public:
JSObject *mJSObj; /* Added as a GC root. */
const NPP mNpp;
static NPObject *GetNewOrUsed(NPP npp, JSContext *cx,
JS::Handle<JSObject*> obj);
void ClearJSObject();
protected:
nsJSObjWrapper(NPP npp);
~nsJSObjWrapper();