From a02e04468f641f88f7a47c6869fefe9bcff6bd51 Mon Sep 17 00:00:00 2001 From: Josh Aas Date: Tue, 19 Jan 2010 15:45:21 -0500 Subject: [PATCH] Add a plugin test that sets and then gets a cookie. b=530980 r=roc --- modules/plugin/test/mochitest/Makefile.in | 1 + .../plugin/test/mochitest/test_cookies.html | 20 ++++ modules/plugin/test/testplugin/README | 10 +- modules/plugin/test/testplugin/nptest.cpp | 96 +++++++++++++++++++ 4 files changed, 126 insertions(+), 1 deletion(-) create mode 100644 modules/plugin/test/mochitest/test_cookies.html diff --git a/modules/plugin/test/mochitest/Makefile.in b/modules/plugin/test/mochitest/Makefile.in index d4af1d1ee3a1..94b7549d3136 100644 --- a/modules/plugin/test/mochitest/Makefile.in +++ b/modules/plugin/test/mochitest/Makefile.in @@ -68,6 +68,7 @@ _MOCHITEST_FILES = \ test_multipleinstanceobjects.html \ test_streamNotify.html \ test_instantiation.html \ + test_cookies.html \ $(NULL) # test_npruntime_npnsetexception.html \ Disabled for e10s diff --git a/modules/plugin/test/mochitest/test_cookies.html b/modules/plugin/test/mochitest/test_cookies.html new file mode 100644 index 000000000000..86f2429aaeec --- /dev/null +++ b/modules/plugin/test/mochitest/test_cookies.html @@ -0,0 +1,20 @@ + + + NPAPI Cookie Tests + + + + + + + + + diff --git a/modules/plugin/test/testplugin/README b/modules/plugin/test/testplugin/README index 64a876251287..7ed9e797b28c 100644 --- a/modules/plugin/test/testplugin/README +++ b/modules/plugin/test/testplugin/README @@ -312,9 +312,17 @@ overridden windowproc. widget, if the plugin is windowed. If it's not windowed they're passed to the overriden windowproc (but hopefully never sent by the browser anyway). +== Getting and Setting Cookies == + +* setCookie(string) +Sets the given string as the cookie for window's URL. + +* getCookie() +Returns the cookie string for the window's URL, the cookie set by setCookie. + == FPU Control == x86-only on some OSes: * The .enableFPExceptions() method will enable floating-point exceptions, - as evil plugins or extensions might do. \ No newline at end of file + as evil plugins or extensions might do. diff --git a/modules/plugin/test/testplugin/nptest.cpp b/modules/plugin/test/testplugin/nptest.cpp index 0ed64938f86a..a48d57760091 100644 --- a/modules/plugin/test/testplugin/nptest.cpp +++ b/modules/plugin/test/testplugin/nptest.cpp @@ -135,6 +135,8 @@ static bool crashOnDestroy(NPObject* npobj, const NPVariant* args, uint32_t argC static bool getObjectValue(NPObject* npobj, const NPVariant* args, uint32_t argCount, NPVariant* result); static bool checkObjectValue(NPObject* npobj, const NPVariant* args, uint32_t argCount, NPVariant* result); static bool enableFPExceptions(NPObject* npobj, const NPVariant* args, uint32_t argCount, NPVariant* result); +static bool setCookie(NPObject* npobj, const NPVariant* args, uint32_t argCount, NPVariant* result); +static bool getCookie(NPObject* npobj, const NPVariant* args, uint32_t argCount, NPVariant* result); static const NPUTF8* sPluginMethodIdentifierNames[] = { "npnEvaluateTest", @@ -169,6 +171,8 @@ static const NPUTF8* sPluginMethodIdentifierNames[] = { "getObjectValue", "checkObjectValue", "enableFPExceptions", + "setCookie", + "getCookie", }; static NPIdentifier sPluginMethodIdentifiers[ARRAY_LENGTH(sPluginMethodIdentifierNames)]; static const ScriptableFunction sPluginMethodFunctions[ARRAY_LENGTH(sPluginMethodIdentifierNames)] = { @@ -204,6 +208,8 @@ static const ScriptableFunction sPluginMethodFunctions[ARRAY_LENGTH(sPluginMetho getObjectValue, checkObjectValue, enableFPExceptions, + setCookie, + getCookie, }; struct URLNotifyData @@ -1313,6 +1319,18 @@ NPN_ConvertPoint(NPP instance, double sourceX, double sourceY, NPCoordinateSpace return sBrowserFuncs->convertpoint(instance, sourceX, sourceY, sourceSpace, destX, destY, destSpace); } +NPError +NPN_SetValueForURL(NPP instance, NPNURLVariable variable, const char *url, const char *value, uint32_t len) +{ + return sBrowserFuncs->setvalueforurl(instance, variable, url, value, len); +} + +NPError +NPN_GetValueForURL(NPP instance, NPNURLVariable variable, const char *url, char **value, uint32_t *len) +{ + return sBrowserFuncs->getvalueforurl(instance, variable, url, value, len); +} + // // npruntime object functions // @@ -2183,3 +2201,81 @@ static bool enableFPExceptions(NPObject* npobj, const NPVariant* args, uint32_t return false; #endif } + +// caller is responsible for freeing return buffer +static char* URLForInstanceWindow(NPP instance) { + char *outString = NULL; + + NPObject* windowObject = NULL; + NPError err = NPN_GetValue(instance, NPNVWindowNPObject, &windowObject); + if (err != NPERR_NO_ERROR || !windowObject) + return NULL; + + NPIdentifier locationIdentifier = NPN_GetStringIdentifier("location"); + NPVariant locationVariant; + if (NPN_GetProperty(instance, windowObject, locationIdentifier, &locationVariant)) { + NPObject *locationObject = locationVariant.value.objectValue; + if (locationObject) { + NPIdentifier hrefIdentifier = NPN_GetStringIdentifier("href"); + NPVariant hrefVariant; + if (NPN_GetProperty(instance, locationObject, hrefIdentifier, &hrefVariant)) { + const NPString* hrefString = &NPVARIANT_TO_STRING(hrefVariant); + if (hrefString) { + outString = (char *)malloc(hrefString->UTF8Length + 1); + if (outString) { + strcpy(outString, hrefString->UTF8Characters); + outString[hrefString->UTF8Length] = '\0'; + } + } + NPN_ReleaseVariantValue(&hrefVariant); + } + } + NPN_ReleaseVariantValue(&locationVariant); + } + + NPN_ReleaseObject(windowObject); + + return outString; +} + +static bool +setCookie(NPObject* npobj, const NPVariant* args, uint32_t argCount, NPVariant* result) +{ + if (argCount != 1) + return false; + if (!NPVARIANT_IS_STRING(args[0])) + return false; + const NPString* cookie = &NPVARIANT_TO_STRING(args[0]); + + NPP npp = static_cast(npobj)->npp; + + char* url = URLForInstanceWindow(npp); + if (!url) + return false; + NPError err = NPN_SetValueForURL(npp, NPNURLVCookie, url, cookie->UTF8Characters, cookie->UTF8Length); + free(url); + + return (err == NPERR_NO_ERROR); +} + +static bool +getCookie(NPObject* npobj, const NPVariant* args, uint32_t argCount, NPVariant* result) +{ + if (argCount != 0) + return false; + + NPP npp = static_cast(npobj)->npp; + + char* url = URLForInstanceWindow(npp); + if (!url) + return false; + char* cookie = NULL; + unsigned int length = 0; + NPError err = NPN_GetValueForURL(npp, NPNURLVCookie, url, &cookie, &length); + free(url); + if (err != NPERR_NO_ERROR || !cookie) + return false; + + STRINGZ_TO_NPVARIANT(cookie, *result); + return true; +}