2012-05-21 04:12:37 -07:00
|
|
|
/* 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/. */
|
2010-12-22 08:39:29 -08:00
|
|
|
/* This code is loaded in every child process that is started by mochitest in
|
|
|
|
* order to be used as a replacement for UniversalXPConnect
|
|
|
|
*/
|
2011-03-17 09:19:13 -07:00
|
|
|
|
2011-03-25 11:12:23 -07:00
|
|
|
function SpecialPowers(window) {
|
2012-04-01 19:23:51 -07:00
|
|
|
this.window = Components.utils.getWeakReference(window);
|
2014-06-05 07:17:23 -07:00
|
|
|
this._windowID = window.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
|
|
|
|
.getInterface(Components.interfaces.nsIDOMWindowUtils)
|
|
|
|
.currentInnerWindowID;
|
2011-06-20 17:11:50 -07:00
|
|
|
this._encounteredCrashDumpFiles = [];
|
|
|
|
this._unexpectedCrashDumpFiles = { };
|
|
|
|
this._crashDumpDir = null;
|
2011-10-06 07:51:03 -07:00
|
|
|
this.DOMWindowUtils = bindDOMWindowUtils(window);
|
2012-11-16 18:53:32 -08:00
|
|
|
Object.defineProperty(this, 'Components', {
|
|
|
|
configurable: true, enumerable: true, get: function() {
|
|
|
|
var win = this.window.get();
|
|
|
|
if (!win)
|
|
|
|
return null;
|
|
|
|
return getRawComponents(win);
|
|
|
|
}});
|
2011-06-20 17:11:50 -07:00
|
|
|
this._pongHandlers = [];
|
|
|
|
this._messageListener = this._messageReceived.bind(this);
|
|
|
|
addMessageListener("SPPingService", this._messageListener);
|
2014-06-05 07:17:23 -07:00
|
|
|
let (self = this) {
|
|
|
|
Services.obs.addObserver(function onInnerWindowDestroyed(subject, topic, data) {
|
|
|
|
var id = subject.QueryInterface(Components.interfaces.nsISupportsPRUint64).data;
|
|
|
|
if (self._windowID === id) {
|
|
|
|
Services.obs.removeObserver(onInnerWindowDestroyed, "inner-window-destroyed");
|
|
|
|
try {
|
|
|
|
removeMessageListener("SPPingService", self._messageListener);
|
|
|
|
} catch (e if e.result == Components.results.NS_ERROR_ILLEGAL_VALUE) {
|
|
|
|
// Ignore the exception which the message manager has been destroyed.
|
|
|
|
;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, "inner-window-destroyed", false);
|
|
|
|
}
|
2011-03-25 11:12:23 -07:00
|
|
|
}
|
|
|
|
|
2011-10-06 07:51:03 -07:00
|
|
|
SpecialPowers.prototype = new SpecialPowersAPI();
|
2011-01-29 18:47:17 -08:00
|
|
|
|
2011-10-06 07:51:03 -07:00
|
|
|
SpecialPowers.prototype.toString = function() { return "[SpecialPowers]"; };
|
|
|
|
SpecialPowers.prototype.sanityCheck = function() { return "foo"; };
|
2012-11-16 18:53:32 -08:00
|
|
|
|
2011-10-06 07:51:03 -07:00
|
|
|
// This gets filled in in the constructor.
|
|
|
|
SpecialPowers.prototype.DOMWindowUtils = undefined;
|
2012-09-24 05:46:27 -07:00
|
|
|
SpecialPowers.prototype.Components = undefined;
|
2011-03-25 06:39:23 -07:00
|
|
|
|
2011-10-06 07:51:03 -07:00
|
|
|
SpecialPowers.prototype._sendSyncMessage = function(msgname, msg) {
|
|
|
|
return sendSyncMessage(msgname, msg);
|
|
|
|
};
|
2011-03-17 09:19:13 -07:00
|
|
|
|
2011-10-06 07:51:03 -07:00
|
|
|
SpecialPowers.prototype._sendAsyncMessage = function(msgname, msg) {
|
|
|
|
sendAsyncMessage(msgname, msg);
|
|
|
|
};
|
2011-07-17 12:09:13 -07:00
|
|
|
|
2013-09-14 19:09:13 -07:00
|
|
|
SpecialPowers.prototype._addMessageListener = function(msgname, listener) {
|
|
|
|
addMessageListener(msgname, listener);
|
|
|
|
};
|
|
|
|
|
|
|
|
SpecialPowers.prototype._removeMessageListener = function(msgname, listener) {
|
|
|
|
removeMessageListener(msgname, listener);
|
|
|
|
};
|
|
|
|
|
2011-10-06 07:51:03 -07:00
|
|
|
SpecialPowers.prototype.registerProcessCrashObservers = function() {
|
|
|
|
addMessageListener("SPProcessCrashService", this._messageListener);
|
|
|
|
sendSyncMessage("SPProcessCrashService", { op: "register-observer" });
|
|
|
|
};
|
2011-07-17 12:09:13 -07:00
|
|
|
|
2011-10-06 07:51:03 -07:00
|
|
|
SpecialPowers.prototype.unregisterProcessCrashObservers = function() {
|
|
|
|
addMessageListener("SPProcessCrashService", this._messageListener);
|
|
|
|
sendSyncMessage("SPProcessCrashService", { op: "unregister-observer" });
|
|
|
|
};
|
2011-07-17 12:09:13 -07:00
|
|
|
|
2011-10-06 07:51:03 -07:00
|
|
|
SpecialPowers.prototype._messageReceived = function(aMessage) {
|
|
|
|
switch (aMessage.name) {
|
|
|
|
case "SPProcessCrashService":
|
|
|
|
if (aMessage.json.type == "crash-observed") {
|
2012-09-08 10:20:59 -07:00
|
|
|
for (let e of aMessage.json.dumpIDs) {
|
|
|
|
this._encounteredCrashDumpFiles.push(e.id + "." + e.extension);
|
|
|
|
}
|
2011-07-17 12:09:13 -07:00
|
|
|
}
|
2011-10-06 07:51:03 -07:00
|
|
|
break;
|
2011-06-20 17:11:50 -07:00
|
|
|
|
2011-10-06 07:51:03 -07:00
|
|
|
case "SPPingService":
|
|
|
|
if (aMessage.json.op == "pong") {
|
|
|
|
var handler = this._pongHandlers.shift();
|
|
|
|
if (handler) {
|
|
|
|
handler();
|
2011-06-20 17:11:50 -07:00
|
|
|
}
|
|
|
|
}
|
2011-10-06 07:51:03 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
};
|
2011-09-15 12:32:42 -07:00
|
|
|
|
2011-10-14 04:52:02 -07:00
|
|
|
SpecialPowers.prototype.quit = function() {
|
|
|
|
sendAsyncMessage("SpecialPowers.Quit", {});
|
|
|
|
};
|
|
|
|
|
2011-10-06 07:51:03 -07:00
|
|
|
SpecialPowers.prototype.executeAfterFlushingMessageQueue = function(aCallback) {
|
|
|
|
this._pongHandlers.push(aCallback);
|
|
|
|
sendAsyncMessage("SPPingService", { op: "ping" });
|
2011-03-17 11:02:44 -07:00
|
|
|
};
|
2010-12-22 08:39:29 -08:00
|
|
|
|
2011-03-17 11:02:44 -07:00
|
|
|
// Expose everything but internal APIs (starting with underscores) to
|
2011-10-06 07:51:03 -07:00
|
|
|
// web content. We cannot use Object.keys to view SpecialPowers.prototype since
|
|
|
|
// we are using the functions from SpecialPowersAPI.prototype
|
2011-03-25 11:12:23 -07:00
|
|
|
SpecialPowers.prototype.__exposedProps__ = {};
|
2011-10-06 07:51:03 -07:00
|
|
|
for (var i in SpecialPowers.prototype) {
|
|
|
|
if (i.charAt(0) != "_")
|
|
|
|
SpecialPowers.prototype.__exposedProps__[i] = "r";
|
2011-01-29 18:47:17 -08:00
|
|
|
}
|
|
|
|
|
2011-03-17 11:02:44 -07:00
|
|
|
// Attach our API to the window.
|
|
|
|
function attachSpecialPowersToWindow(aWindow) {
|
2010-12-22 08:39:29 -08:00
|
|
|
try {
|
2011-03-17 11:02:44 -07:00
|
|
|
if ((aWindow !== null) &&
|
|
|
|
(aWindow !== undefined) &&
|
|
|
|
(aWindow.wrappedJSObject) &&
|
2014-03-24 13:12:41 -07:00
|
|
|
!(aWindow.wrappedJSObject.SpecialPowers)) {
|
2011-03-25 11:12:23 -07:00
|
|
|
aWindow.wrappedJSObject.SpecialPowers = new SpecialPowers(aWindow);
|
2010-12-22 08:39:29 -08:00
|
|
|
}
|
|
|
|
} catch(ex) {
|
|
|
|
dump("TEST-INFO | specialpowers.js | Failed to attach specialpowers to window exception: " + ex + "\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-17 11:02:44 -07:00
|
|
|
// This is a frame script, so it may be running in a content process.
|
|
|
|
// In any event, it is targeted at a specific "tab", so we listen for
|
|
|
|
// the DOMWindowCreated event to be notified about content windows
|
|
|
|
// being created in this context.
|
|
|
|
|
|
|
|
function SpecialPowersManager() {
|
|
|
|
addEventListener("DOMWindowCreated", this, false);
|
2010-12-22 08:39:29 -08:00
|
|
|
}
|
|
|
|
|
2011-03-17 11:02:44 -07:00
|
|
|
SpecialPowersManager.prototype = {
|
|
|
|
handleEvent: function handleEvent(aEvent) {
|
|
|
|
var window = aEvent.target.defaultView;
|
|
|
|
attachSpecialPowersToWindow(window);
|
2010-12-22 08:39:29 -08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-03-17 11:02:44 -07:00
|
|
|
var specialpowersmanager = new SpecialPowersManager();
|
2013-11-23 21:32:27 -08:00
|
|
|
|
|
|
|
this.SpecialPowers = SpecialPowers;
|
|
|
|
this.attachSpecialPowersToWindow = attachSpecialPowersToWindow;
|