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.

126 lines
3.7 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 {classes: Cc, interfaces: Ci, utils: Cu} = Components;
  23. const restartObserver = {
  24. get observer() {
  25. return Cc["@mozilla.org/observer-service;1"]
  26. .getService(Ci.nsIObserverService);
  27. },
  28. QueryInterface: (function() {
  29. let {XPCOMUtils} = Cu['import']('resource://gre/modules/XPCOMUtils.jsm', {});
  30. return XPCOMUtils.generateQI([
  31. Ci.nsIObserver,
  32. Ci.nsISupportsWeakReference
  33. ]);
  34. })(),
  35. register: function() {
  36. this.observer.addObserver(this, hostName + '-restart', true);
  37. },
  38. unregister: function() {
  39. this.observer.removeObserver(this, hostName + '-restart');
  40. },
  41. observe: function() {
  42. shutdown();
  43. startup();
  44. }
  45. };
  46. /******************************************************************************/
  47. function startup(data, reason) {
  48. let onReady = function(e) {
  49. if ( e ) {
  50. this.removeEventListener(e.type, onReady);
  51. }
  52. let hDoc = Cc['@mozilla.org/appshell/appShellService;1']
  53. .getService(Ci.nsIAppShellService)
  54. .hiddenDOMWindow.document;
  55. bgProcess = hDoc.documentElement.appendChild(
  56. hDoc.createElementNS('http://www.w3.org/1999/xhtml', 'iframe')
  57. );
  58. bgProcess.setAttribute(
  59. 'src',
  60. 'chrome://' + hostName + '/content/background.html'
  61. );
  62. restartObserver.register();
  63. };
  64. if ( reason !== APP_STARTUP ) {
  65. onReady();
  66. return;
  67. }
  68. let ww = Cc['@mozilla.org/embedcomp/window-watcher;1']
  69. .getService(Ci.nsIWindowWatcher);
  70. ww.registerNotification({
  71. observe: function(win) {
  72. ww.unregisterNotification(this);
  73. win.addEventListener('DOMContentLoaded', onReady);
  74. }
  75. });
  76. }
  77. /******************************************************************************/
  78. function shutdown(data, reason) {
  79. if ( reason === APP_SHUTDOWN ) {
  80. return;
  81. }
  82. bgProcess.parentNode.removeChild(bgProcess);
  83. // remove the restartObserver only when the extension is being disabled
  84. if ( data !== undefined ) {
  85. restartObserver.unregister();
  86. }
  87. }
  88. /******************************************************************************/
  89. function install() {
  90. // https://bugzil.la/719376
  91. Cc['@mozilla.org/intl/stringbundle;1']
  92. .getService(Ci.nsIStringBundleService).flushBundles();
  93. }
  94. /******************************************************************************/
  95. function uninstall() {}
  96. /******************************************************************************/