2012-08-14 18:06:04 +04:00
|
|
|
Cu.import("resource://testing-common/httpd.js");
|
2014-10-25 02:16:29 +04:00
|
|
|
Cu.import("resource://gre/modules/Services.jsm");
|
2015-12-17 23:47:22 +03:00
|
|
|
Cu.import("resource://gre/modules/NetUtil.jsm");
|
|
|
|
|
2010-07-28 22:33:06 +04:00
|
|
|
|
|
|
|
var httpServer = null;
|
|
|
|
// Need to randomize, because apparently no one clears our cache
|
|
|
|
var randomPath = "/error/" + Math.random();
|
2013-07-19 21:19:28 +04:00
|
|
|
|
|
|
|
XPCOMUtils.defineLazyGetter(this, "randomURI", function() {
|
|
|
|
return "http://localhost:" + httpServer.identity.primaryPort + randomPath;
|
|
|
|
});
|
2010-07-28 22:33:06 +04:00
|
|
|
|
|
|
|
var cacheUpdateObserver = null;
|
2015-10-02 19:16:45 +03:00
|
|
|
var systemPrincipal = Services.scriptSecurityManager.getSystemPrincipal();
|
2010-07-28 22:33:06 +04:00
|
|
|
|
|
|
|
function make_channel(url, callback, ctx) {
|
2015-12-17 23:47:22 +03:00
|
|
|
return NetUtil.newChannel({uri: url, loadUsingSystemPrincipal: true});
|
2010-07-28 22:33:06 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
function make_uri(url) {
|
|
|
|
var ios = Cc["@mozilla.org/network/io-service;1"].
|
|
|
|
getService(Ci.nsIIOService);
|
2017-01-09 22:27:26 +03:00
|
|
|
return ios.newURI(url);
|
2010-07-28 22:33:06 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
var responseBody = "Content body";
|
|
|
|
|
|
|
|
// start the test with loading this master entry referencing the manifest
|
|
|
|
function masterEntryHandler(metadata, response)
|
|
|
|
{
|
|
|
|
var masterEntryContent = "<html manifest='/manifest'></html>";
|
|
|
|
response.setHeader("Content-Type", "text/html");
|
|
|
|
response.bodyOutputStream.write(masterEntryContent, masterEntryContent.length);
|
|
|
|
}
|
|
|
|
|
|
|
|
// manifest defines fallback namespace from any /error path to /content
|
|
|
|
function manifestHandler(metadata, response)
|
|
|
|
{
|
|
|
|
var manifestContent = "CACHE MANIFEST\nFALLBACK:\nerror /content\n";
|
|
|
|
response.setHeader("Content-Type", "text/cache-manifest");
|
|
|
|
response.bodyOutputStream.write(manifestContent, manifestContent.length);
|
|
|
|
}
|
|
|
|
|
|
|
|
// content handler correctly returns some plain text data
|
|
|
|
function contentHandler(metadata, response)
|
|
|
|
{
|
|
|
|
response.setHeader("Content-Type", "text/plain");
|
|
|
|
response.bodyOutputStream.write(responseBody, responseBody.length);
|
|
|
|
}
|
|
|
|
|
|
|
|
// error handler returns error
|
|
|
|
function errorHandler(metadata, response)
|
|
|
|
{
|
|
|
|
response.setStatusLine(metadata.httpVersion, 404, "Bad request");
|
|
|
|
}
|
|
|
|
|
|
|
|
// finally check we got fallback content
|
|
|
|
function finish_test(request, buffer)
|
|
|
|
{
|
|
|
|
do_check_eq(buffer, "");
|
|
|
|
httpServer.stop(do_test_finished);
|
|
|
|
}
|
|
|
|
|
|
|
|
function run_test()
|
|
|
|
{
|
2012-08-14 18:06:04 +04:00
|
|
|
httpServer = new HttpServer();
|
2010-07-28 22:33:06 +04:00
|
|
|
httpServer.registerPathHandler("/masterEntry", masterEntryHandler);
|
|
|
|
httpServer.registerPathHandler("/manifest", manifestHandler);
|
|
|
|
httpServer.registerPathHandler("/content", contentHandler);
|
|
|
|
httpServer.registerPathHandler(randomPath, errorHandler);
|
2013-07-19 21:19:28 +04:00
|
|
|
httpServer.start(-1);
|
2010-07-28 22:33:06 +04:00
|
|
|
|
|
|
|
var pm = Cc["@mozilla.org/permissionmanager;1"]
|
|
|
|
.getService(Ci.nsIPermissionManager);
|
2015-08-18 10:01:42 +03:00
|
|
|
var ssm = Cc["@mozilla.org/scriptsecuritymanager;1"]
|
|
|
|
.getService(Ci.nsIScriptSecurityManager);
|
2013-07-19 21:19:28 +04:00
|
|
|
var uri = make_uri("http://localhost:" + httpServer.identity.primaryPort);
|
2015-08-18 10:01:42 +03:00
|
|
|
var principal = ssm.createCodebasePrincipal(uri, {});
|
2012-07-23 22:08:07 +04:00
|
|
|
|
|
|
|
if (pm.testPermissionFromPrincipal(principal, "offline-app") != 0) {
|
2010-07-28 22:33:06 +04:00
|
|
|
dump("Previous test failed to clear offline-app permission! Expect failures.\n");
|
|
|
|
}
|
2012-07-23 22:08:07 +04:00
|
|
|
pm.addFromPrincipal(principal, "offline-app", Ci.nsIPermissionManager.ALLOW_ACTION);
|
2010-07-28 22:33:06 +04:00
|
|
|
|
|
|
|
var ps = Cc["@mozilla.org/preferences-service;1"]
|
|
|
|
.getService(Ci.nsIPrefBranch);
|
|
|
|
dump(ps.getBoolPref("browser.cache.offline.enable"));
|
|
|
|
ps.setBoolPref("browser.cache.offline.enable", true);
|
|
|
|
ps.setComplexValue("browser.cache.offline.parent_directory", Ci.nsILocalFile, do_get_profile());
|
|
|
|
|
|
|
|
cacheUpdateObserver = {observe: function() {
|
|
|
|
dump("got offline-cache-update-completed\n");
|
|
|
|
// offline cache update completed.
|
|
|
|
var chan = make_channel(randomURI);
|
|
|
|
chan.notificationCallbacks = new ChannelEventSink(ES_ABORT_REDIRECT);
|
|
|
|
var chanac = chan.QueryInterface(Ci.nsIApplicationCacheChannel);
|
|
|
|
chanac.chooseApplicationCache = true;
|
2015-12-17 23:47:22 +03:00
|
|
|
chan.asyncOpen2(new ChannelListener(finish_test, null, CL_EXPECT_FAILURE));
|
2010-07-28 22:33:06 +04:00
|
|
|
}}
|
|
|
|
|
|
|
|
var os = Cc["@mozilla.org/observer-service;1"].
|
|
|
|
getService(Ci.nsIObserverService);
|
2017-04-14 22:51:38 +03:00
|
|
|
os.addObserver(cacheUpdateObserver, "offline-cache-update-completed");
|
2010-07-28 22:33:06 +04:00
|
|
|
|
|
|
|
var us = Cc["@mozilla.org/offlinecacheupdate-service;1"].
|
|
|
|
getService(Ci.nsIOfflineCacheUpdateService);
|
2013-07-19 21:19:28 +04:00
|
|
|
us.scheduleUpdate(make_uri("http://localhost:" +
|
|
|
|
httpServer.identity.primaryPort + "/manifest"),
|
|
|
|
make_uri("http://localhost:" +
|
|
|
|
httpServer.identity.primaryPort + "/masterEntry"),
|
2015-10-02 19:16:45 +03:00
|
|
|
systemPrincipal,
|
2011-05-10 23:58:33 +04:00
|
|
|
null);
|
2010-07-28 22:33:06 +04:00
|
|
|
|
|
|
|
do_test_pending();
|
|
|
|
}
|