From 23edbc2f22b101a769980043d62de801c9d9e28f Mon Sep 17 00:00:00 2001 From: "Nicholas A. Zigarovich" Date: Fri, 10 Oct 2008 13:02:32 -0400 Subject: [PATCH] Updates and additions to cache API. - cache_service: moved from mime-type-override.js to cache.js. - clear_http_cache_entry: moved to from mime-type-overide.js to cache.js, fixed a typo, renamed to cache_entry_clear, made a bit more general. - Split off much of cache_entry_clear into a new function, cache_entry_open. - Added CACHE_SESSION_[HTTP|HTTP_OFFLINE|FTP] constants. - Define constants with 'const' instead of 'var'. - Require cache.js in conkeror.js. (Oops.) --- modules/cache.js | 52 ++++++++++++++++++++++++++++++++++++------- modules/conkeror.js | 1 + modules/mime-type-override.js | 17 +------------- 3 files changed, 46 insertions(+), 24 deletions(-) diff --git a/modules/cache.js b/modules/cache.js index 59d0e32..1ff2bf1 100644 --- a/modules/cache.js +++ b/modules/cache.js @@ -5,17 +5,52 @@ * COPYING file. **/ -var CACHE_MEMORY = Ci.nsICache.STORE_IN_MEMORY; -var CACHE_DISK = Ci.nsICache.STORE_ON_DISK; -var CACHE_OFFLINE = Ci.nsICache.STORE_OFFLINE; -var CACHE_ALL = Ci.nsICache.STORE_ANYWHERE; +const cache_service = Cc["@mozilla.org/network/cache-service;1"] + .getService(Ci.nsICacheService); + +const CACHE_MEMORY = Ci.nsICache.STORE_IN_MEMORY; +const CACHE_DISK = Ci.nsICache.STORE_ON_DISK; +const CACHE_OFFLINE = Ci.nsICache.STORE_OFFLINE; +const CACHE_ALL = Ci.nsICache.STORE_ANYWHERE; + +const CACHE_SESSION_HTTP = "HTTP"; +const CACHE_SESSION_HTTP_OFFLINE = "HTTP-offline"; +const CACHE_SESSION_FTP = "FTP"; + +// Returns null if uri is not cached. +function cache_entry_open(cache_type, cache_session, uri) { + if (uri instanceof Ci.nsIURI) + uri = uri.spec; + let session = cache_service.createSession(cache_session, 0, true); + session.doomEntriesIfExpired = false; + // Remove the ref component of the URL + let cache_key = uri.replace(/#.*$/, ""); + try { + return session.openCacheEntry(cache_key, + Ci.nsICache.ACCESS_READ, + false); + } + catch (ex) { + if (ex.name == "NS_ERROR_CACHE_KEY_NOT_FOUND") + return null; + throw ex; + } +} + +// Returns false if uri is not cached, else true. +function cache_entry_clear(cache_type, cache_session, uri) { + let entry = cache_entry_open(cache_type, cache_session, uri); + if (entry == null) + return false; + entry.doom(); + entry.close(); + return true; +} function cache_clear(cache_type) { - let cs = Cc["@mozilla.org/network/cache-service;1"] - .getService(Ci.nsICacheService); - cs.evictEntries(cache_type); + cache_service.evictEntries(cache_type); if (cache_type == CACHE_DISK) - cs.evictEntries(Ci.nsICache.STORE_ON_DISK_IN_FILE); + cache_service.evictEntries(Ci.nsICache.STORE_ON_DISK_IN_FILE); } function cache_disable(cache_type) { @@ -49,3 +84,4 @@ function cache_enable(cache_type) { else throw new Error("Invalid cache type"); } + diff --git a/modules/conkeror.js b/modules/conkeror.js index afeceb2..7dbff15 100644 --- a/modules/conkeror.js +++ b/modules/conkeror.js @@ -58,3 +58,4 @@ require("search-engine.js"); require("permission-manager.js"); require("cookie.js"); +require("cache.js"); diff --git a/modules/mime-type-override.js b/modules/mime-type-override.js index 49e4b8f..4fb94b6 100644 --- a/modules/mime-type-override.js +++ b/modules/mime-type-override.js @@ -37,20 +37,6 @@ function can_override_mime_type_for_uri(uri) { return uri.schemeIs("http") || uri.schemeIs("https"); } -const cache_service = Cc["@mozilla.org/network/cache-service;1"].getService(Ci.nsICacheService); - -function clear_http_cache_entry(uri_string) { - var http_cache_session = cache_service.createSession("HTTP", 0, true); - http_cache_session.doomEntriesIfExpired = false; - try { - // Remove the ref component of the URL - var cache_key = uri_string.replace(/#.*$/, ""); - var entry = http_cache_session.openCacheEntry(cacheKey, Ci.nsICache.ACCESS_READ, false); - entry.doom(); - entry.close(); - } catch (e) { } -} - let clear_override = function(uri_string) { table.remove(uri_string); table_size--; @@ -91,8 +77,7 @@ let observer = { // mime_type must be a string function override_mime_type_for_next_load(uri, mime_type) { - - clear_http_cache_entry(uri.spec); + cache_entry_clear(CACHE_ALL, CACHE_SESSION_HTTP, uri); var obj = table.get(uri.spec); if (obj) -- 2.11.4.GIT