1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
|
const { NetUtil } = ChromeUtils.import("resource://gre/modules/NetUtil.jsm");
const { HttpServer } = ChromeUtils.import("resource://testing-common/httpd.js");
const ReferrerInfo = Components.Constructor(
"@mozilla.org/referrer-info;1",
"nsIReferrerInfo",
"init"
);
var server = new HttpServer();
server.registerPathHandler("/image.png", imageHandler);
server.start(-1);
/* import-globals-from image_load_helpers.js */
load("image_load_helpers.js");
var gHits = 0;
var gIoService = Services.io;
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();
var nonPrivateLoadContext = Cu.createLoadContext();
var privateLoadContext = Cu.createPrivateLoadContext();
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 = atob(
"iVBORw0KGgoAAAANSUhEUgAAAAMAAAADCAIAAADZSiLoAAAAEUlEQVQImWP4z8AAQTAamQkAhpcI+DeMzFcAAAAASUVORK5CYII="
);
response.bodyOutputStream.write(body, body.length);
}
var requests = [];
var listeners = [];
var gImgPath = "http://localhost:" + server.identity.primaryPort + "/image.png";
function setup_chan(path, isPrivate, callback) {
var uri = NetUtil.newURI(gImgPath);
var securityFlags = Ci.nsILoadInfo.SEC_ALLOW_CROSS_ORIGIN_SEC_CONTEXT_IS_NULL;
var principal = Services.scriptSecurityManager.createContentPrincipal(uri, {
privateBrowsingId: isPrivate ? 1 : 0,
});
var chan = NetUtil.newChannel({
uri,
loadingPrincipal: principal,
securityFlags,
contentPolicyType: Ci.nsIContentPolicy.TYPE_INTERNAL_IMAGE,
});
chan.notificationCallbacks = isPrivate
? privateLoadContext
: nonPrivateLoadContext;
var channelListener = new ChannelListener();
chan.asyncOpen(channelListener);
var listener = new ImageListener(null, callback);
var outlistener = {};
var loader = isPrivate ? gPrivateLoader : gPublicLoader;
var outer = Cc["@mozilla.org/image/tools;1"]
.getService(Ci.imgITools)
.createScriptedObserver(listener);
listeners.push(outer);
requests.push(
loader.loadImageWithChannelXPCOM(chan, outer, null, outlistener)
);
channelListener.outputListener = outlistener.value;
listener.synchronous = false;
}
function loadImage(isPrivate, callback) {
var listener = new ImageListener(null, callback);
var outer = Cc["@mozilla.org/image/tools;1"]
.getService(Ci.imgITools)
.createScriptedObserver(listener);
var uri = gIoService.newURI(gImgPath);
var loadGroup = Cc["@mozilla.org/network/load-group;1"].createInstance(
Ci.nsILoadGroup
);
loadGroup.notificationCallbacks = isPrivate
? privateLoadContext
: nonPrivateLoadContext;
var loader = isPrivate ? gPrivateLoader : gPublicLoader;
var referrerInfo = new ReferrerInfo(
Ci.nsIReferrerInfo.NO_REFERRER_WHEN_DOWNGRADE,
true,
null
);
requests.push(
loader.loadImageXPCOM(
uri,
null,
referrerInfo,
null,
loadGroup,
outer,
null,
0,
null
)
);
listener.synchronous = false;
}
function run_loadImage_tests() {
function observer() {
Services.obs.removeObserver(observer, "cacheservice:empty-cache");
gHits = 0;
loadImage(false, function() {
loadImage(false, function() {
loadImage(true, function() {
loadImage(true, function() {
Assert.equal(gHits, 2);
server.stop(do_test_finished);
});
});
});
});
}
for (let loader of [gPublicLoader, gPrivateLoader]) {
loader.QueryInterface(Ci.imgICache).clearCache(true);
loader.QueryInterface(Ci.imgICache).clearCache(false);
}
Services.obs.addObserver(observer, "cacheservice:empty-cache");
let cs = Services.cache2;
cs.clear();
}
function cleanup() {
for (var i = 0; i < requests.length; ++i) {
requests[i].cancelAndForgetObserver(0);
}
}
function run_test() {
registerCleanupFunction(cleanup);
do_test_pending();
Services.prefs.setBoolPref("network.http.rcwn.enabled", false);
// 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() {
Assert.equal(gHits, 2);
run_loadImage_tests();
});
});
});
});
}
|