bool convert_js_to_mrb_array(NPP npp, const NPVariant &variant, mrb_state *mrb, mrb_value *result) { NPVariant length; NPN_GetProperty(npp, NPVARIANT_TO_OBJECT(variant), NPN_GetStringIdentifier("length"), &length); int len; if (NPVARIANT_IS_INT32(length)){ len = NPVARIANT_TO_INT32(length); }else if (NPVARIANT_IS_DOUBLE(length)){ len = static_cast< int >(NPVARIANT_TO_DOUBLE(length)); // Why double?? }else{ NPN_ReleaseVariantValue(&length); return false; } NPN_ReleaseVariantValue(&length); mrb_value ret_array = mrb_ary_new_capa(mrb, len); for (int32_t i=0; i<len; i++){ NPVariant item; std::ostringstream oss; oss << i; NPN_GetProperty(npp, NPVARIANT_TO_OBJECT(variant), NPN_GetStringIdentifier(oss.str().c_str()), &item); mrb_value mrb_item; if (!convert_js_to_mrb(npp, item, mrb, &mrb_item)){ NPN_ReleaseVariantValue(&item); return false; } NPN_ReleaseVariantValue(&item); mrb_ary_push(mrb, ret_array, mrb_item); } *result = ret_array; return true; }
// XXX FIXME See comment at the other parseOptions variant. void LibvlcPlaylistNPObject::parseOptions(NPObject *obj, int *i_options, char*** ppsz_options) { /* WARNING: Safari does not implement NPN_HasProperty/NPN_HasMethod */ NPVariant value; /* we are expecting to have a Javascript Array object */ NPIdentifier propId = NPN_GetStringIdentifier("length"); if( NPN_GetProperty(_instance, obj, propId, &value) ) { int count = numberValue(value); NPN_ReleaseVariantValue(&value); if( count ) { long capacity = 16; char **options = (char **)malloc(capacity*sizeof(char *)); if( options ) { int nOptions = 0; while( nOptions < count ) { propId = NPN_GetIntIdentifier(nOptions); if( ! NPN_GetProperty(_instance, obj, propId, &value) ) /* return what we got so far */ break; if( ! NPVARIANT_IS_STRING(value) ) { /* return what we got so far */ NPN_ReleaseVariantValue(&value); break; } if( nOptions == capacity ) { capacity += 16; char **moreOptions = (char **)realloc(options, capacity*sizeof(char*)); if( ! moreOptions ) { /* failed to allocate more memory */ NPN_ReleaseVariantValue(&value); /* return what we got so far */ *i_options = nOptions; *ppsz_options = options; break; } options = moreOptions; } options[nOptions++] = stringValue(value); } *i_options = nOptions; *ppsz_options = options; } } } }
// 获取Plugin所在页面的URL CString CPlugin::GetHostURL() const { CString url; BOOL bOK = FALSE; NPObject* pWindow = NULL; NPVariant vLocation; VOID_TO_NPVARIANT(vLocation); NPVariant vHref; VOID_TO_NPVARIANT(vHref); try { if (( NPN_GetValue( m_pNPInstance, NPNVWindowNPObject, &pWindow) != NPERR_NO_ERROR ) || !pWindow ) { throw(CString(_T("Cannot get window"))); } if ((!NPN_GetProperty( m_pNPInstance, pWindow, NPN_GetStringIdentifier ("location"), &vLocation)) || !NPVARIANT_IS_OBJECT (vLocation)) { throw(CString(_T("Cannot get window.location"))); } if ((!NPN_GetProperty( m_pNPInstance, NPVARIANT_TO_OBJECT(vLocation), NPN_GetStringIdentifier ("href"), &vHref)) || !NPVARIANT_IS_STRING(vHref)) { throw(CString(_T("Cannot get window.location.href"))); } // 转换window.location.href的编码 int buffer_size = vHref.value.stringValue.UTF8Length + 1; char* szUnescaped = new char[buffer_size]; DWORD dwSize = buffer_size; if (SUCCEEDED(UrlUnescapeA(const_cast<LPSTR>(vHref.value.stringValue.UTF8Characters), szUnescaped, &dwSize, 0))) { WCHAR* szURL = new WCHAR[dwSize + 1]; if (MultiByteToWideChar(CP_UTF8, 0, szUnescaped, -1, szURL, dwSize + 1) > 0) { url = CW2T(szURL); } delete[] szURL; } delete[] szUnescaped; } catch (CString strMessage) { TRACE(_T("[CPlugin::GetHostURL Exception] %s"), strMessage); } if (!NPVARIANT_IS_VOID(vHref)) NPN_ReleaseVariantValue(&vHref); if (!NPVARIANT_IS_VOID(vLocation)) NPN_ReleaseVariantValue(&vLocation); if (pWindow != NULL) NPN_ReleaseObject(pWindow); return url; }
bool ScriptablePluginObject::Invoke(NPIdentifier name, const NPVariant *args, uint32_t argCount, NPVariant *result) { if (name == sFoo_id) { printf ("foo called!\n"); NPVariant docv; NPN_GetProperty(mNpp, sWindowObj, sDocument_id, &docv); NPObject *doc = NPVARIANT_TO_OBJECT(docv); NPVariant strv; STRINGZ_TO_NPVARIANT("div", strv); NPVariant divv; NPN_Invoke(mNpp, doc, sCreateElement_id, &strv, 1, &divv); STRINGZ_TO_NPVARIANT("I'm created by a plugin!", strv); NPVariant textv; NPN_Invoke(mNpp, doc, sCreateTextNode_id, &strv, 1, &textv); NPVariant v; NPN_Invoke(mNpp, NPVARIANT_TO_OBJECT(divv), sAppendChild_id, &textv, 1, &v); NPN_ReleaseVariantValue(&v); NPN_ReleaseVariantValue(&textv); NPVariant bodyv; NPN_GetProperty(mNpp, doc, sBody_id, &bodyv); NPN_Invoke(mNpp, NPVARIANT_TO_OBJECT(bodyv), sAppendChild_id, &divv, 1, &v); NPN_ReleaseVariantValue(&v); NPN_ReleaseVariantValue(&divv); NPN_ReleaseVariantValue(&bodyv); NPN_ReleaseVariantValue(&docv); STRINGZ_TO_NPVARIANT(strdup("foo return val"), *result); return PR_TRUE; } return PR_FALSE; }
bool convert_js_to_mrb_hash(NPP npp, const NPVariant &variant, mrb_state *mrb, mrb_value *result) { NPIdentifier *id_list = NULL; uint32_t count; NPN_Enumerate(npp, NPVARIANT_TO_OBJECT(variant), &id_list, &count); mrb_value ret_hash = mrb_hash_new_capa(mrb, count); for (uint32_t i=0; i<count; i++){ NPVariant item; NPN_GetProperty(npp, NPVARIANT_TO_OBJECT(variant), id_list[i], &item); mrb_value mrb_item; if (!convert_js_to_mrb(npp, item, mrb, &mrb_item)){ NPN_MemFree(id_list); NPN_ReleaseVariantValue(&item); return false; } NPN_ReleaseVariantValue(&item); NPUTF8 *key = NPN_UTF8FromIdentifier(id_list[i]); mrb_hash_set(mrb, ret_hash, mrb_str_new2(mrb, key), mrb_item); NPN_MemFree(key); } NPN_MemFree(id_list); *result = ret_hash; return true; }
NPObject* CPlugin::GetWindowPropertyObject(const NPUTF8* szPropertyName) const { NPObject* pWindow = GetWindow(); NPVariant vObject; VOID_TO_NPVARIANT(vObject); if ((!NPN_GetProperty(m_pNPInstance, pWindow, GetIdentifier(szPropertyName), &vObject)) || !NPVARIANT_IS_OBJECT(vObject)) { if (!NPVARIANT_IS_VOID(vObject)) NPN_ReleaseVariantValue(&vObject); throw CString(_T("Cannot get window.")) + NPStringCharactersToCString(szPropertyName); } NPObject* pObject = NPVARIANT_TO_OBJECT(vObject); if (!pObject) { NPN_ReleaseVariantValue(&vObject); throw CString(_T("window.")) + NPStringCharactersToCString(szPropertyName) + _T(" is null"); } NPN_RetainObject(pObject); NPN_ReleaseVariantValue(&vObject); return pObject; }
// 清空IECtrl::Navigate的参数 void CPlugin::RemoveNavigateParams() { NPObject* pWindow = NULL; NPVariant vCOBAContainer; VOID_TO_NPVARIANT(vCOBAContainer); NPVariant vResult; VOID_TO_NPVARIANT(vResult); try { if ((NPN_GetValue( m_pNPInstance, NPNVWindowNPObject, &pWindow) != NPERR_NO_ERROR ) || !pWindow ) { throw(CString(_T("Cannot get window"))); } if ((!NPN_GetProperty( m_pNPInstance, pWindow, NPN_GetStringIdentifier ("COBAContainer"), &vCOBAContainer)) || !NPVARIANT_IS_OBJECT (vCOBAContainer)) { throw(CString(_T("Cannot get window.COBAContainer"))); } if (!NPN_Invoke(m_pNPInstance, NPVARIANT_TO_OBJECT(vCOBAContainer), NPN_GetStringIdentifier("removeNavigateParams"), NULL, 0, &vResult)) { throw(CString(_T("Cannot execute window.COBAContainer.removeNavigateParams()"))); } } catch (CString strMessage) { TRACE(_T("[CPlugin::RemoveNavigateParams Exception] %s"), strMessage); } if (!NPVARIANT_IS_VOID(vResult)) NPN_ReleaseVariantValue(&vResult); if (!NPVARIANT_IS_VOID(vCOBAContainer)) NPN_ReleaseVariantValue(&vCOBAContainer); if (pWindow != NULL) NPN_ReleaseObject(pWindow); }
bool JsAgent::onHandleReturn(const Variant& varRet) { if(!varRet.isObject()) { Debug::println("JsAgent::onHandleReturn> NPN_Invoke did not return an object"); return false; } NPObject* pReturn = varRet.asObject(); Variant varFlag; if(!NPN_GetProperty(getNPP(), pReturn, methods.index0, varFlag.ptr())) { Debug::println("JsAgent::onHandleReturn> ret[0] failed"); return false; } Variant varResult; if(!NPN_GetProperty(getNPP(), pReturn, methods.index1, varResult.ptr())) { Debug::println("JsAgent::onHandleReturn> ret[1] failed"); return false; } if(!varFlag.isBool()) { Debug::println("JsAgent::onHandleReturn> flag was not bool"); return false; } ReturnMessage retMsg; retMsg.isException = varFlag.asBool(); std::string strRet = varResult.toString(getNPP()); if(retMsg.isException) { Debug::println("JsAgent::onHandleReturn> Exception: '%s'", strRet.c_str()); NPN_SetException(this, strRet.c_str()); } else { Debug::println("JsAgent::onHandleReturn> returned: %s", strRet.c_str()); } if(!wrapLocalValue(varResult, retMsg.value)) { Debug::println("JsAgent::onHandleReturn> wrapLocalValue() failed"); return false; } ArchiveWriter<Channel> ar(*m_channel); if(!Message::send(retMsg, ar)) { Debug::println("JsAgent::onHandleReturn> Message::send() failed"); return false; } return true; }
bool convert_js_to_mrb_object(NPP npp, const NPVariant &variant, mrb_state *mrb, mrb_value *result) { if (NPVARIANT_IS_MRB_JS_OBJECT(variant)){ MrbJsObject *obj = static_cast< MrbJsObject* >(NPVARIANT_TO_OBJECT(variant)); *result = obj->Value(); return true; } NPObject *window; NPN_GetValue(npp, NPNVWindowNPObject, &window); NPVariant object; NPN_GetProperty(npp, window, NPN_GetStringIdentifier("Object"), &object); NPN_ReleaseObject(window); NPVariant prototype; NPN_GetProperty(npp, NPVARIANT_TO_OBJECT(object), NPN_GetStringIdentifier("prototype"), &prototype); NPN_ReleaseVariantValue(&object); NPVariant to_string; NPN_GetProperty(npp, NPVARIANT_TO_OBJECT(prototype), NPN_GetStringIdentifier("toString"), &to_string); NPN_ReleaseVariantValue(&prototype); NPVariant type_string; NPN_Invoke(npp, NPVARIANT_TO_OBJECT(to_string), NPN_GetStringIdentifier("call"), &variant, 1, &type_string); NPN_ReleaseVariantValue(&to_string); if (!NPVARIANT_IS_STRING(type_string)){ NPN_ReleaseVariantValue(&type_string); return false; } std::string type(NPVARIANT_TO_STRING(type_string).UTF8Characters, NPVARIANT_TO_STRING(type_string).UTF8Length); NPN_ReleaseVariantValue(&type_string); if (type == "[object Array]"){ return convert_js_to_mrb_array(npp, variant, mrb, result); }else if (type == "[object Object]"){ return convert_js_to_mrb_hash(npp, variant, mrb, result); }else if (type == "[object Function]"){ return convert_js_to_mrb_function(npp, variant, mrb, result); }else{ *result = mrb_js_obj_class_new(mrb, NPVARIANT_TO_OBJECT(variant)); return true; } return true; }
std::string getInterfaceName(NPP npp, NPObject* object) { std::string className; NPVariant result; bool asConstructor = true; // true if object can be a constructor VOID_TO_NPVARIANT(result); NPN_Invoke(npp, object, NPN_GetStringIdentifier("toString"), 0, 0, &result); for (;;) { if (NPVARIANT_IS_STRING(result)) { className = std::string(NPVARIANT_TO_STRING(result).utf8characters, NPVARIANT_TO_STRING(result).utf8length); } NPN_ReleaseVariantValue(&result); if (className.compare(0, 9, "function ") == 0) { // In Chrome, a [Constructor] object is represented as a 'Function'. className = className.substr(9); size_t pos = className.find('('); if (pos != std::string::npos) { className = className.substr(0, pos); break; } return "Function"; } if (className.compare(0, 8, "[object ", 8) == 0 && className[className.length() - 1] == ']') { className = className.substr(8, className.length() - 9); break; } // This object is likely to have a stringifier. Check the constructor name directly. NPVariant constructor; VOID_TO_NPVARIANT(constructor); if (asConstructor && NPN_GetProperty(npp, object, NPN_GetStringIdentifier("constructor"), &constructor)) { if (NPVARIANT_IS_OBJECT(constructor) && NPN_Invoke(npp, NPVARIANT_TO_OBJECT(constructor), NPN_GetStringIdentifier("toString"), 0, 0, &result)) { NPN_ReleaseVariantValue(&constructor); asConstructor = false; continue; } NPN_ReleaseVariantValue(&constructor); } return "Object"; } // In Firefox, the constructor and an instance object cannot be distinguished by toString(). // Check if object has a 'prototype' to see if it is a constructor. if (asConstructor && NPN_HasProperty(npp, object, NPN_GetStringIdentifier("prototype"))) { className += "_Constructor"; } return className; }
bool JsObjectWrapper::invokeDefault(const NPVariant* args, unsigned argCount, NPVariant* result) { if(argCount < 1) { Debug::println("JsObjectWrapper::invokeDefault> argCount < 1: %d", argCount); return false; } if(!NPVARIANT_IS_OBJECT(args[0])) { Debug::println("JsObjectWrapper::invokeDefault> args[0] not an object"); return false; } NPObject* pArray = NPVARIANT_TO_OBJECT(args[0]); Variant varRet; if(!NPN_GetProperty(getNPP(), pArray, m_agent->methods.length, varRet.ptr())) { Debug::println("JsObjectWrapper::invokeDefault> get length failed"); return false; } if(!NPVARIANT_IS_INT32(varRet.get())) { Debug::println("JsObjectWrapper::invokeDefault> length did not return an int"); return false; } int len = NPVARIANT_TO_INT32(varRet.get()); VariantArray varArgs(len); for(int i = 0; i < len; i++) { NPIdentifier id = NPN_GetIntIdentifier(i); Variant varItem; if(!NPN_GetProperty(getNPP(), pArray, id, varArgs[i].ptr())) { Debug::println("JsObjectWrapper::invokeDefault> get [%d] failed", i); return false; } } Debug::println("JsObjectWrapper::invokeDefault"); if(!m_agent->invokeRemoteDelegate(m_targetId, varArgs.get(), len, result)) { Debug::println("JsObjectWrapper::invokeDefault> invokeRemoteDelegate() failed"); return false; } return true; }
static mrb_value mrb_js_obj_method_missing(mrb_state *mrb, mrb_value self) { NPP npp = MRB_UD_NPP(mrb); mrb_value name_sym, *args; int len; mrb_get_args(mrb, "o*", &name_sym, &args, &len); if (mrb_type(name_sym) != MRB_TT_SYMBOL){ return mrb_nil_value(); } bool success = true; NPVariant empty; NULL_TO_NPVARIANT(empty); std::vector< NPVariant > var_args(len, empty); for (int i=0; i<len; i++){ if (!convert_mrb_to_js(mrb, args[i], npp, &var_args[i])){ success = false; break; } } mrb_value ret = mrb_nil_value(); if (success){ int name_len; const char *name_char = mrb_sym2name_len(mrb, SYM2ID(name_sym), &name_len); std::string name(name_char, name_len); NPIdentifier name_id = NPN_GetStringIdentifier(name.c_str()); NPObject *obj = (NPObject *)DATA_PTR(self); if (NPN_HasMethod(npp, obj, name_id)){ NPVariant result; NPN_Invoke(npp, obj, name_id, &var_args[0], len, &result); convert_js_to_mrb(npp, result, mrb, &ret); }else if (NPN_HasProperty(npp, obj, name_id)){ NPVariant result; NPN_GetProperty(npp, obj, name_id, &result); convert_js_to_mrb(npp, result, mrb, &ret); }else if (name.size() > 1 && name[name.size() - 1] == '=' && var_args.size() == 1){ name.resize(name.size() - 1); name_id = NPN_GetStringIdentifier(name.c_str()); if (NPN_HasProperty(npp, obj, name_id)){ NPN_SetProperty(npp, obj, name_id, &var_args[0]); } } } for (int i=0; i<len; i++){ NPN_ReleaseVariantValue(&var_args[i]); } return ret; }
static gboolean get_element_property_object (NPP npp, NPObject *object, NPIdentifier property, NPVariant *child) { VOID_TO_NPVARIANT (*child); if (!NPN_GetProperty (npp, object, property, child) || !NPVARIANT_IS_OBJECT (*child)) { NPN_ReleaseVariantValue (child); return FALSE; } return TRUE; }
bool ParserWrap::execute(NPVariant buffer,NPVariant start,NPVariant end) { size_t c_start = static_cast<size_t>(start.value.doubleValue), c_end = static_cast<size_t>(end.value.doubleValue); NPVariant result; NPN_GetProperty(m_Instance,buffer.value.objectValue,NPN_GetStringIdentifier("_handle"),&result); BufferWrap* b = (BufferWrap*)result.value.objectValue; http_parser_execute(parser,&http_parser_callbacks,b->data + c_start,c_end - c_start); return true; }
double CPlugin::GetZoomLevel() { double level = 1; NPObject* pWindow = NULL; NPVariant vCOBA; VOID_TO_NPVARIANT(vCOBA); NPVariant vLevel; VOID_TO_NPVARIANT(vLevel); try { if ((NPN_GetValue( m_pNPInstance, NPNVWindowNPObject, &pWindow) != NPERR_NO_ERROR ) || !pWindow ) { throw(CString(_T("Cannot get window"))); } if ((!NPN_GetProperty( m_pNPInstance, pWindow, NPN_GetStringIdentifier ("COBA"), &vCOBA)) || !NPVARIANT_IS_OBJECT (vCOBA)) { throw(CString(_T("Cannot get window.COBA"))); } if (!NPN_Invoke(m_pNPInstance, NPVARIANT_TO_OBJECT(vCOBA), NPN_GetStringIdentifier("getZoomLevel"), NULL, 0, &vLevel)) { throw(CString(_T("Cannot execute window.COBA.getZoomLevel()"))); } if (NPVARIANT_IS_DOUBLE(vLevel)) level = NPVARIANT_TO_DOUBLE(vLevel); else if ( NPVARIANT_IS_INT32(vLevel) ) level = NPVARIANT_TO_INT32(vLevel); } catch (CString strMessage) { level = 1; TRACE(_T("[CPlugin::FireEvent Exception] %s"), strMessage); } if (!NPVARIANT_IS_VOID(vLevel)) NPN_ReleaseVariantValue(&vLevel); if (!NPVARIANT_IS_VOID(vCOBA)) NPN_ReleaseVariantValue(&vCOBA); if (pWindow != NULL) NPN_ReleaseObject(pWindow); return level; }
CString CPlugin::GetNavigateParam(const NPUTF8* name) const { CString strParam; NPObject* pWindow = NULL; NPVariant vCOBAContainer; VOID_TO_NPVARIANT(vCOBAContainer); NPVariant vParam; VOID_TO_NPVARIANT(vParam); try { if ((NPN_GetValue( m_pNPInstance, NPNVWindowNPObject, &pWindow) != NPERR_NO_ERROR ) || !pWindow ) { throw(CString(_T("Cannot get window"))); } if ((!NPN_GetProperty( m_pNPInstance, pWindow, NPN_GetStringIdentifier ("COBAContainer"), &vCOBAContainer)) || !NPVARIANT_IS_OBJECT (vCOBAContainer)) { throw(CString(_T("Cannot get window.COBAContainer"))); } if (!NPN_Invoke(m_pNPInstance, NPVARIANT_TO_OBJECT(vCOBAContainer), NPN_GetStringIdentifier(name), NULL, 0, &vParam)) { throw(CString(_T("Cannot execute window.COBAContainer.getXXX()"))); } if (!NPVARIANT_IS_STRING(vParam)) { throw(CString(_T("Invalid return value."))); } strParam = NPStringToCString(vParam.value.stringValue); } catch (CString strMessage) { TRACE(_T("[CPlugin::GetNavigateHeaders Exception] %s"), strMessage); } if (!NPVARIANT_IS_VOID(vParam)) NPN_ReleaseVariantValue(&vParam); if (!NPVARIANT_IS_VOID(vCOBAContainer)) NPN_ReleaseVariantValue(&vCOBAContainer); if (pWindow != NULL) NPN_ReleaseObject(pWindow); return strParam; }
void DoTest(NPP npp) { NPError err; NPObject *obj; NPVariant result; err = __pluginfunc.getvalue(npp, NPPVpluginScriptableNPObject, &obj); if (err != NPERR_NO_ERROR) return; if (!NPN_GetProperty(npp, obj, __TextData_id, &result)) return; if (NPVARIANT_IS_STRING(result)) { printf("Hello World 1: sizeof(%d)\n", sizeof(result)); } NPN_ReleaseVariantValue(&result); return; }
unordered_map<wstring, wstring> NPObjectToUnorderedMap(NPP npp, NPObject* npobj) { unordered_map<wstring, wstring> result; NPIdentifier* pIdentifiers = NULL; uint32_t nIdentifiers = 0; if (NPN_Enumerate(npp, npobj, &pIdentifiers, &nIdentifiers)) { for (uint32_t i = 0; i < nIdentifiers; i++) { NPIdentifier npid = pIdentifiers[i]; CString idName = NPIdentifierToCString(npid); NPVariant npvValue; if (NPN_GetProperty(npp, npobj, npid, &npvValue)) { if (NPVARIANT_IS_STRING(npvValue)) { result[idName.GetString()] = NPStringToCString(NPVARIANT_TO_STRING(npvValue)).GetString(); } NPN_ReleaseVariantValue(&npvValue); } } NPN_MemFree(pIdentifiers); } return result; }
CPlugin::CPlugin(NPP pNPInstance) : m_pNPInstance(pNPInstance), m_pNPStream(NULL), m_bInitialized(FALSE), m_pScriptableObject(NULL) { NPVariant v; NPVariant rval; NPObject *browserWndObj = NULL; #ifdef XP_WIN m_hWnd = NULL; #endif // setup the display string const char *ua = NPN_UserAgent(m_pNPInstance); strcpy(m_String, ua); if ( NPERR_NO_ERROR == NPN_GetValue(m_pNPInstance, NPNVWindowNPObject, &browserWndObj)) { // this is interesting. // Here we are setting up an instance in JavaScript of the ScriptablePluginObject // by inserting a new instance into the DOM. this will be referenced by document.pluginobj; // NPObject *myobj = NPN_CreateObject(m_pNPInstance, GET_NPOBJECT_CLASS(ScriptablePluginObject)); if ( myobj ) { // create an identifier for the plugin object NPIdentifier n = NPN_GetStringIdentifier("pluginobj"); // now insert into the DOM OBJECT_TO_NPVARIANT(myobj, v); if (NPN_SetProperty(m_pNPInstance, browserWndObj, n, &v)) { // validate the object if ( NPN_GetProperty(m_pNPInstance, browserWndObj, n, &rval) ) { printf ("Object set/get test "); if (NPVARIANT_IS_OBJECT(rval) && NPVARIANT_TO_OBJECT(rval) == myobj) { printf ("succeeded!\n"); } else { printf ("FAILED!\n"); } } else { printf("failed to get pluginobj"); } NPN_ReleaseVariantValue(&rval); } else { printf("failed to set pluginobj"); } NPN_ReleaseObject(myobj); } else { printf("failed to create pluginobj"); } } else { printf("failed to get browser window"); } }
bool ScriptablePluginObject::Invoke(NPIdentifier name, const NPVariant *args, uint32_t argCount, NPVariant *result) { // 添加自定义方法 if (name == sGetVersion) { //NPString str; //str.UTF8Characters = "1.0"; //str.UTF8Length = strlen(str.UTF8Characters); MessageBox(NULL, "test1", "test", MB_OK); //STRINGZ_TO_NPVARIANT(strdup("1.0"), *result); //INT32_TO_NPVARIANT(10, *result); //MessageBox(NULL, "test2", "test", MB_OK); char * version = "1.0"; char* npOutString = (char *)NPN_MemAlloc(strlen(version) + 1); strcpy(npOutString, version); STRINGZ_TO_NPVARIANT(npOutString, *result); return true; } if (name == sFoo_id) { printf ("foo called!\n"); NPVariant docv; NPN_GetProperty(mNpp, sWindowObj, sDocument_id, &docv); NPObject *doc = NPVARIANT_TO_OBJECT(docv); NPVariant strv; STRINGZ_TO_NPVARIANT("div", strv); NPVariant divv; NPN_Invoke(mNpp, doc, sCreateElement_id, &strv, 1, &divv); STRINGZ_TO_NPVARIANT("I'm created by a plugin!", strv); NPVariant textv; NPN_Invoke(mNpp, doc, sCreateTextNode_id, &strv, 1, &textv); NPVariant v; NPN_Invoke(mNpp, NPVARIANT_TO_OBJECT(divv), sAppendChild_id, &textv, 1, &v); NPN_ReleaseVariantValue(&v); NPN_ReleaseVariantValue(&textv); NPVariant bodyv; NPN_GetProperty(mNpp, doc, sBody_id, &bodyv); NPN_Invoke(mNpp, NPVARIANT_TO_OBJECT(bodyv), sAppendChild_id, &divv, 1, &v); NPN_ReleaseVariantValue(&v); NPN_ReleaseVariantValue(&divv); NPN_ReleaseVariantValue(&bodyv); NPN_ReleaseVariantValue(&docv); STRINGZ_TO_NPVARIANT(_strdup("foo return val"), *result); return true; } return false; }
CPlugin::CPlugin(NPP pNPInstance) : m_pNPInstance(pNPInstance), m_pNPStream(NULL), m_bInitialized(false), m_pScriptableObject(NULL) { #ifdef XP_WIN m_hWnd = NULL; #endif NPN_GetValue(m_pNPInstance, NPNVWindowNPObject, &sWindowObj); NPIdentifier n = NPN_GetStringIdentifier("foof"); sFoo_id = NPN_GetStringIdentifier("foo"); sBar_id = NPN_GetStringIdentifier("bar"); sDocument_id = NPN_GetStringIdentifier("document"); sBody_id = NPN_GetStringIdentifier("body"); sCreateElement_id = NPN_GetStringIdentifier("createElement"); sCreateTextNode_id = NPN_GetStringIdentifier("createTextNode"); sAppendChild_id = NPN_GetStringIdentifier("appendChild"); sPluginType_id = NPN_GetStringIdentifier("PluginType"); NPVariant v; INT32_TO_NPVARIANT(46, v); NPN_SetProperty(m_pNPInstance, sWindowObj, n, &v); NPVariant rval; NPN_GetProperty(m_pNPInstance, sWindowObj, n, &rval); if (NPVARIANT_IS_INT32(rval)) { printf("rval = %d\n", NPVARIANT_TO_INT32(rval)); } n = NPN_GetStringIdentifier("document"); if (!NPN_IdentifierIsString(n)) { NPString str; str.UTF8Characters = "alert('NPN_IdentifierIsString() test failed!');"; str.UTF8Length = strlen(str.UTF8Characters); NPN_Evaluate(m_pNPInstance, sWindowObj, &str, NULL); } NPObject *doc; NPN_GetProperty(m_pNPInstance, sWindowObj, n, &rval); if (NPVARIANT_IS_OBJECT(rval) && (doc = NPVARIANT_TO_OBJECT(rval))) { n = NPN_GetStringIdentifier("title"); NPN_GetProperty(m_pNPInstance, doc, n, &rval); if (NPVARIANT_IS_STRING(rval)) { printf ("title = %s\n", NPVARIANT_TO_STRING(rval).UTF8Characters); NPN_ReleaseVariantValue(&rval); } n = NPN_GetStringIdentifier("plugindoc"); OBJECT_TO_NPVARIANT(doc, v); NPN_SetProperty(m_pNPInstance, sWindowObj, n, &v); NPString str; str.UTF8Characters = "document.getElementById('result').innerHTML += '<p>' + 'NPN_Evaluate() test, document = ' + this + '</p>';"; str.UTF8Length = strlen(str.UTF8Characters); //NPN_Evaluate(m_pNPInstance, doc, &str, NULL); NPN_ReleaseObject(doc); } NPVariant barval; NPN_GetProperty(m_pNPInstance, sWindowObj, sBar_id, &barval); NPVariant arg; OBJECT_TO_NPVARIANT(sWindowObj, arg); NPN_InvokeDefault(m_pNPInstance, NPVARIANT_TO_OBJECT(barval), &arg, 1, &rval); if (NPVARIANT_IS_INT32(rval) && NPVARIANT_TO_INT32(rval) == 4) { printf ("Default function call SUCCEEDED!\n"); } else { printf ("Default function call FAILED!\n"); } NPN_ReleaseVariantValue(&barval); NPN_ReleaseVariantValue(&rval); #if 0 n = NPN_GetStringIdentifier("prompt"); NPVariant vars[3]; STRINGZ_TO_NPVARIANT("foo", vars[0]); STRINGZ_TO_NPVARIANT("bar", vars[1]); STRINGZ_TO_NPVARIANT("foof", vars[2]); NPN_Invoke(sWindowObj, n, vars, 3, &rval); if (NPVARIANT_IS_STRING(rval)) { printf ("prompt returned '%s'\n", NPVARIANT_TO_STRING(rval).UTF8Characters); } NPN_ReleaseVariantValue(&rval); #endif NPObject *myobj = NPN_CreateObject(m_pNPInstance, GET_NPOBJECT_CLASS(ScriptablePluginObject)); n = NPN_GetStringIdentifier("pluginobj"); OBJECT_TO_NPVARIANT(myobj, v); NPN_SetProperty(m_pNPInstance, sWindowObj, n, &v); NPN_GetProperty(m_pNPInstance, sWindowObj, n, &rval); printf ("Object set/get test "); if (NPVARIANT_IS_OBJECT(rval) && NPVARIANT_TO_OBJECT(rval) == myobj) { printf ("succeeded!\n"); } else { printf ("FAILED!\n"); } NPN_ReleaseVariantValue(&rval); NPN_ReleaseObject(myobj); const char *ua = NPN_UserAgent(m_pNPInstance); strcpy(m_String, ua); }
bool ScriptablePluginObject::Invoke(NPIdentifier name, const NPVariant *args, uint32_t argCount, NPVariant *result) { if (name == sFoo_id) { printf ("foo called!\n"); NPIdentifier sAlert = NPN_GetStringIdentifier("alert"); NPVariant alertValue; NPVariant returnValue; STRINGZ_TO_NPVARIANT("hello world",alertValue); NPN_Invoke(mNpp,sWindowObj,sAlert,&alertValue,1,&returnValue); //FFHelper* ffhelper = new FFHelper(); //ffhelper->Invoke(mNpp,sWindowObj,"alert",&alertValue,1,&returnValue); //ffhelper->GetVersion(); //char* p7SignedData = ffhelper->P7Sign("","123"); //MessageBox(NULL,_T(p7SignedData),_T("Information"),MB_OK); //NPVariant npp7SignedData; //STRINGZ_TO_NPVARIANT(p7SignedData, npp7SignedData); //ffhelper->Invoke(mNpp,sWindowObj,"alert",&npp7SignedData,1,&returnValue); NPN_ReleaseVariantValue(&returnValue); NPVariant docv; NPN_GetProperty(mNpp, sWindowObj, sDocument_id, &docv); NPObject *doc = NPVARIANT_TO_OBJECT(docv); NPVariant strv; STRINGZ_TO_NPVARIANT("div", strv); NPVariant divv; NPN_Invoke(mNpp, doc, sCreateElement_id, &strv, 1, &divv); STRINGZ_TO_NPVARIANT("I'm created by a plugin!", strv); NPVariant textv; NPN_Invoke(mNpp, doc, sCreateTextNode_id, &strv, 1, &textv); NPVariant v; NPN_Invoke(mNpp, NPVARIANT_TO_OBJECT(divv), sAppendChild_id, &textv, 1, &v); NPN_ReleaseVariantValue(&v); NPN_ReleaseVariantValue(&textv); NPVariant bodyv; NPN_GetProperty(mNpp, doc, sBody_id, &bodyv); NPN_Invoke(mNpp, NPVARIANT_TO_OBJECT(bodyv), sAppendChild_id, &divv, 1, &v); NPN_ReleaseVariantValue(&v); NPN_ReleaseVariantValue(&divv); NPN_ReleaseVariantValue(&bodyv); NPN_ReleaseVariantValue(&docv); const char* outString = "foo return val"; char* npOutString = (char *)NPN_MemAlloc(strlen(outString) + 1); if (!npOutString) return false; strcpy(npOutString, outString); STRINGZ_TO_NPVARIANT(npOutString, *result); return true; } return false; }
// This function is equivalent to the following JavaScript function: // function FireEvent(strEventType, strDetail) { // var event = document.createEvent("CustomEvent"); // event.initCustomEvent(strEventType, true, true, strDetail); // pluginObject.dispatchEvent(event); // } // // Uses following JavaScript code to listen to the event fired: // pluginObject.addEventListener(strEventType, function(event) { // alert(event.detail); // } BOOL CPlugin::FireEvent(const CString &strEventType, const CString &strDetail) { BOOL bOK = FALSE; NPObject* pWindow = NULL; NPVariant vDocument; VOID_TO_NPVARIANT(vDocument); NPVariant vEvent; NPObject* pDocument = NULL; VOID_TO_NPVARIANT(vEvent); NPObject *pEvent = NULL; NPObject* pPlugin = NULL; try { // get window object if (NPN_GetValue(m_pNPInstance, NPNVWindowNPObject, &pWindow) != NPERR_NO_ERROR || pWindow == NULL) { throw CString(_T("Cannot get window")); } // get window.document bOK = NPN_GetProperty(m_pNPInstance, pWindow, NPN_GetStringIdentifier("document"), &vDocument); if (!NPVARIANT_IS_OBJECT(vDocument) || !bOK) { throw CString(_T("Cannot get window.document")); } pDocument = NPVARIANT_TO_OBJECT(vDocument); // var event = document.createEvent("CustomEvent"); if (pDocument) { NPVariant arg; STRINGZ_TO_NPVARIANT(CStringToNPStringCharacters(_T("CustomEvent")), arg); bOK = NPN_Invoke(m_pNPInstance, pDocument, NPN_GetStringIdentifier("createEvent"), &arg, 1, &vEvent); NPN_ReleaseVariantValue(&arg); if (!NPVARIANT_IS_OBJECT(vEvent) || !bOK) { throw CString(_T("Cannot document.createEvent")); } } else { throw CString(_T("window.document is null")); } pEvent = NPVARIANT_TO_OBJECT(vEvent);; // event.initCustomEvent(strEventType, true, true, strDetail); if (pEvent) { NPVariant args[4]; STRINGZ_TO_NPVARIANT(CStringToNPStringCharacters(strEventType), args[0]); BOOLEAN_TO_NPVARIANT(true, args[1]); BOOLEAN_TO_NPVARIANT(true, args[2]); STRINGZ_TO_NPVARIANT(CStringToNPStringCharacters(strDetail), args[3]); NPVariant vResult; bOK = NPN_Invoke(m_pNPInstance, pEvent, NPN_GetStringIdentifier("initCustomEvent"), args, 4, &vResult); for (int i=0; i<4; i++) { NPN_ReleaseVariantValue(&args[i]); } NPN_ReleaseVariantValue(&vResult); if (!bOK) { throw CString(_T("Cannot event.initCustomEvent")); } } else { throw CString(_T("event is null")); } // get plugin object if (NPN_GetValue(m_pNPInstance, NPNVPluginElementNPObject, &pPlugin) != NPERR_NO_ERROR || pPlugin == NULL) { throw CString(_T("Cannot get window")); } // pluginObject.dispatchEvent(event); NPVariant vNotCanceled; bOK = NPN_Invoke(m_pNPInstance, pPlugin, NPN_GetStringIdentifier("dispatchEvent"), &vEvent, 1, &vNotCanceled); NPN_ReleaseVariantValue(&vEvent); if (!bOK || !NPVARIANT_IS_BOOLEAN(vNotCanceled)) { throw CString(_T("Cannot dispatchEvent")); } if (NPVARIANT_TO_BOOLEAN(vNotCanceled) != true) { throw CString(_T("Event is canceled")); } } catch (CString strMessage) { TRACE(_T("[CPlugin::FireEvent Exception] %s"), strMessage); bOK = FALSE; } if (pPlugin != NULL) NPN_ReleaseObject(pPlugin); if (!NPVARIANT_IS_VOID(vEvent)) NPN_ReleaseVariantValue(&vEvent); if (!NPVARIANT_IS_VOID(vDocument)) NPN_ReleaseVariantValue(&vDocument); if (pWindow != NULL) NPN_ReleaseObject(pWindow); return bOK; }
CPlugin::CPlugin(NPP pNPInstance) : m_pNPInstance(pNPInstance), m_pNPStream(NULL), m_bInitialized(FALSE), m_pScriptableObject(NULL) { #ifdef XP_WIN m_hWnd = NULL; #endif NPN_GetValue(m_pNPInstance, NPNVWindowNPObject, &sWindowObj); NPIdentifier n = NPN_GetStringIdentifier("foof"); sendCmd_id = NPN_GetStringIdentifier("sendCmd"); NPVariant v; INT32_TO_NPVARIANT(46, v); NPN_SetProperty(m_pNPInstance, sWindowObj, n, &v); n = NPN_GetStringIdentifier("document"); NPVariant arg; OBJECT_TO_NPVARIANT(sWindowObj, arg); NPObject *myobj = NPN_CreateObject(m_pNPInstance, GET_NPOBJECT_CLASS(ScriptablePluginObject)); n = NPN_GetStringIdentifier("objJSExt"); OBJECT_TO_NPVARIANT(myobj, v); NPN_SetProperty(m_pNPInstance, sWindowObj, n, &v); NPN_ReleaseObject(myobj); const char *ua = NPN_UserAgent(m_pNPInstance); strcpy(m_String, ua); // Create a "location" identifier. NPIdentifier identifier = NPN_GetStringIdentifier( "location" ); // Declare a local variant value. NPVariant variantValue; // Get the location property from the window object (which is another object). bool b1 = NPN_GetProperty( m_pNPInstance, sWindowObj, identifier, &variantValue ); // Get a pointer to the "location" object. NPObject *locationObj = variantValue.value.objectValue; // Create a "href" identifier. identifier = NPN_GetStringIdentifier( "href" ); // Get the location property from the location object. bool b2 = NPN_GetProperty( m_pNPInstance, locationObj, identifier, &variantValue ); g_strPageURL = ""; NPString npStr = NPVARIANT_TO_STRING( variantValue ); for (unsigned int i=0; i<npStr.utf8length; i++) { g_strPageURL += npStr.utf8characters[ i ]; } NPN_ReleaseObject(locationObj); }
bool TCPWrap::Invoke(NPIdentifier name, const NPVariant *args, uint32_t argCount, NPVariant *result) { if(name == onconnection_cb) { apply(onconnection_callback,args,argCount,result); return true; } if(name == onread_cb) { apply(onread_callback,args,argCount,result); return true; } if(name == onshutdown_cb) { apply(onshutdown_callback,args,argCount,result); return true; } InvokeParams* params = new InvokeParams; params->name = name; params->object = this; params->args = new NPVariant[argCount]; params->argCount = argCount; for(uint32_t q = 0; q < argCount; q++) { params->args[q] = *CopyNPVariant(&args[q]); } // if there is a write request a reference must be returned to javascript so it has // to be done before the rest of the work is handed off to the worker thread if(name == write_func) { argCount++; WriteReq* w = (WriteReq*)NPN_CreateObject(m_Instance,&WriteReq::_npclass); // when we get a buffer we have to find the SlowBuffer._handle to access its data if(params->args[0].type == NPVariantType_Object) { // we dont want to delete the buffer because it's a SlowBuffer and might be accessed later // via a Buffer w->retainBuffer = true; NPVariant offset, length, parent, handle; // buffer.offset NPN_GetProperty(m_Instance,params->args[0].value.objectValue,NPN_GetStringIdentifier("offset"),&offset); // buffer.length NPN_GetProperty(m_Instance,params->args[0].value.objectValue,NPN_GetStringIdentifier("length"),&length); // buffer.parent NPN_GetProperty(m_Instance,params->args[0].value.objectValue,NPN_GetStringIdentifier("parent"),&parent); // buffer.parent._handle NPN_GetProperty(m_Instance,parent.value.objectValue,NPN_GetStringIdentifier("_handle"),&handle); params->args[0].type = NPVariantType_String; params->args[0].value.stringValue.UTF8Length = static_cast<uint32_t>(length.value.doubleValue); params->args[0].value.stringValue.UTF8Characters = ((BufferWrap*)handle.value.objectValue)->data + (uint16_t)offset.value.doubleValue; } w->init(this,args[1],args[2]); NPN_RetainObject((NPObject*)w); params->returnObject = (NPObject*)w; //return the write request result->type = NPVariantType_Object; result->value.objectValue = params->returnObject; } if(name == shutdown_func) { ShutdownReq* w = (ShutdownReq*)NPN_CreateObject(m_Instance,&ShutdownReq::_npclass); w->init(this, args[0]); NPN_RetainObject((NPObject*)w); params->returnObject = (NPObject *)w; //return the shutdown request result->type = NPVariantType_Object; result->value.objectValue = params->returnObject; } invoke_queue.push(params); //wake the worker thread uv_async_send(&TCPWrap::async_handle); return true; }
bool ScriptablePluginObject::Invoke(NPIdentifier name, const NPVariant *args, uint32_t argCount, NPVariant *result) { //kk char *pFunc = NPN_UTF8FromIdentifier(name); if( !strcmp( "Add", pFunc ) ) { int sum = 0; for( unsigned int i = 0; i < argCount; i++ ) { if( args[i].type == NPVariantType_Int32 ) { sum += args[i].value.intValue; } else if( args[i].type == NPVariantType_String ) { CNPString s(args[i].value.stringValue); sum += atoi( s ); } else return false;//an error happenend } //value for GUI output sprintf( m_szTextGui, "Sum = %ld", sum ); //triggering ::InvalidateRect( m_hWnd, 0, true ); //nice and handy little helpers, there are more of it INT32_TO_NPVARIANT( sum,*result); return true; } if (name == sFoo_id) { printf ("foo called!\n"); NPVariant docv; NPN_GetProperty(mNpp, sWindowObj, sDocument_id, &docv); NPObject *doc = NPVARIANT_TO_OBJECT(docv); NPVariant strv; STRINGZ_TO_NPVARIANT("div", strv); NPVariant divv; NPN_Invoke(mNpp, doc, sCreateElement_id, &strv, 1, &divv); STRINGZ_TO_NPVARIANT("I'm created by a plugin!", strv); NPVariant textv; NPN_Invoke(mNpp, doc, sCreateTextNode_id, &strv, 1, &textv); NPVariant v; NPN_Invoke(mNpp, NPVARIANT_TO_OBJECT(divv), sAppendChild_id, &textv, 1, &v); NPN_ReleaseVariantValue(&v); NPN_ReleaseVariantValue(&textv); NPVariant bodyv; NPN_GetProperty(mNpp, doc, sBody_id, &bodyv); NPN_Invoke(mNpp, NPVARIANT_TO_OBJECT(bodyv), sAppendChild_id, &divv, 1, &v); NPN_ReleaseVariantValue(&v); NPN_ReleaseVariantValue(&divv); NPN_ReleaseVariantValue(&bodyv); NPN_ReleaseVariantValue(&docv); STRINGZ_TO_NPVARIANT(strdup("foo return val"), *result); return true; } return false; }