You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

235 lines
7.9 KiB

10 years ago
10 years ago
10 years ago
  1. /*******************************************************************************
  2. uMatrix - a browser extension to block requests.
  3. Copyright (C) 2014-2017 The uMatrix/uBlock Origin authors
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see {http://www.gnu.org/licenses/}.
  14. Home: https://github.com/gorhill/uMatrix
  15. */
  16. /* global ADDON_UNINSTALL, APP_SHUTDOWN */
  17. /* exported startup, shutdown, install, uninstall */
  18. 'use strict';
  19. /******************************************************************************/
  20. const {classes: Cc, interfaces: Ci} = Components;
  21. // Accessing the context of the background page:
  22. // var win = Services.appShell.hiddenDOMWindow.document.querySelector('iframe[src*=umatrix]').contentWindow;
  23. let windowlessBrowser = null;
  24. let windowlessBrowserPL = null;
  25. let bgProcess = null;
  26. let version;
  27. const hostName = 'umatrix';
  28. const restartListener = {
  29. get messageManager() {
  30. return Components.classes['@mozilla.org/parentprocessmessagemanager;1']
  31. .getService(Components.interfaces.nsIMessageListenerManager);
  32. },
  33. receiveMessage: function() {
  34. shutdown();
  35. startup();
  36. }
  37. };
  38. /******************************************************************************/
  39. // https://github.com/gorhill/uBlock/issues/2493
  40. // Fix by https://github.com/gijsk
  41. // imported from https://github.com/gorhill/uBlock/pull/2497
  42. function startup(data/*, reason*/) {
  43. if ( data !== undefined ) {
  44. version = data.version;
  45. }
  46. // Already started?
  47. if ( bgProcess !== null ) {
  48. return;
  49. }
  50. waitForHiddenWindow();
  51. }
  52. function createBgProcess(parentDocument) {
  53. bgProcess = parentDocument.documentElement.appendChild(
  54. parentDocument.createElementNS('http://www.w3.org/1999/xhtml', 'iframe')
  55. );
  56. bgProcess.setAttribute(
  57. 'src',
  58. 'chrome://' + hostName + '/content/background.html#' + version
  59. );
  60. // https://developer.mozilla.org/en-US/docs/Mozilla/Tech/XPCOM/Reference/Interface/nsIMessageListenerManager#addMessageListener%28%29
  61. // "If the same listener registers twice for the same message, the
  62. // "second registration is ignored."
  63. restartListener.messageManager.addMessageListener(
  64. hostName + '-restart',
  65. restartListener
  66. );
  67. }
  68. function getWindowlessBrowserFrame(appShell) {
  69. windowlessBrowser = appShell.createWindowlessBrowser(true);
  70. windowlessBrowser.QueryInterface(Ci.nsIInterfaceRequestor);
  71. let webProgress = windowlessBrowser.getInterface(Ci.nsIWebProgress);
  72. let XPCOMUtils = Components.utils.import('resource://gre/modules/XPCOMUtils.jsm', null).XPCOMUtils;
  73. windowlessBrowserPL = {
  74. QueryInterface: XPCOMUtils.generateQI([
  75. Ci.nsIWebProgressListener,
  76. Ci.nsIWebProgressListener2,
  77. Ci.nsISupportsWeakReference
  78. ]),
  79. onStateChange: function(wbp, request, stateFlags, status) {
  80. if ( !request ) { return; }
  81. if ( stateFlags & Ci.nsIWebProgressListener.STATE_STOP ) {
  82. webProgress.removeProgressListener(windowlessBrowserPL);
  83. windowlessBrowserPL = null;
  84. createBgProcess(windowlessBrowser.document);
  85. }
  86. }
  87. };
  88. webProgress.addProgressListener(windowlessBrowserPL, Ci.nsIWebProgress.NOTIFY_STATE_DOCUMENT);
  89. windowlessBrowser.document.location = "data:application/vnd.mozilla.xul+xml;charset=utf-8,<window%20id='" + hostName + "-win'/>";
  90. }
  91. function waitForHiddenWindow() {
  92. let appShell = Cc['@mozilla.org/appshell/appShellService;1']
  93. .getService(Ci.nsIAppShellService);
  94. let onReady = function(e) {
  95. if ( e ) {
  96. this.removeEventListener(e.type, onReady);
  97. }
  98. let hiddenDoc = appShell.hiddenDOMWindow.document;
  99. // https://github.com/gorhill/uBlock/issues/10
  100. // Fixed by github.com/AlexVallat:
  101. // https://github.com/chrisaljoudi/uBlock/issues/1149
  102. // https://github.com/AlexVallat/uBlock/commit/e762a29d308caa46578cdc34a9be92c4ad5ecdd0
  103. if ( !hiddenDoc || hiddenDoc.readyState === 'loading' ) {
  104. appShell.hiddenDOMWindow.addEventListener('DOMContentLoaded', onReady);
  105. return;
  106. }
  107. // Fix from https://github.com/gijsk, taken from:
  108. // - https://github.com/gorhill/uBlock/commit/53a794d9b2a8c65406ee7a201cacbc91c297b2f8
  109. //
  110. // In theory, it should be possible to create a windowless browser
  111. // immediately, without waiting for the hidden window to have loaded
  112. // completely. However, in practice, on Windows this seems to lead
  113. // to a broken Firefox appearance. To avoid this, we only create the
  114. // windowless browser here. We'll use that rather than the hidden
  115. // window for the actual background page (windowless browsers are
  116. // also what the webextension implementation in Firefox uses for
  117. // background pages).
  118. if ( appShell.createWindowlessBrowser ) {
  119. getWindowlessBrowserFrame(appShell);
  120. } else {
  121. createBgProcess(hiddenDoc);
  122. }
  123. };
  124. var ready = false;
  125. try {
  126. ready = appShell.hiddenDOMWindow &&
  127. appShell.hiddenDOMWindow.document;
  128. } catch (ex) {
  129. }
  130. if ( ready ) {
  131. onReady();
  132. return;
  133. }
  134. let ww = Components.classes['@mozilla.org/embedcomp/window-watcher;1']
  135. .getService(Components.interfaces.nsIWindowWatcher);
  136. ww.registerNotification({
  137. observe: function(win, topic) {
  138. if ( topic !== 'domwindowopened' ) {
  139. return;
  140. }
  141. try {
  142. void appShell.hiddenDOMWindow;
  143. } catch (ex) {
  144. return;
  145. }
  146. ww.unregisterNotification(this);
  147. onReady();
  148. }
  149. });
  150. }
  151. /******************************************************************************/
  152. function shutdown(data, reason) {
  153. if ( reason === APP_SHUTDOWN ) {
  154. return;
  155. }
  156. if ( bgProcess !== null ) {
  157. bgProcess.parentNode.removeChild(bgProcess);
  158. bgProcess = null;
  159. }
  160. if ( windowlessBrowser !== null ) {
  161. // close() does not exist for older versions of Firefox.
  162. if ( typeof windowlessBrowser.close === 'function' ) {
  163. windowlessBrowser.close();
  164. }
  165. windowlessBrowser = null;
  166. windowlessBrowserPL = null;
  167. }
  168. if ( data === undefined ) {
  169. return;
  170. }
  171. // Remove the restartObserver only when the extension is being disabled
  172. restartListener.messageManager.removeMessageListener(
  173. hostName + '-restart',
  174. restartListener
  175. );
  176. }
  177. /******************************************************************************/
  178. function install() {
  179. // https://bugzil.la/719376
  180. Components.classes['@mozilla.org/intl/stringbundle;1']
  181. .getService(Components.interfaces.nsIStringBundleService)
  182. .flushBundles();
  183. }
  184. /******************************************************************************/
  185. function uninstall(data, aReason) {
  186. if ( aReason !== ADDON_UNINSTALL ) {
  187. return;
  188. }
  189. // To cleanup vAPI.localStorage in vapi-common.js, aka
  190. // "extensions.umatrix.*" in `about:config`.
  191. Components.utils.import('resource://gre/modules/Services.jsm', null)
  192. .Services.prefs
  193. .getBranch('extensions.' + hostName + '.')
  194. .deleteBranch('');
  195. }
  196. /******************************************************************************/