michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: // Based on: michael@0: // https://bugzilla.mozilla.org/show_bug.cgi?id=549539 michael@0: // https://bug549539.bugzilla.mozilla.org/attachment.cgi?id=429661 michael@0: // https://developer.mozilla.org/en/XPCOM/XPCOM_changes_in_Gecko_1.9.3 michael@0: // http://mxr.mozilla.org/mozilla-central/source/toolkit/components/console/hudservice/HUDService.jsm#3240 michael@0: // https://developer.mozilla.org/en/how_to_build_an_xpcom_component_in_javascript michael@0: michael@0: Components.utils.import("resource://gre/modules/XPCOMUtils.jsm"); michael@0: Components.utils.import("resource://gre/modules/Services.jsm"); michael@0: michael@0: const Cc = Components.classes; michael@0: const Ci = Components.interfaces; michael@0: michael@0: const CHILD_SCRIPT = "chrome://specialpowers/content/specialpowers.js" michael@0: const CHILD_SCRIPT_API = "chrome://specialpowers/content/specialpowersAPI.js" michael@0: const CHILD_LOGGER_SCRIPT = "chrome://specialpowers/content/MozillaLogger.js" michael@0: michael@0: michael@0: // Glue to add in the observer API to this object. This allows us to share code with chrome tests michael@0: var loader = Components.classes["@mozilla.org/moz/jssubscript-loader;1"] michael@0: .getService(Components.interfaces.mozIJSSubScriptLoader); michael@0: loader.loadSubScript("chrome://specialpowers/content/SpecialPowersObserverAPI.js"); michael@0: michael@0: /* XPCOM gunk */ michael@0: this.SpecialPowersObserver = function SpecialPowersObserver() { michael@0: this._isFrameScriptLoaded = false; michael@0: this._mmIsGlobal = true; michael@0: this._messageManager = Cc["@mozilla.org/globalmessagemanager;1"]. michael@0: getService(Ci.nsIMessageBroadcaster); michael@0: } michael@0: michael@0: michael@0: SpecialPowersObserver.prototype = new SpecialPowersObserverAPI(); michael@0: michael@0: SpecialPowersObserver.prototype.classDescription = "Special powers Observer for use in testing."; michael@0: SpecialPowersObserver.prototype.classID = Components.ID("{59a52458-13e0-4d93-9d85-a637344f29a1}"); michael@0: SpecialPowersObserver.prototype.contractID = "@mozilla.org/special-powers-observer;1"; michael@0: SpecialPowersObserver.prototype.QueryInterface = XPCOMUtils.generateQI([Components.interfaces.nsIObserver]); michael@0: SpecialPowersObserver.prototype._xpcom_categories = [{category: "profile-after-change", service: true }]; michael@0: michael@0: SpecialPowersObserver.prototype.observe = function(aSubject, aTopic, aData) michael@0: { michael@0: switch (aTopic) { michael@0: case "profile-after-change": michael@0: this.init(); michael@0: break; michael@0: michael@0: case "chrome-document-global-created": michael@0: if (!this._isFrameScriptLoaded) { michael@0: // Register for any messages our API needs us to handle michael@0: this._messageManager.addMessageListener("SPPrefService", this); michael@0: this._messageManager.addMessageListener("SPProcessCrashService", this); michael@0: this._messageManager.addMessageListener("SPPingService", this); michael@0: this._messageManager.addMessageListener("SpecialPowers.Quit", this); michael@0: this._messageManager.addMessageListener("SpecialPowers.Focus", this); michael@0: this._messageManager.addMessageListener("SPPermissionManager", this); michael@0: this._messageManager.addMessageListener("SPWebAppService", this); michael@0: this._messageManager.addMessageListener("SPObserverService", this); michael@0: this._messageManager.addMessageListener("SPLoadChromeScript", this); michael@0: this._messageManager.addMessageListener("SPChromeScriptMessage", this); michael@0: michael@0: this._messageManager.loadFrameScript(CHILD_LOGGER_SCRIPT, true); michael@0: this._messageManager.loadFrameScript(CHILD_SCRIPT_API, true); michael@0: this._messageManager.loadFrameScript(CHILD_SCRIPT, true); michael@0: this._isFrameScriptLoaded = true; michael@0: } michael@0: break; michael@0: michael@0: case "http-on-modify-request": michael@0: if (aSubject instanceof Ci.nsIChannel) { michael@0: let uri = aSubject.URI.spec; michael@0: this._sendAsyncMessage("specialpowers-http-notify-request", { uri: uri }); michael@0: } michael@0: break; michael@0: michael@0: case "xpcom-shutdown": michael@0: this.uninit(); michael@0: break; michael@0: michael@0: default: michael@0: this._observe(aSubject, aTopic, aData); michael@0: break; michael@0: } michael@0: }; michael@0: michael@0: SpecialPowersObserver.prototype._sendAsyncMessage = function(msgname, msg) michael@0: { michael@0: if (this._mmIsGlobal) { michael@0: this._messageManager.broadcastAsyncMessage(msgname, msg); michael@0: } michael@0: else { michael@0: this._messageManager.sendAsyncMessage(msgname, msg); michael@0: } michael@0: }; michael@0: michael@0: SpecialPowersObserver.prototype._receiveMessage = function(aMessage) { michael@0: return this._receiveMessageAPI(aMessage); michael@0: }; michael@0: michael@0: SpecialPowersObserver.prototype.init = function(messageManager) michael@0: { michael@0: var obs = Services.obs; michael@0: obs.addObserver(this, "xpcom-shutdown", false); michael@0: obs.addObserver(this, "chrome-document-global-created", false); michael@0: obs.addObserver(this, "http-on-modify-request", false); michael@0: michael@0: if (messageManager) { michael@0: this._messageManager = messageManager; michael@0: this._mmIsGlobal = false; michael@0: } michael@0: }; michael@0: michael@0: SpecialPowersObserver.prototype.uninit = function() michael@0: { michael@0: var obs = Services.obs; michael@0: obs.removeObserver(this, "chrome-document-global-created"); michael@0: obs.removeObserver(this, "http-on-modify-request"); michael@0: this._removeProcessCrashObservers(); michael@0: }; michael@0: michael@0: SpecialPowersObserver.prototype._addProcessCrashObservers = function() { michael@0: if (this._processCrashObserversRegistered) { michael@0: return; michael@0: } michael@0: michael@0: var obs = Components.classes["@mozilla.org/observer-service;1"] michael@0: .getService(Components.interfaces.nsIObserverService); michael@0: michael@0: obs.addObserver(this, "plugin-crashed", false); michael@0: obs.addObserver(this, "ipc:content-shutdown", false); michael@0: this._processCrashObserversRegistered = true; michael@0: }; michael@0: michael@0: SpecialPowersObserver.prototype._removeProcessCrashObservers = function() { michael@0: if (!this._processCrashObserversRegistered) { michael@0: return; michael@0: } michael@0: michael@0: var obs = Components.classes["@mozilla.org/observer-service;1"] michael@0: .getService(Components.interfaces.nsIObserverService); michael@0: michael@0: obs.removeObserver(this, "plugin-crashed"); michael@0: obs.removeObserver(this, "ipc:content-shutdown"); michael@0: this._processCrashObserversRegistered = false; michael@0: }; michael@0: michael@0: /** michael@0: * messageManager callback function michael@0: * This will get requests from our API in the window and process them in chrome for it michael@0: **/ michael@0: SpecialPowersObserver.prototype.receiveMessage = function(aMessage) { michael@0: switch(aMessage.name) { michael@0: case "SPPingService": michael@0: if (aMessage.json.op == "ping") { michael@0: aMessage.target michael@0: .QueryInterface(Ci.nsIFrameLoaderOwner) michael@0: .frameLoader michael@0: .messageManager michael@0: .sendAsyncMessage("SPPingService", { op: "pong" }); michael@0: } michael@0: break; michael@0: case "SpecialPowers.Quit": michael@0: let appStartup = Cc["@mozilla.org/toolkit/app-startup;1"].getService(Ci.nsIAppStartup); michael@0: appStartup.quit(Ci.nsIAppStartup.eForceQuit); michael@0: break; michael@0: case "SpecialPowers.Focus": michael@0: aMessage.target.focus(); michael@0: break; michael@0: default: michael@0: return this._receiveMessage(aMessage); michael@0: } michael@0: }; michael@0: michael@0: this.NSGetFactory = XPCOMUtils.generateNSGetFactory([SpecialPowersObserver]);