isearch-backspace, isearch-done: docstrings
[conkeror.git] / modules / cache.js
blob816760cb1828a1cfb6db07a552cb93a8eae4b7e5
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             ex.name == "NS_ERROR_CACHE_WAIT_FOR_VALIDATION")
36             return null;
37         throw ex;
38     }
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);
44     if (entry == null)
45         return false;
46     entry.doom();
47     entry.close();
48     return true;
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);
68     }
69     else
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);
84     }
85     else
86         throw new Error("Invalid cache type");
89 provide("cache");