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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
|
/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
"use strict";
const { HttpServer } = ChromeUtils.import("resource://testing-common/httpd.js");
const { PermissionTestUtils } = ChromeUtils.import(
"resource://testing-common/PermissionTestUtils.jsm"
);
/**
* This testcase does the following steps to make sure that bug767025 removes
* files as expected.
*
* STEPS:
* - Schedule a offline cache update for app.manifest.
* - pages/foo1, pages/foo2, pages/foo3, and pages/foo4 are cached.
* - Activate pages/foo1
* - Doom pages/foo1, and pages/foo2.
* - pages/foo1 should keep alive while pages/foo2 was gone.
* - Activate pages/foo3
* - Evict all documents.
* - all documents except pages/foo1 are gone since pages/foo1 & pages/foo3
* are activated.
*/
const kNS_OFFLINECACHEUPDATESERVICE_CONTRACTID =
"@mozilla.org/offlinecacheupdate-service;1";
const kNS_CACHESTORAGESERVICE_CONTRACTID =
"@mozilla.org/netwerk/cache-storage-service;1";
const kNS_APPLICATIONCACHESERVICE_CONTRACTID =
"@mozilla.org/network/application-cache-service;1";
const kManifest =
"CACHE MANIFEST\n" +
"/pages/foo1\n" +
"/pages/foo2\n" +
"/pages/foo3\n" +
"/pages/foo4\n";
const kDataFileSize = 1024; // file size for each content page
const kHttpLocation = "http://localhost:4444/";
function manifest_handler(metadata, response) {
info("manifest\n");
response.setHeader("content-type", "text/cache-manifest");
response.write(kManifest);
}
function datafile_handler(metadata, response) {
info("datafile_handler\n");
let data = "";
while (data.length < kDataFileSize) {
data =
data +
Math.random()
.toString(36)
.substring(2, 15);
}
response.setHeader("content-type", "text/plain");
response.write(data.substring(0, kDataFileSize));
}
function app_handler(metadata, response) {
info("app_handler\n");
response.setHeader("content-type", "text/html");
response.write("<html></html>");
}
var httpServer;
function init_profile() {
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.setBoolPref("browser.cache.offline.storage.enable", true);
ps.setComplexValue(
"browser.cache.offline.parent_directory",
Ci.nsIFile,
do_get_profile()
);
info("profile " + do_get_profile());
}
function init_http_server() {
httpServer = new HttpServer();
httpServer.registerPathHandler("/app.appcache", manifest_handler);
httpServer.registerPathHandler("/app", app_handler);
for (let i = 1; i <= 4; i++) {
httpServer.registerPathHandler("/pages/foo" + i, datafile_handler);
}
httpServer.start(4444);
}
function clean_app_cache() {
let cache_service = Cc[kNS_CACHESTORAGESERVICE_CONTRACTID].getService(
Ci.nsICacheStorageService
);
let storage = cache_service.appCacheStorage(
Services.loadContextInfo.default,
null
);
storage.asyncEvictStorage(null);
}
function do_app_cache(manifestURL, pageURL) {
let update_service = Cc[kNS_OFFLINECACHEUPDATESERVICE_CONTRACTID].getService(
Ci.nsIOfflineCacheUpdateService
);
PermissionTestUtils.add(
manifestURL,
"offline-app",
Ci.nsIPermissionManager.ALLOW_ACTION
);
let update = update_service.scheduleUpdate(
manifestURL,
pageURL,
Services.scriptSecurityManager.getSystemPrincipal(),
null
); /* no window */
return update;
}
function watch_update(update, stateChangeHandler, cacheAvailHandler) {
let observer = {
QueryInterface: ChromeUtils.generateQI([]),
updateStateChanged: stateChangeHandler,
applicationCacheAvailable: cacheAvailHandler,
};
~update.addObserver(observer);
return update;
}
function start_and_watch_app_cache(
manifestURL,
pageURL,
stateChangeHandler,
cacheAvailHandler
) {
let ioService = Cc["@mozilla.org/network/io-service;1"].getService(
Ci.nsIIOService
);
let update = do_app_cache(
ioService.newURI(manifestURL),
ioService.newURI(pageURL)
);
watch_update(update, stateChangeHandler, cacheAvailHandler);
return update;
}
const {
STATE_FINISHED: STATE_FINISHED,
STATE_CHECKING: STATE_CHECKING,
STATE_ERROR: STATE_ERROR,
} = Ci.nsIOfflineCacheUpdateObserver;
/*
* Start caching app1 as a non-pinned app.
*/
function start_cache_nonpinned_app() {
info("Start non-pinned App1");
start_and_watch_app_cache(
kHttpLocation + "app.appcache",
kHttpLocation + "app",
function(update, state) {
switch (state) {
case STATE_FINISHED:
check_bug();
break;
case STATE_ERROR:
do_throw("App cache state = " + state);
break;
}
},
function(appcache) {
info("app avail " + appcache + "\n");
}
);
}
var hold_entry_foo1 = null;
function check_bug() {
// activate foo1
asyncOpenCacheEntry(
kHttpLocation + "pages/foo1",
"appcache",
Ci.nsICacheStorage.OPEN_READONLY,
null,
function(status, entry, appcache) {
let storage = get_cache_service().appCacheStorage(
Services.loadContextInfo.default,
appcache
);
// Doom foo1 & foo2
storage.asyncDoomURI(createURI(kHttpLocation + "pages/foo1"), "", {
onCacheEntryDoomed() {
storage.asyncDoomURI(createURI(kHttpLocation + "pages/foo2"), "", {
onCacheEntryDoomed() {
check_evict_cache(appcache);
},
});
},
});
hold_entry_foo1 = entry;
}
);
}
function check_evict_cache(appcache) {
// Only foo2 should be removed.
let file = do_get_profile().clone();
file.append("OfflineCache");
file.append("5");
file.append("9");
file.append("8379C6596B8CA4-0");
Assert.equal(file.exists(), true);
file = do_get_profile().clone();
file.append("OfflineCache");
file.append("C");
file.append("2");
file.append("5F356A168B5E3B-0");
Assert.equal(file.exists(), false);
// activate foo3
asyncOpenCacheEntry(
kHttpLocation + "pages/foo3",
"appcache",
Ci.nsICacheStorage.OPEN_READONLY,
null,
function(status, entry, appcache) {
// evict all documents.
let storage = get_cache_service().appCacheStorage(
Services.loadContextInfo.default,
appcache
);
storage.asyncEvictStorage(null);
// All documents are removed except foo1 & foo3.
syncWithCacheIOThread(function() {
// foo1
let file = do_get_profile().clone();
file.append("OfflineCache");
file.append("5");
file.append("9");
file.append("8379C6596B8CA4-0");
Assert.equal(file.exists(), true);
file = do_get_profile().clone();
file.append("OfflineCache");
file.append("0");
file.append("0");
file.append("61FEE819921D39-0");
Assert.equal(file.exists(), false);
file = do_get_profile().clone();
file.append("OfflineCache");
file.append("3");
file.append("9");
file.append("0D8759F1DE5452-0");
Assert.equal(file.exists(), false);
file = do_get_profile().clone();
file.append("OfflineCache");
file.append("C");
file.append("2");
file.append("5F356A168B5E3B-0");
Assert.equal(file.exists(), false);
// foo3
file = do_get_profile().clone();
file.append("OfflineCache");
file.append("D");
file.append("C");
file.append("1ADCCC843B5C00-0");
Assert.equal(file.exists(), true);
file = do_get_profile().clone();
file.append("OfflineCache");
file.append("F");
file.append("0");
file.append("FC3E6D6C1164E9-0");
Assert.equal(file.exists(), false);
httpServer.stop(do_test_finished);
}, true /* force even with the new cache back end */);
},
appcache
);
}
function run_test() {
if (typeof _XPCSHELL_PROCESS == "undefined" || _XPCSHELL_PROCESS != "child") {
init_profile();
clean_app_cache();
}
init_http_server();
start_cache_nonpinned_app();
do_test_pending();
}
|