#include "Plugin.h"
#ifdef WIN32
#include <stdio.h>
#include <stdlib.h>
#include <Windows.h>
#include <vector>
#include <sstream>
#endif
const char* kSayHello = "sayHello";
const char* kAdd = "Add";
const char* kSubtract = "Subtract";
const char* kMultiplication = "Multiplication";
const char* kDivision = "Division";
static NPClass plugin_ref_obj = {
NP_CLASS_STRUCT_VERSION,
ScriptablePluginObject::Allocate,
ScriptablePluginObject::Deallocate,
NULL,
ScriptablePluginObject::HasMethod,
ScriptablePluginObject::Invoke,
ScriptablePluginObject::InvokeDefault,
ScriptablePluginObject::HasProperty,
ScriptablePluginObject::GetProperty,
NULL,
NULL,
};
ScriptablePluginObject::ScriptablePluginObject(NPP instance)
: npp(instance) {
}
NPObject* ScriptablePluginObject::Allocate(NPP instance, NPClass* npclass) {
return (NPObject*)(new ScriptablePluginObject(instance));
}
void ScriptablePluginObject::Deallocate(NPObject* obj) {
delete (ScriptablePluginObject*)obj;
}
bool ScriptablePluginObject::HasMethod(NPObject* obj, NPIdentifier methodName) {
return true;
}
bool ScriptablePluginObject::InvokeDefault(NPObject* obj, const NPVariant* args,
uint32_t argCount, NPVariant* result) {
return true;
}
void dumpFileEntryAsJson(std::ostringstream &str, const char *file) {
str << "{\"name\": \"" << file << "\", \"type\": \"" <<"file"<< "\"}";
}
NPVariant *eval(NPP instance, const char *scriptString) {
NPString script;
script.UTF8Characters = scriptString;
script.UTF8Length = strlen(script.UTF8Characters);
NPObject *window = NULL;
npnfuncs->getvalue(instance, NPNVWindowNPObject, &window);
NPVariant *result = new NPVariant();
npnfuncs->evaluate(instance, window, &script, result);
npnfuncs->releaseobject(window);
return result;
}
bool ScriptablePluginObject::Invoke(NPObject* obj, NPIdentifier methodName,
const NPVariant* args, uint32_t argCount,
NPVariant* result) {
ScriptablePluginObject *thisObj = (ScriptablePluginObject*)obj;
char* name = npnfuncs->utf8fromidentifier(methodName);
bool ret_val = false;
if (!name) {
return ret_val;
}
if (!strcmp(name, kSayHello))
{
ret_val = true;
int sum = 0;
char ch[10]={0};
itoa( argCount, ch, 10);
MessageBox(NULL, ch, "", MB_OK);
int a = args[0].value.intValue;
memset(ch, 0, 10);
itoa( a, ch, 10);
MessageBox(NULL, ch, "", MB_OK);
//TODO 书写helloworld
const char* outString = "zhsl2007 demo string.";
char* npOutString = (char *)npnfuncs->memalloc(strlen(outString) + 1);
if (!npOutString)
return false;
strcpy(npOutString, outString);
STRINGZ_TO_NPVARIANT(npOutString, *result);
}
if(!strcmp(name, kAdd))
{
//MessageBox(NULL, "1", "1", MB_OK);
int sum = 0;
char ch[10]={0};
itoa( argCount, ch, 10);
MessageBox(NULL, ch, "Add", MB_OK);
MessageBox(NULL,args[0].value.stringValue.UTF8Characters, "Add", MB_OK);
//
std::ostringstream str;
str << "(function() { return [";
dumpFileEntryAsJson(str, "123");
str << ",";
dumpFileEntryAsJson(str, "456");
str << ",";
dumpFileEntryAsJson(str, "789");
str << "]; })()";
MessageBox(NULL, str.str().c_str(),"Add", MB_OK);
*result = *eval(thisObj->npp, str.str().c_str());
for( unsigned int i = 0; i < argCount; i++ )
{
//if(args[i].type == NPVariantType_Int32 )
// {
// MessageBox(NULL, ch, "0", MB_OK);
// int value = args[i].value.intValue;
// char ch[10]={0};
// itoa( value, ch, 10);
// }
// else if(args[i].type == NPVariantType_String )
// {
// NPString str = args[i].value.stringValue;
// MessageBox(NULL, str.UTF8Characters, "2", MB_OK);
// }
// else if(args[i].type == NPVariantType_Double)
// {
// //MessageBox(NULL, ch, "3", MB_OK);
// }
// else
// {
// MessageBox(NULL, "", "4", MB_OK);
// }
//if( args[i].type == NPVariantType_Int32 )
//{
// sum += args[i].value.intValue;
//}
//else if( args[i].type == NPVariantType_String )
//{
// char* s = new char[args[i].value.stringValue.UTF8Length + 1];
// strcpy(s,args[i].value.stringValue.UTF8Characters);
// sum += atoi( s );
//}
//else
//{
// return false;//an error happenend
//}
}
ret_val = true;
//STRINGZ_TO_NPVARIANT(strdup("foo return val"), *result);
//TODO 书写helloworld
//const char* outString = "zhsl2007 demo string.";
// char* npOutString = (char *)npnfuncs->memalloc(strlen(outString) + 1);
// if (!npOutString)
// return false;
// strcpy(npOutString, outString);
// STRINGZ_TO_NPVARIANT(npOutString, *result);
//return true;
//INT32_TO_NPVARIANT( 22,*result);
}
else if(!strcmp(name, kSubtract))
{
ret_val = true;
// Get window object.
NPObject* window = NULL;
NPN_GetValue(thisObj->npp, NPNVWindowNPObject, &window);
//Get NPVariant
NPVariant arrayVar;
char buf[100]="Array()";
NPString str;
str.UTF8Characters = buf;
str.UTF8Length = strlen(buf);
NPN_Evaluate(thisObj->npp, window, &str, &arrayVar);
int lRet=10;
NPVariant v[2];
INT32_TO_NPVARIANT(lRet,v[0]);
NPVariant res;
NPVariant res1;
INT32_TO_NPVARIANT(lRet,v[1]);
NPN_ReleaseVariantValue(&res);
NPN_ReleaseVariantValue(&res1);
NPN_SetProperty(thisObj->npp, arrayVar.value.objectValue, NPN_GetIntIdentifier(0), &v[0]);
NPN_SetProperty(thisObj->npp, arrayVar.value.objectValue, NPN_GetIntIdentifier(1), &v[1]);
OBJECT_TO_NPVARIANT(arrayVar.value.objectValue,*result);
//OBJECT_TO_NPVARIANT( 22,*result);
}
//else if(!strcmp(name, kMultiplication))
//{
//}
//else if(!strcmp(name, kDivision))
//{
//}
//else
//{
// // Exception handling.
// npnfuncs->setexception(obj, "Unknown method");
//}
npnfuncs->memfree(name);
return ret_val;
}
bool ScriptablePluginObject::HasProperty(NPObject* obj, NPIdentifier propertyName) {
return false;
}
bool ScriptablePluginObject::GetProperty(NPObject* obj, NPIdentifier propertyName,
NPVariant* result) {
return false;
}
CPlugin::CPlugin(NPP pNPInstance):m_pNPInstance(pNPInstance),
m_bInitialized(false),
m_pScriptableObject(NULL)
{
#ifdef _WINDOWS
m_hWnd = NULL;
#endif
}
CPlugin::~CPlugin(void)
{
}
NPBool CPlugin::init(NPWindow* pNPWindow) {
if(pNPWindow == NULL)
return false;
#ifdef _WINDOWS
m_hWnd = (HWND)pNPWindow->window;
if(m_hWnd == NULL)
return false;
#endif
m_Window = pNPWindow;
m_bInitialized = true;
return true;
}
NPBool CPlugin::isInitialized() {
return m_bInitialized;
}
ScriptablePluginObject * CPlugin::GetScriptableObject() {
if (!m_pScriptableObject) {
m_pScriptableObject = (ScriptablePluginObject*)npnfuncs->createobject(m_pNPInstance, &plugin_ref_obj);
// Retain the object since we keep it in plugin code
// so that it won't be freed by browser.
npnfuncs->retainobject((NPObject*)m_pScriptableObject);
}
return m_pScriptableObject;
}
#ifdef _WINDOWS
HWND CPlugin::GetHWnd() {
return m_hWnd;
}
#endif