Ejemplo n.º 1
0
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;
}
Ejemplo n.º 2
0
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;
}
Ejemplo n.º 3
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;
}
Ejemplo n.º 4
0
	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;
	}
Ejemplo n.º 5
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);
}
Ejemplo n.º 6
0
// 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;
            }
        }
    }
}
Ejemplo n.º 7
0
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;
}
Ejemplo n.º 8
0
// 获取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;
}
Ejemplo n.º 9
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;
	}
Ejemplo n.º 10
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;
	}
// this is not called on the main thread - so we need to 
// use NPN_PluginThreadAsyncCall
void nsScriptableObjectOverwolfSample::EchoTask(
  const std::string& message, NPObject* callback) {

  if (shutting_down_) {
    return;
  }

  NPVariant arg;
  NPVariant ret_val;

  STRINGN_TO_NPVARIANT(
    message.c_str(),
    message.size(),
    arg);

  // fire callback
  NPN_InvokeDefault(
    __super::npp_, 
    callback, 
    &arg, 
    1, 
    &ret_val);

  NPN_ReleaseVariantValue(&ret_val);
}
bool nsScriptableObjectRawInput::DeleteInstance(
	NPIdentifier name,
	const NPVariant *args,
	uint32_t argCount,
	NPVariant *result) {
	if (argCount != 2 ||
		!NPVARIANT_IS_DOUBLE(args[0]) ||
		!NPVARIANT_IS_OBJECT(args[1])) {
		NPN_SetException(this, "(DeleteInstance) invalid params passed to function");
		return true;
	}
	int32_t id = (int32_t)(int)floor(NPVARIANT_TO_DOUBLE(args[0]) + 0.5);	// all numbers come in as double in chrome...

	bool foundAndDeleted = PluginMethodKeyCapture::DeleteInstance(id);

	NPVariant out_args[2];
	NPVariant ret_val;

	BOOLEAN_TO_NPVARIANT(foundAndDeleted, out_args[0]);
	INT32_TO_NPVARIANT(id, out_args[1]);

	// fire callback
	NPN_InvokeDefault(
		__super::npp_,
		NPVARIANT_TO_OBJECT(args[1]),
		out_args,
		2,
		&ret_val);

	NPN_ReleaseVariantValue(&ret_val);
	return true;
}
static void
retrieve_highres_icon (WebappMonitor *monitor, const gchar *desktop_file)
{
  GKeyFile *key_file;
  gchar *s;
  GError *error = NULL;

  /* Get URL to get icon for */
  key_file = g_key_file_new ();
  if (!g_key_file_load_from_data (key_file, desktop_file, strlen (desktop_file), 0, &error)) {
    g_warning ("Could not parse desktop file: %s", error->message);
    g_error_free (error);

    goto out;
  }

  if (get_icon_size (key_file) >= 64)
    goto out;

  s =  g_key_file_get_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_EXEC, NULL);
  if (s != NULL) {
    gint n_args, i;
    gchar **args, *url = NULL;

    g_debug ("Parsing command line %s", s);

    if (!g_shell_parse_argv (s, &n_args, &args, &error)) {
      g_debug ("Failed parsing command line %s", s);
      goto out;
    }

    for (i = 0; i < n_args && args[i] != NULL; i++) {
      g_debug ("Processing argument %s", args[i]);
      if (g_str_has_prefix (args[i], "--app=")) {
	url = g_strdup (args[i] + 6);
	g_debug ("Found URL %s", url);
	break;
      }
    }

    if (url != NULL) {
      NPVariant url_varg, result;

      STRINGZ_TO_NPVARIANT(url, url_varg);
      NULL_TO_NPVARIANT(result);

      if (!NPN_InvokeDefault (monitor->instance, monitor->icon_loader_callback, &url_varg, 1, &result))
      	g_debug ("Failed calling JS callback");

      g_free (url);
      NPN_ReleaseVariantValue (&result);
    }

    g_strfreev (args);
    g_free (s);
  }

 out:
  g_key_file_free (key_file);
}
Ejemplo n.º 14
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;
}
Ejemplo n.º 15
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;
}
Ejemplo n.º 16
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;
}
Ejemplo n.º 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;
}
Ejemplo n.º 18
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;
}
Ejemplo n.º 19
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;
}
Ejemplo n.º 20
0
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;
}
Ejemplo n.º 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;
		}
	}
Ejemplo n.º 22
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;
}
//virtual 
void PluginMethodListenOnFile::OnError(
  const char* id, 
  const char* message, 
  unsigned int len) {

  TextFileIdToCallbackMap::iterator iter = ids_to_callbacks_.find(id);
  if ((iter == ids_to_callbacks_.end()) || (nullptr == iter->second)) {
    OutputDebugStringA("SimpleIOPlugin OnNewLine - missing callback error!");
    return;
  }

  static bool write_to_trace = utils::ShouldWriteToTrace();

  if (write_to_trace) {
    std::string str = "SimpleIOPlugin OnError - [";
    str += id;
    str += "] ";
    str += message;
    OutputDebugStringA(str.c_str());
  }

  NPVariant args[3];
  NPVariant ret_val;

  STRINGN_TO_NPVARIANT(
    id,
    strlen(id),
    args[0]);

  BOOLEAN_TO_NPVARIANT(
    false,
    args[1]);

  STRINGN_TO_NPVARIANT(
    message,
    len,
    args[2]);

  // fire callback
  NPN_InvokeDefault(
    npp_,
    iter->second,
    args,
    3,
    &ret_val);

  NPN_ReleaseVariantValue(&ret_val);
}
Ejemplo n.º 24
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;
 }
bool
ScriptablePluginObject::SetProperty(NPIdentifier name,
                                        const NPVariant *value)
{
	if (!HasProperty(name)) {
		return false;
	}
	NPIdentifier keyword_id = NPN_GetStringIdentifier("keyword");
	NPIdentifier code_id = NPN_GetStringIdentifier("code");

  if (name == keyword_id) {
		printf("call SetProperty::keyword\n");
		m_strFromVar(&keyword,*value);
	} else if (name == code_id) {
		printf("call SetProperty::code\n");
		m_strFromVar(&code,*value);
	}
	// Release value?
	NPN_ReleaseVariantValue((NPVariant*)value);
	return true;
}
void nsScriptableObjectOverwolfSample::AddTask(
  double first, double second, NPObject* callback) {
  if (shutting_down_) {
    return;
  }

  NPVariant arg;
  NPVariant ret_val;

  DOUBLE_TO_NPVARIANT(first+second, arg);

  // fire callback
  NPN_InvokeDefault(
    __super::npp_, 
    callback, 
    &arg, 
    1, 
    &ret_val);

  NPN_ReleaseVariantValue(&ret_val);
}
Ejemplo n.º 27
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;
}
Ejemplo n.º 28
0
gboolean
mmp_script_evaluate (NPP npp, const gchar *code)
{
	NPObject *host;
	NPString string;
	NPVariant output;
	gboolean result;

	g_return_val_if_fail (npp != NULL, FALSE);
	g_return_val_if_fail (NPN_GetValue (npp, NPNVWindowNPObject, 
		&host) == NPERR_NO_ERROR, FALSE);

	string.UTF8Characters = code;
	string.UTF8Length = strlen (code);

	if ((result = NPN_Evaluate (npp, host, &string, &output))) {
		NPN_ReleaseVariantValue (&output);
	}

	NPN_ReleaseObject (host);
	return result;
}
Ejemplo n.º 29
0
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;
}
Ejemplo n.º 30
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);
	}