]> git.donarmstrong.com Git - dactyl.git/blobdiff - common/modules/cache.jsm
Import r6923 from upstream hg supporting Firefox up to 22.0a1
[dactyl.git] / common / modules / cache.jsm
index c24084671c6b1f9dc71af75a93708fec03ad4872..5f39bc6a3264ca2d43f93cf6785598ceed00e152 100644 (file)
@@ -1,14 +1,16 @@
-// Copyright (c) 2011 by Kris Maglione <maglione.k@gmail.com>
+// Copyright (c) 2011-2012 Kris Maglione <maglione.k@gmail.com>
 //
 // This work is licensed for reuse under an MIT license. Details are
 // given in the LICENSE.txt file included with this file.
-/* use strict */
+"use strict";
 
-Components.utils.import("resource://dactyl/bootstrap.jsm");
 defineModule("cache", {
     exports: ["Cache", "cache"],
     require: ["config", "services", "util"]
-}, this);
+});
+
+lazyRequire("overlay", ["overlay"]);
+lazyRequire("storage", ["File"]);
 
 var Cache = Module("Cache", XPCOM(Ci.nsIRequestObserver), {
     init: function init() {
@@ -77,7 +79,7 @@ var Cache = Module("Cache", XPCOM(Ci.nsIRequestObserver), {
         if (!this._cacheReader && this.cacheFile.exists()
                 && !this.inQueue)
             try {
-                this._cacheReader = services.ZipReader(this.cacheFile);
+                this._cacheReader = services.ZipReader(this.cacheFile.file);
             }
             catch (e if e.result == Cr.NS_ERROR_FILE_CORRUPTED) {
                 util.reportError(e);
@@ -97,14 +99,14 @@ var Cache = Module("Cache", XPCOM(Ci.nsIRequestObserver), {
                 if (!this.cacheFile.exists())
                     mode |= File.MODE_CREATE;
 
-                cache._cacheWriter = services.ZipWriter(this.cacheFile, mode);
+                cache._cacheWriter = services.ZipWriter(this.cacheFile.file, mode);
             }
             catch (e if e.result == Cr.NS_ERROR_FILE_CORRUPTED) {
                 util.reportError(e);
                 this.cacheFile.remove(false);
 
                 mode |= File.MODE_CREATE;
-                cache._cacheWriter = services.ZipWriter(this.cacheFile, mode);
+                cache._cacheWriter = services.ZipWriter(this.cacheFile.file, mode);
             }
         return this._cacheWriter;
     },
@@ -116,7 +118,7 @@ var Cache = Module("Cache", XPCOM(Ci.nsIRequestObserver), {
         }
     },
 
-    closeWriter: function closeWriter() {
+    closeWriter: util.wrapCallback(function closeWriter() {
         this.closeReader();
 
         if (this._cacheWriter) {
@@ -127,7 +129,7 @@ var Cache = Module("Cache", XPCOM(Ci.nsIRequestObserver), {
             if (this.cacheFile.fileSize <= 22)
                 this.cacheFile.remove(false);
         }
-    },
+    }),
 
     flush: function flush() {
         cache.cache = {};
@@ -157,7 +159,7 @@ var Cache = Module("Cache", XPCOM(Ci.nsIRequestObserver), {
     },
 
     flushJAR: function flushJAR(file) {
-        services.observer.notifyObservers(file, "flush-cache-entry", "");
+        services.observer.notifyObservers(File(file).file, "flush-cache-entry", "");
     },
 
     flushStartup: function flushStartup() {
@@ -200,8 +202,12 @@ var Cache = Module("Cache", XPCOM(Ci.nsIRequestObserver), {
             return cache.force(name);
     },
 
-    get: function get(name) {
+    get: function get(name, callback, self) {
         if (!Set.has(this.cache, name)) {
+            if (callback && !(Set.has(this.providers, name) ||
+                              Set.has(this.localProviders, name)))
+                this.register(name, callback, self);
+
             this.cache[name] = this.force(name);
             util.assert(this.cache[name] !== undefined,
                         "No such cache key", false);
@@ -228,11 +234,14 @@ var Cache = Module("Cache", XPCOM(Ci.nsIRequestObserver), {
 
         if (this.queue.length && !this.inQueue) {
             // removeEntry does not work properly with queues.
+            let removed = 0;
             for each (let [, entry] in this.queue)
                 if (this.getCacheWriter().hasEntry(entry)) {
                     this.getCacheWriter().removeEntry(entry, false);
-                    this.closeWriter();
+                    removed++;
                 }
+            if (removed)
+                this.closeWriter();
 
             this.queue.splice(0).forEach(function ([time, entry]) {
                 if (time && Set.has(this.cache, entry)) {