Add a plugin test that sets and then gets a cookie. b=530980 r=roc

This commit is contained in:
Josh Aas 2009-12-15 15:14:03 -05:00
parent 42cb415b08
commit 348c5ba6e1
4 changed files with 116 additions and 0 deletions

View File

@ -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

View File

@ -0,0 +1,20 @@
<html>
<head>
<title>NPAPI Cookie Tests</title>
<script type="application/javascript" src="chrome://mochikit/content/MochiKit/packed.js"></script>
<script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
</head>
<body onload="runTests()">
<embed id="plugin1" type="application/x-test" width="400" height="400"></embed>
<script class="testbody" type="application/javascript">
SimpleTest.waitForExplicitFinish();
function runTests() {
var pluginElement = document.getElementById("plugin1");
pluginElement.setCookie("foo");
ok(pluginElement.getCookie() == "foo", "Cookie was set and retrieved correctly via NPAPI.");
SimpleTest.finish();
}
</script>
</body>
</html>

View File

@ -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.

View File

@ -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<TestNPObject*>(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<TestNPObject*>(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)
{