forked from Infocatcher/Private_Tab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
protocol.jsm
152 lines (145 loc) · 4.61 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
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");
var global = this;
var privateProtocol = {
get compReg() {
return Components.manager
.QueryInterface(Components.interfaces.nsIComponentRegistrar);
},
init: function(_log) {
global._log = _log;
this.compReg.registerFactory(P_CID, P_NAME, P_CONTRACTID, this);
_log("[protocol] Initialized");
},
destroy: function() {
this.compReg.unregisterFactory(P_CID, this);
_log("[protocol] Destroyed");
delete global._log;
},
// 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+
? Services.io.newChannelFromURIWithLoadInfo(
Services.io.newURI(redirect, null, null),
loadInfo
)
: Services.io.newChannel(redirect, null, null); // Removed in Firefox 48+
var ensurePrivate = function(reason) {
_log("[protocol] " + reason + " => ensurePrivate()");
this.makeChannelPrivate(channel);
ensurePrivate = function() {}; // Don't call again in case of success
}.bind(this);
var channelWrapper = {
__proto__: channel,
asyncOpen: function(aListener, aContext) {
ensurePrivate("nsIChannel.asyncOpen()");
return channel.asyncOpen.apply(this, arguments);
},
asyncOpen2: function(aListener) {
ensurePrivate("nsIChannel.asyncOpen2()");
return channel.asyncOpen2.apply(this, arguments);
},
open: function() {
ensurePrivate("nsIChannel.open()");
return channel.open.apply(this, arguments);
},
open2: function() {
ensurePrivate("nsIChannel.open2()");
return channel.open2.apply(this, arguments);
}
};
Services.tm.mainThread.dispatch(function() {
ensurePrivate("fallback delay");
}, Components.interfaces.nsIThread.DISPATCH_NORMAL);
return channelWrapper;
},
makeChannelPrivate: function(channel) {
try {
if(channel.notificationCallbacks) {
channel.notificationCallbacks
.getInterface(Components.interfaces.nsILoadContext)
.usePrivateBrowsing = true;
return;
}
}
catch(e) {
Components.utils.reportError(e);
}
try {
if(channel.loadGroup && channel.loadGroup.notificationCallbacks) {
channel.loadGroup.notificationCallbacks
.getInterface(Components.interfaces.nsILoadContext)
.usePrivateBrowsing = true;
return;
}
}
catch(e) {
Components.utils.reportError(e);
}
if(channel instanceof Components.interfaces.nsIPrivateBrowsingChannel) try {
channel.setPrivate(true);
return;
}
catch(e) {
Components.utils.reportError(e);
}
throw Components.results.NS_ERROR_NO_INTERFACE;
}
};