private-browsing.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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": "stable"
  7. };
  8. const { setMode, getMode, on: onStateChange, isPermanentPrivateBrowsing } = require('./private-browsing/utils');
  9. const { isWindowPrivate } = require('./window/utils');
  10. const { emit, on, once, off } = require('./event/core');
  11. const { when: unload } = require('./system/unload');
  12. const { deprecateUsage, deprecateFunction, deprecateEvent } = require('./util/deprecate');
  13. const { getOwnerWindow } = require('./private-browsing/window/utils');
  14. onStateChange('start', function onStart() {
  15. emit(exports, 'start');
  16. });
  17. onStateChange('stop', function onStop() {
  18. emit(exports, 'stop');
  19. });
  20. Object.defineProperty(exports, "isActive", {
  21. get: deprecateFunction(getMode, 'require("private-browsing").isActive is deprecated.')
  22. });
  23. exports.activate = function activate() setMode(true);
  24. exports.deactivate = function deactivate() setMode(false);
  25. exports.on = deprecateEvents(on.bind(null, exports));
  26. exports.once = deprecateEvents(once.bind(null, exports));
  27. exports.removeListener = deprecateEvents(function removeListener(type, listener) {
  28. // Note: We can't just bind `off` as we do it for other methods cause skipping
  29. // a listener argument will remove all listeners for the given event type
  30. // causing misbehavior. This way we make sure all arguments are passed.
  31. off(exports, type, listener);
  32. });
  33. exports.isPrivate = function(thing) {
  34. // if thing is defined, and we can find a window for it
  35. // then check if the window is private
  36. if (!!thing) {
  37. // if the thing is a window, and the window is private
  38. // then return true
  39. if (isWindowPrivate(thing)) {
  40. return true;
  41. }
  42. // does the thing have an associated tab?
  43. // page-mod instances do..
  44. if (thing.tab) {
  45. let tabWindow = getOwnerWindow(thing.tab);
  46. if (tabWindow) {
  47. let isThingPrivate = isWindowPrivate(tabWindow);
  48. if (isThingPrivate)
  49. return isThingPrivate;
  50. }
  51. }
  52. // can we find an associated window?
  53. let window = getOwnerWindow(thing);
  54. if (window)
  55. return isWindowPrivate(window);
  56. }
  57. // check if the post pwpb, global pb service is enabled.
  58. if (isPermanentPrivateBrowsing())
  59. return true;
  60. // if we get here, and global private browsing
  61. // is available, and it is true, then return
  62. // true otherwise false is returned here
  63. return getMode();
  64. };
  65. function deprecateEvents(func) deprecateEvent(
  66. func,
  67. 'The require("sdk/private-browsing") module\'s "start" and "stop" events ' +
  68. 'are deprecated.',
  69. ['start', 'stop']
  70. );
  71. // Make sure listeners are cleaned up.
  72. unload(function() off(exports));