testing/specialpowers/components/SpecialPowersObserver.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 // Based on:
michael@0 6 // https://bugzilla.mozilla.org/show_bug.cgi?id=549539
michael@0 7 // https://bug549539.bugzilla.mozilla.org/attachment.cgi?id=429661
michael@0 8 // https://developer.mozilla.org/en/XPCOM/XPCOM_changes_in_Gecko_1.9.3
michael@0 9 // http://mxr.mozilla.org/mozilla-central/source/toolkit/components/console/hudservice/HUDService.jsm#3240
michael@0 10 // https://developer.mozilla.org/en/how_to_build_an_xpcom_component_in_javascript
michael@0 11
michael@0 12 Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
michael@0 13 Components.utils.import("resource://gre/modules/Services.jsm");
michael@0 14
michael@0 15 const Cc = Components.classes;
michael@0 16 const Ci = Components.interfaces;
michael@0 17
michael@0 18 const CHILD_SCRIPT = "chrome://specialpowers/content/specialpowers.js"
michael@0 19 const CHILD_SCRIPT_API = "chrome://specialpowers/content/specialpowersAPI.js"
michael@0 20 const CHILD_LOGGER_SCRIPT = "chrome://specialpowers/content/MozillaLogger.js"
michael@0 21
michael@0 22
michael@0 23 // Glue to add in the observer API to this object. This allows us to share code with chrome tests
michael@0 24 var loader = Components.classes["@mozilla.org/moz/jssubscript-loader;1"]
michael@0 25 .getService(Components.interfaces.mozIJSSubScriptLoader);
michael@0 26 loader.loadSubScript("chrome://specialpowers/content/SpecialPowersObserverAPI.js");
michael@0 27
michael@0 28 /* XPCOM gunk */
michael@0 29 this.SpecialPowersObserver = function SpecialPowersObserver() {
michael@0 30 this._isFrameScriptLoaded = false;
michael@0 31 this._mmIsGlobal = true;
michael@0 32 this._messageManager = Cc["@mozilla.org/globalmessagemanager;1"].
michael@0 33 getService(Ci.nsIMessageBroadcaster);
michael@0 34 }
michael@0 35
michael@0 36
michael@0 37 SpecialPowersObserver.prototype = new SpecialPowersObserverAPI();
michael@0 38
michael@0 39 SpecialPowersObserver.prototype.classDescription = "Special powers Observer for use in testing.";
michael@0 40 SpecialPowersObserver.prototype.classID = Components.ID("{59a52458-13e0-4d93-9d85-a637344f29a1}");
michael@0 41 SpecialPowersObserver.prototype.contractID = "@mozilla.org/special-powers-observer;1";
michael@0 42 SpecialPowersObserver.prototype.QueryInterface = XPCOMUtils.generateQI([Components.interfaces.nsIObserver]);
michael@0 43 SpecialPowersObserver.prototype._xpcom_categories = [{category: "profile-after-change", service: true }];
michael@0 44
michael@0 45 SpecialPowersObserver.prototype.observe = function(aSubject, aTopic, aData)
michael@0 46 {
michael@0 47 switch (aTopic) {
michael@0 48 case "profile-after-change":
michael@0 49 this.init();
michael@0 50 break;
michael@0 51
michael@0 52 case "chrome-document-global-created":
michael@0 53 if (!this._isFrameScriptLoaded) {
michael@0 54 // Register for any messages our API needs us to handle
michael@0 55 this._messageManager.addMessageListener("SPPrefService", this);
michael@0 56 this._messageManager.addMessageListener("SPProcessCrashService", this);
michael@0 57 this._messageManager.addMessageListener("SPPingService", this);
michael@0 58 this._messageManager.addMessageListener("SpecialPowers.Quit", this);
michael@0 59 this._messageManager.addMessageListener("SpecialPowers.Focus", this);
michael@0 60 this._messageManager.addMessageListener("SPPermissionManager", this);
michael@0 61 this._messageManager.addMessageListener("SPWebAppService", this);
michael@0 62 this._messageManager.addMessageListener("SPObserverService", this);
michael@0 63 this._messageManager.addMessageListener("SPLoadChromeScript", this);
michael@0 64 this._messageManager.addMessageListener("SPChromeScriptMessage", this);
michael@0 65
michael@0 66 this._messageManager.loadFrameScript(CHILD_LOGGER_SCRIPT, true);
michael@0 67 this._messageManager.loadFrameScript(CHILD_SCRIPT_API, true);
michael@0 68 this._messageManager.loadFrameScript(CHILD_SCRIPT, true);
michael@0 69 this._isFrameScriptLoaded = true;
michael@0 70 }
michael@0 71 break;
michael@0 72
michael@0 73 case "http-on-modify-request":
michael@0 74 if (aSubject instanceof Ci.nsIChannel) {
michael@0 75 let uri = aSubject.URI.spec;
michael@0 76 this._sendAsyncMessage("specialpowers-http-notify-request", { uri: uri });
michael@0 77 }
michael@0 78 break;
michael@0 79
michael@0 80 case "xpcom-shutdown":
michael@0 81 this.uninit();
michael@0 82 break;
michael@0 83
michael@0 84 default:
michael@0 85 this._observe(aSubject, aTopic, aData);
michael@0 86 break;
michael@0 87 }
michael@0 88 };
michael@0 89
michael@0 90 SpecialPowersObserver.prototype._sendAsyncMessage = function(msgname, msg)
michael@0 91 {
michael@0 92 if (this._mmIsGlobal) {
michael@0 93 this._messageManager.broadcastAsyncMessage(msgname, msg);
michael@0 94 }
michael@0 95 else {
michael@0 96 this._messageManager.sendAsyncMessage(msgname, msg);
michael@0 97 }
michael@0 98 };
michael@0 99
michael@0 100 SpecialPowersObserver.prototype._receiveMessage = function(aMessage) {
michael@0 101 return this._receiveMessageAPI(aMessage);
michael@0 102 };
michael@0 103
michael@0 104 SpecialPowersObserver.prototype.init = function(messageManager)
michael@0 105 {
michael@0 106 var obs = Services.obs;
michael@0 107 obs.addObserver(this, "xpcom-shutdown", false);
michael@0 108 obs.addObserver(this, "chrome-document-global-created", false);
michael@0 109 obs.addObserver(this, "http-on-modify-request", false);
michael@0 110
michael@0 111 if (messageManager) {
michael@0 112 this._messageManager = messageManager;
michael@0 113 this._mmIsGlobal = false;
michael@0 114 }
michael@0 115 };
michael@0 116
michael@0 117 SpecialPowersObserver.prototype.uninit = function()
michael@0 118 {
michael@0 119 var obs = Services.obs;
michael@0 120 obs.removeObserver(this, "chrome-document-global-created");
michael@0 121 obs.removeObserver(this, "http-on-modify-request");
michael@0 122 this._removeProcessCrashObservers();
michael@0 123 };
michael@0 124
michael@0 125 SpecialPowersObserver.prototype._addProcessCrashObservers = function() {
michael@0 126 if (this._processCrashObserversRegistered) {
michael@0 127 return;
michael@0 128 }
michael@0 129
michael@0 130 var obs = Components.classes["@mozilla.org/observer-service;1"]
michael@0 131 .getService(Components.interfaces.nsIObserverService);
michael@0 132
michael@0 133 obs.addObserver(this, "plugin-crashed", false);
michael@0 134 obs.addObserver(this, "ipc:content-shutdown", false);
michael@0 135 this._processCrashObserversRegistered = true;
michael@0 136 };
michael@0 137
michael@0 138 SpecialPowersObserver.prototype._removeProcessCrashObservers = function() {
michael@0 139 if (!this._processCrashObserversRegistered) {
michael@0 140 return;
michael@0 141 }
michael@0 142
michael@0 143 var obs = Components.classes["@mozilla.org/observer-service;1"]
michael@0 144 .getService(Components.interfaces.nsIObserverService);
michael@0 145
michael@0 146 obs.removeObserver(this, "plugin-crashed");
michael@0 147 obs.removeObserver(this, "ipc:content-shutdown");
michael@0 148 this._processCrashObserversRegistered = false;
michael@0 149 };
michael@0 150
michael@0 151 /**
michael@0 152 * messageManager callback function
michael@0 153 * This will get requests from our API in the window and process them in chrome for it
michael@0 154 **/
michael@0 155 SpecialPowersObserver.prototype.receiveMessage = function(aMessage) {
michael@0 156 switch(aMessage.name) {
michael@0 157 case "SPPingService":
michael@0 158 if (aMessage.json.op == "ping") {
michael@0 159 aMessage.target
michael@0 160 .QueryInterface(Ci.nsIFrameLoaderOwner)
michael@0 161 .frameLoader
michael@0 162 .messageManager
michael@0 163 .sendAsyncMessage("SPPingService", { op: "pong" });
michael@0 164 }
michael@0 165 break;
michael@0 166 case "SpecialPowers.Quit":
michael@0 167 let appStartup = Cc["@mozilla.org/toolkit/app-startup;1"].getService(Ci.nsIAppStartup);
michael@0 168 appStartup.quit(Ci.nsIAppStartup.eForceQuit);
michael@0 169 break;
michael@0 170 case "SpecialPowers.Focus":
michael@0 171 aMessage.target.focus();
michael@0 172 break;
michael@0 173 default:
michael@0 174 return this._receiveMessage(aMessage);
michael@0 175 }
michael@0 176 };
michael@0 177
michael@0 178 this.NSGetFactory = XPCOMUtils.generateNSGetFactory([SpecialPowersObserver]);

mercurial