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.

129 lines
3.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
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. const hostName = 'ublock';
  24. const restartListener = {
  25. get messageManager() {
  26. return Components.classes['@mozilla.org/parentprocessmessagemanager;1']
  27. .getService(Components.interfaces.nsIMessageListenerManager);
  28. },
  29. receiveMessage: function() {
  30. shutdown();
  31. startup();
  32. }
  33. };
  34. /******************************************************************************/
  35. function startup(data, reason) {
  36. let appShell = Components.classes['@mozilla.org/appshell/appShellService;1']
  37. .getService(Components.interfaces.nsIAppShellService);
  38. let onReady = function(e) {
  39. if ( e ) {
  40. this.removeEventListener(e.type, onReady);
  41. }
  42. let hiddenDoc = appShell.hiddenDOMWindow.document;
  43. bgProcess = hiddenDoc.documentElement.appendChild(
  44. hiddenDoc.createElementNS('http://www.w3.org/1999/xhtml', 'iframe')
  45. );
  46. bgProcess.setAttribute(
  47. 'src',
  48. 'chrome://' + hostName + '/content/background.html'
  49. );
  50. restartListener.messageManager.addMessageListener(
  51. hostName + '-restart',
  52. restartListener
  53. );
  54. };
  55. if ( reason !== APP_STARTUP ) {
  56. onReady();
  57. return;
  58. }
  59. let ww = Components.classes['@mozilla.org/embedcomp/window-watcher;1']
  60. .getService(Components.interfaces.nsIWindowWatcher);
  61. ww.registerNotification({
  62. observe: function(win, topic) {
  63. if ( topic !== 'domwindowopened' ) {
  64. return;
  65. }
  66. try {
  67. appShell.hiddenDOMWindow;
  68. } catch (ex) {
  69. return;
  70. }
  71. ww.unregisterNotification(this);
  72. win.addEventListener('DOMContentLoaded', onReady);
  73. }
  74. });
  75. }
  76. /******************************************************************************/
  77. function shutdown(data, reason) {
  78. if ( reason === APP_SHUTDOWN ) {
  79. return;
  80. }
  81. bgProcess.parentNode.removeChild(bgProcess);
  82. // Remove the restartObserver only when the extension is being disabled
  83. if ( data !== undefined ) {
  84. restartListener.messageManager.removeMessageListener(
  85. hostName + '-restart',
  86. restartListener
  87. );
  88. }
  89. }
  90. /******************************************************************************/
  91. function install() {
  92. // https://bugzil.la/719376
  93. Components.classes['@mozilla.org/intl/stringbundle;1']
  94. .getService(Components.interfaces.nsIStringBundleService)
  95. .flushBundles();
  96. }
  97. /******************************************************************************/
  98. function uninstall() {}
  99. /******************************************************************************/