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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
// Based on:
// https://bugzilla.mozilla.org/show_bug.cgi?id=549539
// https://bug549539.bugzilla.mozilla.org/attachment.cgi?id=429661
// https://developer.mozilla.org/en/XPCOM/XPCOM_changes_in_Gecko_1.9.3
// https://developer.mozilla.org/en/how_to_build_an_xpcom_component_in_javascript
var EXPORTED_SYMBOLS = ["SpecialPowersObserver", "SpecialPowersObserverFactory"];
Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
Components.utils.import("resource://gre/modules/Services.jsm");
Components.utils.importGlobalProperties(['File']);
if (typeof(Cc) == "undefined") {
const Cc = Components.classes;
const Ci = Components.interfaces;
}
const CHILD_SCRIPT = "chrome://specialpowers/content/specialpowers.js"
const CHILD_SCRIPT_API = "chrome://specialpowers/content/specialpowersAPI.js"
const CHILD_LOGGER_SCRIPT = "chrome://specialpowers/content/MozillaLogger.js"
// Glue to add in the observer API to this object. This allows us to share code with chrome tests
var loader = Components.classes["@mozilla.org/moz/jssubscript-loader;1"]
.getService(Components.interfaces.mozIJSSubScriptLoader);
loader.loadSubScript("chrome://specialpowers/content/SpecialPowersObserverAPI.js");
/* XPCOM gunk */
this.SpecialPowersObserver = function SpecialPowersObserver() {
this._isFrameScriptLoaded = false;
this._messageManager = Cc["@mozilla.org/globalmessagemanager;1"].
getService(Ci.nsIMessageBroadcaster);
}
SpecialPowersObserver.prototype = new SpecialPowersObserverAPI();
SpecialPowersObserver.prototype.classDescription = "Special powers Observer for use in testing.";
SpecialPowersObserver.prototype.classID = Components.ID("{59a52458-13e0-4d93-9d85-a637344f29a1}");
SpecialPowersObserver.prototype.contractID = "@mozilla.org/special-powers-observer;1";
SpecialPowersObserver.prototype.QueryInterface = XPCOMUtils.generateQI([Components.interfaces.nsIObserver]);
SpecialPowersObserver.prototype.observe = function(aSubject, aTopic, aData)
{
switch (aTopic) {
case "chrome-document-global-created":
this._loadFrameScript();
break;
case "http-on-modify-request":
if (aSubject instanceof Ci.nsIChannel) {
let uri = aSubject.URI.spec;
this._sendAsyncMessage("specialpowers-http-notify-request", { uri: uri });
}
break;
default:
this._observe(aSubject, aTopic, aData);
break;
}
};
SpecialPowersObserver.prototype._loadFrameScript = function()
{
if (!this._isFrameScriptLoaded) {
// Register for any messages our API needs us to handle
this._messageManager.addMessageListener("SPPrefService", this);
this._messageManager.addMessageListener("SPProcessCrashService", this);
this._messageManager.addMessageListener("SPPingService", this);
this._messageManager.addMessageListener("SpecialPowers.Quit", this);
this._messageManager.addMessageListener("SpecialPowers.Focus", this);
this._messageManager.addMessageListener("SpecialPowers.CreateFiles", this);
this._messageManager.addMessageListener("SpecialPowers.RemoveFiles", this);
this._messageManager.addMessageListener("SPPermissionManager", this);
this._messageManager.addMessageListener("SPObserverService", this);
this._messageManager.addMessageListener("SPLoadChromeScript", this);
this._messageManager.addMessageListener("SPImportInMainProcess", this);
this._messageManager.addMessageListener("SPChromeScriptMessage", this);
this._messageManager.addMessageListener("SPQuotaManager", this);
this._messageManager.addMessageListener("SPSetTestPluginEnabledState", this);
this._messageManager.addMessageListener("SPLoadExtension", this);
this._messageManager.addMessageListener("SPStartupExtension", this);
this._messageManager.addMessageListener("SPUnloadExtension", this);
this._messageManager.addMessageListener("SPExtensionMessage", this);
this._messageManager.addMessageListener("SPCleanUpSTSData", this);
this._messageManager.addMessageListener("SPClearAppPrivateData", this);
this._messageManager.loadFrameScript(CHILD_LOGGER_SCRIPT, true);
this._messageManager.loadFrameScript(CHILD_SCRIPT_API, true);
this._messageManager.loadFrameScript(CHILD_SCRIPT, true);
this._isFrameScriptLoaded = true;
this._createdFiles = null;
}
};
SpecialPowersObserver.prototype._sendAsyncMessage = function(msgname, msg)
{
this._messageManager.broadcastAsyncMessage(msgname, msg);
};
SpecialPowersObserver.prototype._receiveMessage = function(aMessage) {
return this._receiveMessageAPI(aMessage);
};
SpecialPowersObserver.prototype.init = function()
{
var obs = Services.obs;
obs.addObserver(this, "chrome-document-global-created", false);
// Register special testing modules.
var testsURI = Cc["@mozilla.org/file/directory_service;1"].
getService(Ci.nsIProperties).
get("ProfD", Ci.nsILocalFile);
testsURI.append("tests.manifest");
var ioSvc = Cc["@mozilla.org/network/io-service;1"].
getService(Ci.nsIIOService);
var manifestFile = ioSvc.newFileURI(testsURI).
QueryInterface(Ci.nsIFileURL).file;
Components.manager.QueryInterface(Ci.nsIComponentRegistrar).
autoRegister(manifestFile);
obs.addObserver(this, "http-on-modify-request", false);
this._loadFrameScript();
};
SpecialPowersObserver.prototype.uninit = function()
{
var obs = Services.obs;
obs.removeObserver(this, "chrome-document-global-created");
obs.removeObserver(this, "http-on-modify-request");
this._registerObservers._topics.forEach(function(element) {
obs.removeObserver(this._registerObservers, element);
});
this._removeProcessCrashObservers();
if (this._isFrameScriptLoaded) {
this._messageManager.removeMessageListener("SPPrefService", this);
this._messageManager.removeMessageListener("SPProcessCrashService", this);
this._messageManager.removeMessageListener("SPPingService", this);
this._messageManager.removeMessageListener("SpecialPowers.Quit", this);
this._messageManager.removeMessageListener("SpecialPowers.Focus", this);
this._messageManager.removeMessageListener("SpecialPowers.CreateFiles", this);
this._messageManager.removeMessageListener("SpecialPowers.RemoveFiles", this);
this._messageManager.removeMessageListener("SPPermissionManager", this);
this._messageManager.removeMessageListener("SPObserverService", this);
this._messageManager.removeMessageListener("SPLoadChromeScript", this);
this._messageManager.removeMessageListener("SPImportInMainProcess", this);
this._messageManager.removeMessageListener("SPChromeScriptMessage", this);
this._messageManager.removeMessageListener("SPQuotaManager", this);
this._messageManager.removeMessageListener("SPSetTestPluginEnabledState", this);
this._messageManager.removeMessageListener("SPLoadExtension", this);
this._messageManager.removeMessageListener("SPStartupExtension", this);
this._messageManager.removeMessageListener("SPUnloadExtension", this);
this._messageManager.removeMessageListener("SPExtensionMessage", this);
this._messageManager.removeMessageListener("SPCleanUpSTSData", this);
this._messageManager.removeMessageListener("SPClearAppPrivateData", this);
this._messageManager.removeDelayedFrameScript(CHILD_LOGGER_SCRIPT);
this._messageManager.removeDelayedFrameScript(CHILD_SCRIPT_API);
this._messageManager.removeDelayedFrameScript(CHILD_SCRIPT);
this._isFrameScriptLoaded = false;
}
};
SpecialPowersObserver.prototype._addProcessCrashObservers = function() {
if (this._processCrashObserversRegistered) {
return;
}
var obs = Components.classes["@mozilla.org/observer-service;1"]
.getService(Components.interfaces.nsIObserverService);
obs.addObserver(this, "plugin-crashed", false);
obs.addObserver(this, "ipc:content-shutdown", false);
this._processCrashObserversRegistered = true;
};
SpecialPowersObserver.prototype._removeProcessCrashObservers = function() {
if (!this._processCrashObserversRegistered) {
return;
}
var obs = Components.classes["@mozilla.org/observer-service;1"]
.getService(Components.interfaces.nsIObserverService);
obs.removeObserver(this, "plugin-crashed");
obs.removeObserver(this, "ipc:content-shutdown");
this._processCrashObserversRegistered = false;
};
SpecialPowersObserver.prototype._registerObservers = {
_self: null,
_topics: [],
_add: function(topic) {
if (this._topics.indexOf(topic) < 0) {
this._topics.push(topic);
Services.obs.addObserver(this, topic, false);
}
},
observe: function (aSubject, aTopic, aData) {
var msg = { aData: aData };
switch (aTopic) {
case "perm-changed":
var permission = aSubject.QueryInterface(Ci.nsIPermission);
// specialPowersAPI will consume this value, and it is used as a
// fake permission, but only type and principal.appId will be used.
//
// We need to ensure that it looks the same as a real permission,
// so we fake these properties.
msg.permission = {
principal: {
originAttributes: {appId: permission.principal.appId}
},
type: permission.type
};
default:
this._self._sendAsyncMessage("specialpowers-" + aTopic, msg);
}
}
};
/**
* messageManager callback function
* This will get requests from our API in the window and process them in chrome for it
**/
SpecialPowersObserver.prototype.receiveMessage = function(aMessage) {
switch(aMessage.name) {
case "SPPingService":
if (aMessage.json.op == "ping") {
aMessage.target
.QueryInterface(Ci.nsIFrameLoaderOwner)
.frameLoader
.messageManager
.sendAsyncMessage("SPPingService", { op: "pong" });
}
break;
case "SpecialPowers.Quit":
let appStartup = Cc["@mozilla.org/toolkit/app-startup;1"].getService(Ci.nsIAppStartup);
appStartup.quit(Ci.nsIAppStartup.eForceQuit);
break;
case "SpecialPowers.Focus":
aMessage.target.focus();
break;
case "SpecialPowers.CreateFiles":
let filePaths = new Array;
if (!this._createdFiles) {
this._createdFiles = new Array;
}
let createdFiles = this._createdFiles;
try {
aMessage.data.forEach(function(request) {
const filePerms = 0666;
let testFile = Services.dirsvc.get("ProfD", Ci.nsIFile);
if (request.name) {
testFile.appendRelativePath(request.name);
} else {
testFile.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, filePerms);
}
let outStream = Cc["@mozilla.org/network/file-output-stream;1"].createInstance(Ci.nsIFileOutputStream);
outStream.init(testFile, 0x02 | 0x08 | 0x20, // PR_WRONLY | PR_CREATE_FILE | PR_TRUNCATE
filePerms, 0);
if (request.data) {
outStream.write(request.data, request.data.length);
}
outStream.close();
filePaths.push(File.createFromFileName(testFile.path, request.options));
createdFiles.push(testFile);
});
aMessage.target
.QueryInterface(Ci.nsIFrameLoaderOwner)
.frameLoader
.messageManager
.sendAsyncMessage("SpecialPowers.FilesCreated", filePaths);
} catch (e) {
aMessage.target
.QueryInterface(Ci.nsIFrameLoaderOwner)
.frameLoader
.messageManager
.sendAsyncMessage("SpecialPowers.FilesError", e.toString());
}
break;
case "SpecialPowers.RemoveFiles":
if (this._createdFiles) {
this._createdFiles.forEach(function (testFile) {
try {
testFile.remove(false);
} catch (e) {}
});
this._createdFiles = null;
}
break;
default:
return this._receiveMessage(aMessage);
}
};
this.NSGetFactory = XPCOMUtils.generateNSGetFactory([SpecialPowersObserver]);
this.SpecialPowersObserverFactory = Object.freeze({
createInstance: function(outer, id) {
if (outer) { throw Components.results.NS_ERROR_NO_AGGREGATION };
return new SpecialPowersObserver();
},
loadFactory: function(lock){},
QueryInterface: XPCOMUtils.generateQI([Ci.nsIFactory])
});
|