Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
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';
6 module.metadata = {
7 "stability": "unstable"
8 };
10 const { Cc, Ci, Cu } = require('chrome');
11 const { defer } = require('../lang/functional');
12 const { emit, on, once, off } = require('../event/core');
13 const { when: unload } = require('../system/unload');
14 const events = require('../system/events');
15 const { deprecateFunction } = require('../util/deprecate');
16 const { isOneOf, is, satisfiesVersion, version } = require('../system/xul-app');
17 const { isWindowPrivate } = require('../window/utils');
18 const { isPrivateBrowsingSupported } = require('../self');
20 let deferredEmit = defer(emit);
21 let pbService;
22 let PrivateBrowsingUtils;
24 // Private browsing is only supported in Fx
25 if (isOneOf(['Firefox', 'Fennec'])) {
26 // get the nsIPrivateBrowsingService if it exists
27 try {
28 pbService = Cc["@mozilla.org/privatebrowsing;1"].
29 getService(Ci.nsIPrivateBrowsingService);
31 // a dummy service exists for the moment (Fx20 atleast), but will be removed eventually
32 // ie: the service will exist, but it won't do anything and the global private browing
33 // feature is not really active. See Bug 818800 and Bug 826037
34 if (!('privateBrowsingEnabled' in pbService))
35 pbService = undefined;
36 }
37 catch(e) { /* Private Browsing Service has been removed (Bug 818800) */ }
39 try {
40 PrivateBrowsingUtils = Cu.import('resource://gre/modules/PrivateBrowsingUtils.jsm', {}).PrivateBrowsingUtils;
41 }
42 catch(e) { /* if this file DNE then an error will be thrown */ }
43 }
45 // checks that global private browsing is implemented
46 let isGlobalPBSupported = exports.isGlobalPBSupported = !!pbService && is('Firefox');
48 // checks that per-window private browsing is implemented
49 let isWindowPBSupported = exports.isWindowPBSupported =
50 !pbService && !!PrivateBrowsingUtils && is('Firefox');
52 // checks that per-tab private browsing is implemented
53 let isTabPBSupported = exports.isTabPBSupported =
54 !pbService && !!PrivateBrowsingUtils && is('Fennec') && satisfiesVersion(version, '>=20.0*');
56 exports.isPermanentPrivateBrowsing = function() {
57 return !!(PrivateBrowsingUtils && PrivateBrowsingUtils.permanentPrivateBrowsing);
58 }
60 function ignoreWindow(window) {
61 return !isPrivateBrowsingSupported && isWindowPrivate(window) && !isGlobalPBSupported;
62 }
63 exports.ignoreWindow = ignoreWindow;
65 function onChange() {
66 // Emit event with in next turn of event loop.
67 deferredEmit(exports, pbService.privateBrowsingEnabled ? 'start' : 'stop');
68 }
70 // Currently, only Firefox implements the private browsing service.
71 if (isGlobalPBSupported) {
72 // set up an observer for private browsing switches.
73 events.on('private-browsing-transition-complete', onChange);
74 }
76 // We toggle private browsing mode asynchronously in order to work around
77 // bug 659629. Since private browsing transitions are asynchronous
78 // anyway, this doesn't significantly change the behavior of the API.
79 let setMode = defer(function setMode(value) {
80 value = !!value; // Cast to boolean.
82 // default
83 return pbService && (pbService.privateBrowsingEnabled = value);
84 });
85 exports.setMode = deprecateFunction(
86 setMode,
87 'require("sdk/private-browsing").activate and ' +
88 'require("sdk/private-browsing").deactivate ' +
89 'are deprecated.'
90 );
92 let getMode = function getMode(chromeWin) {
93 if (isWindowPrivate(chromeWin))
94 return true;
96 // default
97 return pbService ? pbService.privateBrowsingEnabled : false;
98 };
99 exports.getMode = getMode;
101 exports.on = on.bind(null, exports);
103 // Make sure listeners are cleaned up.
104 unload(function() off(exports));