Ejemplo n.º 1
0
void
cloud_spy_init_npvariant_with_other (NPVariant * var, const NPVariant * other)
{
  memcpy (var, other, sizeof (NPVariant));

  if (other->type == NPVariantType_String)
  {
    const NPString * from = &NPVARIANT_TO_STRING (*other);
    NPString * to = &NPVARIANT_TO_STRING (*var);
    to->UTF8Characters = static_cast<NPUTF8 *> (cloud_spy_nsfuncs->memalloc (from->UTF8Length));
    memcpy (const_cast<NPUTF8 *> (to->UTF8Characters), from->UTF8Characters, from->UTF8Length);
  }
  else if (other->type == NPVariantType_Object)
  {
    cloud_spy_nsfuncs->retainobject (NPVARIANT_TO_OBJECT (*var));
  }
}
Ejemplo n.º 2
0
static bool testGetPropertyReturnValue(PluginObject* obj, const NPVariant* args, uint32_t argCount, NPVariant* result)
{
    if (argCount != 2 || !NPVARIANT_IS_OBJECT(args[0]) || !NPVARIANT_IS_STRING(args[1]))
        return false;

    NPUTF8* propertyString = createCStringFromNPVariant(&args[1]);
    NPIdentifier propertyIdentifier = browser->getstringidentifier(propertyString);
    free(propertyString);

    NPVariant variant;
    bool retval = browser->getproperty(obj->npp, NPVARIANT_TO_OBJECT(args[0]), propertyIdentifier, &variant);
    if (retval)
        browser->releasevariantvalue(&variant);

    BOOLEAN_TO_NPVARIANT(retval, *result);
    return true;
}
Ejemplo n.º 3
0
ObjectManager* ObjectManager::GetManager(NPP npp) {
	NPObjectProxy window;
	NPNFuncs.getvalue(npp, NPNVWindowNPObject, &window);
	NPVariantProxy document, obj;
	NPVariant par;
	STRINGZ_TO_NPVARIANT(MANAGER_OBJECT_ID, par);
	if (!NPNFuncs.getproperty(npp, window, NPNFuncs.getstringidentifier("document"), &document))
		return NULL;
	if (!NPNFuncs.invoke(npp, document.value.objectValue, NPNFuncs.getstringidentifier("getElementById"), &par, 1, &obj))
		return NULL;
	if (NPVARIANT_IS_OBJECT(obj)) {
		NPObject *manager = NPVARIANT_TO_OBJECT(obj);
		CHost *host = GetInternalObject(npp, manager);
		if (host)
			return dynamic_cast<ObjectManager*>(host);
	}
	return NULL;
}
Ejemplo n.º 4
0
bool testDocumentOpen(NPP npp)
{
    NPIdentifier documentId = browser->getstringidentifier("document");
    NPIdentifier openId = browser->getstringidentifier("open");

    NPObject* windowObject = 0;
    browser->getvalue(npp, NPNVWindowNPObject, &windowObject);
    if (!windowObject)
        return false;

    NPVariant docVariant;
    browser->getproperty(npp, windowObject, documentId, &docVariant);
    if (docVariant.type != NPVariantType_Object) {
        browser->releaseobject(windowObject);
        return false;
    }

    NPObject* documentObject = NPVARIANT_TO_OBJECT(docVariant);

    NPVariant openArgs[2];
    STRINGZ_TO_NPVARIANT("text/html", openArgs[0]);
    STRINGZ_TO_NPVARIANT("_blank", openArgs[1]);

    NPVariant result;
    if (!browser->invoke(npp, documentObject, openId, openArgs, 2, &result)) {
        browser->releaseobject(windowObject);
        browser->releaseobject(documentObject);
        return false;
    }

    browser->releaseobject(documentObject);

    if (result.type != NPVariantType_Object) {
        browser->releaseobject(windowObject);
        browser->releasevariantvalue(&result);
        return false;
    }

    pluginLogWithWindowObjectVariableArgs(windowObject, npp, "DOCUMENT OPEN SUCCESS");
    notifyTestCompletion(npp, result.value.objectValue);
    browser->releaseobject(result.value.objectValue);
    browser->releaseobject(windowObject);
    return true;
}
Ejemplo n.º 5
0
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;
}
Ejemplo n.º 6
0
ptr<Script::Any> Any::CallWith(ptr<Script::Any> arguments[], int count)
{
	State::Scope scope(state);

	// convert args
	NPVariant* args = (NPVariant*)alloca(sizeof(NPVariant) * count);
	for(int i = 0; i < count; ++i)
		args[i] = fast_cast<Any*>(&*arguments[i])->variant;

	// make a call
	NPVariant result;
	if(!Platform::NpapiPlugin::browserFuncs.invokeDefault(
		state->GetPluginInstance()->GetNpp(),
		NPVARIANT_TO_OBJECT(variant),
		args, count, &result))
		THROW("Can't call Np::Any as a function");

	return state->CreateAny(result);
}
Ejemplo n.º 7
0
static bool testInvokeDefault(PluginObject* obj, const NPVariant* args, uint32_t argCount, NPVariant* result)
{
    if (!NPVARIANT_IS_OBJECT(args[0]))
        return false;

    NPObject* callback = NPVARIANT_TO_OBJECT(args[0]);

    NPVariant invokeArgs[1];
    NPVariant browserResult;

    STRINGZ_TO_NPVARIANT("test", invokeArgs[0]);
    bool retval = browser->invokeDefault(obj->npp, callback, invokeArgs, 1, &browserResult);

    if (retval)
        browser->releasevariantvalue(&browserResult);

    BOOLEAN_TO_NPVARIANT(retval, *result);
    return true;
}
static bool
plugin_object_set_callback (NPObject        **listener,
                            const NPVariant  *value)
{
  if (!NPVARIANT_IS_OBJECT (*value) && !NPVARIANT_IS_NULL (*value))
    return FALSE;

  if (*listener)
    funcs.releaseobject (*listener);
  *listener = NULL;

  if (NPVARIANT_IS_OBJECT (*value))
    {
      *listener = NPVARIANT_TO_OBJECT (*value);
      funcs.retainobject (*listener);
    }

  return TRUE;
}
Ejemplo n.º 9
0
bool
xplayerNPObject::GetObjectFromArguments (const NPVariant* argv,
                                        uint32_t argc,
                                        uint32_t argNum,
                                        NPObject*& _result)
{
  if (!CheckArg (argv, argc, argNum, NPVariantType_Object))
    return false;

  NPVariant arg = argv[argNum];
  if (NPVARIANT_IS_STRING (arg)) {
    _result = NPVARIANT_TO_OBJECT (arg);
  } else if (NPVARIANT_IS_NULL (arg) ||
             NPVARIANT_IS_VOID (arg)) {
    _result = NULL;
  }

  return true;
}
Ejemplo n.º 10
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.º 11
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.º 12
0
static void testDOMAccess(PluginObject *obj)
{
    // Get plug-in's DOM element
    NPObject *elementObject;
    if (browser->getvalue(obj->npp, NPNVPluginElementNPObject, &elementObject) == NPERR_NO_ERROR) {
        // Get style
        NPVariant styleVariant;
        NPIdentifier styleIdentifier = browser->getstringidentifier("style");
        if (browser->getproperty(obj->npp, elementObject, styleIdentifier, &styleVariant) && NPVARIANT_IS_OBJECT(styleVariant)) {
            // Set style.border
            NPIdentifier borderIdentifier = browser->getstringidentifier("border");
            NPVariant borderVariant;
            STRINGZ_TO_NPVARIANT("3px solid red", borderVariant);
            browser->setproperty(obj->npp, NPVARIANT_TO_OBJECT(styleVariant), borderIdentifier, &borderVariant);
            browser->releasevariantvalue(&styleVariant);
        }
        
        browser->releaseobject(elementObject);
    }
}
Ejemplo n.º 13
0
ptr<Script::Any> Any::ApplyWith(ptr<Script::Any> thisValue, ptr<Script::Any> arguments[], int count)
{
	State::Scope scope(state);

	// convert args
	NPVariant* args = (NPVariant*)alloca(sizeof(NPVariant) * (count + 1));
	args[0] = fast_cast<Any*>(&*thisValue)->variant;
	for(int i = 0; i < count; ++i)
		args[i + 1] = fast_cast<Any*>(&*arguments[i])->variant;

	// make a call
	NPVariant result;
	if(!Platform::NpapiPlugin::browserFuncs.invoke(
		state->GetPluginInstance()->GetNpp(),
		NPVARIANT_TO_OBJECT(variant),
		Platform::NpapiPlugin::callIdentifier,
		args, count + 1, &result))
		THROW("Can't apply Np::Any as a function");

	return state->CreateAny(result);
}
Ejemplo n.º 14
0
bool CJSMethods::CallJSMethod(NPNetscapeFuncs *pBrowserFuncs, NPP pluginInstance, const char* lpszMethod, NPVariant *pArgs, const uint32_t argCount, NPVariant &varResult)
{
	if(pluginInstance == NULL || pBrowserFuncs == NULL)
	{
		return false;
	}

	NPObject* npWindow = NULL;

	pBrowserFuncs->getvalue(pluginInstance, NPNVWindowNPObject, &npWindow);

	if(npWindow == NULL)
	{
		return false;
	}

	NPVariant varDocument;

	NPIdentifier idDocument = pBrowserFuncs->getstringidentifier("document");

	pBrowserFuncs->getproperty(pluginInstance, npWindow, idDocument, &varDocument);

	NPObject* npDocument = NPVARIANT_TO_OBJECT(varDocument);

	if(npDocument == NULL)
	{
		pBrowserFuncs->releaseobject(npWindow);

		return false;
	}

	NPIdentifier idMethod = pBrowserFuncs->getstringidentifier(lpszMethod);

	bool bResult = pBrowserFuncs->invoke(pluginInstance, npWindow, idMethod, pArgs, argCount, &varResult);
	
	pBrowserFuncs->releasevariantvalue(&varDocument);
	pBrowserFuncs->releaseobject(npWindow);

	return bResult;
}
Ejemplo n.º 15
0
static Eina_Bool ewk_js_npvariant_to_variant(Ewk_JS_Variant* data, const NPVariant* result)
{
    EINA_SAFETY_ON_NULL_RETURN_VAL(data, false);
    EINA_SAFETY_ON_NULL_RETURN_VAL(result, false);
    switch (result->type) {
    case NPVariantType_Void:
        data->type = EWK_JS_VARIANT_VOID;
        data->value.o = 0;
        break;
    case NPVariantType_Null:
        data->type = EWK_JS_VARIANT_NULL;
        data->value.o = 0;
        break;
    case NPVariantType_Int32:
        data->type = EWK_JS_VARIANT_INT32;
        data->value.i = NPVARIANT_TO_INT32(*result);
        break;
    case NPVariantType_Double:
        data->type = EWK_JS_VARIANT_DOUBLE;
        data->value.d = NPVARIANT_TO_DOUBLE(*result);
        break;
    case NPVariantType_String:
        data->value.s = eina_stringshare_add_length(NPVARIANT_TO_STRING(*result).UTF8Characters, NPVARIANT_TO_STRING(*result).UTF8Length);
        data->type = EWK_JS_VARIANT_STRING;
        break;
    case NPVariantType_Bool:
        data->type = EWK_JS_VARIANT_BOOL;
        data->value.b = NPVARIANT_TO_BOOLEAN(*result);
        break;
    case NPVariantType_Object:
        data->type = EWK_JS_VARIANT_OBJECT;
        data->value.o = ewk_js_npobject_to_object(NPVARIANT_TO_OBJECT(*result));
        break;
    default:
        return false;
    }

    return true;
}
Ejemplo n.º 16
0
// Helper function which takes in the plugin window object for logging to the console object.
static void pluginLogWithWindowObject(NPObject* windowObject, NPP instance, const char* message)
{
    NPVariant consoleVariant;
    if (!browser->getproperty(instance, windowObject, browser->getstringidentifier("console"), &consoleVariant)) {
        fprintf(stderr, "Failed to retrieve console object while logging: %s\n", message);
        return;
    }

    NPObject* consoleObject = NPVARIANT_TO_OBJECT(consoleVariant);

    NPVariant messageVariant;
    STRINGZ_TO_NPVARIANT(message, messageVariant);

    NPVariant result;
    if (!browser->invoke(instance, consoleObject, browser->getstringidentifier("log"), &messageVariant, 1, &result)) {
        fprintf(stderr, "Failed to invoke console.log while logging: %s\n", message);
        browser->releaseobject(consoleObject);
        return;
    }

    browser->releasevariantvalue(&result);
    browser->releaseobject(consoleObject);
}
Ejemplo n.º 17
0
const char *
ShockerScriptableControlObject::GetTestPath ()
{
	if (test_path == NULL) {
		NPVariant nplocation;
		NPVariant nppath;
		NPObject *window = NULL;
		NPIdentifier identifier = Browser::Instance ()->GetStringIdentifier ("location");
	
	
		Browser::Instance ()->GetValue (instance, NPNVWindowNPObject, &window);
		Browser::Instance ()->GetProperty (instance, window, identifier, &nplocation);
	
		identifier = Browser::Instance ()->GetStringIdentifier ("pathname");
		Browser::Instance ()->GetProperty (instance, NPVARIANT_TO_OBJECT (nplocation), identifier, &nppath);
	
		test_path = g_path_get_basename (STR_FROM_VARIANT (nppath));
	}

	LOG_PLUGIN ("[%i shocker] ShockerScriptableControlObject::GetTestPath (): %s\n", getpid (), test_path);

	return test_path;
}
Ejemplo n.º 18
0
bool NPSafeArray::InvokeDefault(NPObject *npobj, const NPVariant *args, uint32_t argCount,
								NPVariant *result) {
	NPSafeArray *safe = static_cast<NPSafeArray*>(npobj);
	if (safe->arr_.m_psa != NULL)
		return false;
	if (argCount < 1)
		return false;
	if (!NPVARIANT_IS_OBJECT(*args)) {
		return false;
	}
	NPObject *obj = NPVARIANT_TO_OBJECT(*args);
	if (obj->_class != &NPSafeArray::npClass) {
		return false;
	}
	
	NPSafeArray *safe_original = static_cast<NPSafeArray*>(obj);
	if (safe_original->arr_.m_psa == NULL) {
		return false;
	}

	NPSafeArray *ret = CreateFromArray(safe->instance, safe_original->arr_);
	OBJECT_TO_NPVARIANT(ret, *result);
	return true;
}
Ejemplo n.º 19
0
bool CJSMethods::GetOptions(ChromeTrayIconOptions &options)
{
	if(m_pScriptableObject == NULL)
	{
		return false;
	}

	NPP pluginInstance				= m_pScriptableObject->GetPluginInstance();
	NPNetscapeFuncs *pBrowserFuncs	= m_pScriptableObject->GetBrowserFuncs();

	if(pluginInstance == NULL || pBrowserFuncs == NULL)
	{
		return false;
	}

	NPVariant varResults;
	NPVariant varArgs;

	bool bResult = CallJSMethod(pBrowserFuncs, pluginInstance, "getOptions", &varArgs, 0, varResults);

	if(bResult)
	{
		if(NPVARIANT_IS_OBJECT(varResults))
		{
			NPObject *pArray = NPVARIANT_TO_OBJECT(varResults);

			if(pArray != NULL)
			{
				wstring strValue;

				if(CJSValue::GetProperty(pBrowserFuncs, pluginInstance, pArray, "minimizeOnLeftButton", strValue))
				{
					options.bMinimizeOnLeftButton = (strValue == _T("true"));
				}

				if(CJSValue::GetProperty(pBrowserFuncs, pluginInstance, pArray, "minimizeOnRightButton", strValue))
				{
					options.bMinimizeOnRightButton = (strValue == _T("true"));
				}

				if(CJSValue::GetProperty(pBrowserFuncs, pluginInstance, pArray, "trayHide", strValue))
				{
					options.bHideTray = (strValue == _T("true"));
				}

				if(CJSValue::GetProperty(pBrowserFuncs, pluginInstance, pArray, "actLClick", strValue))
				{
					options.actLClick = (TrayAction)_wtoi(strValue.c_str());
				}

				if(CJSValue::GetProperty(pBrowserFuncs, pluginInstance, pArray, "actRClick", strValue))
				{
					options.actRClick = (TrayAction)_wtoi(strValue.c_str());
				}

				if(CJSValue::GetProperty(pBrowserFuncs, pluginInstance, pArray, "actLDblClick", strValue))
				{
					options.actLDblClick = (TrayAction)_wtoi(strValue.c_str());
				}

				if(CJSValue::GetProperty(pBrowserFuncs, pluginInstance, pArray, "displayOptions", strValue))
				{
					options.bShowOptions = (strValue == _T("true"));
				}

				if(CJSValue::GetProperty(pBrowserFuncs, pluginInstance, pArray, "displayNewWindow", strValue))
				{
					options.bShowNewWindow = (strValue == _T("true"));
				}

				if(CJSValue::GetProperty(pBrowserFuncs, pluginInstance, pArray, "displayNewTab", strValue))
				{
					options.bShowNewTab = (strValue == _T("true"));
				}

				if(CJSValue::GetProperty(pBrowserFuncs, pluginInstance, pArray, "displayFavorites", strValue))
				{
					options.bShowFavorites = (strValue == _T("true"));
				}

				if(CJSValue::GetProperty(pBrowserFuncs, pluginInstance, pArray, "favorites", strValue))
				{
					options.strFavorites = strValue;
				}

				//////////////////////////////////////////////////////////////////////////
				// Boss key
				//////////////////////////////////////////////////////////////////////////

				if(CJSValue::GetProperty(pBrowserFuncs, pluginInstance, pArray, "enableBossKey", strValue))
				{
					options.bEnableBossKey = (strValue == _T("true"));
				}

				if(CJSValue::GetProperty(pBrowserFuncs, pluginInstance, pArray, "bossMod1", strValue))
				{
					options.wBossModifier = StringToKey(strValue.c_str());
				}

				if(CJSValue::GetProperty(pBrowserFuncs, pluginInstance, pArray, "bossMod2", strValue))
				{
					options.wBossModifier |= StringToKey(strValue.c_str());
				}

				if(CJSValue::GetProperty(pBrowserFuncs, pluginInstance, pArray, "bossKey", strValue))
				{
					if(strValue.empty() == FALSE)
					{
						transform(strValue.begin(), strValue.end(), strValue.begin(), toupper);
						options.wBossKey = (WORD)strValue[0];
					}
				}

				if(CJSValue::GetProperty(pBrowserFuncs, pluginInstance, pArray, "bossHideTrayIcon", strValue))
				{
					options.bBossHideTrayIcon = (strValue == _T("true"));
				}
			}
		}
	}

	pBrowserFuncs->releasevariantvalue(&varArgs);
	pBrowserFuncs->releasevariantvalue(&varResults);

	return bResult;
}
Ejemplo n.º 20
0
bool CJSMethods::GetWindowTabs(int nWindowId, vector<ChromeTab> &tabs)
{
	if(m_pScriptableObject == NULL)
	{
		return false;
	}

	NPP pluginInstance				= m_pScriptableObject->GetPluginInstance();
	NPNetscapeFuncs *pBrowserFuncs	= m_pScriptableObject->GetBrowserFuncs();

	if(pluginInstance == NULL || pBrowserFuncs == NULL)
	{
		return false;
	}

	NPVariant varResults;

	NPVariant varArgs;
	INT32_TO_NPVARIANT(nWindowId, varArgs);

	bool bResult = CallJSMethod(pBrowserFuncs, pluginInstance, "getWindowTabs", &varArgs, 1, varResults);

	if(bResult)
	{
		if(NPVARIANT_IS_OBJECT(varResults))
		{
			NPObject *pArray = NPVARIANT_TO_OBJECT(varResults);

			if(pArray != NULL)
			{
				int			nLength = 0;
				NPVariant	varTab;

				if(CJSValue::GetProperty(pBrowserFuncs, pluginInstance, pArray, "length", nLength))
				{
					for(size_t i = 0; i < (size_t)nLength; ++i)
					{
						if(CJSValue::GetProperty(pBrowserFuncs, pluginInstance, pArray, i, varTab))
						{
							if(NPVARIANT_IS_OBJECT(varTab))
							{
								NPObject *pTab = NPVARIANT_TO_OBJECT(varTab);

								ChromeTab newTab;
								
								if(CJSValue::GetProperty(pBrowserFuncs, pluginInstance, pTab, "title", newTab.strTitle) &&
								   CJSValue::GetProperty(pBrowserFuncs, pluginInstance, pTab, "id", newTab.nId))
								{
									tabs.push_back(newTab);
								}
							}
						}

						pBrowserFuncs->releasevariantvalue(&varTab);
					}
				}
			}
			
		}
	}

	pBrowserFuncs->releasevariantvalue(&varArgs);
	pBrowserFuncs->releasevariantvalue(&varResults);

	return bResult;
}
Ejemplo n.º 21
0
JavaValue convertNPVariantToJavaValue(NPVariant value, const String& javaClass)
{
    CString javaClassName = javaClass.utf8();
    JavaType javaType = javaTypeFromClassName(javaClassName.data());
    JavaValue result;
    result.m_type = javaType;
    NPVariantType type = value.type;

    switch (javaType) {
    case JavaTypeArray:
    case JavaTypeObject:
        {
            // See if we have a Java instance.
            if (type == NPVariantType_Object) {
                NPObject* objectImp = NPVARIANT_TO_OBJECT(value);
                result.m_objectValue = ExtractJavaInstance(objectImp);
            }
        }
        break;

    case JavaTypeString:
        {
#ifdef CONVERT_NULL_TO_EMPTY_STRING
            if (type == NPVariantType_Null) {
                result.m_type = JavaTypeString;
                result.m_stringValue = String::fromUTF8("");
            } else
#else
            if (type == NPVariantType_String)
#endif
            {
                NPString src = NPVARIANT_TO_STRING(value);
                result.m_type = JavaTypeString;
                result.m_stringValue = String::fromUTF8(src.UTF8Characters);
            }
        }
        break;

    case JavaTypeBoolean:
        {
            if (type == NPVariantType_Bool)
                result.m_booleanValue = NPVARIANT_TO_BOOLEAN(value);
        }
        break;

    case JavaTypeByte:
        {
            if (type == NPVariantType_Int32)
                result.m_byteValue = static_cast<signed char>(NPVARIANT_TO_INT32(value));
            else if (type == NPVariantType_Double)
                result.m_byteValue = static_cast<signed char>(NPVARIANT_TO_DOUBLE(value));
        }
        break;

    case JavaTypeChar:
        {
            if (type == NPVariantType_Int32)
                result.m_charValue = static_cast<unsigned short>(NPVARIANT_TO_INT32(value));
        }
        break;

    case JavaTypeShort:
        {
            if (type == NPVariantType_Int32)
                result.m_shortValue = static_cast<short>(NPVARIANT_TO_INT32(value));
            else if (type == NPVariantType_Double)
                result.m_shortValue = static_cast<short>(NPVARIANT_TO_DOUBLE(value));
        }
        break;

    case JavaTypeInt:
        {
            if (type == NPVariantType_Int32)
                result.m_intValue = static_cast<int>(NPVARIANT_TO_INT32(value));
            else if (type == NPVariantType_Double)
                result.m_intValue = static_cast<int>(NPVARIANT_TO_DOUBLE(value));
        }
        break;

    case JavaTypeLong:
        {
            if (type == NPVariantType_Int32)
                result.m_longValue = static_cast<long long>(NPVARIANT_TO_INT32(value));
            else if (type == NPVariantType_Double)
                result.m_longValue = static_cast<long long>(NPVARIANT_TO_DOUBLE(value));
        }
        break;

    case JavaTypeFloat:
        {
            if (type == NPVariantType_Int32)
                result.m_floatValue = static_cast<float>(NPVARIANT_TO_INT32(value));
            else if (type == NPVariantType_Double)
                result.m_floatValue = static_cast<float>(NPVARIANT_TO_DOUBLE(value));
        }
        break;

    case JavaTypeDouble:
        {
            if (type == NPVariantType_Int32)
                result.m_doubleValue = static_cast<double>(NPVARIANT_TO_INT32(value));
            else if (type == NPVariantType_Double)
                result.m_doubleValue = static_cast<double>(NPVARIANT_TO_DOUBLE(value));
        }
        break;
    default:
        break;
    }
    return result;
}
Ejemplo n.º 22
0
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");
	}
}
Ejemplo n.º 23
0
bool
StatementObject::Invoke(NPIdentifier name, const NPVariant *args,
                               uint32_t argCount, NPVariant *result)
{
//sprintf(tmp, "stmt Invoke [%s]\n", NPN_UTF8FromIdentifier(name)); log(tmp);
  NPError rc;
  VOID_TO_NPVARIANT(*result);
  int index = 0;

  if (name == mc_Init_id) {

    if (argCount < 2) {
      NPN_SetException(this, "Too few parameters count");
      return true;
    }
#if 0
    if (!(NPVARIANT_IS_INT32(args[0]) && NPVARIANT_IS_OBJECT(args[1]))) {
      NPN_SetException(this, "Wrong argument type");
      return true;
    }

    SQLHANDLE hdbc = (SQLHANDLE)(long)NPVARIANT_TO_INT32(args[0]);
#else
    if (!(NPVARIANT_IS_OBJECT(args[0]) && NPVARIANT_IS_OBJECT(args[1]))) {
      NPN_SetException(this, "Wrong argument type");
      return true;
    }

    SQLHANDLE hdbc = (SQLHANDLE)NPVARIANT_TO_OBJECT(args[0]);
#endif

    Init(hdbc, NPVARIANT_TO_OBJECT(args[1]));

    return true;

  } else if (name == mc_AddParameter_id) {
    if (argCount < 1) {
      NPN_SetException(this, "Too few parameters count");
      return true;
    }

    AddParam(args);
    return true;

  } else if (name == mc_Execute_id) {
    if (argCount < 1) {
      NPN_SetException(this, "Too few parameters count");
      return true;
    }
    if (!NPVARIANT_IS_STRING(args[0])) {
      NPN_SetException(this, "Wrong argument type");
      return true;
    }

    Execute((NPString*)&NPVARIANT_TO_STRING(args[0]));
    return true;

  } else if (name == mc_Close_id) {
    Close();
    return true;

  } else if (name == mc_Fetch_id) {
    bool ret;
    rc = Fetch(&ret);
    if (rc != NPERR_NO_ERROR)
      return true;
    BOOLEAN_TO_NPVARIANT(ret, *result);
    return true;

  } else if (name == mc_MoreResults_id) {
    bool ret;
    rc = MoreResults(&ret);
    if (rc != NPERR_NO_ERROR)
      return true;
    BOOLEAN_TO_NPVARIANT(ret, *result);
    return true;

  } else if (name == mc_GetColumnName_id) {
    const char *ret;
    if (argCount < 1) {
      NPN_SetException(this, "Too few parameters count");
      return true;
    }

    if (NPVARIANT_IS_INT32(args[0]))
      index = NPVARIANT_TO_INT32(args[0]);
    else if (NPVARIANT_IS_DOUBLE(args[0]))
      index = (int)NPVARIANT_TO_DOUBLE(args[0]);
    else {
      NPN_SetException(this, "Wrong argument type");
      return true;
    }

    rc = GetColumnName(index, &ret);
    if (rc != NPERR_NO_ERROR)
      return true;
    STRING_TO_NPVARIANT(ret, *result);
    return true;

  } else if (name == mc_GetVariant_id) {
    if (argCount < 1) {
      NPN_SetException(this, "Too few parameters count");
      return true;
    }

    if (NPVARIANT_IS_INT32(args[0]))
      index = NPVARIANT_TO_INT32(args[0]);
    else if (NPVARIANT_IS_DOUBLE(args[0]))
      index = (int)NPVARIANT_TO_DOUBLE(args[0]);
    else {
      NPN_SetException(this, "Wrong argument type");
      return true;
    }

    GetVariant(index, result);
    return true;

  } else if (name == mc_GetColumnType_id) {
    int ret;
    if (argCount < 1) {
      NPN_SetException(this, "Too few parameters count");
      return true;
    }

    if (NPVARIANT_IS_INT32(args[0]))
      index = NPVARIANT_TO_INT32(args[0]);
    else if (NPVARIANT_IS_DOUBLE(args[0]))
      index = (int)NPVARIANT_TO_DOUBLE(args[0]);
    else {
      NPN_SetException(this, "Wrong argument type");
      return true;
    }

    rc = GetColumnType(index, &ret);
    if (rc != NPERR_NO_ERROR)
      return true;
    INT32_TO_NPVARIANT(ret, *result);
    return true;

  } else if (name == mc_IsColumnNullable_id) {
    bool ret;
    if (argCount < 1) {
      NPN_SetException(this, "Too few parameters count");
      return true;
    }

    if (NPVARIANT_IS_INT32(args[0]))
      index = NPVARIANT_TO_INT32(args[0]);
    else if (NPVARIANT_IS_DOUBLE(args[0]))
      index = (int)NPVARIANT_TO_DOUBLE(args[0]);
    else {
      NPN_SetException(this, "Wrong argument type");
      return true;
    }

    rc = IsColumnNullable(index, &ret);
    if (rc != NPERR_NO_ERROR)
      return true;
    BOOLEAN_TO_NPVARIANT(ret, *result);
    return true;

  } else if (name == mc_GetTables_id) {
    if (argCount < 4) {
      NPN_SetException(this, "Too few parameters count");
      return true;
    }
    if (!(NPVARIANT_IS_NULL(args[0]) || NPVARIANT_IS_STRING(args[0]))) {
      NPN_SetException(this, "Wrong 1 argument type");
      return true;
    }
    if (!(NPVARIANT_IS_NULL(args[1]) || NPVARIANT_IS_STRING(args[1]))) {
      NPN_SetException(this, "Wrong 2 argument type");
      return true;
    }
    if (!(NPVARIANT_IS_NULL(args[2]) || NPVARIANT_IS_STRING(args[2]))) {
      NPN_SetException(this, "Wrong 3 argument type");
      return true;
    }
    if (!(NPVARIANT_IS_NULL(args[3]) || NPVARIANT_IS_STRING(args[3]))) {
      NPN_SetException(this, "Wrong 4 argument type");
      return true;
    }

    GetTables(&args[0], &args[1], &args[2], &args[3]);
    return true;

  } else if (name == mc_GetColumns_id) {
    if (argCount < 4) {
      NPN_SetException(this, "Too few parameters count");
      return true;
    }
    if (!(NPVARIANT_IS_NULL(args[0]) || NPVARIANT_IS_STRING(args[0]))) {
      NPN_SetException(this, "Wrong 1 argument type");
      return true;
    }
    if (!(NPVARIANT_IS_NULL(args[1]) || NPVARIANT_IS_STRING(args[1]))) {
      NPN_SetException(this, "Wrong 2 argument type");
      return true;
    }
    if (!(NPVARIANT_IS_NULL(args[2]) || NPVARIANT_IS_STRING(args[2]))) {
      NPN_SetException(this, "Wrong 3 argument type");
      return true;
    }
    if (!(NPVARIANT_IS_NULL(args[3]) || NPVARIANT_IS_STRING(args[3]))) {
      NPN_SetException(this, "Wrong 4 argument type");
      return true;
    }

    GetColumns(&args[0], &args[1], &args[2], &args[3]);
    return true;

  } else if (name == mc_GetTypeInfo_id) {
    if (argCount < 1) {
      NPN_SetException(this, "Too few parameters count");
      return true;
    }

    if (NPVARIANT_IS_INT32(args[0]))
      index = NPVARIANT_TO_INT32(args[0]);
    else if (NPVARIANT_IS_DOUBLE(args[0]))
      index = (int)NPVARIANT_TO_DOUBLE(args[0]);
    else {
      NPN_SetException(this, "Wrong argument type");
      return true;
    }

    GetTypeInfo(index);
    return true;

  } else if (name == mc_GetPrimaryKeys_id) {
    if (argCount < 3) {
      NPN_SetException(this, "Too few parameters count");
      return true;
    }
    if (!(NPVARIANT_IS_NULL(args[0]) || NPVARIANT_IS_STRING(args[0]))) {
      NPN_SetException(this, "Wrong 1 argument type");
      return true;
    }
    if (!(NPVARIANT_IS_NULL(args[1]) || NPVARIANT_IS_STRING(args[1]))) {
      NPN_SetException(this, "Wrong 2 argument type");
      return true;
    }
    if (!(NPVARIANT_IS_NULL(args[2]) || NPVARIANT_IS_STRING(args[2]))) {
      NPN_SetException(this, "Wrong 3 argument type");
      return true;
    }

    GetPrimaryKeys(&args[0], &args[1], &args[2]);
    return true;

  } else if (name == mc_GetForeignKeys_id) {
    if (argCount < 6) {
      NPN_SetException(this, "Too few parameters count");
      return true;
    }
    if (!(NPVARIANT_IS_NULL(args[0]) || NPVARIANT_IS_STRING(args[0]))) {
      NPN_SetException(this, "Wrong 1 argument type");
      return true;
    }
    if (!(NPVARIANT_IS_NULL(args[1]) || NPVARIANT_IS_STRING(args[1]))) {
      NPN_SetException(this, "Wrong 2 argument type");
      return true;
    }
    if (!(NPVARIANT_IS_NULL(args[2]) || NPVARIANT_IS_STRING(args[2]))) {
      NPN_SetException(this, "Wrong 3 argument type");
      return true;
    }
    if (!(NPVARIANT_IS_NULL(args[3]) || NPVARIANT_IS_STRING(args[3]))) {
      NPN_SetException(this, "Wrong 4 argument type");
      return true;
    }
    if (!(NPVARIANT_IS_NULL(args[4]) || NPVARIANT_IS_STRING(args[4]))) {
      NPN_SetException(this, "Wrong 5 argument type");
      return true;
    }
    if (!(NPVARIANT_IS_NULL(args[5]) || NPVARIANT_IS_STRING(args[5]))) {
      NPN_SetException(this, "Wrong 6 argument type");
      return true;
    }

    GetForeignKeys(&args[0], &args[1], &args[2], &args[3], &args[4], &args[5]);
    return true;

  } else if (name == mc_GetProcedures_id) {
    if (argCount < 3) {
      NPN_SetException(this, "Too few parameters count");
      return true;
    }
    if (!(NPVARIANT_IS_NULL(args[0]) || NPVARIANT_IS_STRING(args[0]))) {
      NPN_SetException(this, "Wrong 1 argument type");
      return true;
    }
    if (!(NPVARIANT_IS_NULL(args[1]) || NPVARIANT_IS_STRING(args[1]))) {
      NPN_SetException(this, "Wrong 2 argument type");
      return true;
    }
    if (!(NPVARIANT_IS_NULL(args[2]) || NPVARIANT_IS_STRING(args[2]))) {
      NPN_SetException(this, "Wrong 3 argument type");
      return true;
    }

    GetProcedures(&args[0], &args[1], &args[2]);
    return true;

  } else if (name == mc_GetProcedureColumns_id) {
    if (argCount < 4) {
      NPN_SetException(this, "Too few parameters count");
      return true;
    }
    if (!(NPVARIANT_IS_NULL(args[0]) || NPVARIANT_IS_STRING(args[0]))) {
      NPN_SetException(this, "Wrong 1 argument type");
      return true;
    }
    if (!(NPVARIANT_IS_NULL(args[1]) || NPVARIANT_IS_STRING(args[1]))) {
      NPN_SetException(this, "Wrong 2 argument type");
      return true;
    }
    if (!(NPVARIANT_IS_NULL(args[2]) || NPVARIANT_IS_STRING(args[2]))) {
      NPN_SetException(this, "Wrong 3 argument type");
      return true;
    }
    if (!(NPVARIANT_IS_NULL(args[3]) || NPVARIANT_IS_STRING(args[3]))) {
      NPN_SetException(this, "Wrong 4 argument type");
      return true;
    }

    GetProcedureColumns(&args[0], &args[1], &args[2], &args[3]);
    return true;
  
  }  

  return false;
}
Ejemplo n.º 24
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;
}
Ejemplo n.º 25
0
static bool pluginInvoke(NPObject *header, NPIdentifier name, const NPVariant *args, uint32_t argCount, NPVariant *result)
{
    PluginObject *obj = (PluginObject *)header;
    if (name == pluginMethodIdentifiers[ID_TEST_CALLBACK_METHOD]) {
        // call whatever method name we're given
        if (argCount > 0 && NPVARIANT_IS_STRING(args[0])) {
            NPObject *windowScriptObject;
            browser->getvalue(obj->npp, NPNVWindowNPObject, &windowScriptObject);

            NPUTF8* callbackString = createCStringFromNPVariant(&args[0]);
            NPIdentifier callbackIdentifier = browser->getstringidentifier(callbackString);
            free(callbackString);

            NPVariant browserResult;
            browser->invoke(obj->npp, windowScriptObject, callbackIdentifier, 0, 0, &browserResult);
            browser->releasevariantvalue(&browserResult);

            VOID_TO_NPVARIANT(*result);
            return true;
        }
    } else if (name == pluginMethodIdentifiers[ID_TEST_GETURL]) {
        if (argCount == 2 && NPVARIANT_IS_STRING(args[0]) && NPVARIANT_IS_STRING(args[1])) {
            NPUTF8* urlString = createCStringFromNPVariant(&args[0]);
            NPUTF8* targetString = createCStringFromNPVariant(&args[1]);
            browser->geturl(obj->npp, urlString, targetString);
            free(urlString);
            free(targetString);

            VOID_TO_NPVARIANT(*result);
            return true;
        } else if (argCount == 1 && NPVARIANT_IS_STRING(args[0])) {
            NPUTF8* urlString = createCStringFromNPVariant(&args[0]);
            browser->geturl(obj->npp, urlString, 0);
            free(urlString);

            VOID_TO_NPVARIANT(*result);
            return true;
        }
    } else if (name == pluginMethodIdentifiers[ID_REMOVE_DEFAULT_METHOD]) {
        pluginClass.invokeDefault = 0;
        VOID_TO_NPVARIANT(*result);
        return true;
    } else if (name == pluginMethodIdentifiers[ID_TEST_DOM_ACCESS]) {
        testDOMAccess(obj);
        VOID_TO_NPVARIANT(*result);
        return true;
    } else if (name == pluginMethodIdentifiers[ID_TEST_GET_URL_NOTIFY]) {
        if (argCount == 3
          && NPVARIANT_IS_STRING(args[0])
          && (NPVARIANT_IS_STRING(args[1]) || NPVARIANT_IS_NULL(args[1]))
          && NPVARIANT_IS_STRING(args[2])) {
            NPUTF8* urlString = createCStringFromNPVariant(&args[0]);
            NPUTF8* targetString = (NPVARIANT_IS_STRING(args[1]) ? createCStringFromNPVariant(&args[1]) : NULL);
            NPUTF8* callbackString = createCStringFromNPVariant(&args[2]);
            
            NPIdentifier callbackIdentifier = browser->getstringidentifier(callbackString);
            browser->geturlnotify(obj->npp, urlString, targetString, callbackIdentifier);

            free(urlString);
            free(targetString);
            free(callbackString);
            
            VOID_TO_NPVARIANT(*result);
            return true;
        }
    } else if (name == pluginMethodIdentifiers[ID_TEST_INVOKE_DEFAULT] && NPVARIANT_IS_OBJECT(args[0])) {
        NPObject *callback = NPVARIANT_TO_OBJECT(args[0]);
        
        NPVariant args[1];
        NPVariant browserResult;
        
        STRINGZ_TO_NPVARIANT("test", args[0]);
        bool retval = browser->invokeDefault(obj->npp, callback, args, 1, &browserResult);
        
        if (retval)
            browser->releasevariantvalue(&browserResult);
        
        BOOLEAN_TO_NPVARIANT(retval, *result);
        return true;
    } else if (name == pluginMethodIdentifiers[ID_TEST_ENUMERATE]) {
        if (argCount == 2 && NPVARIANT_IS_OBJECT(args[0]) && NPVARIANT_IS_OBJECT(args[1])) {
            uint32_t count;            
            NPIdentifier* identifiers;

            if (browser->enumerate(obj->npp, NPVARIANT_TO_OBJECT(args[0]), &identifiers, &count)) {
                NPObject* outArray = NPVARIANT_TO_OBJECT(args[1]);
                NPIdentifier pushIdentifier = browser->getstringidentifier("push");
                
                for (uint32_t i = 0; i < count; i++) {
                    NPUTF8* string = browser->utf8fromidentifier(identifiers[i]);
                    
                    if (!string)
                        continue;
                                        
                    NPVariant args[1];
                    STRINGZ_TO_NPVARIANT(string, args[0]);
                    NPVariant browserResult;
                    browser->invoke(obj->npp, outArray, pushIdentifier, args, 1, &browserResult);
                    browser->releasevariantvalue(&browserResult);
                    browser->memfree(string);
                }
                
                browser->memfree(identifiers);
            }
            
            VOID_TO_NPVARIANT(*result);
            return true;            
        }
        return false;
    } else if (name == pluginMethodIdentifiers[ID_DESTROY_STREAM]) {
        NPError npError = browser->destroystream(obj->npp, obj->stream, NPRES_USER_BREAK);
        INT32_TO_NPVARIANT(npError, *result);
        return true;        
    } else if (name == pluginMethodIdentifiers[ID_TEST_GETINTIDENTIFIER]) {
        if (argCount == 1) {
            NPIdentifier identifier;

            if (NPVARIANT_IS_DOUBLE(args[0])) {
                identifier = browser->getintidentifier((int)NPVARIANT_TO_DOUBLE(args[0]));
                INT32_TO_NPVARIANT((int32)identifier, *result);
                return true;
            } else if (NPVARIANT_IS_INT32(args[0])) {
                identifier = browser->getintidentifier((int)NPVARIANT_TO_INT32(args[0]));
                INT32_TO_NPVARIANT((int32)identifier, *result);
                return true;
            }
        }
    } else if (name == pluginMethodIdentifiers[ID_TEST_EVALUATE] && 
               argCount == 1 && NPVARIANT_IS_STRING(args[0])) {
        NPObject *windowScriptObject;
        browser->getvalue(obj->npp, NPNVWindowNPObject, &windowScriptObject);

        NPString s = NPVARIANT_TO_STRING(args[0]);
        
        bool retval = browser->evaluate(obj->npp, windowScriptObject, &s, result);
        browser->releaseobject(windowScriptObject);
        return retval;
    } else if (name == pluginMethodIdentifiers[ID_TEST_GET_PROPERTY] &&
               argCount > 0) {
        NPObject *object;
        browser->getvalue(obj->npp, NPNVWindowNPObject, &object);

        for (uint32_t i = 0; i < argCount; i++) {
            //assert(NPVARIANT_IS_STRING(args[i]));
            NPUTF8* propertyString = createCStringFromNPVariant(&args[i]);
            NPIdentifier propertyIdentifier = browser->getstringidentifier(propertyString);
            free(propertyString);
            
            NPVariant variant;
            bool retval = browser->getproperty(obj->npp, object, propertyIdentifier, &variant);
            browser->releaseobject(object);
            
            if (!retval)
                break;
            
            if (i + 1 < argCount) {
                //assert(NPVARIANT_IS_OBJECT(variant));
                object = NPVARIANT_TO_OBJECT(variant);
            } else {                
                *result = variant;
                return true;
            }
        }
        
        VOID_TO_NPVARIANT(*result);
        return false;
    } else if (name == pluginMethodIdentifiers[ID_TEST_GET_PROPERTY_RETURN_VALUE] &&
        argCount == 2 && NPVARIANT_IS_OBJECT(args[0]) && NPVARIANT_IS_STRING(args[1])) {
        NPUTF8* propertyString = createCStringFromNPVariant(&args[1]);
        NPIdentifier propertyIdentifier = browser->getstringidentifier(propertyString);
        free(propertyString);

        NPVariant variant;
        bool retval = browser->getproperty(obj->npp, NPVARIANT_TO_OBJECT(args[0]), propertyIdentifier, &variant);
        if (retval)
            browser->releasevariantvalue(&variant);

        BOOLEAN_TO_NPVARIANT(retval, *result);
        return true;
    } else if (name == pluginMethodIdentifiers[ID_TEST_CALLBACK_METHOD_RET]) {
        // call whatever method name we're given, and pass it the 'window' obj.
        // we expect the function to return its argument.
        if (argCount > 0 && NPVARIANT_IS_STRING(args[0])) {
            NPObject *windowScriptObject;
            browser->getvalue(obj->npp, NPNVWindowNPObject, &windowScriptObject);

            NPUTF8* callbackString = createCStringFromNPVariant(&args[0]);
            NPIdentifier callbackIdentifier = browser->getstringidentifier(callbackString);
            free(callbackString);

            NPVariant callbackArgs[1];
            OBJECT_TO_NPVARIANT(windowScriptObject, callbackArgs[0]);

            NPVariant browserResult;
            browser->invoke(obj->npp, windowScriptObject, callbackIdentifier,
                            callbackArgs, 1, &browserResult);

            if (NPVARIANT_IS_OBJECT(browserResult)) {
                // Now return the callbacks return value back to our caller.
                // BUG 897451: This should be the same as the
                // windowScriptObject, but its not (in Chrome) - or at least, it
                // has a different refcount. This means Chrome will delete the
                // object before returning it and the calling JS gets a garbage
                // value.  Firefox handles it fine.
                OBJECT_TO_NPVARIANT(NPVARIANT_TO_OBJECT(browserResult), *result);
            } else {                
                browser->releasevariantvalue(&browserResult);
                VOID_TO_NPVARIANT(*result);
            }

            return true;
        }
    } else if (name == pluginMethodIdentifiers[ID_TEST_CREATE_TEST_OBJECT]) {
        NPObject *testObject = browser->createobject(obj->npp, getTestClass());
        //assert(testObject->referenceCount == 1);
        OBJECT_TO_NPVARIANT(testObject, *result);
        return true;
    } else if (name == pluginMethodIdentifiers[ID_TEST_PASS_TEST_OBJECT]) {
        // call whatever method name we're given, and pass it our second
        // argument.
        if (argCount > 1 && NPVARIANT_IS_STRING(args[0])) {
            NPObject *windowScriptObject;
            browser->getvalue(obj->npp, NPNVWindowNPObject, &windowScriptObject);

            NPUTF8* callbackString = createCStringFromNPVariant(&args[0]);
            NPIdentifier callbackIdentifier = browser->getstringidentifier(callbackString);
            free(callbackString);

            NPVariant browserResult;
            browser->invoke(obj->npp, windowScriptObject, callbackIdentifier, &args[1], 1, &browserResult);
            browser->releasevariantvalue(&browserResult);

            VOID_TO_NPVARIANT(*result);
            return true;
        }
    } else if (name == pluginMethodIdentifiers[ID_TEST_CLONE_OBJECT]) {
        // Create another instance of the same class
        NPObject *new_object = browser->createobject(obj->npp, &pluginClass);
        //assert(new_object->referenceCount == 1);
        OBJECT_TO_NPVARIANT(new_object, *result);
        return true;
    } else if (name == pluginMethodIdentifiers[ID_TEST_SCRIPT_OBJECT_INVOKE]) {
        if (argCount > 1 && NPVARIANT_IS_STRING(args[0])) {
            // Invoke a script callback to get a script NPObject. Then call
            // a method on the script NPObject passing it a freshly created
            // NPObject.
            // Arguments:
            // arg1:  Callback that returns a script object.
            // arg2:  Name of the method to call on the script object returned 
            //        from the callback
            NPObject *windowScriptObject;
            browser->getvalue(obj->npp, NPNVWindowNPObject, 
                              &windowScriptObject);

            // Arg1 is the name of the callback
            NPUTF8* callbackString = createCStringFromNPVariant(&args[0]);
            NPIdentifier callbackIdentifier = 
                  browser->getstringidentifier(callbackString);
            free(callbackString);

            // Invoke a callback that returns a script object
            NPVariant object_result;
            browser->invoke(obj->npp, windowScriptObject, callbackIdentifier, 
                            &args[1], 1, &object_result);

            // Script object returned
            NPObject *script_object = object_result.value.objectValue;

            // Arg2 is the name of the method to be called on the script object
            NPUTF8* object_mehod_string = createCStringFromNPVariant(&args[1]);
            NPIdentifier object_method = 
                browser->getstringidentifier(object_mehod_string);
            free(object_mehod_string);

            // Create a fresh NPObject to be passed as an argument
            NPObject *object_arg = browser->createobject(obj->npp, &pluginClass);
            NPVariant invoke_args[1];
            OBJECT_TO_NPVARIANT(object_arg, invoke_args[0]);

            // Invoke the script method
            NPVariant object_method_result;
            browser->invoke(obj->npp, script_object, object_method,
                            invoke_args, 1, &object_method_result);

            browser->releasevariantvalue(&object_result);
            VOID_TO_NPVARIANT(*result);
            if (NPVARIANT_IS_OBJECT(object_method_result)) {
                // Now return the callbacks return value back to our caller.
                // BUG 897451: This should be the same as the
                // windowScriptObject, but its not (in Chrome) - or at least, it
                // has a different refcount. This means Chrome will delete the
                // object before returning it and the calling JS gets a garbage
                // value.  Firefox handles it fine.
                OBJECT_TO_NPVARIANT(NPVARIANT_TO_OBJECT(object_method_result),
                                    *result);
            } else {                
                browser->releasevariantvalue(&object_method_result);
                VOID_TO_NPVARIANT(*result);
            }
            return true;
        }
    }
    return false;
}
Ejemplo n.º 26
0
static bool pluginInvoke(NPObject* header, NPIdentifier name, const NPVariant* args, uint32_t argCount, NPVariant* result)
{
    PluginObject* plugin = reinterpret_cast<PluginObject*>(header);
    if (name == pluginMethodIdentifiers[ID_TEST_CALLBACK_METHOD])
        return testCallback(plugin, args, argCount, result);
    if (name == pluginMethodIdentifiers[ID_TEST_CALLBACK_METHOD_RETURN])
        return testCallbackReturn(plugin, args, argCount, result);
    if (name == pluginMethodIdentifiers[ID_TEST_GETURL])
        return getURL(plugin, args, argCount, result);
    if (name == pluginMethodIdentifiers[ID_TEST_DOM_ACCESS])
        return testDOMAccess(plugin, args, argCount, result);
    if (name == pluginMethodIdentifiers[ID_TEST_GET_URL_NOTIFY])
        return getURLNotify(plugin, args, argCount, result);
    if (name == pluginMethodIdentifiers[ID_TEST_INVOKE_DEFAULT])
        return testInvokeDefault(plugin, args, argCount, result);
    if (name == pluginMethodIdentifiers[ID_TEST_ENUMERATE])
        return testEnumerate(plugin, args, argCount, result);
    if (name == pluginMethodIdentifiers[ID_DESTROY_STREAM])
        return destroyStream(plugin, args, argCount, result);
    if (name == pluginMethodIdentifiers[ID_TEST_GETINTIDENTIFIER])
        return testGetIntIdentifier(plugin, args, argCount, result);
    if (name == pluginMethodIdentifiers[ID_TEST_EVALUATE])
        return testEvaluate(plugin, args, argCount, result);
    if (name == pluginMethodIdentifiers[ID_TEST_GET_PROPERTY])
        return testGetProperty(plugin, args, argCount, result);
    if (name == pluginMethodIdentifiers[ID_TEST_GET_PROPERTY_RETURN_VALUE])
        return testGetPropertyReturnValue(plugin, args, argCount, result);
    if (name == pluginMethodIdentifiers[ID_TEST_HAS_PROPERTY])
        return testHasProperty(plugin, args, argCount, result);
    if (name == pluginMethodIdentifiers[ID_TEST_HAS_METHOD])
        return testHasMethod(plugin, args, argCount, result);
    if (name == pluginMethodIdentifiers[ID_TEST_IDENTIFIER_TO_STRING])
        return testIdentifierToString(plugin, args, argCount, result);
    if (name == pluginMethodIdentifiers[ID_TEST_IDENTIFIER_TO_INT])
        return testIdentifierToInt(plugin, args, argCount, result);
    if (name == pluginMethodIdentifiers[ID_TEST_PASS_TEST_OBJECT])
        return testPassTestObject(plugin, args, argCount, result);
    if (name == pluginMethodIdentifiers[ID_TEST_POSTURL_FILE])
        return testPostURLFile(plugin, args, argCount, result);
    if (name == pluginMethodIdentifiers[ID_TEST_CONSTRUCT])
        return testConstruct(plugin, args, argCount, result);
    if (name == pluginMethodIdentifiers[ID_TEST_SCRIPT_OBJECT_INVOKE])
        return testScriptObjectInvoke(plugin, args, argCount, result);
    if (name == pluginMethodIdentifiers[ID_TEST_THROW_EXCEPTION_METHOD]) {
        browser->setexception(header, "plugin object testThrowException SUCCESS");
        return true;
    }
    if (name == pluginMethodIdentifiers[ID_TEST_FAIL_METHOD]) {
        NPObject* windowScriptObject;
        browser->getvalue(plugin->npp, NPNVWindowNPObject, &windowScriptObject);
        browser->invoke(plugin->npp, windowScriptObject, name, args, argCount, result);
        return false;
    }
    if (name == pluginMethodIdentifiers[ID_TEST_CLONE_OBJECT]) {
        NPObject* new_object = browser->createobject(plugin->npp, &pluginClass);
        assert(new_object->referenceCount == 1);
        OBJECT_TO_NPVARIANT(new_object, *result);
        return true;
    }
    if (name == pluginMethodIdentifiers[ID_TEST_CREATE_TEST_OBJECT]) {
        NPObject* testObject = browser->createobject(plugin->npp, getTestClass());
        assert(testObject->referenceCount == 1);
        OBJECT_TO_NPVARIANT(testObject, *result);
        return true;
    }
    if (name == pluginMethodIdentifiers[ID_DESTROY_NULL_STREAM])
        return destroyNullStream(plugin, args, argCount, result);
    if (name == pluginMethodIdentifiers[ID_TEST_RELOAD_PLUGINS_NO_PAGES]) {
        browser->reloadplugins(false);
        return true;
    }
    if (name == pluginMethodIdentifiers[ID_TEST_RELOAD_PLUGINS_AND_PAGES]) {
        browser->reloadplugins(true);
        return true;
    }
    if (name == pluginMethodIdentifiers[ID_TEST_GET_BROWSER_PROPERTY]) {
        browser->getproperty(plugin->npp, NPVARIANT_TO_OBJECT(args[0]), stringVariantToIdentifier(args[1]), result);
        return true;
    }
    if (name == pluginMethodIdentifiers[ID_TEST_SET_BROWSER_PROPERTY]) {
        browser->setproperty(plugin->npp, NPVARIANT_TO_OBJECT(args[0]), stringVariantToIdentifier(args[1]), &args[2]);
        return true;
    }
    if (name == pluginMethodIdentifiers[ID_REMEMBER]) {
        if (plugin->rememberedObject)
            browser->releaseobject(plugin->rememberedObject);
        plugin->rememberedObject = NPVARIANT_TO_OBJECT(args[0]);
        browser->retainobject(plugin->rememberedObject);
        VOID_TO_NPVARIANT(*result);
        return true;
    }
    if (name == pluginMethodIdentifiers[ID_GET_REMEMBERED_OBJECT]) {
        assert(plugin->rememberedObject);
        browser->retainobject(plugin->rememberedObject);
        OBJECT_TO_NPVARIANT(plugin->rememberedObject, *result);
        return true;
    }
    if (name == pluginMethodIdentifiers[ID_GET_AND_FORGET_REMEMBERED_OBJECT]) {
        assert(plugin->rememberedObject);
        OBJECT_TO_NPVARIANT(plugin->rememberedObject, *result);
        plugin->rememberedObject = 0;
        return true;
    }
    if (name == pluginMethodIdentifiers[ID_REF_COUNT]) {
        uint32_t refCount = NPVARIANT_TO_OBJECT(args[0])->referenceCount;
        INT32_TO_NPVARIANT(refCount, *result);
        return true;
    }
    if (name == pluginMethodIdentifiers[ID_SET_STATUS])
        return testSetStatus(plugin, args, argCount, result);
    if (name == pluginMethodIdentifiers[ID_RESIZE_TO])
        return testResizeTo(plugin, args, argCount, result);
    if (name == pluginMethodIdentifiers[ID_NORMALIZE])
        return normalizeOverride(plugin, args, argCount, result);
    if (name == pluginMethodIdentifiers[ID_INVALIDATE_RECT])
        return invalidateRect(plugin, args, argCount, result);
    if (name == pluginMethodIdentifiers[ID_OBJECTS_ARE_SAME])
        return objectsAreSame(plugin, args, argCount, result);

    return false;
}
Ejemplo n.º 27
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);
}
Ejemplo n.º 28
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;
}
Ejemplo n.º 29
0
jvalue convertNPVariantToJValue(NPVariant value, JNIType jniType, const char* javaClassName)
{
    jvalue result;
    NPVariantType type = value.type;

    switch (jniType) {
    case array_type:
    case object_type:
        {
            result.l = static_cast<jobject>(0);

            // First see if we have a Java instance.
            if (type == NPVariantType_Object) {
                NPObject* objectImp = NPVARIANT_TO_OBJECT(value);
                if (JavaInstance* instance = ExtractJavaInstance(objectImp))
                    result.l = instance->javaInstance();
            }

            // Now convert value to a string if the target type is a java.lang.string, and we're not
            // converting from a Null.
            if (!result.l && !strcmp(javaClassName, "java.lang.String")) {
#ifdef CONVERT_NULL_TO_EMPTY_STRING
                if (type == NPVariantType_Null) {
                    JNIEnv* env = getJNIEnv();
                    jchar buf[2];
                    jobject javaString = env->functions->NewString(env, buf, 0);
                    result.l = javaString;
                } else
#else
                if (type == NPVariantType_String)
#endif
                {
                    NPString src = NPVARIANT_TO_STRING(value);
                    JNIEnv* env = getJNIEnv();
                    jobject javaString = env->NewStringUTF(src.UTF8Characters);
                    result.l = javaString;
                }
            } else if (!result.l)
                memset(&result, 0, sizeof(jvalue)); // Handle it the same as a void case
        }
        break;

    case boolean_type:
        {
            if (type == NPVariantType_Bool)
                result.z = NPVARIANT_TO_BOOLEAN(value);
            else
                memset(&result, 0, sizeof(jvalue)); // as void case
        }
        break;

    case byte_type:
        {
            if (type == NPVariantType_Int32)
                result.b = static_cast<char>(NPVARIANT_TO_INT32(value));
            else
                memset(&result, 0, sizeof(jvalue));
        }
        break;

    case char_type:
        {
            if (type == NPVariantType_Int32)
                result.c = static_cast<char>(NPVARIANT_TO_INT32(value));
            else
                memset(&result, 0, sizeof(jvalue));
        }
        break;

    case short_type:
        {
            if (type == NPVariantType_Int32)
                result.s = static_cast<jshort>(NPVARIANT_TO_INT32(value));
            else
                memset(&result, 0, sizeof(jvalue));
        }
        break;

    case int_type:
        {
            if (type == NPVariantType_Int32)
                result.i = static_cast<jint>(NPVARIANT_TO_INT32(value));
            else
                memset(&result, 0, sizeof(jvalue));
        }
        break;

    case long_type:
        {
            if (type == NPVariantType_Int32)
                result.j = static_cast<jlong>(NPVARIANT_TO_INT32(value));
            else if (type == NPVariantType_Double)
                result.j = static_cast<jlong>(NPVARIANT_TO_DOUBLE(value));
            else
                memset(&result, 0, sizeof(jvalue));
        }
        break;

    case float_type:
        {
            if (type == NPVariantType_Int32)
                result.f = static_cast<jfloat>(NPVARIANT_TO_INT32(value));
            else if (type == NPVariantType_Double)
                result.f = static_cast<jfloat>(NPVARIANT_TO_DOUBLE(value));
            else
                memset(&result, 0, sizeof(jvalue));
        }
        break;

    case double_type:
        {
            if (type == NPVariantType_Int32)
                result.d = static_cast<jdouble>(NPVARIANT_TO_INT32(value));
            else if (type == NPVariantType_Double)
                result.d = static_cast<jdouble>(NPVARIANT_TO_DOUBLE(value));
            else
                memset(&result, 0, sizeof(jvalue));
        }
        break;

        break;

    case invalid_type:
    default:
    case void_type:
        {
            memset(&result, 0, sizeof(jvalue));
        }
        break;
    }
    return result;
}
Ejemplo n.º 30
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;
}