bootstrap.js 7.5 KB

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