This repository has been archived by the owner on Jun 26, 2023. It is now read-only.
/
OpenInBrowser.js
185 lines (155 loc) · 5.56 KB
/
OpenInBrowser.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is the Open in Browser extension.
*
* The Initial Developer of the Original Code is
* Sylvain Pasche <sylvain.pasche@gmail.com>.
* Portions created by the Initial Developer are Copyright (C) 2009
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*
* ***** END LICENSE BLOCK ***** */
const Cc = Components.classes;
const Ci = Components.interfaces;
const Cr = Components.results;
const MAX_INTERCEPT_TIME = 10000;
const EXAMINE_TOPIC = "http-on-examine-response";
const EXAMINE_MERGED_TOPIC = "http-on-examine-merged-response";
const DEBUG = false;
Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
/**
* This object stores information which will be used for intercepting URL's
*/
function InterceptedInfo(url, mime) {
this.url = url;
this.mime = mime;
}
function debug(msg) {
if (DEBUG)
dump(msg + "\n");
}
/**
* The service
*/
function OpenInBrowser() {
this.wrappedJSObject = this;
this._interceptedInfos = [];
}
OpenInBrowser.prototype = {
classDescription: "Open in browser Javascript XPCOM Component",
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);
httpCacheSession.doomEntriesIfExpired = false;
try {
var cacheKey = url.replace(/#.*$/, "");
var clearEntryListener = {
onCacheEntryAvailable: function(entry, access, status) {
try {
entry.doom();
entry.close();
} catch (e) {
debug("Exception during entry clearing: " + e);
}
callback();
}
};
httpCacheSession.asyncOpenCacheEntry(cacheKey, Ci.nsICache.ACCESS_READ_WRITE, clearEntryListener, false);
} catch (e) {
debug("Exception during cache clearing: " + e);
callback();
}
},
addInterceptInfo: function OIB_addInterceptInfo(url, mime) {
debug("Added intercept info " + url);
var 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 = {
notify: function notifyCallback(timer) {
self._removeInterceptedInfo(url);
}
};
timer.initWithCallback(callback, MAX_INTERCEPT_TIME, Ci.nsITimer.TYPE_ONE_SHOT);
});
},
_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);
},
_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);
debug("capture stopped");
},
_getInterceptedInfo: function OIB_getInterceptedInfo(url) {
for (var 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++) {
if (this._interceptedInfos[i].url == url) {
index = i;
break;
}
}
if (index == -1) {
return;
}
this._interceptedInfos.splice(index, 1);
if (this._interceptedInfos.length == 0) {
this._stopCapture();
}
},
observe: function OIB_observe(aSubject, aTopic, aData) {
if (aTopic != EXAMINE_TOPIC && aTopic != EXAMINE_MERGED_TOPIC)
return;
var channel = aSubject.QueryInterface(Ci.nsIHttpChannel);
var url = channel.originalURI.spec;
debug("Observer inspecting: " + url);
var interceptedInfo = this._getInterceptedInfo(url);
if (!interceptedInfo)
return;
debug("Got a match " + url);
channel.contentType = interceptedInfo.mime;
// Disable content sniffers that could override the new mime type
channel.loadFlags &= ~Ci.nsIChannel.LOAD_CALL_CONTENT_SNIFFERS;
// drop content-disposition header
channel.setResponseHeader("Content-Disposition", "", false);
this._removeInterceptedInfo(url);
},
QueryInterface: XPCOMUtils.generateQI([Ci.nsIObserver])
};
if (XPCOMUtils.generateNSGetFactory)
var NSGetFactory = XPCOMUtils.generateNSGetFactory([OpenInBrowser]);
else
var NSGetModule = XPCOMUtils.generateNSGetModule([OpenInBrowser]);