gmane page-mode: binding for browser-object-links
[conkeror.git] / modules / cache.js
blob1ff2bf1406ebe306c7cef981c88c61ab002276df
1 /**
2  * (C) Copyright 2008 Nicholas A. Zigarovich
3  *
4  * Use, modification, and distribution are subject to the terms specified in the
5  * COPYING file.
6 **/
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)
23         uri = uri.spec;
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(/#.*$/, "");
28     try {
29         return session.openCacheEntry(cache_key,
30                                       Ci.nsICache.ACCESS_READ,
31                                       false);
32     }
33     catch (ex) {
34         if (ex.name == "NS_ERROR_CACHE_KEY_NOT_FOUND")
35             return null;
36         throw ex;
37     }
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);
43     if (entry == null)
44         return false;
45     entry.doom();
46     entry.close();
47     return true;
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);
67     }
68     else
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);
83     }
84     else
85         throw new Error("Invalid cache type");