aboutsummaryrefslogtreecommitdiff
path: root/tools/addon-sdk-1.12/lib/sdk/private-browsing/utils.js
blob: 1f57ca0ffa8f774f4283678f366d30245f59a727 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
'use strict';

module.metadata = {
  "stability": "unstable"
};

const { Cc, Ci } = require('chrome');
const { defer } = require('../lang/functional');
const observers = require('../deprecated/observer-service');
const { emit, on, once, off } = require('../event/core');
const { when: unload } = require('../system/unload');
const { getWindowLoadingContext } = require('../window/utils');
const { deprecateFunction } = require('../util/deprecate');

let deferredEmit = defer(emit);

let pbService;

// Currently, only Firefox implements the private browsing service.
if (require("../system/xul-app").is("Firefox")) {
  pbService = Cc["@mozilla.org/privatebrowsing;1"].
              getService(Ci.nsIPrivateBrowsingService);

  // set up an observer for private browsing switches.
  observers.add('private-browsing-transition-complete', function onChange() {
    // Emit event with in next turn of event loop.
    deferredEmit(exports, pbService.privateBrowsingEnabled ? 'start' : 'stop');
  });
}

// checks that per-window private browsing implemented
let isWindowPBEnabled = function isWindowPBEnabled(chromeWin) {
  return !!(chromeWin &&
         'gPrivateBrowsingUI' in chromeWin &&
         'privateWindow' in chromeWin.gPrivateBrowsingUI);
}
exports.isWindowPBEnabled = isWindowPBEnabled;

// We toggle private browsing mode asynchronously in order to work around
// bug 659629.  Since private browsing transitions are asynchronous
// anyway, this doesn't significantly change the behavior of the API.
// Note: this method should not be used with `chromeWin` argument until
//       the UI has been implemented. Bug 729865
let setMode = defer(function setMode(value, chromeWin) {
  value = !!value;  // Cast to boolean.

  if (isWindowPBEnabled(chromeWin))
    return getWindowLoadingContext(chromeWin).usePrivateBrowsing = value;

  // default
  return pbService && (pbService.privateBrowsingEnabled = value);
});
exports.setMode = deprecateFunction(
  setMode,
  'require("private-browsing").activate and require("private-browsing").deactivate ' +
  'is deprecated.'
);

let getMode = function getMode(chromeWin) {
  if (isWindowPBEnabled(chromeWin))
    return getWindowLoadingContext(chromeWin).usePrivateBrowsing;

  // default
  return pbService ? pbService.privateBrowsingEnabled : false;
};
exports.getMode = getMode;

exports.on = on.bind(null, exports);

// Make sure listeners are cleaned up.
unload(function() off(exports));