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
|
/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
/*
* This testcase performs 3 requests against the offline cache. They
* are
*
* - start_cache_nonpinned_app1()
*
* - Request nsOfflineCacheService to skip pages (4) of app1 on
* the cache storage.
*
* - The offline cache storage is empty at this monent.
*
* - start_cache_nonpinned_app2_for_partial()
*
* - Request nsOfflineCacheService to skip pages of app2 on the
* cache storage.
*
* - The offline cache storage has only enough space for one more
* additional page. Only first of pages is really in the cache.
*
* - start_cache_pinned_app2_for_success()
*
* - Request nsOfflineCacheService to skip pages of app2 on the
* cache storage.
*
* - The offline cache storage has only enough space for one
* additional page. But, this is a pinned request,
* nsOfflineCacheService will make more space for this request
* by discarding app1 (non-pinned)
*
*/
Cu.import("resource://testing-common/httpd.js");
// const {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components;
Cu.import("resource://gre/modules/Services.jsm");
const kNS_OFFLINECACHEUPDATESERVICE_CONTRACTID =
"@mozilla.org/offlinecacheupdate-service;1";
const kManifest1 = "CACHE MANIFEST\n" +
"/pages/foo1\n" +
"/pages/foo2\n" +
"/pages/foo3\n" +
"/pages/foo4\n";
const kManifest2 = "CACHE MANIFEST\n" +
"/pages/foo5\n" +
"/pages/foo6\n" +
"/pages/foo7\n" +
"/pages/foo8\n";
const kDataFileSize = 1024; // file size for each content page
const kCacheSize = kDataFileSize * 5; // total space for offline cache storage
XPCOMUtils.defineLazyGetter(this, "kHttpLocation", function() {
return "http://localhost:" + httpServer.identity.primaryPort + "/";
});
XPCOMUtils.defineLazyGetter(this, "kHttpLocation_ip", function() {
return "http://127.0.0.1:" + httpServer.identity.primaryPort + "/";
});
function manifest1_handler(metadata, response) {
do_print("manifest1\n");
response.setHeader("content-type", "text/cache-manifest");
response.write(kManifest1);
}
function manifest2_handler(metadata, response) {
do_print("manifest2\n");
response.setHeader("content-type", "text/cache-manifest");
response.write(kManifest2);
}
function app_handler(metadata, response) {
do_print("app_handler\n");
response.setHeader("content-type", "text/html");
response.write("<html></html>");
}
function datafile_handler(metadata, response) {
do_print("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));
}
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.setComplexValue("browser.cache.offline.parent_directory",
Ci.nsILocalFile, do_get_profile());
}
function init_http_server() {
httpServer = new HttpServer();
httpServer.registerPathHandler("/app1", app_handler);
httpServer.registerPathHandler("/app2", app_handler);
httpServer.registerPathHandler("/app1.appcache", manifest1_handler);
httpServer.registerPathHandler("/app2.appcache", manifest2_handler);
for (i = 1; i <= 8; i++) {
httpServer.registerPathHandler("/pages/foo" + i, datafile_handler);
}
httpServer.start(-1);
}
function init_cache_capacity() {
let prefs = Cc["@mozilla.org/preferences-service;1"]
.getService(Components.interfaces.nsIPrefBranch);
prefs.setIntPref("browser.cache.offline.capacity", kCacheSize / 1024);
}
function clean_app_cache() {
evict_cache_entries("appcache");
}
function do_app_cache(manifestURL, pageURL, pinned) {
let update_service = Cc[kNS_OFFLINECACHEUPDATESERVICE_CONTRACTID].
getService(Ci.nsIOfflineCacheUpdateService);
Services.perms.add(manifestURL,
"pin-app",
pinned ?
Ci.nsIPermissionManager.ALLOW_ACTION :
Ci.nsIPermissionManager.DENY_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: function QueryInterface(iftype) {
return this;
},
updateStateChanged: stateChangeHandler,
applicationCacheAvailable: cacheAvailHandler
};
update.addObserver(observer, false);
return update;
}
function start_and_watch_app_cache(manifestURL,
pageURL,
pinned,
stateChangeHandler,
cacheAvailHandler) {
let ioService = Cc["@mozilla.org/network/io-service;1"].
getService(Ci.nsIIOService);
let update = do_app_cache(ioService.newURI(manifestURL, null, null),
ioService.newURI(pageURL, null, null),
pinned);
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() {
do_print("Start non-pinned App1");
start_and_watch_app_cache(kHttpLocation + "app1.appcache",
kHttpLocation + "app1",
false,
function (update, state) {
switch(state) {
case STATE_FINISHED:
start_cache_nonpinned_app2_for_partial();
break;
case STATE_ERROR:
do_throw("App1 cache state = " + state);
break;
}
},
function (appcahe) {
do_print("app1 avail " + appcache + "\n");
});
}
/*
* Start caching app2 as a non-pinned app.
*
* This cache request is supposed to be saved partially in the cache
* storage for running out of the cache storage. The offline cache
* storage can hold 5 files at most. (kDataFileSize bytes for each
* file)
*/
function start_cache_nonpinned_app2_for_partial() {
let error_count = [0];
do_print("Start non-pinned App2 for partial\n");
start_and_watch_app_cache(kHttpLocation_ip + "app2.appcache",
kHttpLocation_ip + "app2",
false,
function (update, state) {
switch(state) {
case STATE_FINISHED:
start_cache_pinned_app2_for_success();
break;
case STATE_ERROR:
do_throw("App2 cache state = " + state);
break;
}
},
function (appcahe) {
});
}
/*
* Start caching app2 as a pinned app.
*
* This request use IP address (127.0.0.1) as the host name instead of
* the one used by app1. Because, app1 is also pinned when app2 is
* pinned if they have the same host name (localhost).
*/
function start_cache_pinned_app2_for_success() {
let error_count = [0];
do_print("Start pinned App2 for success\n");
start_and_watch_app_cache(kHttpLocation_ip + "app2.appcache",
kHttpLocation_ip + "app2",
true,
function (update, state) {
switch(state) {
case STATE_FINISHED:
do_check_true(error_count[0] == 0,
"Do not discard app1?");
httpServer.stop(do_test_finished);
break;
case STATE_ERROR:
do_print("STATE_ERROR\n");
error_count[0]++;
break;
}
},
function (appcahe) {
do_print("app2 avail " + appcache + "\n");
});
}
function run_test() {
if (typeof _XPCSHELL_PROCESS == "undefined" ||
_XPCSHELL_PROCESS != "child") {
init_profile();
init_cache_capacity();
clean_app_cache();
}
init_http_server();
start_cache_nonpinned_app();
do_test_pending();
}
|