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.

136 lines
4.0 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 = 'ublock';
  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. bgProcess = hiddenDoc.documentElement.appendChild(
  48. hiddenDoc.createElementNS('http://www.w3.org/1999/xhtml', 'iframe')
  49. );
  50. bgProcess.setAttribute(
  51. 'src',
  52. 'chrome://' + hostName + '/content/background.html#' + version
  53. );
  54. restartListener.messageManager.addMessageListener(
  55. hostName + '-restart',
  56. restartListener
  57. );
  58. };
  59. if ( reason !== APP_STARTUP ) {
  60. onReady();
  61. return;
  62. }
  63. let ww = Components.classes['@mozilla.org/embedcomp/window-watcher;1']
  64. .getService(Components.interfaces.nsIWindowWatcher);
  65. ww.registerNotification({
  66. observe: function(win, topic) {
  67. if ( topic !== 'domwindowopened' ) {
  68. return;
  69. }
  70. try {
  71. appShell.hiddenDOMWindow;
  72. } catch (ex) {
  73. return;
  74. }
  75. ww.unregisterNotification(this);
  76. win.addEventListener('DOMContentLoaded', onReady);
  77. }
  78. });
  79. }
  80. /******************************************************************************/
  81. function shutdown(data, reason) {
  82. if ( reason === APP_SHUTDOWN ) {
  83. return;
  84. }
  85. bgProcess.parentNode.removeChild(bgProcess);
  86. if ( data === undefined ) {
  87. return;
  88. }
  89. // Remove the restartObserver only when the extension is being disabled
  90. restartListener.messageManager.removeMessageListener(
  91. hostName + '-restart',
  92. restartListener
  93. );
  94. }
  95. /******************************************************************************/
  96. function install() {
  97. // https://bugzil.la/719376
  98. Components.classes['@mozilla.org/intl/stringbundle;1']
  99. .getService(Components.interfaces.nsIStringBundleService)
  100. .flushBundles();
  101. }
  102. /******************************************************************************/
  103. function uninstall() {}
  104. /******************************************************************************/