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.

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