michael@0: Components.utils.import('resource://gre/modules/XPCOMUtils.jsm'); michael@0: Components.utils.import('resource://gre/modules/LoadContextInfo.jsm'); michael@0: michael@0: var _CSvc; michael@0: function get_cache_service() { michael@0: if (_CSvc) michael@0: return _CSvc; michael@0: michael@0: return _CSvc = Components.classes["@mozilla.org/netwerk/cache-storage-service;1"] michael@0: .getService(Components.interfaces.nsICacheStorageService); michael@0: } michael@0: michael@0: function evict_cache_entries(where) michael@0: { michael@0: var clearDisk = (!where || where == "disk" || where == "all"); michael@0: var clearMem = (!where || where == "memory" || where == "all"); michael@0: var clearAppCache = (where == "appcache"); michael@0: michael@0: var svc = get_cache_service(); michael@0: var storage; michael@0: michael@0: if (clearMem) { michael@0: storage = svc.memoryCacheStorage(LoadContextInfo.default); michael@0: storage.asyncEvictStorage(null); michael@0: } michael@0: michael@0: if (clearDisk) { michael@0: storage = svc.diskCacheStorage(LoadContextInfo.default, false); michael@0: storage.asyncEvictStorage(null); michael@0: } michael@0: michael@0: if (clearAppCache) { michael@0: storage = svc.appCacheStorage(LoadContextInfo.default, null); michael@0: storage.asyncEvictStorage(null); michael@0: } michael@0: } michael@0: michael@0: function createURI(urispec) michael@0: { michael@0: var ioServ = Components.classes["@mozilla.org/network/io-service;1"] michael@0: .getService(Components.interfaces.nsIIOService); michael@0: return ioServ.newURI(urispec, null, null); michael@0: } michael@0: michael@0: function getCacheStorage(where, lci, appcache) michael@0: { michael@0: if (!lci) lci = LoadContextInfo.default; michael@0: var svc = get_cache_service(); michael@0: switch (where) { michael@0: case "disk": return svc.diskCacheStorage(lci, false); michael@0: case "memory": return svc.memoryCacheStorage(lci); michael@0: case "appcache": return svc.appCacheStorage(lci, appcache); michael@0: } michael@0: return null; michael@0: } michael@0: michael@0: function asyncOpenCacheEntry(key, where, flags, lci, callback, appcache) michael@0: { michael@0: key = createURI(key); michael@0: michael@0: function CacheListener() { } michael@0: CacheListener.prototype = { michael@0: _appCache: appcache, michael@0: michael@0: QueryInterface: function (iid) { michael@0: if (iid.equals(Components.interfaces.nsICacheEntryOpenCallback) || michael@0: iid.equals(Components.interfaces.nsISupports)) michael@0: return this; michael@0: throw Components.results.NS_ERROR_NO_INTERFACE; michael@0: }, michael@0: michael@0: onCacheEntryCheck: function(entry, appCache) { michael@0: if (typeof callback === "object") michael@0: return callback.onCacheEntryCheck(entry, appCache); michael@0: return Ci.nsICacheEntryOpenCallback.ENTRY_WANTED; michael@0: }, michael@0: michael@0: onCacheEntryAvailable: function (entry, isnew, appCache, status) { michael@0: if (typeof callback === "object") { michael@0: // Root us at the callback michael@0: callback.__cache_listener_root = this; michael@0: callback.onCacheEntryAvailable(entry, isnew, appCache, status); michael@0: } michael@0: else michael@0: callback(status, entry, appCache); michael@0: }, michael@0: michael@0: run: function () { michael@0: var storage = getCacheStorage(where, lci, this._appCache); michael@0: storage.asyncOpenURI(key, "", flags, this); michael@0: } michael@0: }; michael@0: michael@0: (new CacheListener()).run(); michael@0: } michael@0: michael@0: function syncWithCacheIOThread(callback) michael@0: { michael@0: if (!newCacheBackEndUsed()) { michael@0: asyncOpenCacheEntry( michael@0: "http://nonexistententry/", "disk", Ci.nsICacheStorage.OPEN_READONLY, null, michael@0: function(status, entry) { michael@0: do_check_eq(status, Components.results.NS_ERROR_CACHE_KEY_NOT_FOUND); michael@0: callback(); michael@0: }); michael@0: } michael@0: else { michael@0: callback(); michael@0: } michael@0: } michael@0: michael@0: function get_device_entry_count(where, lci, continuation) { michael@0: var storage = getCacheStorage(where, lci); michael@0: if (!storage) { michael@0: continuation(-1, 0); michael@0: return; michael@0: } michael@0: michael@0: var visitor = { michael@0: onCacheStorageInfo: function (entryCount, consumption) { michael@0: do_execute_soon(function() { michael@0: continuation(entryCount, consumption); michael@0: }); michael@0: }, michael@0: }; michael@0: michael@0: // get the device entry count michael@0: storage.asyncVisitStorage(visitor, false); michael@0: } michael@0: michael@0: function asyncCheckCacheEntryPresence(key, where, shouldExist, continuation, appCache) michael@0: { michael@0: asyncOpenCacheEntry(key, where, Ci.nsICacheStorage.OPEN_READONLY, null, michael@0: function(status, entry) { michael@0: if (shouldExist) { michael@0: dump("TEST-INFO | checking cache key " + key + " exists @ " + where); michael@0: do_check_eq(status, Cr.NS_OK); michael@0: do_check_true(!!entry); michael@0: } else { michael@0: dump("TEST-INFO | checking cache key " + key + " doesn't exist @ " + where); michael@0: do_check_eq(status, Cr.NS_ERROR_CACHE_KEY_NOT_FOUND); michael@0: do_check_null(entry); michael@0: } michael@0: continuation(); michael@0: }, appCache); michael@0: }