2018-01-30 02:20:18 +03:00
|
|
|
ChromeUtils.import("resource://gre/modules/Services.jsm");
|
|
|
|
ChromeUtils.import("resource://gre/modules/NetUtil.jsm");
|
|
|
|
ChromeUtils.import("resource://testing-common/httpd.js");
|
2012-06-26 08:04:59 +04:00
|
|
|
|
|
|
|
var server = new HttpServer();
|
|
|
|
server.registerPathHandler('/image.png', imageHandler);
|
2013-07-09 23:48:50 +04:00
|
|
|
server.start(-1);
|
2012-06-26 08:04:59 +04:00
|
|
|
|
|
|
|
load('image_load_helpers.js');
|
|
|
|
|
|
|
|
var gHits = 0;
|
|
|
|
|
2013-07-09 23:48:50 +04:00
|
|
|
var gIoService = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService);
|
2012-06-26 08:20:12 +04:00
|
|
|
var gPublicLoader = Cc["@mozilla.org/image/loader;1"].createInstance(Ci.imgILoader);
|
|
|
|
var gPrivateLoader = Cc["@mozilla.org/image/loader;1"].createInstance(Ci.imgILoader);
|
|
|
|
gPrivateLoader.QueryInterface(Ci.imgICache).respectPrivacyNotifications();
|
2012-06-26 08:04:59 +04:00
|
|
|
|
2017-04-09 23:58:40 +03:00
|
|
|
var nonPrivateLoadContext = Cc["@mozilla.org/loadcontext;1"].createInstance(Ci.nsILoadContext);
|
|
|
|
var privateLoadContext = Cc["@mozilla.org/privateloadcontext;1"].createInstance(Ci.nsILoadContext);
|
|
|
|
|
2012-06-26 08:04:59 +04:00
|
|
|
function imageHandler(metadata, response) {
|
|
|
|
gHits++;
|
|
|
|
response.setHeader("Cache-Control", "max-age=10000", false);
|
|
|
|
response.setStatusLine(metadata.httpVersion, 200, "OK");
|
|
|
|
response.setHeader("Content-Type", "image/png", false);
|
|
|
|
var body = "iVBORw0KGgoAAAANSUhEUgAAAAMAAAADCAIAAADZSiLoAAAAEUlEQVQImWP4z8AAQTAamQkAhpcI+DeMzFcAAAAASUVORK5CYII=";
|
|
|
|
response.bodyOutputStream.write(body, body.length);
|
|
|
|
}
|
|
|
|
|
|
|
|
var requests = [];
|
|
|
|
var listeners = [];
|
|
|
|
|
2013-07-09 23:48:50 +04:00
|
|
|
var gImgPath = 'http://localhost:' + server.identity.primaryPort + '/image.png';
|
2012-06-26 08:04:59 +04:00
|
|
|
|
|
|
|
function setup_chan(path, isPrivate, callback) {
|
2015-12-07 07:57:08 +03:00
|
|
|
var uri = NetUtil.newURI(gImgPath);
|
2016-04-13 16:34:36 +03:00
|
|
|
var securityFlags = Ci.nsILoadInfo.SEC_ALLOW_CROSS_ORIGIN_DATA_IS_NULL;
|
2016-08-31 00:54:58 +03:00
|
|
|
var principal = Services.scriptSecurityManager
|
|
|
|
.createCodebasePrincipal(uri, {privateBrowsingId: isPrivate ? 1 : 0});
|
|
|
|
var chan = NetUtil.newChannel({uri: uri, loadingPrincipal: principal,
|
|
|
|
securityFlags: securityFlags,
|
|
|
|
contentPolicyType: Ci.nsIContentPolicy.TYPE_INTERNAL_IMAGE});
|
2017-04-09 23:58:40 +03:00
|
|
|
chan.notificationCallbacks = isPrivate ? privateLoadContext
|
|
|
|
: nonPrivateLoadContext;
|
2012-06-26 08:04:59 +04:00
|
|
|
var channelListener = new ChannelListener();
|
2015-12-07 07:57:08 +03:00
|
|
|
chan.asyncOpen2(channelListener);
|
2013-07-09 23:48:50 +04:00
|
|
|
|
2012-06-26 08:04:59 +04:00
|
|
|
var listener = new ImageListener(null, callback);
|
|
|
|
var outlistener = {};
|
2012-06-26 08:20:12 +04:00
|
|
|
var loader = isPrivate ? gPrivateLoader : gPublicLoader;
|
2012-10-12 20:11:22 +04:00
|
|
|
var outer = Cc["@mozilla.org/image/tools;1"].getService(Ci.imgITools)
|
|
|
|
.createScriptedObserver(listener);
|
|
|
|
listeners.push(outer);
|
2012-10-12 16:43:01 +04:00
|
|
|
requests.push(loader.loadImageWithChannelXPCOM(chan, outer, null, outlistener));
|
2012-06-26 08:04:59 +04:00
|
|
|
channelListener.outputListener = outlistener.value;
|
|
|
|
listener.synchronous = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
function loadImage(isPrivate, callback) {
|
|
|
|
var listener = new ImageListener(null, callback);
|
2012-10-12 20:11:22 +04:00
|
|
|
var outer = Cc["@mozilla.org/image/tools;1"].getService(Ci.imgITools)
|
|
|
|
.createScriptedObserver(listener);
|
2017-01-09 22:27:26 +03:00
|
|
|
var uri = gIoService.newURI(gImgPath);
|
2012-06-26 08:04:59 +04:00
|
|
|
var loadGroup = Cc["@mozilla.org/network/load-group;1"].createInstance(Ci.nsILoadGroup);
|
2017-04-09 23:58:40 +03:00
|
|
|
loadGroup.notificationCallbacks = isPrivate ? privateLoadContext
|
|
|
|
: nonPrivateLoadContext;
|
2012-06-26 08:20:12 +04:00
|
|
|
var loader = isPrivate ? gPrivateLoader : gPublicLoader;
|
2016-06-14 14:43:21 +03:00
|
|
|
requests.push(loader.loadImageXPCOM(uri, null, null, "default", null, loadGroup, outer, null, 0, null));
|
2013-07-09 23:48:50 +04:00
|
|
|
listener.synchronous = false;
|
2012-06-26 08:04:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
function run_loadImage_tests() {
|
2013-09-03 18:45:50 +04:00
|
|
|
function observer() {
|
|
|
|
Services.obs.removeObserver(observer, "cacheservice:empty-cache");
|
|
|
|
gHits = 0;
|
2012-06-26 08:04:59 +04:00
|
|
|
loadImage(false, function() {
|
2013-09-03 18:45:50 +04:00
|
|
|
loadImage(false, function() {
|
2012-06-26 08:04:59 +04:00
|
|
|
loadImage(true, function() {
|
2013-09-03 18:45:50 +04:00
|
|
|
loadImage(true, function() {
|
2017-12-21 13:08:17 +03:00
|
|
|
Assert.equal(gHits, 2);
|
2013-09-03 18:45:50 +04:00
|
|
|
server.stop(do_test_finished);
|
|
|
|
});
|
2012-06-26 08:04:59 +04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2013-09-03 18:45:50 +04:00
|
|
|
}
|
|
|
|
|
2017-04-14 22:51:38 +03:00
|
|
|
Services.obs.addObserver(observer, "cacheservice:empty-cache");
|
2013-09-20 13:11:25 +04:00
|
|
|
let cs = Cc["@mozilla.org/netwerk/cache-storage-service;1"]
|
|
|
|
.getService(Ci.nsICacheStorageService);
|
|
|
|
cs.clear();
|
2012-06-26 08:04:59 +04:00
|
|
|
}
|
|
|
|
|
2013-01-19 01:47:18 +04:00
|
|
|
function cleanup()
|
|
|
|
{
|
|
|
|
for (var i = 0; i < requests.length; ++i) {
|
|
|
|
requests[i].cancelAndForgetObserver(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-26 08:04:59 +04:00
|
|
|
function run_test() {
|
2017-12-21 13:10:23 +03:00
|
|
|
registerCleanupFunction(cleanup);
|
2013-01-19 01:47:18 +04:00
|
|
|
|
2012-06-26 08:04:59 +04:00
|
|
|
do_test_pending();
|
2013-07-09 23:48:50 +04:00
|
|
|
|
2017-06-16 04:55:00 +03:00
|
|
|
Services.prefs.setBoolPref("network.http.rcwn.enabled", false);
|
|
|
|
|
2012-06-26 08:04:59 +04:00
|
|
|
// We create a public channel that loads an image, then an identical
|
|
|
|
// one that should cause a cache read. We then create a private channel
|
|
|
|
// and load the same image, and do that a second time to ensure a cache
|
|
|
|
// read. In total, we should cause two separate http responses to occur,
|
|
|
|
// since the private channels shouldn't be able to use the public cache.
|
|
|
|
setup_chan('/image.png', false, function() {
|
|
|
|
setup_chan('/image.png', false, function() {
|
|
|
|
setup_chan('/image.png', true, function() {
|
|
|
|
setup_chan('/image.png', true, function() {
|
2017-12-21 13:08:17 +03:00
|
|
|
Assert.equal(gHits, 2);
|
2012-06-26 08:04:59 +04:00
|
|
|
run_loadImage_tests();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2012-11-04 07:04:07 +04:00
|
|
|
}
|