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.

162 lines
4.9 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  1. /*******************************************************************************
  2. µBlock - a browser extension to block requests.
  3. Copyright (C) 2014 The µBlock 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. // Accessing the context of the background page:
  21. // var win = Services.appShell.hiddenDOMWindow.document.querySelector('iframe[src*=umatrix]').contentWindow;
  22. let bgProcess;
  23. let version;
  24. const hostName = 'umatrix';
  25. const restartListener = {
  26. get messageManager() {
  27. return Components.classes['@mozilla.org/parentprocessmessagemanager;1']
  28. .getService(Components.interfaces.nsIMessageListenerManager);
  29. },
  30. receiveMessage: function() {
  31. shutdown();
  32. startup();
  33. }
  34. };
  35. /******************************************************************************/
  36. function startup(data/*, reason*/) {
  37. if ( data !== undefined ) {
  38. version = data.version;
  39. }
  40. let appShell = Components.classes['@mozilla.org/appshell/appShellService;1']
  41. .getService(Components.interfaces.nsIAppShellService);
  42. let onReady = function(e) {
  43. if ( e ) {
  44. this.removeEventListener(e.type, onReady);
  45. }
  46. let hiddenDoc = appShell.hiddenDOMWindow.document;
  47. // https://github.com/gorhill/uBlock/issues/10
  48. // Fixed by github.com/AlexVallat:
  49. // https://github.com/chrisaljoudi/uBlock/issues/1149
  50. // https://github.com/AlexVallat/uBlock/commit/e762a29d308caa46578cdc34a9be92c4ad5ecdd0
  51. if ( hiddenDoc.readyState === 'loading' ) {
  52. hiddenDoc.addEventListener('DOMContentLoaded', onReady);
  53. return;
  54. }
  55. bgProcess = hiddenDoc.documentElement.appendChild(
  56. hiddenDoc.createElementNS('http://www.w3.org/1999/xhtml', 'iframe')
  57. );
  58. bgProcess.setAttribute(
  59. 'src',
  60. 'chrome://' + hostName + '/content/background.html#' + version
  61. );
  62. restartListener.messageManager.addMessageListener(
  63. hostName + '-restart',
  64. restartListener
  65. );
  66. };
  67. var ready = false;
  68. try {
  69. ready = appShell.hiddenDOMWindow &&
  70. appShell.hiddenDOMWindow.document;
  71. } catch (ex) {
  72. }
  73. if ( ready ) {
  74. onReady();
  75. return;
  76. }
  77. let ww = Components.classes['@mozilla.org/embedcomp/window-watcher;1']
  78. .getService(Components.interfaces.nsIWindowWatcher);
  79. ww.registerNotification({
  80. observe: function(win, topic) {
  81. if ( topic !== 'domwindowopened' ) {
  82. return;
  83. }
  84. try {
  85. void appShell.hiddenDOMWindow;
  86. } catch (ex) {
  87. return;
  88. }
  89. ww.unregisterNotification(this);
  90. win.addEventListener('DOMContentLoaded', onReady);
  91. }
  92. });
  93. }
  94. /******************************************************************************/
  95. function shutdown(data, reason) {
  96. if ( reason === APP_SHUTDOWN ) {
  97. return;
  98. }
  99. bgProcess.parentNode.removeChild(bgProcess);
  100. if ( data === undefined ) {
  101. return;
  102. }
  103. // Remove the restartObserver only when the extension is being disabled
  104. restartListener.messageManager.removeMessageListener(
  105. hostName + '-restart',
  106. restartListener
  107. );
  108. }
  109. /******************************************************************************/
  110. function install() {
  111. // https://bugzil.la/719376
  112. Components.classes['@mozilla.org/intl/stringbundle;1']
  113. .getService(Components.interfaces.nsIStringBundleService)
  114. .flushBundles();
  115. }
  116. /******************************************************************************/
  117. function uninstall(data, aReason) {
  118. if ( aReason !== ADDON_UNINSTALL ) {
  119. return;
  120. }
  121. // To cleanup vAPI.localStorage in vapi-common.js, aka
  122. // "extensions.umatrix.*" in `about:config`.
  123. Components.utils.import('resource://gre/modules/Services.jsm', null)
  124. .Services.prefs
  125. .getBranch('extensions.' + hostName + '.')
  126. .deleteBranch('');
  127. }
  128. /******************************************************************************/