-
Notifications
You must be signed in to change notification settings - Fork 2
/
substitution-protocol.jsm
156 lines (138 loc) · 4.98 KB
/
substitution-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
"use strict";
const EXPORTED_SYMBOLS = ["SubstitutionProtocol"];
const { utils: Cu, classes: Cc, interfaces: Ci, manager: Cm,
results: Cr } = Components;
Cm.QueryInterface(Ci.nsIComponentRegistrar);
const io = Cc["@mozilla.org/network/io-service;1"]
.getService(Ci.nsIIOService);
const branch = Cc["@mozilla.org/preferences-service;1"].
getService(Ci.nsIPrefService).
QueryInterface(Ci.nsIPrefBranch2).
getBranch("network.protocol.substitutions.");
const childBus = Cc["@mozilla.org/childprocessmessagemanager;1"].
getService(Ci.nsISyncMessageSender);
const parentBus = Cc["@mozilla.org/parentprocessmessagemanager;1"].
getService(Ci.nsIMessageBroadcaster);
const runtime = Cc["@mozilla.org/xre/app-info;1"].
getService(Ci.nsIXULRuntime);
const uuid = Cc["@mozilla.org/uuid-generator;1"].
getService(Ci.nsIUUIDGenerator);
const securityManager = Cc["@mozilla.org/scriptsecuritymanager;1"].
getService(Ci.nsIScriptSecurityManager);
const isParentProcess = runtime.processType == runtime.PROCESS_TYPE_DEFAULT;
const SubstitutionProtocol = function(scheme, classID=uuid.generateUUID()) {
this.scheme = scheme;
this.contract = `@mozilla.org/network/protocol;1?name=${scheme}`;
this.classID = classID;
}
SubstitutionProtocol.prototype = {
constructor: SubstitutionProtocol,
description: "Custom substitution protocol implemantion",
QueryInterface(iid) {
if (iid.equals(Ci.nsISupports) ||
iid.equals(Ci.nsIProtocolHandler) ||
iid.equals(Ci.nsIResProtocolHandler) ||
iid.equals(Ci.nsIFactory))
{
return this
}
throw Cr.NS_ERROR_NO_INTERFACE
},
// nsIResProtocolHandler
getSubstitution(host) {
if (!this.hasSubstitution(host)) {
throw Cr.NS_ERROR_NOT_AVAILABLE;
}
return io.newURI(branch.getCharPref(`${this.scheme}.${host}`),
null, null);
},
hasSubstitution(host) {
return branch.prefHasUserValue(`${this.scheme}.${host}`);
},
setSubstitution(host, uri) {
if (uri) {
branch.setCharPref(`${this.scheme}.${host}`, uri.spec);
} else {
branch.clearUserPref(`${this.scheme}.${host}`);
}
},
resolveURI({host, path}) {
let uri = this.getSubstitution(host);
if (!uri) {
throw Cr.NS_ERROR_NOT_AVAILABLE;
}
return uri.resolve(path.substr(1));
},
// nsIProtocolHandler
defaultPort: -1,
protocolFlags: Ci.nsIProtocolHandler.URI_NOAUTH |
Ci.nsIProtocolHandler.URI_DANGEROUS_TO_LOAD |
Ci.nsIProtocolHandler.URI_IS_UI_RESOURCE |
Ci.nsIProtocolHandler.URI_CROSS_ORIGIN_NEEDS_WEBAPPS_PERM,
scheme: null,
newURI(spec, originCharset, baseURI) {
let uri = Cc["@mozilla.org/network/standard-url;1"]
.createInstance(Ci.nsIStandardURL)
uri.init(Ci.nsIStandardURL.URLTYPE_STANDARD,
-1, spec, originCharset, baseURI)
return uri.QueryInterface(Ci.nsIURI)
},
newChannel(uri) {
let channel = io.newChannel(this.resolveURI(uri), null, null);
channel.QueryInterface(Ci.nsIChannel).originalURI = uri;
// channel.owner = securityManager.getSimpleCodebasePrincipal(this.newURI(`${uri.scheme}://${uri.host}`, null, null));
return channel;
},
// nsIFactory
createInstance(outer, iid) {
if (outer) {
throw Cr.NS_ERROR_NO_AGGREGATION;
}
return this
},
lockFactory: function(aLock) {
throw Cr.NS_ERROR_NOT_IMPLEMENTED;
},
// component
register() {
Cm.registerFactory(this.classID,
this.description,
this.contract,
this);
if (isParentProcess) {
parentBus.broadcastAsyncMessage("network/protocol/substitution/register",
this.toJSON());
}
},
unregister() {
branch.deleteBranch(this.scheme);
Cm.unregisterFactory(this.classID, this);
if (isParentProcess) {
parentBus.broadcastAsyncMessage("network/protocol/substitution/unregister",
this.toJSON());
}
},
toJSON() {
return {
scheme: this.scheme,
contract: this.contract,
classID: this.classID.toString()
}
}
}
// E10S Propagation.
if (runtime.processType == runtime.PROCESS_TYPE_DEFAULT) {
Cc["@mozilla.org/globalmessagemanager;1"].
getService(Ci.nsIMessageListenerManager).
loadFrameScript(`data:,Components.utils.import("${__URI__}", {})`, true);
} else {
childBus.addMessageListener("network/protocol/substitution/register", ({data}) => {
new SubstitutionProtocol(data.scheme, Components.ID(data.classID)).register();
});
childBus.addMessageListener("network/protocol/substitution/unregister", ({data}) => {
let factory = Cm.getClassObjectByContractID(data.contract,
Ci.nsIFactory,
{});
Cm.unregisterFactory(Components.ID(data.classID), factory);
});
}