2 * (C) Copyright 2008 Nicholas A. Zigarovich
4 * Use, modification, and distribution are subject to the terms specified in the
8 const cache_service = Cc["@mozilla.org/network/cache-service;1"]
9 .getService(Ci.nsICacheService);
11 const CACHE_MEMORY = Ci.nsICache.STORE_IN_MEMORY;
12 const CACHE_DISK = Ci.nsICache.STORE_ON_DISK;
13 const CACHE_OFFLINE = Ci.nsICache.STORE_OFFLINE;
14 const CACHE_ALL = Ci.nsICache.STORE_ANYWHERE;
16 const CACHE_SESSION_HTTP = "HTTP";
17 const CACHE_SESSION_HTTP_OFFLINE = "HTTP-offline";
18 const CACHE_SESSION_FTP = "FTP";
20 // Returns null if uri is not cached.
21 function cache_entry_open(cache_type, cache_session, uri) {
22 if (uri instanceof Ci.nsIURI)
24 let session = cache_service.createSession(cache_session, 0, true);
25 session.doomEntriesIfExpired = false;
26 // Remove the ref component of the URL
27 let cache_key = uri.replace(/#.*$/, "");
29 return session.openCacheEntry(cache_key,
30 Ci.nsICache.ACCESS_READ,
34 if (ex.name == "NS_ERROR_CACHE_KEY_NOT_FOUND")
40 // Returns false if uri is not cached, else true.
41 function cache_entry_clear(cache_type, cache_session, uri) {
42 let entry = cache_entry_open(cache_type, cache_session, uri);
50 function cache_clear(cache_type) {
51 cache_service.evictEntries(cache_type);
52 if (cache_type == CACHE_DISK)
53 cache_service.evictEntries(Ci.nsICache.STORE_ON_DISK_IN_FILE);
56 function cache_disable(cache_type) {
57 if (cache_type == CACHE_MEMORY)
58 session_pref("browser.cache.memory.enable", false);
59 else if (cache_type == CACHE_DISK)
60 session_pref("browser.cache.disk.enable", false);
61 else if (cache_type == CACHE_OFFLINE)
62 session_pref("browser.cache.offline.enable", false);
63 else if (cache_type == CACHE_ALL) {
64 cache_disable(CACHE_MEMORY);
65 cache_disable(CACHE_DISK);
66 cache_disable(CACHE_OFFLINE);
69 throw new Error("Invalid cache type");
72 function cache_enable(cache_type) {
73 if (cache_type == CACHE_MEMORY)
74 session_pref("browser.cache.memory.enable", true);
75 else if (cache_type == CACHE_DISK)
76 session_pref("browser.cache.disk.enable", true);
77 else if (cache_type == CACHE_OFFLINE)
78 session_pref("browser.cache.offline.enable", true);
79 else if (cache_type == CACHE_ALL) {
80 cache_enable(CACHE_MEMORY);
81 cache_enable(CACHE_DISK);
82 cache_enable(CACHE_OFFLINE);
85 throw new Error("Invalid cache type");