This source file includes following definitions.
- npObjectInvokeImpl
- npObjectMethodHandler
- npObjectInvokeDefaultHandler
- get
- set
- sharedInstance
- clear
- setWeakCallback
- npObjectGetProperty
- npObjectNamedPropertyGetter
- npObjectIndexedPropertyGetter
- npObjectGetNamedProperty
- npObjectGetIndexedProperty
- npObjectQueryProperty
- npObjectSetProperty
- npObjectNamedPropertySetter
- npObjectIndexedPropertySetter
- npObjectSetNamedProperty
- npObjectSetIndexedProperty
- npObjectPropertyEnumerator
- npObjectNamedPropertyEnumerator
- npObjectIndexedPropertyEnumerator
- staticNPObjectMap
- setWeakCallback
- createV8ObjectForNPObject
- forgetV8ObjectForNPObject
#include "config.h"
#include "bindings/v8/V8NPObject.h"
#include "V8HTMLAppletElement.h"
#include "V8HTMLEmbedElement.h"
#include "V8HTMLObjectElement.h"
#include "bindings/v8/NPV8Object.h"
#include "bindings/v8/UnsafePersistent.h"
#include "bindings/v8/V8Binding.h"
#include "bindings/v8/V8NPUtils.h"
#include "bindings/v8/V8ObjectConstructor.h"
#include "bindings/v8/npruntime_impl.h"
#include "bindings/v8/npruntime_priv.h"
#include "core/html/HTMLPlugInElement.h"
#include "wtf/OwnPtr.h"
namespace WebCore {
enum InvokeFunctionType {
InvokeMethod = 1,
InvokeConstruct = 2,
InvokeDefault = 3
};
struct IdentifierRep {
int number() const { return m_isString ? 0 : m_value.m_number; }
const char* string() const { return m_isString ? m_value.m_string : 0; }
union {
const char* m_string;
int m_number;
} m_value;
bool m_isString;
};
static void npObjectInvokeImpl(const v8::FunctionCallbackInfo<v8::Value>& info, InvokeFunctionType functionId)
{
NPObject* npObject;
v8::Isolate* isolate = info.GetIsolate();
HTMLPlugInElement* element = V8HTMLAppletElement::toNativeWithTypeCheck(isolate, info.Holder());
if (!element) {
element = V8HTMLEmbedElement::toNativeWithTypeCheck(isolate, info.Holder());
if (!element) {
element = V8HTMLObjectElement::toNativeWithTypeCheck(isolate, info.Holder());
}
}
if (element) {
if (RefPtr<SharedPersistent<v8::Object> > wrapper = element->pluginWrapper()) {
v8::HandleScope handleScope(isolate);
npObject = v8ObjectToNPObject(wrapper->newLocal(isolate));
} else
npObject = 0;
} else {
if (info.Holder()->InternalFieldCount() != npObjectInternalFieldCount) {
throwError(v8ReferenceError, "NPMethod called on non-NPObject", info.GetIsolate());
return;
}
npObject = v8ObjectToNPObject(info.Holder());
}
if (!npObject || !_NPN_IsAlive(npObject)) {
throwError(v8ReferenceError, "NPObject deleted", isolate);
return;
}
int numArgs = info.Length();
OwnPtr<NPVariant[]> npArgs = adoptArrayPtr(new NPVariant[numArgs]);
for (int i = 0; i < numArgs; i++)
convertV8ObjectToNPVariant(info[i], npObject, &npArgs[i], isolate);
NPVariant result;
VOID_TO_NPVARIANT(result);
bool retval = true;
switch (functionId) {
case InvokeMethod:
if (npObject->_class->invoke) {
v8::Handle<v8::String> functionName = v8::Handle<v8::String>::Cast(info.Data());
NPIdentifier identifier = getStringIdentifier(functionName);
retval = npObject->_class->invoke(npObject, identifier, npArgs.get(), numArgs, &result);
}
break;
case InvokeConstruct:
if (npObject->_class->construct)
retval = npObject->_class->construct(npObject, npArgs.get(), numArgs, &result);
break;
case InvokeDefault:
if (npObject->_class->invokeDefault)
retval = npObject->_class->invokeDefault(npObject, npArgs.get(), numArgs, &result);
break;
default:
break;
}
if (!retval)
throwError(v8GeneralError, "Error calling method on NPObject.", isolate);
for (int i = 0; i < numArgs; i++)
_NPN_ReleaseVariantValue(&npArgs[i]);
v8::Handle<v8::Value> returnValue;
if (_NPN_IsAlive(npObject))
returnValue = convertNPVariantToV8Object(&result, npObject, isolate);
_NPN_ReleaseVariantValue(&result);
v8SetReturnValue(info, returnValue);
}
void npObjectMethodHandler(const v8::FunctionCallbackInfo<v8::Value>& info)
{
return npObjectInvokeImpl(info, InvokeMethod);
}
void npObjectInvokeDefaultHandler(const v8::FunctionCallbackInfo<v8::Value>& info)
{
if (info.IsConstructCall()) {
npObjectInvokeImpl(info, InvokeConstruct);
return;
}
npObjectInvokeImpl(info, InvokeDefault);
}
class V8NPTemplateMap {
public:
typedef HashMap<PrivateIdentifier*, UnsafePersistent<v8::FunctionTemplate> > MapType;
UnsafePersistent<v8::FunctionTemplate> get(PrivateIdentifier* key)
{
return m_map.get(key);
}
void set(PrivateIdentifier* key, v8::Handle<v8::FunctionTemplate> handle)
{
ASSERT(!m_map.contains(key));
v8::Persistent<v8::FunctionTemplate> wrapper(m_isolate, handle);
wrapper.SetWeak(key, &setWeakCallback);
m_map.set(key, UnsafePersistent<v8::FunctionTemplate>(wrapper));
}
static V8NPTemplateMap& sharedInstance(v8::Isolate* isolate)
{
DEFINE_STATIC_LOCAL(V8NPTemplateMap, map, (isolate));
ASSERT(isolate == map.m_isolate);
return map;
}
private:
explicit V8NPTemplateMap(v8::Isolate* isolate)
: m_isolate(isolate)
{
}
void clear(PrivateIdentifier* key)
{
MapType::iterator it = m_map.find(key);
ASSERT_WITH_SECURITY_IMPLICATION(it != m_map.end());
it->value.dispose();
m_map.remove(it);
}
static void setWeakCallback(const v8::WeakCallbackData<v8::FunctionTemplate, PrivateIdentifier>& data)
{
V8NPTemplateMap::sharedInstance(data.GetIsolate()).clear(data.GetParameter());
}
MapType m_map;
v8::Isolate* m_isolate;
};
static v8::Handle<v8::Value> npObjectGetProperty(v8::Local<v8::Object> self, NPIdentifier identifier, v8::Local<v8::Value> key, v8::Isolate* isolate)
{
NPObject* npObject = v8ObjectToNPObject(self);
if (!npObject || !_NPN_IsAlive(npObject))
return throwError(v8ReferenceError, "NPObject deleted", isolate);
if (npObject->_class->hasProperty && npObject->_class->getProperty && npObject->_class->hasProperty(npObject, identifier)) {
if (!_NPN_IsAlive(npObject))
return throwError(v8ReferenceError, "NPObject deleted", isolate);
NPVariant result;
VOID_TO_NPVARIANT(result);
if (!npObject->_class->getProperty(npObject, identifier, &result))
return v8Undefined();
v8::Handle<v8::Value> returnValue;
if (_NPN_IsAlive(npObject))
returnValue = convertNPVariantToV8Object(&result, npObject, isolate);
_NPN_ReleaseVariantValue(&result);
return returnValue;
}
if (!_NPN_IsAlive(npObject))
return throwError(v8ReferenceError, "NPObject deleted", isolate);
if (key->IsString() && npObject->_class->hasMethod && npObject->_class->hasMethod(npObject, identifier)) {
if (!_NPN_IsAlive(npObject))
return throwError(v8ReferenceError, "NPObject deleted", isolate);
PrivateIdentifier* id = static_cast<PrivateIdentifier*>(identifier);
UnsafePersistent<v8::FunctionTemplate> functionTemplate = V8NPTemplateMap::sharedInstance(isolate).get(id);
v8::Local<v8::Function> v8Function;
if (functionTemplate.isEmpty()) {
v8::Local<v8::FunctionTemplate> temp = v8::FunctionTemplate::New(isolate);
temp->SetCallHandler(npObjectMethodHandler, key);
V8NPTemplateMap::sharedInstance(isolate).set(id, temp);
v8Function = temp->GetFunction();
} else {
v8Function = functionTemplate.newLocal(isolate)->GetFunction();
}
v8Function->SetName(v8::Handle<v8::String>::Cast(key));
return v8Function;
}
return v8Undefined();
}
void npObjectNamedPropertyGetter(v8::Local<v8::String> name, const v8::PropertyCallbackInfo<v8::Value>& info)
{
NPIdentifier identifier = getStringIdentifier(name);
v8SetReturnValue(info, npObjectGetProperty(info.Holder(), identifier, name, info.GetIsolate()));
}
void npObjectIndexedPropertyGetter(uint32_t index, const v8::PropertyCallbackInfo<v8::Value>& info)
{
NPIdentifier identifier = _NPN_GetIntIdentifier(index);
v8SetReturnValue(info, npObjectGetProperty(info.Holder(), identifier, v8::Number::New(info.GetIsolate(), index), info.GetIsolate()));
}
void npObjectGetNamedProperty(v8::Local<v8::Object> self, v8::Local<v8::String> name, const v8::PropertyCallbackInfo<v8::Value>& info)
{
NPIdentifier identifier = getStringIdentifier(name);
v8SetReturnValue(info, npObjectGetProperty(self, identifier, name, info.GetIsolate()));
}
void npObjectGetIndexedProperty(v8::Local<v8::Object> self, uint32_t index, const v8::PropertyCallbackInfo<v8::Value>& info)
{
NPIdentifier identifier = _NPN_GetIntIdentifier(index);
v8SetReturnValue(info, npObjectGetProperty(self, identifier, v8::Number::New(info.GetIsolate(), index), info.GetIsolate()));
}
void npObjectQueryProperty(v8::Local<v8::String> name, const v8::PropertyCallbackInfo<v8::Integer>& info)
{
NPIdentifier identifier = getStringIdentifier(name);
if (npObjectGetProperty(info.Holder(), identifier, name, info.GetIsolate()).IsEmpty())
return;
v8SetReturnValueInt(info, 0);
}
static v8::Handle<v8::Value> npObjectSetProperty(v8::Local<v8::Object> self, NPIdentifier identifier, v8::Local<v8::Value> value, v8::Isolate* isolate)
{
NPObject* npObject = v8ObjectToNPObject(self);
if (!npObject || !_NPN_IsAlive(npObject)) {
throwError(v8ReferenceError, "NPObject deleted", isolate);
return value;
}
if (npObject->_class->hasProperty && npObject->_class->setProperty && npObject->_class->hasProperty(npObject, identifier)) {
if (!_NPN_IsAlive(npObject))
return throwError(v8ReferenceError, "NPObject deleted", isolate);
NPVariant npValue;
VOID_TO_NPVARIANT(npValue);
convertV8ObjectToNPVariant(value, npObject, &npValue, isolate);
bool success = npObject->_class->setProperty(npObject, identifier, &npValue);
_NPN_ReleaseVariantValue(&npValue);
if (success)
return value;
}
return v8Undefined();
}
void npObjectNamedPropertySetter(v8::Local<v8::String> name, v8::Local<v8::Value> value, const v8::PropertyCallbackInfo<v8::Value>& info)
{
NPIdentifier identifier = getStringIdentifier(name);
v8SetReturnValue(info, npObjectSetProperty(info.Holder(), identifier, value, info.GetIsolate()));
}
void npObjectIndexedPropertySetter(uint32_t index, v8::Local<v8::Value> value, const v8::PropertyCallbackInfo<v8::Value>& info)
{
NPIdentifier identifier = _NPN_GetIntIdentifier(index);
v8SetReturnValue(info, npObjectSetProperty(info.Holder(), identifier, value, info.GetIsolate()));
}
void npObjectSetNamedProperty(v8::Local<v8::Object> self, v8::Local<v8::String> name, v8::Local<v8::Value> value, const v8::PropertyCallbackInfo<v8::Value>& info)
{
NPIdentifier identifier = getStringIdentifier(name);
v8SetReturnValue(info, npObjectSetProperty(self, identifier, value, info.GetIsolate()));
}
void npObjectSetIndexedProperty(v8::Local<v8::Object> self, uint32_t index, v8::Local<v8::Value> value, const v8::PropertyCallbackInfo<v8::Value>& info)
{
NPIdentifier identifier = _NPN_GetIntIdentifier(index);
v8SetReturnValue(info, npObjectSetProperty(self, identifier, value, info.GetIsolate()));
}
void npObjectPropertyEnumerator(const v8::PropertyCallbackInfo<v8::Array>& info, bool namedProperty)
{
NPObject* npObject = v8ObjectToNPObject(info.Holder());
if (!npObject || !_NPN_IsAlive(npObject))
throwError(v8ReferenceError, "NPObject deleted", info.GetIsolate());
if (NP_CLASS_STRUCT_VERSION_HAS_ENUM(npObject->_class) && npObject->_class->enumerate) {
uint32_t count;
NPIdentifier* identifiers;
if (npObject->_class->enumerate(npObject, &identifiers, &count)) {
uint32_t propertiesCount = 0;
for (uint32_t i = 0; i < count; ++i) {
IdentifierRep* identifier = static_cast<IdentifierRep*>(identifiers[i]);
if (namedProperty == identifier->m_isString)
++propertiesCount;
}
v8::Handle<v8::Array> properties = v8::Array::New(info.GetIsolate(), propertiesCount);
for (uint32_t i = 0, propertyIndex = 0; i < count; ++i) {
IdentifierRep* identifier = static_cast<IdentifierRep*>(identifiers[i]);
if (namedProperty == identifier->m_isString) {
ASSERT(propertyIndex < propertiesCount);
if (namedProperty)
properties->Set(v8::Integer::New(info.GetIsolate(), propertyIndex++), v8AtomicString(info.GetIsolate(), identifier->string()));
else
properties->Set(v8::Integer::New(info.GetIsolate(), propertyIndex++), v8::Integer::New(info.GetIsolate(), identifier->number()));
}
}
v8SetReturnValue(info, properties);
return;
}
}
}
void npObjectNamedPropertyEnumerator(const v8::PropertyCallbackInfo<v8::Array>& info)
{
npObjectPropertyEnumerator(info, true);
}
void npObjectIndexedPropertyEnumerator(const v8::PropertyCallbackInfo<v8::Array>& info)
{
npObjectPropertyEnumerator(info, false);
}
static DOMWrapperMap<NPObject>& staticNPObjectMap()
{
DEFINE_STATIC_LOCAL(DOMWrapperMap<NPObject>, npObjectMap, (v8::Isolate::GetCurrent()));
return npObjectMap;
}
template<>
inline void DOMWrapperMap<NPObject>::setWeakCallback(const v8::WeakCallbackData<v8::Object, DOMWrapperMap<NPObject> >& data)
{
NPObject* npObject = static_cast<NPObject*>(toNative(data.GetValue()));
ASSERT(npObject);
ASSERT(staticNPObjectMap().containsKeyAndValue(npObject, data.GetValue()));
staticNPObjectMap().removeAndDispose(npObject);
if (_NPN_IsAlive(npObject))
_NPN_ReleaseObject(npObject);
}
v8::Local<v8::Object> createV8ObjectForNPObject(NPObject* object, NPObject* root, v8::Isolate* isolate)
{
static v8::Eternal<v8::FunctionTemplate> npObjectDesc;
ASSERT(isolate->InContext());
V8NPObject* v8NPObject = npObjectToV8NPObject(object);
if (v8NPObject)
return v8::Local<v8::Object>::New(isolate, v8NPObject->v8Object);
v8::Handle<v8::Object> wrapper = staticNPObjectMap().newLocal(object, isolate);
if (!wrapper.IsEmpty())
return wrapper;
if (npObjectDesc.IsEmpty()) {
v8::Local<v8::FunctionTemplate> templ = v8::FunctionTemplate::New(isolate);
templ->InstanceTemplate()->SetInternalFieldCount(npObjectInternalFieldCount);
templ->InstanceTemplate()->SetNamedPropertyHandler(npObjectNamedPropertyGetter, npObjectNamedPropertySetter, npObjectQueryProperty, 0, npObjectNamedPropertyEnumerator);
templ->InstanceTemplate()->SetIndexedPropertyHandler(npObjectIndexedPropertyGetter, npObjectIndexedPropertySetter, 0, 0, npObjectIndexedPropertyEnumerator);
templ->InstanceTemplate()->SetCallAsFunctionHandler(npObjectInvokeDefaultHandler);
npObjectDesc.Set(isolate, templ);
}
v8::Handle<v8::Function> v8Function = npObjectDesc.Get(isolate)->GetFunction();
v8::Local<v8::Object> value = V8ObjectConstructor::newInstance(isolate, v8Function);
if (value.IsEmpty())
return value;
V8DOMWrapper::setNativeInfo(value, npObjectTypeInfo(), object);
_NPN_RetainObject(object);
_NPN_RegisterObject(object, root);
WrapperConfiguration configuration = buildWrapperConfiguration(object, WrapperConfiguration::Dependent);
staticNPObjectMap().set(object, value, configuration);
ASSERT(V8DOMWrapper::isDOMWrapper(value));
return value;
}
void forgetV8ObjectForNPObject(NPObject* object)
{
v8::Isolate* isolate = v8::Isolate::GetCurrent();
v8::HandleScope scope(isolate);
v8::Handle<v8::Object> wrapper = staticNPObjectMap().newLocal(object, isolate);
if (!wrapper.IsEmpty()) {
V8DOMWrapper::clearNativeInfo(wrapper, npObjectTypeInfo());
staticNPObjectMap().removeAndDispose(object);
_NPN_ReleaseObject(object);
}
}
}