This source file includes following definitions.
- WrapperClass_Allocate
- WrapperClass_Deallocate
- WrapperClass_Invalidate
- WrapperClass_HasMethod
- WrapperClass_Invoke
- WrapperClass_InvokeDefault
- WrapperClass_HasProperty
- WrapperClass_GetProperty
- WrapperClass_SetProperty
- WrapperClass_RemoveProperty
- WrapperClass_Enumerate
- WrapperClass_Construct
- ppp_class_data_
- Create
- GetNPObject
- IsInstanceOf
- FromNPObject
- AllocateObjectWrapper
#include "content/renderer/pepper/plugin_object.h"
#include "base/logging.h"
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "content/renderer/pepper/npapi_glue.h"
#include "content/renderer/pepper/pepper_plugin_instance_impl.h"
#include "content/renderer/pepper/plugin_module.h"
#include "ppapi/c/dev/ppb_var_deprecated.h"
#include "ppapi/c/dev/ppp_class_deprecated.h"
#include "ppapi/c/pp_resource.h"
#include "ppapi/c/pp_var.h"
#include "ppapi/shared_impl/ppapi_globals.h"
#include "ppapi/shared_impl/resource_tracker.h"
#include "ppapi/shared_impl/var.h"
#include "ppapi/shared_impl/var_tracker.h"
#include "third_party/WebKit/public/web/WebBindings.h"
#include "third_party/npapi/bindings/npapi.h"
#include "third_party/npapi/bindings/npruntime.h"
using ppapi::PpapiGlobals;
using ppapi::StringVar;
using ppapi::Var;
using blink::WebBindings;
namespace content {
namespace {
const char kInvalidValueException[] = "Error: Invalid value";
NPObject* WrapperClass_Allocate(NPP npp, NPClass* unused) {
return PluginObject::AllocateObjectWrapper();
}
void WrapperClass_Deallocate(NPObject* np_object) {
PluginObject* plugin_object = PluginObject::FromNPObject(np_object);
if (!plugin_object)
return;
plugin_object->ppp_class()->Deallocate(plugin_object->ppp_class_data());
delete plugin_object;
}
void WrapperClass_Invalidate(NPObject* object) {
}
bool WrapperClass_HasMethod(NPObject* object, NPIdentifier method_name) {
NPObjectAccessorWithIdentifier accessor(object, method_name, false);
if (!accessor.is_valid())
return false;
PPResultAndExceptionToNPResult result_converter(
accessor.object()->GetNPObject(), NULL);
bool rv = accessor.object()->ppp_class()->HasMethod(
accessor.object()->ppp_class_data(), accessor.identifier(),
result_converter.exception());
result_converter.CheckExceptionForNoResult();
return rv;
}
bool WrapperClass_Invoke(NPObject* object, NPIdentifier method_name,
const NPVariant* argv, uint32_t argc,
NPVariant* result) {
NPObjectAccessorWithIdentifier accessor(object, method_name, false);
if (!accessor.is_valid())
return false;
PPResultAndExceptionToNPResult result_converter(
accessor.object()->GetNPObject(), result);
PPVarArrayFromNPVariantArray args(accessor.object()->instance(),
argc, argv);
scoped_refptr<PluginModule> ref(accessor.object()->instance()->module());
return result_converter.SetResult(accessor.object()->ppp_class()->Call(
accessor.object()->ppp_class_data(), accessor.identifier(),
argc, args.array(), result_converter.exception()));
}
bool WrapperClass_InvokeDefault(NPObject* np_object, const NPVariant* argv,
uint32_t argc, NPVariant* result) {
PluginObject* obj = PluginObject::FromNPObject(np_object);
if (!obj)
return false;
PPVarArrayFromNPVariantArray args(obj->instance(), argc, argv);
PPResultAndExceptionToNPResult result_converter(obj->GetNPObject(), result);
scoped_refptr<PluginModule> ref(obj->instance()->module());
result_converter.SetResult(obj->ppp_class()->Call(
obj->ppp_class_data(), PP_MakeUndefined(), argc, args.array(),
result_converter.exception()));
return result_converter.success();
}
bool WrapperClass_HasProperty(NPObject* object, NPIdentifier property_name) {
NPObjectAccessorWithIdentifier accessor(object, property_name, true);
if (!accessor.is_valid())
return false;
PPResultAndExceptionToNPResult result_converter(
accessor.object()->GetNPObject(), NULL);
bool rv = accessor.object()->ppp_class()->HasProperty(
accessor.object()->ppp_class_data(), accessor.identifier(),
result_converter.exception());
result_converter.CheckExceptionForNoResult();
return rv;
}
bool WrapperClass_GetProperty(NPObject* object, NPIdentifier property_name,
NPVariant* result) {
NPObjectAccessorWithIdentifier accessor(object, property_name, true);
if (!accessor.is_valid())
return false;
PPResultAndExceptionToNPResult result_converter(
accessor.object()->GetNPObject(), result);
return result_converter.SetResult(accessor.object()->ppp_class()->GetProperty(
accessor.object()->ppp_class_data(), accessor.identifier(),
result_converter.exception()));
}
bool WrapperClass_SetProperty(NPObject* object, NPIdentifier property_name,
const NPVariant* value) {
NPObjectAccessorWithIdentifier accessor(object, property_name, true);
if (!accessor.is_valid())
return false;
PPResultAndExceptionToNPResult result_converter(
accessor.object()->GetNPObject(), NULL);
PP_Var value_var = NPVariantToPPVar(accessor.object()->instance(), value);
accessor.object()->ppp_class()->SetProperty(
accessor.object()->ppp_class_data(), accessor.identifier(), value_var,
result_converter.exception());
PpapiGlobals::Get()->GetVarTracker()->ReleaseVar(value_var);
return result_converter.CheckExceptionForNoResult();
}
bool WrapperClass_RemoveProperty(NPObject* object, NPIdentifier property_name) {
NPObjectAccessorWithIdentifier accessor(object, property_name, true);
if (!accessor.is_valid())
return false;
PPResultAndExceptionToNPResult result_converter(
accessor.object()->GetNPObject(), NULL);
accessor.object()->ppp_class()->RemoveProperty(
accessor.object()->ppp_class_data(), accessor.identifier(),
result_converter.exception());
return result_converter.CheckExceptionForNoResult();
}
bool WrapperClass_Enumerate(NPObject* object, NPIdentifier** values,
uint32_t* count) {
*values = NULL;
*count = 0;
PluginObject* obj = PluginObject::FromNPObject(object);
if (!obj)
return false;
uint32_t property_count = 0;
PP_Var* properties = NULL;
PPResultAndExceptionToNPResult result_converter(obj->GetNPObject(), NULL);
obj->ppp_class()->GetAllPropertyNames(obj->ppp_class_data(),
&property_count, &properties,
result_converter.exception());
if (!result_converter.has_exception()) {
if (property_count > 0) {
*values = static_cast<NPIdentifier*>(
calloc(property_count, sizeof(NPIdentifier)));
*count = 0;
for (uint32_t i = 0; i < property_count; ++i) {
if (!((*values)[i] = PPVarToNPIdentifier(properties[i]))) {
*result_converter.exception() =
StringVar::StringToPPVar(kInvalidValueException);
break;
}
(*count)++;
}
if (result_converter.has_exception()) {
free(*values);
*values = NULL;
*count = 0;
}
}
}
result_converter.CheckExceptionForNoResult();
ppapi::VarTracker* var_tracker = PpapiGlobals::Get()->GetVarTracker();
for (uint32_t i = 0; i < property_count; ++i)
var_tracker->ReleaseVar(properties[i]);
free(properties);
return result_converter.success();
}
bool WrapperClass_Construct(NPObject* object, const NPVariant* argv,
uint32_t argc, NPVariant* result) {
PluginObject* obj = PluginObject::FromNPObject(object);
if (!obj)
return false;
PPVarArrayFromNPVariantArray args(obj->instance(), argc, argv);
PPResultAndExceptionToNPResult result_converter(obj->GetNPObject(), result);
return result_converter.SetResult(obj->ppp_class()->Construct(
obj->ppp_class_data(), argc, args.array(),
result_converter.exception()));
}
const NPClass wrapper_class = {
NP_CLASS_STRUCT_VERSION,
WrapperClass_Allocate,
WrapperClass_Deallocate,
WrapperClass_Invalidate,
WrapperClass_HasMethod,
WrapperClass_Invoke,
WrapperClass_InvokeDefault,
WrapperClass_HasProperty,
WrapperClass_GetProperty,
WrapperClass_SetProperty,
WrapperClass_RemoveProperty,
WrapperClass_Enumerate,
WrapperClass_Construct
};
}
struct PluginObject::NPObjectWrapper : public NPObject {
PluginObject* obj;
};
PluginObject::PluginObject(PepperPluginInstanceImpl* instance,
NPObjectWrapper* object_wrapper,
const PPP_Class_Deprecated* ppp_class,
void* ppp_class_data)
: instance_(instance),
object_wrapper_(object_wrapper),
ppp_class_(ppp_class),
ppp_class_data_(ppp_class_data) {
object_wrapper_->obj = this;
instance_->AddPluginObject(this);
}
PluginObject::~PluginObject() {
DCHECK(object_wrapper_->obj == this);
object_wrapper_->obj = NULL;
instance_->RemovePluginObject(this);
}
PP_Var PluginObject::Create(PepperPluginInstanceImpl* instance,
const PPP_Class_Deprecated* ppp_class,
void* ppp_class_data) {
NPObjectWrapper* wrapper = static_cast<NPObjectWrapper*>(
WebBindings::createObject(instance->instanceNPP(),
const_cast<NPClass*>(&wrapper_class)));
new PluginObject(instance, wrapper, ppp_class, ppp_class_data);
PP_Var obj_var(NPObjectToPPVar(instance, wrapper));
WebBindings::releaseObject(wrapper);
return obj_var;
}
NPObject* PluginObject::GetNPObject() const {
return object_wrapper_;
}
bool PluginObject::IsInstanceOf(NPObject* np_object,
const PPP_Class_Deprecated* ppp_class,
void** ppp_class_data) {
if (np_object->_class != &wrapper_class)
return false;
PluginObject* plugin_object = FromNPObject(np_object);
if (!plugin_object)
return false;
if (plugin_object->ppp_class() != ppp_class)
return false;
if (ppp_class_data)
*ppp_class_data = plugin_object->ppp_class_data();
return true;
}
PluginObject* PluginObject::FromNPObject(NPObject* object) {
return static_cast<NPObjectWrapper*>(object)->obj;
}
NPObject* PluginObject::AllocateObjectWrapper() {
NPObjectWrapper* wrapper = new NPObjectWrapper;
memset(wrapper, 0, sizeof(NPObjectWrapper));
return wrapper;
}
}