SpecialPowersObserver.jsm 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. // Based on:
  5. // https://bugzilla.mozilla.org/show_bug.cgi?id=549539
  6. // https://bug549539.bugzilla.mozilla.org/attachment.cgi?id=429661
  7. // https://developer.mozilla.org/en/XPCOM/XPCOM_changes_in_Gecko_1.9.3
  8. // https://developer.mozilla.org/en/how_to_build_an_xpcom_component_in_javascript
  9. var EXPORTED_SYMBOLS = ["SpecialPowersObserver", "SpecialPowersObserverFactory"];
  10. Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
  11. Components.utils.import("resource://gre/modules/Services.jsm");
  12. Components.utils.importGlobalProperties(['File']);
  13. if (typeof(Cc) == "undefined") {
  14. const Cc = Components.classes;
  15. const Ci = Components.interfaces;
  16. }
  17. const CHILD_SCRIPT = "chrome://specialpowers/content/specialpowers.js"
  18. const CHILD_SCRIPT_API = "chrome://specialpowers/content/specialpowersAPI.js"
  19. const CHILD_LOGGER_SCRIPT = "chrome://specialpowers/content/MozillaLogger.js"
  20. // Glue to add in the observer API to this object. This allows us to share code with chrome tests
  21. var loader = Components.classes["@mozilla.org/moz/jssubscript-loader;1"]
  22. .getService(Components.interfaces.mozIJSSubScriptLoader);
  23. loader.loadSubScript("chrome://specialpowers/content/SpecialPowersObserverAPI.js");
  24. /* XPCOM gunk */
  25. this.SpecialPowersObserver = function SpecialPowersObserver() {
  26. this._isFrameScriptLoaded = false;
  27. this._messageManager = Cc["@mozilla.org/globalmessagemanager;1"].
  28. getService(Ci.nsIMessageBroadcaster);
  29. }
  30. SpecialPowersObserver.prototype = new SpecialPowersObserverAPI();
  31. SpecialPowersObserver.prototype.classDescription = "Special powers Observer for use in testing.";
  32. SpecialPowersObserver.prototype.classID = Components.ID("{59a52458-13e0-4d93-9d85-a637344f29a1}");
  33. SpecialPowersObserver.prototype.contractID = "@mozilla.org/special-powers-observer;1";
  34. SpecialPowersObserver.prototype.QueryInterface = XPCOMUtils.generateQI([Components.interfaces.nsIObserver]);
  35. SpecialPowersObserver.prototype.observe = function(aSubject, aTopic, aData)
  36. {
  37. switch (aTopic) {
  38. case "chrome-document-global-created":
  39. this._loadFrameScript();
  40. break;
  41. case "http-on-modify-request":
  42. if (aSubject instanceof Ci.nsIChannel) {
  43. let uri = aSubject.URI.spec;
  44. this._sendAsyncMessage("specialpowers-http-notify-request", { uri: uri });
  45. }
  46. break;
  47. default:
  48. this._observe(aSubject, aTopic, aData);
  49. break;
  50. }
  51. };
  52. SpecialPowersObserver.prototype._loadFrameScript = function()
  53. {
  54. if (!this._isFrameScriptLoaded) {
  55. // Register for any messages our API needs us to handle
  56. this._messageManager.addMessageListener("SPPrefService", this);
  57. this._messageManager.addMessageListener("SPProcessCrashService", this);
  58. this._messageManager.addMessageListener("SPPingService", this);
  59. this._messageManager.addMessageListener("SpecialPowers.Quit", this);
  60. this._messageManager.addMessageListener("SpecialPowers.Focus", this);
  61. this._messageManager.addMessageListener("SpecialPowers.CreateFiles", this);
  62. this._messageManager.addMessageListener("SpecialPowers.RemoveFiles", this);
  63. this._messageManager.addMessageListener("SPPermissionManager", this);
  64. this._messageManager.addMessageListener("SPObserverService", this);
  65. this._messageManager.addMessageListener("SPLoadChromeScript", this);
  66. this._messageManager.addMessageListener("SPImportInMainProcess", this);
  67. this._messageManager.addMessageListener("SPChromeScriptMessage", this);
  68. this._messageManager.addMessageListener("SPQuotaManager", this);
  69. this._messageManager.addMessageListener("SPSetTestPluginEnabledState", this);
  70. this._messageManager.addMessageListener("SPLoadExtension", this);
  71. this._messageManager.addMessageListener("SPStartupExtension", this);
  72. this._messageManager.addMessageListener("SPUnloadExtension", this);
  73. this._messageManager.addMessageListener("SPExtensionMessage", this);
  74. this._messageManager.addMessageListener("SPCleanUpSTSData", this);
  75. this._messageManager.addMessageListener("SPClearAppPrivateData", this);
  76. this._messageManager.loadFrameScript(CHILD_LOGGER_SCRIPT, true);
  77. this._messageManager.loadFrameScript(CHILD_SCRIPT_API, true);
  78. this._messageManager.loadFrameScript(CHILD_SCRIPT, true);
  79. this._isFrameScriptLoaded = true;
  80. this._createdFiles = null;
  81. }
  82. };
  83. SpecialPowersObserver.prototype._sendAsyncMessage = function(msgname, msg)
  84. {
  85. this._messageManager.broadcastAsyncMessage(msgname, msg);
  86. };
  87. SpecialPowersObserver.prototype._receiveMessage = function(aMessage) {
  88. return this._receiveMessageAPI(aMessage);
  89. };
  90. SpecialPowersObserver.prototype.init = function()
  91. {
  92. var obs = Services.obs;
  93. obs.addObserver(this, "chrome-document-global-created", false);
  94. // Register special testing modules.
  95. var testsURI = Cc["@mozilla.org/file/directory_service;1"].
  96. getService(Ci.nsIProperties).
  97. get("ProfD", Ci.nsILocalFile);
  98. testsURI.append("tests.manifest");
  99. var ioSvc = Cc["@mozilla.org/network/io-service;1"].
  100. getService(Ci.nsIIOService);
  101. var manifestFile = ioSvc.newFileURI(testsURI).
  102. QueryInterface(Ci.nsIFileURL).file;
  103. Components.manager.QueryInterface(Ci.nsIComponentRegistrar).
  104. autoRegister(manifestFile);
  105. obs.addObserver(this, "http-on-modify-request", false);
  106. this._loadFrameScript();
  107. };
  108. SpecialPowersObserver.prototype.uninit = function()
  109. {
  110. var obs = Services.obs;
  111. obs.removeObserver(this, "chrome-document-global-created");
  112. obs.removeObserver(this, "http-on-modify-request");
  113. this._registerObservers._topics.forEach(function(element) {
  114. obs.removeObserver(this._registerObservers, element);
  115. });
  116. this._removeProcessCrashObservers();
  117. if (this._isFrameScriptLoaded) {
  118. this._messageManager.removeMessageListener("SPPrefService", this);
  119. this._messageManager.removeMessageListener("SPProcessCrashService", this);
  120. this._messageManager.removeMessageListener("SPPingService", this);
  121. this._messageManager.removeMessageListener("SpecialPowers.Quit", this);
  122. this._messageManager.removeMessageListener("SpecialPowers.Focus", this);
  123. this._messageManager.removeMessageListener("SpecialPowers.CreateFiles", this);
  124. this._messageManager.removeMessageListener("SpecialPowers.RemoveFiles", this);
  125. this._messageManager.removeMessageListener("SPPermissionManager", this);
  126. this._messageManager.removeMessageListener("SPObserverService", this);
  127. this._messageManager.removeMessageListener("SPLoadChromeScript", this);
  128. this._messageManager.removeMessageListener("SPImportInMainProcess", this);
  129. this._messageManager.removeMessageListener("SPChromeScriptMessage", this);
  130. this._messageManager.removeMessageListener("SPQuotaManager", this);
  131. this._messageManager.removeMessageListener("SPSetTestPluginEnabledState", this);
  132. this._messageManager.removeMessageListener("SPLoadExtension", this);
  133. this._messageManager.removeMessageListener("SPStartupExtension", this);
  134. this._messageManager.removeMessageListener("SPUnloadExtension", this);
  135. this._messageManager.removeMessageListener("SPExtensionMessage", this);
  136. this._messageManager.removeMessageListener("SPCleanUpSTSData", this);
  137. this._messageManager.removeMessageListener("SPClearAppPrivateData", this);
  138. this._messageManager.removeDelayedFrameScript(CHILD_LOGGER_SCRIPT);
  139. this._messageManager.removeDelayedFrameScript(CHILD_SCRIPT_API);
  140. this._messageManager.removeDelayedFrameScript(CHILD_SCRIPT);
  141. this._isFrameScriptLoaded = false;
  142. }
  143. };
  144. SpecialPowersObserver.prototype._addProcessCrashObservers = function() {
  145. if (this._processCrashObserversRegistered) {
  146. return;
  147. }
  148. var obs = Components.classes["@mozilla.org/observer-service;1"]
  149. .getService(Components.interfaces.nsIObserverService);
  150. obs.addObserver(this, "plugin-crashed", false);
  151. obs.addObserver(this, "ipc:content-shutdown", false);
  152. this._processCrashObserversRegistered = true;
  153. };
  154. SpecialPowersObserver.prototype._removeProcessCrashObservers = function() {
  155. if (!this._processCrashObserversRegistered) {
  156. return;
  157. }
  158. var obs = Components.classes["@mozilla.org/observer-service;1"]
  159. .getService(Components.interfaces.nsIObserverService);
  160. obs.removeObserver(this, "plugin-crashed");
  161. obs.removeObserver(this, "ipc:content-shutdown");
  162. this._processCrashObserversRegistered = false;
  163. };
  164. SpecialPowersObserver.prototype._registerObservers = {
  165. _self: null,
  166. _topics: [],
  167. _add: function(topic) {
  168. if (this._topics.indexOf(topic) < 0) {
  169. this._topics.push(topic);
  170. Services.obs.addObserver(this, topic, false);
  171. }
  172. },
  173. observe: function (aSubject, aTopic, aData) {
  174. var msg = { aData: aData };
  175. switch (aTopic) {
  176. case "perm-changed":
  177. var permission = aSubject.QueryInterface(Ci.nsIPermission);
  178. // specialPowersAPI will consume this value, and it is used as a
  179. // fake permission, but only type and principal.appId will be used.
  180. //
  181. // We need to ensure that it looks the same as a real permission,
  182. // so we fake these properties.
  183. msg.permission = {
  184. principal: {
  185. originAttributes: {appId: permission.principal.appId}
  186. },
  187. type: permission.type
  188. };
  189. default:
  190. this._self._sendAsyncMessage("specialpowers-" + aTopic, msg);
  191. }
  192. }
  193. };
  194. /**
  195. * messageManager callback function
  196. * This will get requests from our API in the window and process them in chrome for it
  197. **/
  198. SpecialPowersObserver.prototype.receiveMessage = function(aMessage) {
  199. switch(aMessage.name) {
  200. case "SPPingService":
  201. if (aMessage.json.op == "ping") {
  202. aMessage.target
  203. .QueryInterface(Ci.nsIFrameLoaderOwner)
  204. .frameLoader
  205. .messageManager
  206. .sendAsyncMessage("SPPingService", { op: "pong" });
  207. }
  208. break;
  209. case "SpecialPowers.Quit":
  210. let appStartup = Cc["@mozilla.org/toolkit/app-startup;1"].getService(Ci.nsIAppStartup);
  211. appStartup.quit(Ci.nsIAppStartup.eForceQuit);
  212. break;
  213. case "SpecialPowers.Focus":
  214. aMessage.target.focus();
  215. break;
  216. case "SpecialPowers.CreateFiles":
  217. let filePaths = new Array;
  218. if (!this._createdFiles) {
  219. this._createdFiles = new Array;
  220. }
  221. let createdFiles = this._createdFiles;
  222. try {
  223. aMessage.data.forEach(function(request) {
  224. const filePerms = 0666;
  225. let testFile = Services.dirsvc.get("ProfD", Ci.nsIFile);
  226. if (request.name) {
  227. testFile.appendRelativePath(request.name);
  228. } else {
  229. testFile.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, filePerms);
  230. }
  231. let outStream = Cc["@mozilla.org/network/file-output-stream;1"].createInstance(Ci.nsIFileOutputStream);
  232. outStream.init(testFile, 0x02 | 0x08 | 0x20, // PR_WRONLY | PR_CREATE_FILE | PR_TRUNCATE
  233. filePerms, 0);
  234. if (request.data) {
  235. outStream.write(request.data, request.data.length);
  236. }
  237. outStream.close();
  238. filePaths.push(File.createFromFileName(testFile.path, request.options));
  239. createdFiles.push(testFile);
  240. });
  241. aMessage.target
  242. .QueryInterface(Ci.nsIFrameLoaderOwner)
  243. .frameLoader
  244. .messageManager
  245. .sendAsyncMessage("SpecialPowers.FilesCreated", filePaths);
  246. } catch (e) {
  247. aMessage.target
  248. .QueryInterface(Ci.nsIFrameLoaderOwner)
  249. .frameLoader
  250. .messageManager
  251. .sendAsyncMessage("SpecialPowers.FilesError", e.toString());
  252. }
  253. break;
  254. case "SpecialPowers.RemoveFiles":
  255. if (this._createdFiles) {
  256. this._createdFiles.forEach(function (testFile) {
  257. try {
  258. testFile.remove(false);
  259. } catch (e) {}
  260. });
  261. this._createdFiles = null;
  262. }
  263. break;
  264. default:
  265. return this._receiveMessage(aMessage);
  266. }
  267. };
  268. this.NSGetFactory = XPCOMUtils.generateNSGetFactory([SpecialPowersObserver]);
  269. this.SpecialPowersObserverFactory = Object.freeze({
  270. createInstance: function(outer, id) {
  271. if (outer) { throw Components.results.NS_ERROR_NO_AGGREGATION };
  272. return new SpecialPowersObserver();
  273. },
  274. loadFactory: function(lock){},
  275. QueryInterface: XPCOMUtils.generateQI([Ci.nsIFactory])
  276. });