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.

146 lines
4.4 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
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/uBlock
  15. */
  16. /* global APP_SHUTDOWN, APP_STARTUP */
  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*=ublock]').contentWindow;
  22. let bgProcess;
  23. let version;
  24. const hostName = 'ublock0';
  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. if ( reason !== APP_STARTUP ) {
  68. onReady();
  69. return;
  70. }
  71. let ww = Components.classes['@mozilla.org/embedcomp/window-watcher;1']
  72. .getService(Components.interfaces.nsIWindowWatcher);
  73. ww.registerNotification({
  74. observe: function(win, topic) {
  75. if ( topic !== 'domwindowopened' ) {
  76. return;
  77. }
  78. try {
  79. appShell.hiddenDOMWindow;
  80. } catch (ex) {
  81. return;
  82. }
  83. ww.unregisterNotification(this);
  84. win.addEventListener('DOMContentLoaded', onReady);
  85. }
  86. });
  87. }
  88. /******************************************************************************/
  89. function shutdown(data, reason) {
  90. if ( reason === APP_SHUTDOWN ) {
  91. return;
  92. }
  93. bgProcess.parentNode.removeChild(bgProcess);
  94. if ( data === undefined ) {
  95. return;
  96. }
  97. // Remove the restartObserver only when the extension is being disabled
  98. restartListener.messageManager.removeMessageListener(
  99. hostName + '-restart',
  100. restartListener
  101. );
  102. }
  103. /******************************************************************************/
  104. function install() {
  105. // https://bugzil.la/719376
  106. Components.classes['@mozilla.org/intl/stringbundle;1']
  107. .getService(Components.interfaces.nsIStringBundleService)
  108. .flushBundles();
  109. }
  110. /******************************************************************************/
  111. function uninstall() {}
  112. /******************************************************************************/