Permalink
Browse files

Firefox 32 compatibility: use the new cache2 API for clearing cache e…

…ntries (fixes #5).
  • Loading branch information...
1 parent 6846a3b commit c83ea9e5913056f44ed711f1364671ad37d11849 @spasche committed Jun 18, 2014
Showing with 65 additions and 38 deletions.
  1. +57 −29 components/OpenInBrowser.js
  2. +1 −1 install.rdf
  3. +5 −2 tests/mime.php
  4. +2 −6 tests/tests.html
@@ -24,7 +24,7 @@
const Cc = Components.classes;
const Ci = Components.interfaces;
-const Cr = Components.results;
+const Cu = Components.utils;
const MAX_INTERCEPT_TIME = 10000;
@@ -33,7 +33,10 @@ const EXAMINE_MERGED_TOPIC = "http-on-examine-merged-response";
const DEBUG = false;
-Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
+const { Services } = Cu.import("resource://gre/modules/Services.jsm", {});
+const { XPCOMUtils } = Cu.import("resource://gre/modules/XPCOMUtils.jsm", {});
+const { LoadContextInfo } = Cu.import("resource://gre/modules/LoadContextInfo.jsm", {});
+
/**
* This object stores information which will be used for intercepting URL's
@@ -62,15 +65,44 @@ OpenInBrowser.prototype = {
classID: Components.ID("{14aa9340-c449-4956-a5f9-a52fb32f933d}"),
contractID: "@spasche.net/openinbrowser;1",
- _clearCacheEntry: function OIB_clearCacheEntry(url, callback) {
- const cacheService = Cc["@mozilla.org/network/cache-service;1"].
- getService(Ci.nsICacheService);
- var httpCacheSession = cacheService.createSession("HTTP", 0, true);
+ // Clear cache entry using the new cache2 service, introduced in Firefox 32.
+ _clearCacheEntryV2: function OIB__clearCacheEntryV2(url, callback) {
+
+ function doomURL(storage, url, callback) {
+ storage.asyncDoomURI(Services.io.newURI(url, null, null), "", {
+ onCacheEntryDoomed: function(result) {
+ debug("onCacheEntryDoomed result for url '" + url + "': " + result);
+ callback();
+ }
+ });
+ }
+
+ try {
+ let diskStorage = Services.cache2.diskCacheStorage(LoadContextInfo.default, false);
+ doomURL(diskStorage, url, function() {
+ let memoryStorage = Services.cache2.memoryCacheStorage(LoadContextInfo.default);
+ doomURL(memoryStorage, url, callback);
+ });
+
+ } catch (e) {
+ debug("Exception during cache clearing: " + e);
+ callback();
+ }
+ },
+
+ _clearCacheEntry: function OIB__clearCacheEntry(url, callback) {
+ let httpCacheSession;
+ try {
+ httpCacheSession = Services.cache.createSession("HTTP", 0, true);
+ } catch (ex) {
+ debug("Trying to clear cache entry using cache2 service");
+ return this._clearCacheEntryV2(url, callback);
+ }
httpCacheSession.doomEntriesIfExpired = false;
try {
- var cacheKey = url.replace(/#.*$/, "");
+ let cacheKey = url.replace(/#.*$/, "");
- var clearEntryListener = {
+ let clearEntryListener = {
onCacheEntryAvailable: function(entry, access, status) {
try {
entry.doom();
@@ -92,16 +124,16 @@ OpenInBrowser.prototype = {
addInterceptInfo: function OIB_addInterceptInfo(url, mime) {
debug("Added intercept info " + url);
- var self = this;
+ let self = this;
this._clearCacheEntry(url, function() {
self._interceptedInfos.push(new InterceptedInfo(url, mime));
if (self._interceptedInfos.length == 1) {
self._startCapture();
}
// remove intercepted url's after a while in case the observer is not triggered
- var timer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
- var callback = {
+ let timer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
+ let callback = {
notify: function notifyCallback(timer) {
self._removeInterceptedInfo(url);
}
@@ -110,33 +142,29 @@ OpenInBrowser.prototype = {
});
},
- _startCapture: function OIB_startCapture() {
+ _startCapture: function OIB__startCapture() {
debug("Start capture");
- var observerService = Cc["@mozilla.org/observer-service;1"].
- getService(Ci.nsIObserverService);
- observerService.addObserver(this, EXAMINE_TOPIC, false);
- observerService.addObserver(this, EXAMINE_MERGED_TOPIC, false);
+ Services.obs.addObserver(this, EXAMINE_TOPIC, false);
+ Services.obs.addObserver(this, EXAMINE_MERGED_TOPIC, false);
},
- _stopCapture: function OIB_stopCapture() {
- var observerService = Cc["@mozilla.org/observer-service;1"].
- getService(Ci.nsIObserverService);
- observerService.removeObserver(this, EXAMINE_TOPIC);
- observerService.removeObserver(this, EXAMINE_MERGED_TOPIC);
+ _stopCapture: function OIB__stopCapture() {
+ Services.obs.removeObserver(this, EXAMINE_TOPIC);
+ Services.obs.removeObserver(this, EXAMINE_MERGED_TOPIC);
debug("capture stopped");
},
- _getInterceptedInfo: function OIB_getInterceptedInfo(url) {
- for (var i = 0; i < this._interceptedInfos.length; i++) {
+ _getInterceptedInfo: function OIB__getInterceptedInfo(url) {
+ for (let i = 0; i < this._interceptedInfos.length; i++) {
if (this._interceptedInfos[i].url == url)
return this._interceptedInfos[i];
}
return null;
},
- _removeInterceptedInfo: function OIB_removeInterceptedInfo(url) {
- var index = -1;
- for (var i = 0; i < this._interceptedInfos.length; i++) {
+ _removeInterceptedInfo: function OIB__removeInterceptedInfo(url) {
+ let index = -1;
+ for (let i = 0; i < this._interceptedInfos.length; i++) {
if (this._interceptedInfos[i].url == url) {
index = i;
break;
@@ -156,11 +184,11 @@ OpenInBrowser.prototype = {
if (aTopic != EXAMINE_TOPIC && aTopic != EXAMINE_MERGED_TOPIC)
return;
- var channel = aSubject.QueryInterface(Ci.nsIHttpChannel);
- var url = channel.originalURI.spec;
+ let channel = aSubject.QueryInterface(Ci.nsIHttpChannel);
+ let url = channel.originalURI.spec;
debug("Observer inspecting: " + url);
- var interceptedInfo = this._getInterceptedInfo(url);
+ let interceptedInfo = this._getInterceptedInfo(url);
if (!interceptedInfo)
return;
View
@@ -6,7 +6,7 @@
<em:id>openinbrowser@www.spasche.net</em:id>
<em:name>Open in Browser</em:name>
- <em:version>1.13</em:version>
+ <em:version>1.14</em:version>
<em:type>2</em:type>
<em:description>Offers the possibility to display documents in browser window.</em:description>
<em:creator>Sylvain Pasche</em:creator>
View
@@ -47,8 +47,11 @@
}
}
?>
-<title>MIME General-purpose testcase</title>
-
+<DOCTYPE html>
+<head>
+ <title>MIME General-purpose testcase</title>
+ <meta charset="utf-8">
+</head>
<h1>GET</h1>
<form action="<?echo $_SERVER['PHP_SELF'];?>" method="GET">
<input type="hidden" name="do" value="1">
View
@@ -109,20 +109,16 @@
</li>
<li>
Tab selection test 2: open <a href="open_after_timeout.html">this page</a>.
- Then open a new window (you have 5sec to do so). When the "Opening..." dialog appears, select to open in browser
+ Then open a new window (you have 5 seconds to do so). When the "Opening..." dialog appears, select to open in browser
as text. The content should be opened in original tab and not the new window.
</li>
</ul>
- TODO:
- Not possible to open the following url as text. Need to investigate
- "http://releases.mozilla.org/pub/mozilla.org/firefox/releases/3.0/win32/en-US/Firefox%20Setup%203.0.exe"
-
</div>
<div class="file">
<p>
File tests, follow the instructions below.
- For the HTTP tests, open this page using http:/// protocol.
+ For the HTTP tests, open this page using http:// protocol.
</p>
<ul>
<li>

0 comments on commit c83ea9e

Please sign in to comment.