This source file includes following definitions.
- m_length
- m_length
- m_length
- isHashTableDeletedValue
- hashTableDeletedValue
- getStringIdentifierMap
- getIntIdentifierMap
- _NPN_GetStringIdentifier
- _NPN_GetStringIdentifiers
- _NPN_GetIntIdentifier
- _NPN_IdentifierIsString
- _NPN_UTF8FromIdentifier
- _NPN_IntFromIdentifier
- _NPN_ReleaseVariantValue
- _NPN_CreateObject
- _NPN_RetainObject
- _NPN_DeallocateObject
- _NPN_ReleaseObject
- _NPN_InitializeVariantWithStringCopy
- liveObjectMap
- rootObjectMap
- _NPN_RegisterObject
- _NPN_UnregisterObject
- _NPN_IsAlive
#include "config.h"
#include "bindings/v8/NPV8Object.h"
#include "bindings/v8/V8NPObject.h"
#include "bindings/v8/npruntime_impl.h"
#include "bindings/v8/npruntime_priv.h"
#include "wtf/Assertions.h"
#include "wtf/HashMap.h"
#include "wtf/HashSet.h"
#include "wtf/HashTableDeletedValueType.h"
#include <stdlib.h>
using namespace WebCore;
namespace npruntime {
class StringKey {
public:
explicit StringKey(const char* str) : m_string(str), m_length(strlen(str)) { }
StringKey() : m_string(0), m_length(0) { }
explicit StringKey(WTF::HashTableDeletedValueType) : m_string(hashTableDeletedValue()), m_length(0) { }
StringKey& operator=(const StringKey& other)
{
this->m_string = other.m_string;
this->m_length = other.m_length;
return *this;
}
bool isHashTableDeletedValue() const
{
return m_string == hashTableDeletedValue();
}
const char* m_string;
size_t m_length;
private:
const char* hashTableDeletedValue() const
{
return reinterpret_cast<const char*>(-1);
}
};
inline bool operator==(const StringKey& x, const StringKey& y)
{
if (x.m_length != y.m_length)
return false;
if (x.m_string == y.m_string)
return true;
ASSERT(!x.isHashTableDeletedValue() && !y.isHashTableDeletedValue());
return !memcmp(x.m_string, y.m_string, y.m_length);
}
struct StringKeyHash {
static unsigned hash(const StringKey& key)
{
unsigned hash = 0;
size_t len = key.m_length;
const char* str = key.m_string;
for (size_t i = 0; i < len; i++) {
char c = str[i];
hash += c;
hash += (hash << 10);
hash ^= (hash >> 6);
}
hash += (hash << 3);
hash ^= (hash >> 11);
hash += (hash << 15);
if (hash == 0)
hash = 27;
return hash;
}
static bool equal(const StringKey& x, const StringKey& y)
{
return x == y;
}
static const bool safeToCompareToEmptyOrDeleted = true;
};
}
using npruntime::StringKey;
using npruntime::StringKeyHash;
struct StringKeyHashTraits : WTF::GenericHashTraits<StringKey> {
static void constructDeletedValue(StringKey& slot)
{
new (&slot) StringKey(WTF::HashTableDeletedValue);
}
static bool isDeletedValue(const StringKey& value)
{
return value.isHashTableDeletedValue();
}
};
typedef WTF::HashMap<StringKey, PrivateIdentifier*, StringKeyHash, StringKeyHashTraits> StringIdentifierMap;
static StringIdentifierMap* getStringIdentifierMap()
{
static StringIdentifierMap* stringIdentifierMap = 0;
if (!stringIdentifierMap)
stringIdentifierMap = new StringIdentifierMap();
return stringIdentifierMap;
}
typedef WTF::HashMap<int, PrivateIdentifier*> IntIdentifierMap;
static IntIdentifierMap* getIntIdentifierMap()
{
static IntIdentifierMap* intIdentifierMap = 0;
if (!intIdentifierMap)
intIdentifierMap = new IntIdentifierMap();
return intIdentifierMap;
}
extern "C" {
NPIdentifier _NPN_GetStringIdentifier(const NPUTF8* name)
{
ASSERT(name);
if (name) {
StringKey key(name);
StringIdentifierMap* identMap = getStringIdentifierMap();
StringIdentifierMap::iterator iter = identMap->find(key);
if (iter != identMap->end())
return static_cast<NPIdentifier>(iter->value);
size_t nameLen = key.m_length;
PrivateIdentifier* identifier = static_cast<PrivateIdentifier*>(malloc(sizeof(PrivateIdentifier) + nameLen + 1));
char* nameStorage = reinterpret_cast<char*>(identifier + 1);
memcpy(nameStorage, name, nameLen + 1);
identifier->isString = true;
identifier->value.string = reinterpret_cast<NPUTF8*>(nameStorage);
key.m_string = nameStorage;
identMap->set(key, identifier);
return (NPIdentifier)identifier;
}
return 0;
}
void _NPN_GetStringIdentifiers(const NPUTF8** names, int32_t nameCount, NPIdentifier* identifiers)
{
ASSERT(names);
ASSERT(identifiers);
if (names && identifiers) {
for (int i = 0; i < nameCount; i++)
identifiers[i] = _NPN_GetStringIdentifier(names[i]);
}
}
NPIdentifier _NPN_GetIntIdentifier(int32_t intId)
{
if (!intId || intId == -1) {
static PrivateIdentifier* minusOneOrZeroIds[2];
PrivateIdentifier* id = minusOneOrZeroIds[intId + 1];
if (!id) {
id = reinterpret_cast<PrivateIdentifier*>(malloc(sizeof(PrivateIdentifier)));
id->isString = false;
id->value.number = intId;
minusOneOrZeroIds[intId + 1] = id;
}
return (NPIdentifier) id;
}
IntIdentifierMap* identMap = getIntIdentifierMap();
IntIdentifierMap::iterator iter = identMap->find(intId);
if (iter != identMap->end())
return static_cast<NPIdentifier>(iter->value);
PrivateIdentifier* identifier = reinterpret_cast<PrivateIdentifier*>(malloc(sizeof(PrivateIdentifier)));
identifier->isString = false;
identifier->value.number = intId;
identMap->set(intId, identifier);
return (NPIdentifier)identifier;
}
bool _NPN_IdentifierIsString(NPIdentifier identifier)
{
PrivateIdentifier* privateIdentifier = reinterpret_cast<PrivateIdentifier*>(identifier);
return privateIdentifier->isString;
}
NPUTF8 *_NPN_UTF8FromIdentifier(NPIdentifier identifier)
{
PrivateIdentifier* privateIdentifier = reinterpret_cast<PrivateIdentifier*>(identifier);
if (!privateIdentifier->isString || !privateIdentifier->value.string)
return 0;
return (NPUTF8*) strdup(privateIdentifier->value.string);
}
int32_t _NPN_IntFromIdentifier(NPIdentifier identifier)
{
PrivateIdentifier* privateIdentifier = reinterpret_cast<PrivateIdentifier*>(identifier);
if (privateIdentifier->isString)
return 0;
return privateIdentifier->value.number;
}
void _NPN_ReleaseVariantValue(NPVariant* variant)
{
ASSERT(variant);
if (variant->type == NPVariantType_Object) {
_NPN_ReleaseObject(variant->value.objectValue);
variant->value.objectValue = 0;
} else if (variant->type == NPVariantType_String) {
free((void*)variant->value.stringValue.UTF8Characters);
variant->value.stringValue.UTF8Characters = 0;
variant->value.stringValue.UTF8Length = 0;
}
variant->type = NPVariantType_Void;
}
NPObject *_NPN_CreateObject(NPP npp, NPClass* npClass)
{
ASSERT(npClass);
if (npClass) {
NPObject* npObject;
if (npClass->allocate != 0)
npObject = npClass->allocate(npp, npClass);
else
npObject = reinterpret_cast<NPObject*>(malloc(sizeof(NPObject)));
npObject->_class = npClass;
npObject->referenceCount = 1;
return npObject;
}
return 0;
}
NPObject* _NPN_RetainObject(NPObject* npObject)
{
ASSERT(npObject);
ASSERT(npObject->referenceCount > 0);
if (npObject)
npObject->referenceCount++;
return npObject;
}
void _NPN_DeallocateObject(NPObject* npObject)
{
ASSERT(npObject);
if (npObject) {
if (_NPN_IsAlive(npObject))
_NPN_UnregisterObject(npObject);
npObject->referenceCount = -1;
if (npObject->_class->deallocate)
npObject->_class->deallocate(npObject);
else
free(npObject);
}
}
void _NPN_ReleaseObject(NPObject* npObject)
{
ASSERT(npObject);
ASSERT(npObject->referenceCount >= 1);
if (npObject && npObject->referenceCount >= 1) {
if (!--npObject->referenceCount)
_NPN_DeallocateObject(npObject);
}
}
void _NPN_InitializeVariantWithStringCopy(NPVariant* variant, const NPString* value)
{
variant->type = NPVariantType_String;
variant->value.stringValue.UTF8Length = value->UTF8Length;
variant->value.stringValue.UTF8Characters = reinterpret_cast<NPUTF8*>(malloc(sizeof(NPUTF8) * value->UTF8Length));
memcpy((void*)variant->value.stringValue.UTF8Characters, value->UTF8Characters, sizeof(NPUTF8) * value->UTF8Length);
}
}
typedef WTF::HashSet<NPObject*> NPObjectSet;
typedef WTF::HashMap<NPObject*, NPObject*> NPObjectMap;
typedef WTF::HashMap<NPObject*, NPObjectSet*> NPRootObjectMap;
static NPObjectMap& liveObjectMap()
{
DEFINE_STATIC_LOCAL(NPObjectMap, objectMap, ());
return objectMap;
}
static NPRootObjectMap& rootObjectMap()
{
DEFINE_STATIC_LOCAL(NPRootObjectMap, objectMap, ());
return objectMap;
}
extern "C" {
void _NPN_RegisterObject(NPObject* npObject, NPObject* owner)
{
ASSERT(npObject);
if (liveObjectMap().find(npObject) != liveObjectMap().end())
return;
if (!owner) {
ASSERT(rootObjectMap().find(npObject) == rootObjectMap().end());
rootObjectMap().set(npObject, new NPObjectSet());
} else {
NPObjectMap::iterator ownerEntry = liveObjectMap().find(owner);
NPObject* parent = 0;
if (liveObjectMap().end() != ownerEntry)
parent = ownerEntry->value;
if (parent)
owner = parent;
ASSERT(rootObjectMap().find(npObject) == rootObjectMap().end());
if (rootObjectMap().find(owner) != rootObjectMap().end())
rootObjectMap().get(owner)->add(npObject);
}
ASSERT(liveObjectMap().find(npObject) == liveObjectMap().end());
liveObjectMap().set(npObject, owner);
}
void _NPN_UnregisterObject(NPObject* npObject)
{
ASSERT(npObject);
ASSERT_WITH_SECURITY_IMPLICATION(liveObjectMap().find(npObject) != liveObjectMap().end());
NPObject* owner = 0;
if (liveObjectMap().find(npObject) != liveObjectMap().end())
owner = liveObjectMap().find(npObject)->value;
if (!owner) {
ASSERT_WITH_SECURITY_IMPLICATION(rootObjectMap().find(npObject) != rootObjectMap().end());
NPObjectSet* set = rootObjectMap().get(npObject);
while (set->size() > 0) {
#ifndef NDEBUG
unsigned size = set->size();
#endif
NPObject* sub_object = *(set->begin());
ASSERT(rootObjectMap().find(sub_object) == rootObjectMap().end());
set->remove(sub_object);
liveObjectMap().remove(sub_object);
if (V8NPObject* v8npObject = npObjectToV8NPObject(sub_object))
v8npObject->rootObject = 0;
forgetV8ObjectForNPObject(sub_object);
ASSERT(set->size() < size);
}
delete set;
rootObjectMap().remove(npObject);
} else {
NPRootObjectMap::iterator ownerEntry = rootObjectMap().find(owner);
if (ownerEntry != rootObjectMap().end()) {
NPObjectSet* list = ownerEntry->value;
ASSERT(list->find(npObject) != list->end());
list->remove(npObject);
}
}
liveObjectMap().remove(npObject);
forgetV8ObjectForNPObject(npObject);
}
bool _NPN_IsAlive(NPObject* npObject)
{
return liveObjectMap().find(npObject) != liveObjectMap().end();
}
}