1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/testing/specialpowers/content/specialpowers.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,134 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 +/* This code is loaded in every child process that is started by mochitest in 1.8 + * order to be used as a replacement for UniversalXPConnect 1.9 + */ 1.10 + 1.11 +function SpecialPowers(window) { 1.12 + this.window = Components.utils.getWeakReference(window); 1.13 + this._encounteredCrashDumpFiles = []; 1.14 + this._unexpectedCrashDumpFiles = { }; 1.15 + this._crashDumpDir = null; 1.16 + this.DOMWindowUtils = bindDOMWindowUtils(window); 1.17 + Object.defineProperty(this, 'Components', { 1.18 + configurable: true, enumerable: true, get: function() { 1.19 + var win = this.window.get(); 1.20 + if (!win) 1.21 + return null; 1.22 + return getRawComponents(win); 1.23 + }}); 1.24 + this._pongHandlers = []; 1.25 + this._messageListener = this._messageReceived.bind(this); 1.26 + addMessageListener("SPPingService", this._messageListener); 1.27 +} 1.28 + 1.29 +SpecialPowers.prototype = new SpecialPowersAPI(); 1.30 + 1.31 +SpecialPowers.prototype.toString = function() { return "[SpecialPowers]"; }; 1.32 +SpecialPowers.prototype.sanityCheck = function() { return "foo"; }; 1.33 + 1.34 +// This gets filled in in the constructor. 1.35 +SpecialPowers.prototype.DOMWindowUtils = undefined; 1.36 +SpecialPowers.prototype.Components = undefined; 1.37 + 1.38 +SpecialPowers.prototype._sendSyncMessage = function(msgname, msg) { 1.39 + return sendSyncMessage(msgname, msg); 1.40 +}; 1.41 + 1.42 +SpecialPowers.prototype._sendAsyncMessage = function(msgname, msg) { 1.43 + sendAsyncMessage(msgname, msg); 1.44 +}; 1.45 + 1.46 +SpecialPowers.prototype._addMessageListener = function(msgname, listener) { 1.47 + addMessageListener(msgname, listener); 1.48 +}; 1.49 + 1.50 +SpecialPowers.prototype._removeMessageListener = function(msgname, listener) { 1.51 + removeMessageListener(msgname, listener); 1.52 +}; 1.53 + 1.54 +SpecialPowers.prototype.registerProcessCrashObservers = function() { 1.55 + addMessageListener("SPProcessCrashService", this._messageListener); 1.56 + sendSyncMessage("SPProcessCrashService", { op: "register-observer" }); 1.57 +}; 1.58 + 1.59 +SpecialPowers.prototype.unregisterProcessCrashObservers = function() { 1.60 + addMessageListener("SPProcessCrashService", this._messageListener); 1.61 + sendSyncMessage("SPProcessCrashService", { op: "unregister-observer" }); 1.62 +}; 1.63 + 1.64 +SpecialPowers.prototype._messageReceived = function(aMessage) { 1.65 + switch (aMessage.name) { 1.66 + case "SPProcessCrashService": 1.67 + if (aMessage.json.type == "crash-observed") { 1.68 + for (let e of aMessage.json.dumpIDs) { 1.69 + this._encounteredCrashDumpFiles.push(e.id + "." + e.extension); 1.70 + } 1.71 + } 1.72 + break; 1.73 + 1.74 + case "SPPingService": 1.75 + if (aMessage.json.op == "pong") { 1.76 + var handler = this._pongHandlers.shift(); 1.77 + if (handler) { 1.78 + handler(); 1.79 + } 1.80 + } 1.81 + break; 1.82 + } 1.83 + return true; 1.84 +}; 1.85 + 1.86 +SpecialPowers.prototype.quit = function() { 1.87 + sendAsyncMessage("SpecialPowers.Quit", {}); 1.88 +}; 1.89 + 1.90 +SpecialPowers.prototype.executeAfterFlushingMessageQueue = function(aCallback) { 1.91 + this._pongHandlers.push(aCallback); 1.92 + sendAsyncMessage("SPPingService", { op: "ping" }); 1.93 +}; 1.94 + 1.95 +// Expose everything but internal APIs (starting with underscores) to 1.96 +// web content. We cannot use Object.keys to view SpecialPowers.prototype since 1.97 +// we are using the functions from SpecialPowersAPI.prototype 1.98 +SpecialPowers.prototype.__exposedProps__ = {}; 1.99 +for (var i in SpecialPowers.prototype) { 1.100 + if (i.charAt(0) != "_") 1.101 + SpecialPowers.prototype.__exposedProps__[i] = "r"; 1.102 +} 1.103 + 1.104 +// Attach our API to the window. 1.105 +function attachSpecialPowersToWindow(aWindow) { 1.106 + try { 1.107 + if ((aWindow !== null) && 1.108 + (aWindow !== undefined) && 1.109 + (aWindow.wrappedJSObject) && 1.110 + !(aWindow.wrappedJSObject.SpecialPowers)) { 1.111 + aWindow.wrappedJSObject.SpecialPowers = new SpecialPowers(aWindow); 1.112 + } 1.113 + } catch(ex) { 1.114 + dump("TEST-INFO | specialpowers.js | Failed to attach specialpowers to window exception: " + ex + "\n"); 1.115 + } 1.116 +} 1.117 + 1.118 +// This is a frame script, so it may be running in a content process. 1.119 +// In any event, it is targeted at a specific "tab", so we listen for 1.120 +// the DOMWindowCreated event to be notified about content windows 1.121 +// being created in this context. 1.122 + 1.123 +function SpecialPowersManager() { 1.124 + addEventListener("DOMWindowCreated", this, false); 1.125 +} 1.126 + 1.127 +SpecialPowersManager.prototype = { 1.128 + handleEvent: function handleEvent(aEvent) { 1.129 + var window = aEvent.target.defaultView; 1.130 + attachSpecialPowersToWindow(window); 1.131 + } 1.132 +}; 1.133 + 1.134 +var specialpowersmanager = new SpecialPowersManager(); 1.135 + 1.136 +this.SpecialPowers = SpecialPowers; 1.137 +this.attachSpecialPowersToWindow = attachSpecialPowersToWindow;