Thu, 15 Jan 2015 15:55:04 +0100
Back out 97036ab72558 which inappropriately compared turds to third parties.
michael@0 | 1 | Components.utils.import('resource://gre/modules/XPCOMUtils.jsm'); |
michael@0 | 2 | Components.utils.import('resource://gre/modules/LoadContextInfo.jsm'); |
michael@0 | 3 | |
michael@0 | 4 | var _CSvc; |
michael@0 | 5 | function get_cache_service() { |
michael@0 | 6 | if (_CSvc) |
michael@0 | 7 | return _CSvc; |
michael@0 | 8 | |
michael@0 | 9 | return _CSvc = Components.classes["@mozilla.org/netwerk/cache-storage-service;1"] |
michael@0 | 10 | .getService(Components.interfaces.nsICacheStorageService); |
michael@0 | 11 | } |
michael@0 | 12 | |
michael@0 | 13 | function evict_cache_entries(where) |
michael@0 | 14 | { |
michael@0 | 15 | var clearDisk = (!where || where == "disk" || where == "all"); |
michael@0 | 16 | var clearMem = (!where || where == "memory" || where == "all"); |
michael@0 | 17 | var clearAppCache = (where == "appcache"); |
michael@0 | 18 | |
michael@0 | 19 | var svc = get_cache_service(); |
michael@0 | 20 | var storage; |
michael@0 | 21 | |
michael@0 | 22 | if (clearMem) { |
michael@0 | 23 | storage = svc.memoryCacheStorage(LoadContextInfo.default); |
michael@0 | 24 | storage.asyncEvictStorage(null); |
michael@0 | 25 | } |
michael@0 | 26 | |
michael@0 | 27 | if (clearDisk) { |
michael@0 | 28 | storage = svc.diskCacheStorage(LoadContextInfo.default, false); |
michael@0 | 29 | storage.asyncEvictStorage(null); |
michael@0 | 30 | } |
michael@0 | 31 | |
michael@0 | 32 | if (clearAppCache) { |
michael@0 | 33 | storage = svc.appCacheStorage(LoadContextInfo.default, null); |
michael@0 | 34 | storage.asyncEvictStorage(null); |
michael@0 | 35 | } |
michael@0 | 36 | } |
michael@0 | 37 | |
michael@0 | 38 | function createURI(urispec) |
michael@0 | 39 | { |
michael@0 | 40 | var ioServ = Components.classes["@mozilla.org/network/io-service;1"] |
michael@0 | 41 | .getService(Components.interfaces.nsIIOService); |
michael@0 | 42 | return ioServ.newURI(urispec, null, null); |
michael@0 | 43 | } |
michael@0 | 44 | |
michael@0 | 45 | function getCacheStorage(where, lci, appcache) |
michael@0 | 46 | { |
michael@0 | 47 | if (!lci) lci = LoadContextInfo.default; |
michael@0 | 48 | var svc = get_cache_service(); |
michael@0 | 49 | switch (where) { |
michael@0 | 50 | case "disk": return svc.diskCacheStorage(lci, false); |
michael@0 | 51 | case "memory": return svc.memoryCacheStorage(lci); |
michael@0 | 52 | case "appcache": return svc.appCacheStorage(lci, appcache); |
michael@0 | 53 | } |
michael@0 | 54 | return null; |
michael@0 | 55 | } |
michael@0 | 56 | |
michael@0 | 57 | function asyncOpenCacheEntry(key, where, flags, lci, callback, appcache) |
michael@0 | 58 | { |
michael@0 | 59 | key = createURI(key); |
michael@0 | 60 | |
michael@0 | 61 | function CacheListener() { } |
michael@0 | 62 | CacheListener.prototype = { |
michael@0 | 63 | _appCache: appcache, |
michael@0 | 64 | |
michael@0 | 65 | QueryInterface: function (iid) { |
michael@0 | 66 | if (iid.equals(Components.interfaces.nsICacheEntryOpenCallback) || |
michael@0 | 67 | iid.equals(Components.interfaces.nsISupports)) |
michael@0 | 68 | return this; |
michael@0 | 69 | throw Components.results.NS_ERROR_NO_INTERFACE; |
michael@0 | 70 | }, |
michael@0 | 71 | |
michael@0 | 72 | onCacheEntryCheck: function(entry, appCache) { |
michael@0 | 73 | if (typeof callback === "object") |
michael@0 | 74 | return callback.onCacheEntryCheck(entry, appCache); |
michael@0 | 75 | return Ci.nsICacheEntryOpenCallback.ENTRY_WANTED; |
michael@0 | 76 | }, |
michael@0 | 77 | |
michael@0 | 78 | onCacheEntryAvailable: function (entry, isnew, appCache, status) { |
michael@0 | 79 | if (typeof callback === "object") { |
michael@0 | 80 | // Root us at the callback |
michael@0 | 81 | callback.__cache_listener_root = this; |
michael@0 | 82 | callback.onCacheEntryAvailable(entry, isnew, appCache, status); |
michael@0 | 83 | } |
michael@0 | 84 | else |
michael@0 | 85 | callback(status, entry, appCache); |
michael@0 | 86 | }, |
michael@0 | 87 | |
michael@0 | 88 | run: function () { |
michael@0 | 89 | var storage = getCacheStorage(where, lci, this._appCache); |
michael@0 | 90 | storage.asyncOpenURI(key, "", flags, this); |
michael@0 | 91 | } |
michael@0 | 92 | }; |
michael@0 | 93 | |
michael@0 | 94 | (new CacheListener()).run(); |
michael@0 | 95 | } |
michael@0 | 96 | |
michael@0 | 97 | function syncWithCacheIOThread(callback) |
michael@0 | 98 | { |
michael@0 | 99 | if (!newCacheBackEndUsed()) { |
michael@0 | 100 | asyncOpenCacheEntry( |
michael@0 | 101 | "http://nonexistententry/", "disk", Ci.nsICacheStorage.OPEN_READONLY, null, |
michael@0 | 102 | function(status, entry) { |
michael@0 | 103 | do_check_eq(status, Components.results.NS_ERROR_CACHE_KEY_NOT_FOUND); |
michael@0 | 104 | callback(); |
michael@0 | 105 | }); |
michael@0 | 106 | } |
michael@0 | 107 | else { |
michael@0 | 108 | callback(); |
michael@0 | 109 | } |
michael@0 | 110 | } |
michael@0 | 111 | |
michael@0 | 112 | function get_device_entry_count(where, lci, continuation) { |
michael@0 | 113 | var storage = getCacheStorage(where, lci); |
michael@0 | 114 | if (!storage) { |
michael@0 | 115 | continuation(-1, 0); |
michael@0 | 116 | return; |
michael@0 | 117 | } |
michael@0 | 118 | |
michael@0 | 119 | var visitor = { |
michael@0 | 120 | onCacheStorageInfo: function (entryCount, consumption) { |
michael@0 | 121 | do_execute_soon(function() { |
michael@0 | 122 | continuation(entryCount, consumption); |
michael@0 | 123 | }); |
michael@0 | 124 | }, |
michael@0 | 125 | }; |
michael@0 | 126 | |
michael@0 | 127 | // get the device entry count |
michael@0 | 128 | storage.asyncVisitStorage(visitor, false); |
michael@0 | 129 | } |
michael@0 | 130 | |
michael@0 | 131 | function asyncCheckCacheEntryPresence(key, where, shouldExist, continuation, appCache) |
michael@0 | 132 | { |
michael@0 | 133 | asyncOpenCacheEntry(key, where, Ci.nsICacheStorage.OPEN_READONLY, null, |
michael@0 | 134 | function(status, entry) { |
michael@0 | 135 | if (shouldExist) { |
michael@0 | 136 | dump("TEST-INFO | checking cache key " + key + " exists @ " + where); |
michael@0 | 137 | do_check_eq(status, Cr.NS_OK); |
michael@0 | 138 | do_check_true(!!entry); |
michael@0 | 139 | } else { |
michael@0 | 140 | dump("TEST-INFO | checking cache key " + key + " doesn't exist @ " + where); |
michael@0 | 141 | do_check_eq(status, Cr.NS_ERROR_CACHE_KEY_NOT_FOUND); |
michael@0 | 142 | do_check_null(entry); |
michael@0 | 143 | } |
michael@0 | 144 | continuation(); |
michael@0 | 145 | }, appCache); |
michael@0 | 146 | } |