2 * (C) Copyright 2008 Nicholas A. Zigarovich
4 * Use, modification, and distribution are subject to the terms specified in the
10 const cache_service = Cc["@mozilla.org/network/cache-service;1"]
11 .getService(Ci.nsICacheService);
13 const CACHE_MEMORY = Ci.nsICache.STORE_IN_MEMORY;
14 const CACHE_DISK = Ci.nsICache.STORE_ON_DISK;
15 const CACHE_OFFLINE = Ci.nsICache.STORE_OFFLINE;
16 const CACHE_ALL = Ci.nsICache.STORE_ANYWHERE;
18 const CACHE_SESSION_HTTP = "HTTP";
19 const CACHE_SESSION_HTTP_OFFLINE = "HTTP-offline";
20 const CACHE_SESSION_FTP = "FTP";
22 // Returns null if uri is not cached.
23 function cache_entry_open (cache_type, cache_session, uri) {
24 if (uri instanceof Ci.nsIURI)
26 let session = cache_service.createSession(cache_session, 0, true);
27 session.doomEntriesIfExpired = false;
28 // Remove the ref component of the URL
29 let cache_key = uri.replace(/#.*$/, "");
31 return session.openCacheEntry(cache_key,
32 Ci.nsICache.ACCESS_READ,
36 if (ex.name == "NS_ERROR_CACHE_KEY_NOT_FOUND" ||
37 ex.name == "NS_ERROR_CACHE_WAIT_FOR_VALIDATION")
43 // Returns false if uri is not cached, else true.
44 function cache_entry_clear (cache_type, cache_session, uri) {
45 let entry = cache_entry_open(cache_type, cache_session, uri);
53 function cache_clear (cache_type) {
54 cache_service.evictEntries(cache_type);
55 if (cache_type == CACHE_DISK)
56 cache_service.evictEntries(Ci.nsICache.STORE_ON_DISK_IN_FILE);
59 function cache_disable (cache_type) {
60 if (cache_type == CACHE_MEMORY)
61 session_pref("browser.cache.memory.enable", false);
62 else if (cache_type == CACHE_DISK)
63 session_pref("browser.cache.disk.enable", false);
64 else if (cache_type == CACHE_OFFLINE)
65 session_pref("browser.cache.offline.enable", false);
66 else if (cache_type == CACHE_ALL) {
67 cache_disable(CACHE_MEMORY);
68 cache_disable(CACHE_DISK);
69 cache_disable(CACHE_OFFLINE);
72 throw new Error("Invalid cache type");
75 function cache_enable (cache_type) {
76 if (cache_type == CACHE_MEMORY)
77 session_pref("browser.cache.memory.enable", true);
78 else if (cache_type == CACHE_DISK)
79 session_pref("browser.cache.disk.enable", true);
80 else if (cache_type == CACHE_OFFLINE)
81 session_pref("browser.cache.offline.enable", true);
82 else if (cache_type == CACHE_ALL) {
83 cache_enable(CACHE_MEMORY);
84 cache_enable(CACHE_DISK);
85 cache_enable(CACHE_OFFLINE);
88 throw new Error("Invalid cache type");