utils.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. 'use strict';
  5. module.metadata = {
  6. "stability": "unstable"
  7. };
  8. const { Cc, Ci, Cu } = require('chrome');
  9. const { defer } = require('../lang/functional');
  10. const { emit, on, once, off } = require('../event/core');
  11. const { when: unload } = require('../system/unload');
  12. const events = require('../system/events');
  13. const { deprecateFunction } = require('../util/deprecate');
  14. const { isOneOf, is, satisfiesVersion, version } = require('../system/xul-app');
  15. const { isWindowPrivate } = require('../window/utils');
  16. const { isPrivateBrowsingSupported } = require('../self');
  17. let deferredEmit = defer(emit);
  18. let pbService;
  19. let PrivateBrowsingUtils;
  20. // Private browsing is only supported in Fx
  21. if (isOneOf(['Firefox', 'Fennec'])) {
  22. // get the nsIPrivateBrowsingService if it exists
  23. try {
  24. pbService = Cc["@mozilla.org/privatebrowsing;1"].
  25. getService(Ci.nsIPrivateBrowsingService);
  26. // a dummy service exists for the moment (Fx20 atleast), but will be removed eventually
  27. // ie: the service will exist, but it won't do anything and the global private browing
  28. // feature is not really active. See Bug 818800 and Bug 826037
  29. if (!('privateBrowsingEnabled' in pbService))
  30. pbService = undefined;
  31. }
  32. catch(e) { /* Private Browsing Service has been removed (Bug 818800) */ }
  33. try {
  34. PrivateBrowsingUtils = Cu.import('resource://gre/modules/PrivateBrowsingUtils.jsm', {}).PrivateBrowsingUtils;
  35. }
  36. catch(e) { /* if this file DNE then an error will be thrown */ }
  37. }
  38. // checks that global private browsing is implemented
  39. let isGlobalPBSupported = exports.isGlobalPBSupported = !!pbService && is('Firefox');
  40. // checks that per-window private browsing is implemented
  41. let isWindowPBSupported = exports.isWindowPBSupported =
  42. !pbService && !!PrivateBrowsingUtils && is('Firefox');
  43. // checks that per-tab private browsing is implemented
  44. let isTabPBSupported = exports.isTabPBSupported =
  45. !pbService && !!PrivateBrowsingUtils && is('Fennec') && satisfiesVersion(version, '>=20.0*');
  46. exports.isPermanentPrivateBrowsing = function() {
  47. return !!(PrivateBrowsingUtils && PrivateBrowsingUtils.permanentPrivateBrowsing);
  48. }
  49. function ignoreWindow(window) {
  50. return !isPrivateBrowsingSupported && isWindowPrivate(window) && !isGlobalPBSupported;
  51. }
  52. exports.ignoreWindow = ignoreWindow;
  53. function onChange() {
  54. // Emit event with in next turn of event loop.
  55. deferredEmit(exports, pbService.privateBrowsingEnabled ? 'start' : 'stop');
  56. }
  57. // Currently, only Firefox implements the private browsing service.
  58. if (isGlobalPBSupported) {
  59. // set up an observer for private browsing switches.
  60. events.on('private-browsing-transition-complete', onChange);
  61. }
  62. // We toggle private browsing mode asynchronously in order to work around
  63. // bug 659629. Since private browsing transitions are asynchronous
  64. // anyway, this doesn't significantly change the behavior of the API.
  65. let setMode = defer(function setMode(value) {
  66. value = !!value; // Cast to boolean.
  67. // default
  68. return pbService && (pbService.privateBrowsingEnabled = value);
  69. });
  70. exports.setMode = deprecateFunction(
  71. setMode,
  72. 'require("sdk/private-browsing").activate and ' +
  73. 'require("sdk/private-browsing").deactivate ' +
  74. 'are deprecated.'
  75. );
  76. let getMode = function getMode(chromeWin) {
  77. if (isWindowPrivate(chromeWin))
  78. return true;
  79. // default
  80. return pbService ? pbService.privateBrowsingEnabled : false;
  81. };
  82. exports.getMode = getMode;
  83. exports.on = on.bind(null, exports);
  84. // Make sure listeners are cleaned up.
  85. unload(function() off(exports));