From 348c5ba6e143b75111b040efc2ef64ed714519a8 Mon Sep 17 00:00:00 2001 From: Josh Aas Date: Tue, 15 Dec 2009 15:14:03 -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 | 8 ++ modules/plugin/test/testplugin/nptest.cpp | 87 +++++++++++++++++++ 4 files changed, 116 insertions(+) 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 d1be0e2421ca..df9e0d027c43 100644 --- a/modules/plugin/test/mochitest/Makefile.in +++ b/modules/plugin/test/mochitest/Makefile.in @@ -65,6 +65,7 @@ _MOCHITEST_FILES = \ test_pluginstream_seek.html \ test_pluginstream_newstream.html \ test_streamNotify.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 3bd1ae9c8552..b1658f85d252 100644 --- a/modules/plugin/test/testplugin/README +++ b/modules/plugin/test/testplugin/README @@ -301,3 +301,11 @@ overridden windowproc. * WM_MOUSEACTIVATE events are handled by calling SetFocus on the plugin's 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. diff --git a/modules/plugin/test/testplugin/nptest.cpp b/modules/plugin/test/testplugin/nptest.cpp index 9ae8257d4825..8e95934c54c2 100644 --- a/modules/plugin/test/testplugin/nptest.cpp +++ b/modules/plugin/test/testplugin/nptest.cpp @@ -103,6 +103,8 @@ static bool convertPointX(NPObject* npobj, const NPVariant* args, uint32_t argCo static bool convertPointY(NPObject* npobj, const NPVariant* args, uint32_t argCount, NPVariant* result); static bool streamTest(NPObject* npobj, const NPVariant* args, uint32_t argCount, NPVariant* result); static bool crashPlugin(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", @@ -132,6 +134,8 @@ static const NPUTF8* sPluginMethodIdentifierNames[] = { "convertPointY", "streamTest", "crash", + "setCookie", + "getCookie", }; static NPIdentifier sPluginMethodIdentifiers[ARRAY_LENGTH(sPluginMethodIdentifierNames)]; static const ScriptableFunction sPluginMethodFunctions[ARRAY_LENGTH(sPluginMethodIdentifierNames)] = { @@ -162,6 +166,8 @@ static const ScriptableFunction sPluginMethodFunctions[ARRAY_LENGTH(sPluginMetho convertPointY, streamTest, crashPlugin, + setCookie, + getCookie, }; struct URLNotifyData @@ -1263,6 +1269,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 // @@ -2048,6 +2066,75 @@ crashPlugin(NPObject* npobj, const NPVariant* args, uint32_t argCount, NPVariant return true; } +// caller is responsible for freeing return buffer +static char* URLForInstanceWindow(NPP instance) { + char *outString = NULL; + + NPObject* windowObject = NULL; + NPN_GetValue(instance, NPNVWindowNPObject, &windowObject); + + 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); + } + 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); + 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* cookie = NULL; + unsigned int length = 0; + char* url = URLForInstanceWindow(npp); + 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; +} + static bool setColor(NPObject* npobj, const NPVariant* args, uint32_t argCount, NPVariant* result) {