gecko-dev/dom/workers/test/serviceworkers/periodic.sjs
Ehsan Akhgari 33bf6126a9 Bug 1159378 - Part 3: Refactor the logic of test_periodic_update.html into a helper script; r=nsm
This helper script will allow us to run the same test across both HTTP
and HTTPS origins.  This patch also uses postMessage instead of a direct
function call on the parent window, because in the HTTPS case, that
window will be cross-origin.  It also moves the code to trigger the
update and get the corresponding updatefound event to
wait_for_update.html, since getting the service worker registration
cross-origin is not possible.

--HG--
extra : rebase_source : 4df46df635f6445f286b54a8a3d9b2e3f8b8447a
2015-05-04 10:18:12 -04:00

25 lines
754 B
JavaScript

function handleRequest(request, response) {
var stateName = request.scheme + 'periodiccounter';
if (request.queryString == 'clearcounter') {
setState(stateName, '');
return;
}
if (!getState(stateName)) {
setState(stateName, '1');
} else {
// Make sure that we pass a string value to setState!
setState(stateName, "" + (parseInt(getState(stateName)) + 1));
}
response.setHeader("Content-Type", "application/javascript", false);
response.write(getScript(stateName));
}
function getScript(stateName) {
return "onfetch = function(e) {" +
"if (e.request.url.indexOf('get-sw-version') > -1) {" +
"e.respondWith(new Response('" + getState(stateName) + "'));" +
"}" +
"};";
}