Skip to content

Commit 894ba59

Browse files
committed
Bug 913807 - HTTP cache v2: API+top service+integration+tests, off by default, r=michal+ehsan+mark.finkle+fabrice+mhammond+gavin
1 parent d1b254f commit 894ba59

File tree

274 files changed

+9748
-2852
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

274 files changed

+9748
-2852
lines changed

b2g/app/b2g.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ pref("browser.cache.disk.smart_size.first_run", false);
2525
pref("browser.cache.memory.enable", true);
2626
pref("browser.cache.memory.capacity", 1024); // kilobytes
2727

28+
pref("browser.cache.memory_limit", 2048); // 2 MB
29+
2830
/* image cache prefs */
2931
pref("image.cache.size", 1048576); // bytes
3032
pref("image.high_quality_downscaling.enabled", false);

b2g/installer/package-manifest.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,7 @@
254254
@BINPATH@/components/mozfind.xpt
255255
@BINPATH@/components/necko_about.xpt
256256
@BINPATH@/components/necko_cache.xpt
257+
@BINPATH@/components/necko_cache2.xpt
257258
@BINPATH@/components/necko_cookie.xpt
258259
@BINPATH@/components/necko_dns.xpt
259260
@BINPATH@/components/necko_file.xpt

browser/base/content/pageinfo/pageInfo.js

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
33
* You can obtain one at http://mozilla.org/MPL/2.0/. */
44

5+
const Cu = Components.utils;
6+
Cu.import("resource://gre/modules/LoadContextInfo.jsm");
7+
Cu.import("resource://gre/modules/Services.jsm");
8+
59
//******** define a js object to implement nsITreeView
610
function pageInfoTreeView(treeid, copycol)
711
{
@@ -216,13 +220,15 @@ const ATOM_CONTRACTID = "@mozilla.org/atom-service;1";
216220

217221
// a number of services I'll need later
218222
// the cache services
219-
const nsICacheService = Components.interfaces.nsICacheService;
220-
const ACCESS_READ = Components.interfaces.nsICache.ACCESS_READ;
221-
const cacheService = Components.classes["@mozilla.org/network/cache-service;1"].getService(nsICacheService);
222-
var httpCacheSession = cacheService.createSession("HTTP", 0, true);
223-
httpCacheSession.doomEntriesIfExpired = false;
224-
var ftpCacheSession = cacheService.createSession("FTP", 0, true);
225-
ftpCacheSession.doomEntriesIfExpired = false;
223+
const nsICacheStorageService = Components.interfaces.nsICacheStorageService;
224+
const nsICacheStorage = Components.interfaces.nsICacheStorage;
225+
const cacheService = Components.classes["@mozilla.org/netwerk/cache-storage-service;1"].getService(nsICacheStorageService);
226+
227+
var loadContextInfo = LoadContextInfo.fromLoadContext(
228+
window.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
229+
.getInterface(Components.interfaces.nsIWebNavigation)
230+
.QueryInterface(Components.interfaces.nsILoadContext), false);
231+
var diskStorage = cacheService.diskCacheStorage(loadContextInfo, false);
226232

227233
const nsICookiePermission = Components.interfaces.nsICookiePermission;
228234
const nsIPermissionManager = Components.interfaces.nsIPermissionManager;
@@ -463,19 +469,16 @@ function toggleGroupbox(id)
463469

464470
function openCacheEntry(key, cb)
465471
{
466-
var tries = 0;
467472
var checkCacheListener = {
468-
onCacheEntryAvailable: function(entry, access, status) {
469-
if (entry || tries == 1) {
470-
cb(entry);
471-
}
472-
else {
473-
tries++;
474-
ftpCacheSession.asyncOpenCacheEntry(key, ACCESS_READ, this, true);
475-
}
476-
}
473+
onCacheEntryCheck: function(entry, appCache) {
474+
return nsICacheEntryOpenCallback.ENTRY_VALID;
475+
},
476+
onCacheEntryAvailable: function(entry, isNew, appCache, status) {
477+
cb(entry);
478+
},
479+
get mainThreadOnly() { return true; }
477480
};
478-
httpCacheSession.asyncOpenCacheEntry(key, ACCESS_READ, checkCacheListener, true);
481+
diskStorage.asyncOpenURI(Services.io.newURI(key, null, null), "", nsICacheStorage.OPEN_READONLY, checkCacheListener);
479482
}
480483

481484
function makeGeneralTab()

browser/base/content/sanitize.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,12 +113,12 @@ Sanitizer.prototype = {
113113
cache: {
114114
clear: function ()
115115
{
116-
var cacheService = Cc["@mozilla.org/network/cache-service;1"].
117-
getService(Ci.nsICacheService);
116+
var cache = Cc["@mozilla.org/netwerk/cache-storage-service;1"].
117+
getService(Ci.nsICacheStorageService);
118118
try {
119119
// Cache doesn't consult timespan, nor does it have the
120120
// facility for timespan-based eviction. Wipe it.
121-
cacheService.evictEntries(Ci.nsICache.STORE_ANYWHERE);
121+
cache.clear();
122122
} catch(er) {}
123123

124124
var imageCache = Cc["@mozilla.org/image/tools;1"].

browser/base/content/test/general/browser_bookmark_titles.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ function generatorTest() {
5959
});
6060

6161
// LOAD_FLAGS_BYPASS_CACHE isn't good enough. So clear the cache.
62-
Services.cache.evictEntries(Services.cache.STORE_ANYWHERE);
62+
Services.cache2.clear();
6363

6464
let [uri, title] = tests[0];
6565
content.location = uri;

browser/base/content/test/general/browser_save_private_link_perwindowpb.js

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,43 @@
11
/* This Source Code Form is subject to the terms of the Mozilla Public
22
* License, v. 2.0. If a copy of the MPL was not distributed with this
33
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4+
5+
let {LoadContextInfo} = Cu.import("resource://gre/modules/LoadContextInfo.jsm", null);
6+
47
function test() {
58
// initialization
69
waitForExplicitFinish();
710
let windowsToClose = [];
811
let testURI = "http://mochi.test:8888/browser/browser/base/content/test/general/bug792517.html";
912
let fileName;
1013
let MockFilePicker = SpecialPowers.MockFilePicker;
11-
let cache = Cc["@mozilla.org/network/cache-service;1"]
12-
.getService(Ci.nsICacheService);
13-
14-
function checkDiskCacheFor(filename) {
15-
let visitor = {
16-
visitDevice: function(deviceID, deviceInfo) {
17-
if (deviceID == "disk")
18-
info(deviceID + " device contains " + deviceInfo.entryCount + " entries");
19-
return deviceID == "disk";
14+
let cache = Cc["@mozilla.org/netwerk/cache-storage-service;1"]
15+
.getService(Ci.nsICacheStorageService);
16+
17+
function checkDiskCacheFor(filename, goon) {
18+
Visitor.prototype = {
19+
onCacheStorageInfo: function(num, consumption)
20+
{
21+
info("disk storage contains " + num + " entries");
2022
},
21-
22-
visitEntry: function(deviceID, entryInfo) {
23-
info(entryInfo.key);
24-
is(entryInfo.key.contains(filename), false, "web content present in disk cache");
23+
onCacheEntryInfo: function(entry)
24+
{
25+
info(entry.key);
26+
is(entry.key.contains(filename), false, "web content present in disk cache");
27+
},
28+
onCacheEntryVisitCompleted: function()
29+
{
30+
goon();
2531
}
2632
};
27-
cache.visitEntries(visitor);
33+
function Visitor() {}
34+
35+
var storage = cache.diskCacheStorage(LoadContextInfo.default, false);
36+
storage.asyncVisitStorage(new Visitor(), true /* Do walk entries */);
2837
}
2938

3039
function contextMenuOpened(aWindow, event) {
31-
cache.evictEntries(Ci.nsICache.STORE_ANYWHERE);
40+
cache.clear();
3241

3342
event.currentTarget.removeEventListener("popupshown", contextMenuOpened);
3443

@@ -65,8 +74,7 @@ function test() {
6574

6675
// Give the request a chance to finish and create a cache entry
6776
executeSoon(function() {
68-
checkDiskCacheFor(fileName);
69-
finish();
77+
checkDiskCacheFor(fileName, finish);
7078
});
7179
}
7280

browser/base/content/test/social/browser_social_errorPage.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ function goOffline() {
2020
BrowserOffline.toggleOfflineStatus();
2121
Services.prefs.setIntPref('network.proxy.type', 0);
2222
// LOAD_FLAGS_BYPASS_CACHE isn't good enough. So clear the cache.
23-
Services.cache.evictEntries(Components.interfaces.nsICache.STORE_ANYWHERE);
23+
Services.cache2.clear();
2424
}
2525

2626
function goOnline(callback) {

browser/components/places/tests/unit/head_bookmarks.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const Cr = Components.results;
99
const Cu = Components.utils;
1010

1111
Cu.import("resource://gre/modules/Services.jsm");
12+
Cu.import("resource://gre/modules/LoadContextInfo.jsm");
1213

1314
// Import common head.
1415
let (commonFile = do_get_file("../../../../../toolkit/components/places/tests/head_common.js", false)) {

browser/components/places/tests/unit/test_clearHistory_shutdown.js

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -134,13 +134,15 @@ function getDistinctNotifications() {
134134
}
135135

136136
function storeCache(aURL, aContent) {
137-
let cache = Cc["@mozilla.org/network/cache-service;1"].
138-
getService(Ci.nsICacheService);
139-
let session = cache.createSession("FTP", Ci.nsICache.STORE_ANYWHERE,
140-
Ci.nsICache.STREAM_BASED);
137+
let cache = Services.cache2;
138+
let storage = cache.diskCacheStorage(LoadContextInfo.default, false);
141139

142140
var storeCacheListener = {
143-
onCacheEntryAvailable: function (entry, access, status) {
141+
onCacheEntryCheck: function (entry, appcache) {
142+
return nsICacheEntryOpenCallback.ENTRY_VALID;
143+
},
144+
145+
onCacheEntryAvailable: function (entry, isnew, appcache, status) {
144146
do_check_eq(status, Cr.NS_OK);
145147

146148
entry.setMetaDataElement("servertype", "0");
@@ -158,26 +160,24 @@ function storeCache(aURL, aContent) {
158160
}
159161
};
160162

161-
session.asyncOpenCacheEntry(aURL,
162-
Ci.nsICache.ACCESS_READ_WRITE,
163-
storeCacheListener);
163+
storage.asyncOpenURI(Services.io.newURI(aURL, null, null), "",
164+
Ci.nsICacheStorage.OPEN_NORMALLY,
165+
storeCacheListener);
164166
}
165167

166168

167169
function checkCache(aURL) {
168-
let cache = Cc["@mozilla.org/network/cache-service;1"].
169-
getService(Ci.nsICacheService);
170-
let session = cache.createSession("FTP", Ci.nsICache.STORE_ANYWHERE,
171-
Ci.nsICache.STREAM_BASED);
170+
let cache = Services.cache2;
171+
let storage = cache.diskCacheStorage(LoadContextInfo.default, false);
172172

173173
var checkCacheListener = {
174-
onCacheEntryAvailable: function (entry, access, status) {
174+
onCacheEntryAvailable: function (entry, isnew, appcache, status) {
175175
do_check_eq(status, Cr.NS_ERROR_CACHE_KEY_NOT_FOUND);
176176
do_test_finished();
177177
}
178178
};
179179

180-
session.asyncOpenCacheEntry(aURL,
181-
Ci.nsICache.ACCESS_READ,
182-
checkCacheListener);
180+
storage.asyncOpenURI(Services.io.newURI(aURL, null, null), "",
181+
Ci.nsICacheStorage.OPEN_READONLY,
182+
checkCacheListener);
183183
}

browser/components/preferences/advanced.js

Lines changed: 61 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
Components.utils.import("resource://gre/modules/DownloadUtils.jsm");
88
Components.utils.import("resource://gre/modules/ctypes.jsm");
99
Components.utils.import("resource://gre/modules/Services.jsm");
10+
Components.utils.import("resource://gre/modules/LoadContextInfo.jsm");
1011

1112
var gAdvancedPane = {
1213
_inited: false,
@@ -77,8 +78,8 @@ var gAdvancedPane = {
7778
this.initSubmitHealthReport();
7879
#endif
7980

80-
this.updateActualCacheSize("disk");
81-
this.updateActualCacheSize("offline");
81+
this.updateActualCacheSize();
82+
this.updateActualAppCacheSize();
8283

8384
// Notify observers that the UI is now ready
8485
Services.obs.notifyObservers(window, "advanced-pane-loaded", null);
@@ -296,22 +297,64 @@ var gAdvancedPane = {
296297
"", null);
297298
},
298299

299-
// Retrieves the amount of space currently used by disk or offline cache
300-
updateActualCacheSize: function (device)
300+
// Retrieves the amount of space currently used by disk cache
301+
updateActualCacheSize: function ()
302+
{
303+
var sum = 0;
304+
function updateUI(consumption) {
305+
var actualSizeLabel = document.getElementById("actualDiskCacheSize");
306+
var sizeStrings = DownloadUtils.convertByteUnits(consumption);
307+
var prefStrBundle = document.getElementById("bundlePreferences");
308+
var sizeStr = prefStrBundle.getFormattedString("actualDiskCacheSize", sizeStrings);
309+
actualSizeLabel.value = sizeStr;
310+
}
311+
312+
Visitor.prototype = {
313+
expected: 0,
314+
sum: 0,
315+
QueryInterface: function listener_qi(iid) {
316+
if (iid.equals(Ci.nsISupports) ||
317+
iid.equals(Ci.nsICacheStorageVisitor)) {
318+
return this;
319+
}
320+
throw Components.results.NS_ERROR_NO_INTERFACE;
321+
},
322+
onCacheStorageInfo: function(num, consumption)
323+
{
324+
this.sum += consumption;
325+
if (!--this.expected)
326+
updateUI(this.sum);
327+
}
328+
};
329+
function Visitor(callbacksExpected) {
330+
this.expected = callbacksExpected;
331+
}
332+
333+
var cacheService =
334+
Components.classes["@mozilla.org/netwerk/cache-storage-service;1"]
335+
.getService(Components.interfaces.nsICacheStorageService);
336+
// non-anonymous
337+
var storage1 = cacheService.diskCacheStorage(LoadContextInfo.default, false);
338+
// anonymous
339+
var storage2 = cacheService.diskCacheStorage(LoadContextInfo.anonymous, false);
340+
341+
// expect 2 callbacks
342+
var visitor = new Visitor(2);
343+
storage1.asyncVisitStorage(visitor, false /* Do not walk entries */);
344+
storage2.asyncVisitStorage(visitor, false /* Do not walk entries */);
345+
},
346+
347+
// Retrieves the amount of space currently used by offline cache
348+
updateActualAppCacheSize: function ()
301349
{
302350
var visitor = {
303351
visitDevice: function (deviceID, deviceInfo)
304352
{
305-
if (deviceID == device) {
306-
var actualSizeLabel = document.getElementById(device == "disk" ?
307-
"actualDiskCacheSize" :
308-
"actualAppCacheSize");
353+
if (deviceID == "offline") {
354+
var actualSizeLabel = document.getElementById("actualAppCacheSize");
309355
var sizeStrings = DownloadUtils.convertByteUnits(deviceInfo.totalSize);
310356
var prefStrBundle = document.getElementById("bundlePreferences");
311-
var sizeStr = prefStrBundle.getFormattedString(device == "disk" ?
312-
"actualDiskCacheSize" :
313-
"actualAppCacheSize",
314-
sizeStrings);
357+
var sizeStr = prefStrBundle.getFormattedString("actualAppCacheSize", sizeStrings);
315358
actualSizeLabel.value = sizeStr;
316359
}
317360
// Do not enumerate entries
@@ -372,12 +415,12 @@ var gAdvancedPane = {
372415
*/
373416
clearCache: function ()
374417
{
375-
var cacheService = Components.classes["@mozilla.org/network/cache-service;1"]
376-
.getService(Components.interfaces.nsICacheService);
418+
var cache = Components.classes["@mozilla.org/netwerk/cache-storage-service;1"]
419+
.getService(Components.interfaces.nsICacheStorageService);
377420
try {
378-
cacheService.evictEntries(Components.interfaces.nsICache.STORE_ANYWHERE);
421+
cache.clear();
379422
} catch(ex) {}
380-
this.updateActualCacheSize("disk");
423+
this.updateActualCacheSize();
381424
},
382425

383426
/**
@@ -388,7 +431,7 @@ var gAdvancedPane = {
388431
Components.utils.import("resource:///modules/offlineAppCache.jsm");
389432
OfflineAppCacheHelper.clear();
390433

391-
this.updateActualCacheSize("offline");
434+
this.updateActualAppCacheSize();
392435
this.updateOfflineApps();
393436
},
394437

@@ -533,7 +576,7 @@ var gAdvancedPane = {
533576

534577
list.removeChild(item);
535578
gAdvancedPane.offlineAppSelected();
536-
this.updateActualCacheSize("offline");
579+
this.updateActualAppCacheSize();
537580
},
538581

539582
// UPDATE TAB

0 commit comments

Comments
 (0)