40496918a438e586f2e286e757ff9f5e8bd1ce26
[conkeror.git] / modules / cache.js
blob40496918a438e586f2e286e757ff9f5e8bd1ce26
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 function cache_error (code) {
21     let xpcom_exc = Components.Exception("", code);
22     let e = new Error("cache error: " + xpcom_exc.name);
23     e.result = code;
24     e.__proto__ = cache_error.prototype;
25     return e;
27 cache_error.prototype.__proto__ = Error.prototype;
29 function cache_entry_open(cache_session, uri) {
30     if (uri instanceof Ci.nsIURI)
31         uri = uri.spec;
33     let session = cache_service.createSession(cache_session, 0, true);
34     session.doomEntriesIfExpired = false;
36     let cc = yield CONTINUATION;
38     let cache_listener = {
39         onCacheEntryAvailable: function onCacheEntryAvailable(descriptor, accessGranted, status) {
40             if (status != Cr.NS_OK)
41                 cc.throw(cache_error(status));
42             else
43                 cc(descriptor);
44         }
45     };
47     let cache_key = uri.replace(/#.*$/, "");
48     session.asyncOpenCacheEntry(cache_key, Ci.nsICache.ACCESS_READ, cache_listener);
49     yield co_return(yield SUSPEND);
52 function cache_entry_clear(cache_session, uri) {
53     if (uri instanceof Ci.nsIURI)
54         uri = uri.spec;
56     let session = cache_service.createSession(cache_session, 0, true);
57     session.doomEntriesIfExpired = false;
59     let cc = yield CONTINUATION;
61     let cache_listener = {
62         onCacheEntryDoomed: function onCacheEntryDoomed(status) {
63             switch (status) {
64             case Cr.NS_OK:
65                 cc(true);
66                 break;
67             case Cr.NS_ERROR_NOT_AVAILABLE:
68                 cc(false);
69                 break;
70             default:
71                 cc.throw(cache_error(status));
72             }
73         }
74     };
76     let cache_key = uri.replace(/#.*$/, "");
77     session.doomEntry(cache_key, cache_listener);
78     yield co_return(yield SUSPEND);
81 function cache_clear (cache_type) {
82     cache_service.evictEntries(cache_type);
83     if (cache_type == CACHE_DISK)
84         cache_service.evictEntries(Ci.nsICache.STORE_ON_DISK_IN_FILE);
87 function cache_disable (cache_type) {
88     if (cache_type == CACHE_MEMORY)
89         session_pref("browser.cache.memory.enable", false);
90     else if (cache_type == CACHE_DISK)
91         session_pref("browser.cache.disk.enable", false);
92     else if (cache_type == CACHE_OFFLINE)
93         session_pref("browser.cache.offline.enable", false);
94     else if (cache_type == CACHE_ALL) {
95         cache_disable(CACHE_MEMORY);
96         cache_disable(CACHE_DISK);
97         cache_disable(CACHE_OFFLINE);
98     }
99     else
100         throw new Error("Invalid cache type");
103 function cache_enable (cache_type) {
104     if (cache_type == CACHE_MEMORY)
105         session_pref("browser.cache.memory.enable", true);
106     else if (cache_type == CACHE_DISK)
107         session_pref("browser.cache.disk.enable", true);
108     else if (cache_type == CACHE_OFFLINE)
109         session_pref("browser.cache.offline.enable", true);
110     else if (cache_type == CACHE_ALL) {
111         cache_enable(CACHE_MEMORY);
112         cache_enable(CACHE_DISK);
113         cache_enable(CACHE_OFFLINE);
114     }
115     else
116         throw new Error("Invalid cache type");
119 provide("cache");