Exemple #1
0
bool convert_mrb_to_js_array(mrb_state *mrb, mrb_value value, NPP npp, NPVariant *result)
{
    NPObject *window;
    NPN_GetValue(npp, NPNVWindowNPObject, &window);

    NPVariant array;
    NPN_Invoke(npp, window, NPN_GetStringIdentifier("Array"), NULL, 0, &array);
    NPN_ReleaseObject(window);

    NPIdentifier push = NPN_GetStringIdentifier("push");
    mrb_value *ptr = RARRAY_PTR(value);
    for (int i=0, len=RARRAY_LEN(value); i<len; i++){
        NPVariant var;
        if (!convert_mrb_to_js(mrb, ptr[i], npp, &var)){
            NPN_ReleaseVariantValue(&array);
            return false;
        }
        NPVariant ret;
        NPN_Invoke(npp, NPVARIANT_TO_OBJECT(array), push, &var, 1, &ret);
        NPN_ReleaseVariantValue(&ret);
    }
    *result = array;

    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;
}
Exemple #3
0
 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;
 }
Exemple #4
0
	double CPlugin::GetZoomLevel()
	{
		double level = 1;

		NPVariant vLevel;
		VOID_TO_NPVARIANT(vLevel);

		try
		{
			NPObject* pContainer = GetContainer();

			if (!NPN_Invoke(m_pNPInstance, pContainer, GetIdentifier("getZoomLevel"), NULL, 0, &vLevel))
			{
				throw CString(_T("Cannot invoke window.Container.getZoomLevel()"));
			}
			if (NPVARIANT_IS_DOUBLE(vLevel)) 
				level = NPVARIANT_TO_DOUBLE(vLevel);
			else if ( NPVARIANT_IS_INT32(vLevel) ) 
				level = NPVARIANT_TO_INT32(vLevel);
		}
		catch (const CString& strMessage)
		{
			level = 1;
			UNUSED(strMessage);
			TRACE(_T("[CPlugin::GetZoomLevel Exception] %s\n"), strMessage);
		}

		if (!NPVARIANT_IS_VOID(vLevel))	NPN_ReleaseVariantValue(&vLevel);

		return level;
	}
Exemple #5
0
	CString CPlugin::GetNavigateParam(const NPUTF8* name) const
	{
		CString strParam;

		NPVariant vParam;
		VOID_TO_NPVARIANT(vParam);

		try
		{
			NPObject* pContainer = GetContainer();

			if (!NPN_Invoke(m_pNPInstance, pContainer, GetIdentifier(name), NULL, 0, &vParam))
			{
				throw(CString(_T("Cannot invoke window.Container.getXXX()")));
			}
			if (!NPVARIANT_IS_STRING(vParam)) 
			{
				throw(CString(_T("Invalid return value.")));
			}
			strParam = NPStringToCString(vParam.value.stringValue);
		}
		catch (const CString& strMessage)
		{
			UNUSED(strMessage);
			TRACE(_T("[CPlugin::GetNavigateHeaders Exception] %s\n"), strMessage);
		}

		if (!NPVARIANT_IS_VOID(vParam))	NPN_ReleaseVariantValue(&vParam);

		return strParam;
	}
Exemple #6
0
bool JsAgent::onInvokeFunction(const InvokeFunctionMessage& msg) {
	Debug::println("JsAgent::onInvokeFunction: %s", msg.name.c_str());

	VariantArray args(msg.args.size() + 2);
	int i = 0;
	args[i++].set(msg.name);

	int scopeId = msg.scopeId;
	if(scopeId != 0) {
		NPObject* obj = getLocalObject(scopeId);
		if(obj == NULL) {
			Debug::println("JsAgent::onInvokeFunction> getLocalObject() returned NULL");
			return false;
		}
		args[i++].set(obj);
	} 
	else {
		args[i++].setNull();
	}

	std::list<JsValue>::const_iterator it = msg.args.begin();
	std::list<JsValue>::const_iterator itEnd = msg.args.end();
	for(; it != itEnd; ++it) {
		const JsValue& value = *it;
		wrapRemoteValue(value, args[i++]);
	}

	Variant varRet;
	if(!NPN_Invoke(getNPP(), m_helper, methods.invokeFunction,  args.get(), args.size(), varRet.ptr())) {
		Debug::println("JsAgent::onInvokeFunction> NPN_Invoke failed");
		return false;
	}

	return onHandleReturn(varRet);
}
Exemple #7
0
bool JsAgent::onInvokeDelegate(const InvokeDelegateMessage& msg) {
	Debug::println("JsAgent::onInvokeDelegate: %d", msg.targetId);
	VariantArray args(msg.args.size() + 1);

	NPObject* obj = getLocalObject(msg.targetId);
	if(obj == NULL) {
		Debug::println("JsAgent::onInvokeDelegate> getLocalObject() returned NULL");
		return false;
	}

	int i = 0;
	args[i++].set(obj);

	std::list<JsValue>::const_iterator it = msg.args.begin();
	std::list<JsValue>::const_iterator itEnd = msg.args.end();
	for(; it != itEnd; ++it) {
		const JsValue& value = *it;
		wrapRemoteValue(value, args[i++]);
	}

	Variant varRet;
	if(!NPN_Invoke(getNPP(), m_helper, methods.invokeDelegate, args.get(), args.size(), varRet.ptr())) {
		Debug::println("JsAgent::onInvokeDelegate> NPN_Invoke failed");
		return false;
	}

	return onHandleReturn(varRet);
}
Exemple #8
0
bool convert_mrb_to_js_hash(mrb_state *mrb, mrb_value value, NPP npp, NPVariant *result)
{
    NPObject *window;
    NPN_GetValue(npp, NPNVWindowNPObject, &window);

    NPVariant hash;
    NPN_Invoke(npp, window, NPN_GetStringIdentifier("Object"), NULL, 0, &hash);
    NPN_ReleaseObject(window);

    khash_t(ht) *h = RHASH_TBL(value);
    if (!h){
        *result = hash;
        return true;
    }

    NPObject *hash_obj = NPVARIANT_TO_OBJECT(hash);
    for (khiter_t k=kh_begin(h); k!=kh_end(h); k++){
        if (kh_exist(h, k)) {
            mrb_value key = mrb_funcall(mrb, kh_key(h, k), "to_s", 0);
            mrb_value val = kh_value(h, k);

            NPVariant var;
            if (!convert_mrb_to_js(mrb, val, npp, &var)){
                NPN_ReleaseVariantValue(&hash);
                return false;
            }

            std::string name(RSTRING_PTR(key), RSTRING_LEN(key));
            NPN_SetProperty(npp, hash_obj, NPN_GetStringIdentifier(name.c_str()), &var);
        }
    }
    *result = hash;

    return true;
}
Exemple #9
0
// 清空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);
}
Exemple #10
0
bool JsAgent::onDefineFunction(const DefineFunctionMessage& msg) {
	Debug::println("JsAgent::onDefineFunction: %s", msg.name.c_str());

	VariantArray args(3);
	args[0].set(msg.name);
	args[1].set(msg.args);
	args[2].set(msg.body);

	Variant varRet;
	return NPN_Invoke(getNPP(), m_helper, methods.defineFunction, args.get(), args.size(), varRet.ptr());
}
Exemple #11
0
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;
}
/**
 * Calls the function in the reply with the reply of the function. This has to
 * be done in the browser's main thread, and hence this functions
 *
 * @param reply - Struct containing the callback object, and the reply param.
 */
static void 
jsReply (ScriptReply* reply)
{
    if (reply)
    {
        NPVariant result;
        NPN_Invoke(reply->plugin, reply->callb, NPN_GetStringIdentifier("callback"), reply->result, 1, &result);

        NPN_ReleaseObject(reply->callb);
        NPN_MemFree(reply->result);
        free(reply);
    }
}
Exemple #13
0
gboolean
mmp_script_element_append_child (NPP npp, NPVariant *element, NPVariant *child)
{
	gboolean success;
	NPVariant result;
	VOID_TO_NPVARIANT (result);

	LOAD_IDENTIFIERS ();

	success = NPN_Invoke (npp, NPVARIANT_TO_OBJECT (*element), id_append_child, child, 1, &result);
	NPN_ReleaseVariantValue (&result);
	return success;
}
Exemple #14
0
NPObject* JsAgent::wrapDelegate(NPObject* obj) {
	VariantArray args(1);
	args[0].set(obj);

	Variant varRet;
	if(!NPN_Invoke(getNPP(), m_helper, methods.wrapDelegate, args.get(), args.size(), varRet.ptr()))
		return NULL;

	if(!varRet.isObject())
		return NULL;

	return NPN_RetainObject(varRet.asObject());
}
Exemple #15
0
static gboolean
invoke_in_s_out_o (NPP npp, NPVariant *object, NPIdentifier method, const gchar *in_s, NPVariant *out_o)
{
	NPVariant string;
	STRINGZ_TO_NPVARIANT (in_s, string);
	VOID_TO_NPVARIANT (*out_o);

	if (!NPN_Invoke (npp, NPVARIANT_TO_OBJECT (*object), method, &string, 1, out_o) ||
		!NPVARIANT_IS_OBJECT (*out_o)) {
		NPN_ReleaseVariantValue (out_o);
		return FALSE;
	}

	return TRUE;
}
Exemple #16
0
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;
}
Exemple #17
0
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;
}
Exemple #18
0
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;
}
Exemple #19
0
static mrb_value mrb_js_func_call(mrb_state *mrb, mrb_value self)
{
    NPP npp = MRB_UD_NPP(mrb);

    NPObject *func = (NPObject *)DATA_PTR(self);
    if (!func){
        return mrb_nil_value();
    }

    mrb_value *values;
    int len;
    mrb_get_args(mrb, "*", &values, &len);

    NPVariant empty = { NPVariantType_Void, NULL };
    std::vector< NPVariant > args(len + 1, empty);
    bool success = true;
    for (int i=0; i<len; i++){
        if (!convert_mrb_to_js(mrb, values[i], npp, &args[i + 1])){
            success = false;
            break;
        }
    }

    mrb_value ret = mrb_nil_value();
    if (success){
        NPObject *window;
        NPN_GetValue(npp, NPNVWindowNPObject, &window);

        NPVariant result;
        OBJECT_TO_NPVARIANT(window, args[0]);
        NPN_Invoke(npp, func, NPN_GetStringIdentifier("call"), &args[0], args.size(), &result);
        convert_js_to_mrb(npp, result, mrb, &ret);
    }

    for (int i=0; i<len+1; i++){
        NPN_ReleaseVariantValue(&args[i]);
    }

    return ret;
}
Exemple #20
0
	// Clear all the paramters for IECtrl::Navigate
	void CPlugin::RemoveNavigateParams()
	{
		NPVariant vResult;
		VOID_TO_NPVARIANT(vResult);

		try
		{
			NPObject* pContainer = GetContainer();

			if (!NPN_Invoke(m_pNPInstance, pContainer, GetIdentifier("removeNavigateParams"), NULL, 0, &vResult))
			{
				throw(CString(_T("Cannot execute window.Container.removeNavigateParams()")));
			}
		}
		catch (const CString& strMessage)
		{
			UNUSED(strMessage);
			TRACE(_T("[CPlugin::RemoveNavigateParams Exception] %s\n"), strMessage);
		}

		if (!NPVARIANT_IS_VOID(vResult))	NPN_ReleaseVariantValue(&vResult);
	}
Exemple #21
0
	// This function is equivalent to the following JavaScript function:
	// function FireEvent(strEventType, strDetail) {
	//   FireIEContainer.dispatchEvent(strEventType, strDetail)
	// }
	// 
	// 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)
	{
		// Fast event dispatching, requires helper function in container object
		try
		{
			// FireIEContainer.dispatchEvent(strEventType, strDetail);
			NPObject* pContainer = GetContainer();
			NPVariant args[2];
			STRINGZ_TO_NPVARIANT(CStringToNPStringCharacters(strEventType), args[0]);
			STRINGZ_TO_NPVARIANT(CStringToNPStringCharacters(strDetail), args[1]);
			NPVariant vSucceeded;
			VOID_TO_NPVARIANT(vSucceeded);

			BOOL bOK = NPN_Invoke(m_pNPInstance, pContainer, GetIdentifier("dispatchEvent"), args, 2, &vSucceeded);
				
			for (int i = 0; i < 2; i++)
				NPN_ReleaseVariantValue(&args[i]);

			if (!bOK || !NPVARIANT_IS_BOOLEAN(vSucceeded))
			{
				NPN_ReleaseVariantValue(&vSucceeded);
				throw CString(_T("Cannot invoke dispatchEvent"));
			}
			bool bSucceeded = NPVARIANT_TO_BOOLEAN(vSucceeded);
			NPN_ReleaseVariantValue(&vSucceeded);
			if (!bSucceeded)
				throw CString(_T("Event dispatch failed"));

			return TRUE;
		}
		catch (const CString& strMessage)
		{
			UNUSED(strMessage);
			TRACE(_T("[CPlugin::FireEvent Exception] Fast event dispatching failed: %s\n"), strMessage);
			return FALSE;
		}
	}
Exemple #22
0
// 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;
}
Exemple #23
0
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);
}
Exemple #24
0
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;
}
int QtSignalForwarder::qt_metacall(QMetaObject::Call call, int index, void **args)
{
    // no support for QObject method/properties etc!
    if (!This || !This->npp || call != QMetaObject::InvokeMetaMethod
        || !This->qt.object)
        return index;

    switch (index) {
    case -1:
        {
            QString msg = *(QString*)args[1];
            NPN_Status(This->npp, msg.toLocal8Bit().constData());
        }
        break;
    default:
        {
            QObject *qobject = This->qt.object;
            if (!domNode)
                NPN_GetValue(This->npp, NPNVPluginElementNPObject, &domNode);
            if (!domNode)
                break;
            const QMetaObject *metaObject = qobject->metaObject();
            if (index < metaOffset(metaObject, MetaMethod))
                break;

            const QMetaMethod method = metaObject->method(index);
            Q_ASSERT(method.methodType() == QMetaMethod::Signal);

            QByteArray signalSignature = method.signature();
            QByteArray scriptFunction = signalSignature.left(signalSignature.indexOf('('));
            NPIdentifier id = NPN_GetStringIdentifier(scriptFunction.constData());
            if (NPN_HasMethod(This->npp, domNode, id)) {
                QList<QByteArray> parameterTypes = method.parameterTypes();
                QVector<NPVariant> parameters;
                NPVariant result;
                bool error = false;
                for (int p = 0; p < parameterTypes.count(); ++p) {
                    QVariant::Type type = QVariant::nameToType(parameterTypes.at(p));
                    if (type == QVariant::Invalid) {
                        NPN_SetException(domNode, QByteArray("Unsupported parameter type in ") + scriptFunction);
                        error = true;
                        break;
                    }
                    QVariant qvar(type, args[p + 1]);
                    NPVariant npvar = NPVariant::fromQVariant(This, qvar);
                    if (npvar.type == NPVariant::Null || npvar.type == NPVariant::Void) {
                        NPN_SetException(domNode, QByteArray("Unsupported parameter value in ") + scriptFunction);
                        error =true;
                        break;
                    }
                    parameters += npvar;
                }
                if (error)
                    break;

                NPError nperror = NPN_Invoke(This->npp, domNode, id, parameters.constData(), parameters.count(), &result);
                if (nperror != NPERR_NO_ERROR && false) { // disabled, as NPN_Invoke seems to always return GENERICERROR
                    NPN_SetException(domNode, QByteArray("Error invoking event handler ") + scriptFunction);
                }
                // ### TODO: update return value (args[0]) (out-parameters not supported anyway)
                NPN_ReleaseVariantValue(&result);
            }
        }
        break;
    }

    return index;
}
Exemple #26
0
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;
}
Exemple #27
0
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;
}