forked from Infocatcher/Private_Tab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
protocol.jsm
200 lines (193 loc) · 6.37 KB
/
protocol.jsm
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
var EXPORTED_SYMBOLS = ["privateProtocol"];
const P_CID = Components.ID("{e974cf10-11cb-4293-af88-e61c7dfe717c}"),
P_CONTRACTID = "@mozilla.org/network/protocol;1?name=private",
P_HANDLER = Components.interfaces.nsIProtocolHandler,
P_SCHEME = "private",
P_NAME = "Private Tab protocol handler";
Components.utils.import("resource://gre/modules/Services.jsm");
this.__defineGetter__("_log", function() {
delete this._log;
Services.scriptloader.loadSubScript("chrome://privatetab/content/log.js");
return _log;
});
this.__defineSetter__("_log", function(logger) {
delete this._log;
this._log = logger;
});
var privateProtocol = {
get platformVersion() {
delete this.platformVersion;
return this.platformVersion = parseFloat(Services.appinfo.platformVersion);
},
get compReg() {
return Components.manager
.QueryInterface(Components.interfaces.nsIComponentRegistrar);
},
init: function(logger) {
if(logger)
_log = logger;
this.compReg.registerFactory(P_CID, P_NAME, P_CONTRACTID, this);
_log("[protocol] Initialized");
},
destroy: function() {
this.compReg.unregisterFactory(P_CID, this);
_log("[protocol] Destroyed");
_log = function() {};
},
// nsIFactory
createInstance: function(outer, iid) {
if(outer != null)
throw Components.results.NS_ERROR_NO_AGGREGATION;
if(iid.equals(P_HANDLER))
return this;
throw Components.results.NS_ERROR_NO_INTERFACE;
},
lockFactory: function(lock) {
throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
},
// nsISupports
QueryInterface: function(iid) {
if(
iid.equals(Components.interfaces.nsISupports)
|| iid.equals(Components.interfaces.nsIFactory)
|| iid.equals(P_HANDLER)
)
return this;
throw Components.results.NS_ERROR_NO_INTERFACE;
},
// nsIProtocolHandler
defaultPort: -1,
protocolFlags: P_HANDLER.URI_NORELATIVE
| P_HANDLER.URI_NOAUTH
| P_HANDLER.URI_FORBIDS_AUTOMATIC_DOCUMENT_REPLACEMENT
//| P_HANDLER.URI_LOADABLE_BY_ANYONE
| P_HANDLER.URI_DANGEROUS_TO_LOAD
| P_HANDLER.URI_NON_PERSISTABLE,
scheme: P_SCHEME,
allowPort: function() {
return false;
},
newURI: function(spec, originCharset, baseURI) {
var uri = Components.classes["@mozilla.org/network/simple-uri;1"]
.createInstance(Components.interfaces.nsIURI);
uri.spec = spec;
return uri;
},
newChannel: function(uri) {
return this.newChannel2(uri, null);
},
newChannel2: function(uri, loadInfo) {
var spec = uri.spec;
_log("[protocol] newChannel(): spec = " + spec);
var newSpec = "";
var schemePrefix = P_SCHEME + ":";
// Example: private:///#http://example.com/ (legacy) or private:http://example.com/
if(spec && spec.startsWith(schemePrefix))
newSpec = spec.substr(schemePrefix.length).replace(/^\/*#?/, "");
_log("[protocol] newChannel(): newSpec = " + newSpec);
// We can't use newChannel(newSpec, ...) here - strange things happens
// Also we can't use nsIPrivateBrowsingChannel.setPrivate(true) for chrome:// URI
var redirect = "chrome://privatetab/content/protocolRedirect.html#" + newSpec;
var channel = "newChannelFromURIWithLoadInfo" in Services.io // Firefox 37+
&& (loadInfo || this.platformVersion >= 44) // Throws in Firefox 37-43 with null nsILoadInfo
? Services.io.newChannelFromURIWithLoadInfo(
Services.io.newURI(redirect, null, null),
loadInfo
)
: Services.io.newChannel(redirect, null, null); // Deprecated in Firefox 48+
var ensurePrivate = function(reason) {
_log("[protocol] " + reason + " => ensurePrivate()");
var isPrivate = this.makeChannelPrivate(channel);
ensurePrivate = function() { return true; }; // Don't call again in case of success
if(!isPrivate) // Don't load, will use workaround with tab duplication
channel.cancel(Components.results.NS_BINDING_ABORTED);
return isPrivate;
}.bind(this);
function proxy(method) {
return function() {
return ensurePrivate("nsIChannel." + method + "()")
&& channel[method].apply(this, arguments);
};
}
Services.tm.mainThread.dispatch(function() {
ensurePrivate("fallback delay");
}, Components.interfaces.nsIThread.DISPATCH_NORMAL);
return {
__proto__: channel,
asyncOpen: proxy("asyncOpen"),
asyncOpen2: proxy("asyncOpen2"),
open: proxy("open"),
open2: proxy("open2")
};
},
makeChannelPrivate: function(channel) {
var loadContext = this.getLoadContext(channel);
if(loadContext) try {
if(loadContext.usePrivateBrowsing) // Already private
return true;
loadContext.usePrivateBrowsing = true;
return true;
}
catch(e) {
if( // See https://github.com/Infocatcher/Private_Tab/issues/251
this.platformVersion >= 52
&& this.fixTabFromLoadContext(loadContext, channel.URI.ref)
)
return undefined;
Components.utils.reportError(e);
}
if(channel instanceof Components.interfaces.nsIPrivateBrowsingChannel) try {
channel.setPrivate(true);
return true;
}
catch(e) {
Components.utils.reportError(e);
}
throw Components.results.NS_ERROR_NO_INTERFACE;
},
getLoadContext: function(channel) {
try {
if(channel.notificationCallbacks) {
return channel.notificationCallbacks
.getInterface(Components.interfaces.nsILoadContext);
}
}
catch(e) {
Components.utils.reportError(e);
}
try {
if(channel.loadGroup && channel.loadGroup.notificationCallbacks) {
return channel.loadGroup.notificationCallbacks
.getInterface(Components.interfaces.nsILoadContext);
}
}
catch(e) {
Components.utils.reportError(e);
}
return null;
},
fixTabFromLoadContext: function(loadContext, uri) {
if(loadContext.useRemoteTabs) {
_log("[protocol] fixTabFromLoadContext(): in content process");
var mm = loadContext.associatedWindow
.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
.getInterface(Components.interfaces.nsIDocShell)
.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
.getInterface(Components.interfaces.nsIContentFrameMessageManager);
var res = mm.sendSyncMessage("PrivateTab:ProtocolReplaceTab", { URI: uri });
return res && res[0];
}
var browser = loadContext.topFrameElement;
if(!browser) {
_log("[protocol] fixTabFromLoadContext(): missing loadContext.topFrameElement");
return false;
}
var window = browser.ownerDocument.defaultView;
var pt = window.privateTab || null;
if(!pt) {
_log("[protocol] fixTabFromLoadContext(): missing window.privateTab");
return false;
}
return pt._fixBrowserFromProtocol(browser, uri);
}
};