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" ||
35 ex.name == "NS_ERROR_CACHE_WAIT_FOR_VALIDATION")
41 // Returns false if uri is not cached, else true.
42 function cache_entry_clear (cache_type, cache_session, uri) {
43 let entry = cache_entry_open(cache_type, cache_session, uri);
51 function cache_clear (cache_type) {
52 cache_service.evictEntries(cache_type);
53 if (cache_type == CACHE_DISK)
54 cache_service.evictEntries(Ci.nsICache.STORE_ON_DISK_IN_FILE);
57 function cache_disable (cache_type) {
58 if (cache_type == CACHE_MEMORY)
59 session_pref("browser.cache.memory.enable", false);
60 else if (cache_type == CACHE_DISK)
61 session_pref("browser.cache.disk.enable", false);
62 else if (cache_type == CACHE_OFFLINE)
63 session_pref("browser.cache.offline.enable", false);
64 else if (cache_type == CACHE_ALL) {
65 cache_disable(CACHE_MEMORY);
66 cache_disable(CACHE_DISK);
67 cache_disable(CACHE_OFFLINE);
70 throw new Error("Invalid cache type");
73 function cache_enable (cache_type) {
74 if (cache_type == CACHE_MEMORY)
75 session_pref("browser.cache.memory.enable", true);
76 else if (cache_type == CACHE_DISK)
77 session_pref("browser.cache.disk.enable", true);
78 else if (cache_type == CACHE_OFFLINE)
79 session_pref("browser.cache.offline.enable", true);
80 else if (cache_type == CACHE_ALL) {
81 cache_enable(CACHE_MEMORY);
82 cache_enable(CACHE_DISK);
83 cache_enable(CACHE_OFFLINE);
86 throw new Error("Invalid cache type");