2012-06-25 21:04:59 -07:00
|
|
|
Components.utils.import("resource://testing-common/httpd.js");
|
|
|
|
const Cc = Components.classes;
|
|
|
|
const Ci = Components.interfaces;
|
|
|
|
|
|
|
|
var server = new HttpServer();
|
|
|
|
server.registerPathHandler('/image.png', imageHandler);
|
2013-07-09 12:48:50 -07:00
|
|
|
server.start(-1);
|
2012-06-25 21:04:59 -07:00
|
|
|
|
|
|
|
load('image_load_helpers.js');
|
|
|
|
|
|
|
|
var gHits = 0;
|
|
|
|
|
2013-07-09 12:48:50 -07:00
|
|
|
var gIoService = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService);
|
2012-06-25 21:20:12 -07: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-25 21:04:59 -07: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 = [];
|
|
|
|
|
|
|
|
function NotificationCallbacks(isPrivate) {
|
|
|
|
this.usePrivateBrowsing = isPrivate;
|
|
|
|
}
|
|
|
|
|
|
|
|
NotificationCallbacks.prototype = {
|
|
|
|
QueryInterface: function (iid) {
|
|
|
|
if (iid.equals(Ci.nsISupports) ||
|
|
|
|
iid.equals(Ci.nsILoadContext))
|
|
|
|
return this;
|
|
|
|
throw Cr.NS_ERROR_NO_INTERFACE;
|
|
|
|
},
|
|
|
|
getInterface: function(iid) {
|
|
|
|
if (iid.equals(Ci.nsILoadContext))
|
|
|
|
return this;
|
|
|
|
throw Cr.NS_ERROR_NO_INTERFACE;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-07-09 12:48:50 -07:00
|
|
|
var gImgPath = 'http://localhost:' + server.identity.primaryPort + '/image.png';
|
2012-06-25 21:04:59 -07:00
|
|
|
|
|
|
|
function setup_chan(path, isPrivate, callback) {
|
|
|
|
var uri = gIoService.newURI(gImgPath, null, null);
|
|
|
|
var chan = gIoService.newChannelFromURI(uri);
|
|
|
|
chan.notificationCallbacks = new NotificationCallbacks(isPrivate);
|
|
|
|
var channelListener = new ChannelListener();
|
|
|
|
chan.asyncOpen(channelListener, null);
|
2013-07-09 12:48:50 -07:00
|
|
|
|
2012-06-25 21:04:59 -07:00
|
|
|
var listener = new ImageListener(null, callback);
|
|
|
|
var outlistener = {};
|
2012-06-25 21:20:12 -07:00
|
|
|
var loader = isPrivate ? gPrivateLoader : gPublicLoader;
|
2012-10-12 09:11:22 -07:00
|
|
|
var outer = Cc["@mozilla.org/image/tools;1"].getService(Ci.imgITools)
|
|
|
|
.createScriptedObserver(listener);
|
|
|
|
listeners.push(outer);
|
2012-10-12 05:43:01 -07:00
|
|
|
requests.push(loader.loadImageWithChannelXPCOM(chan, outer, null, outlistener));
|
2012-06-25 21:04:59 -07:00
|
|
|
channelListener.outputListener = outlistener.value;
|
|
|
|
listener.synchronous = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
function loadImage(isPrivate, callback) {
|
|
|
|
var listener = new ImageListener(null, callback);
|
2012-10-12 09:11:22 -07:00
|
|
|
var outer = Cc["@mozilla.org/image/tools;1"].getService(Ci.imgITools)
|
|
|
|
.createScriptedObserver(listener);
|
2012-06-25 21:04:59 -07:00
|
|
|
var uri = gIoService.newURI(gImgPath, null, null);
|
|
|
|
var loadGroup = Cc["@mozilla.org/network/load-group;1"].createInstance(Ci.nsILoadGroup);
|
|
|
|
loadGroup.notificationCallbacks = new NotificationCallbacks(isPrivate);
|
2012-06-25 21:20:12 -07:00
|
|
|
var loader = isPrivate ? gPrivateLoader : gPublicLoader;
|
2012-10-12 05:43:01 -07:00
|
|
|
requests.push(loader.loadImageXPCOM(uri, null, null, null, loadGroup, outer, null, 0, null, null));
|
2013-07-09 12:48:50 -07:00
|
|
|
listener.synchronous = false;
|
2012-06-25 21:04:59 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
function run_loadImage_tests() {
|
|
|
|
let cs = Cc["@mozilla.org/network/cache-service;1"].getService(Ci.nsICacheService);
|
|
|
|
cs.evictEntries(Ci.nsICache.STORE_ANYWHERE);
|
|
|
|
|
|
|
|
gHits = 0;
|
|
|
|
loadImage(false, function() {
|
|
|
|
loadImage(false, function() {
|
|
|
|
loadImage(true, function() {
|
|
|
|
loadImage(true, function() {
|
|
|
|
do_check_eq(gHits, 2);
|
2012-06-25 21:20:12 -07:00
|
|
|
server.stop(do_test_finished);
|
2012-06-25 21:04:59 -07:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2013-01-18 13:47:18 -08:00
|
|
|
function cleanup()
|
|
|
|
{
|
|
|
|
for (var i = 0; i < requests.length; ++i) {
|
|
|
|
requests[i].cancelAndForgetObserver(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-25 21:04:59 -07:00
|
|
|
function run_test() {
|
2013-01-18 13:47:18 -08:00
|
|
|
do_register_cleanup(cleanup);
|
|
|
|
|
2012-06-25 21:04:59 -07:00
|
|
|
do_test_pending();
|
2013-07-09 12:48:50 -07:00
|
|
|
|
2012-06-25 21:04:59 -07: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() {
|
|
|
|
do_check_eq(gHits, 2);
|
|
|
|
run_loadImage_tests();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2012-11-03 20:04:07 -07:00
|
|
|
}
|