-
Notifications
You must be signed in to change notification settings - Fork 20
/
protocol.js
124 lines (119 loc) · 3.66 KB
/
protocol.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
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";
var privateProtocol = {
get compReg() {
return Components.manager
.QueryInterface(Components.interfaces.nsIComponentRegistrar);
},
init: function() {
this.compReg.registerFactory(P_CID, P_NAME, P_CONTRACTID, this);
_log("[protocol] Initialized");
},
destroy: function() {
this.compReg.unregisterFactory(P_CID, this);
_log("[protocol] Destroyed");
},
// 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) {
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 = Services.io.newChannel(redirect, null, null);
var channelWrapper = {
__proto__: channel,
_makePrivate: this.makeChannelPrivate.bind(this, channel),
asyncOpen: function(aListener, aContext) {
_log("[protocol] nsIChannel.asyncOpen()");
this._makePrivate();
return channel.asyncOpen.apply(this, arguments);
},
open: function() {
_log("[protocol] nsIChannel.open()");
this._makePrivate();
return channel.open.apply(this, arguments);
}
};
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;
}
};