|
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 |
|
6 module.metadata = { |
|
7 "stability": "unstable" |
|
8 }; |
|
9 |
|
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'); |
|
19 |
|
20 let deferredEmit = defer(emit); |
|
21 let pbService; |
|
22 let PrivateBrowsingUtils; |
|
23 |
|
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); |
|
30 |
|
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) */ } |
|
38 |
|
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 } |
|
44 |
|
45 // checks that global private browsing is implemented |
|
46 let isGlobalPBSupported = exports.isGlobalPBSupported = !!pbService && is('Firefox'); |
|
47 |
|
48 // checks that per-window private browsing is implemented |
|
49 let isWindowPBSupported = exports.isWindowPBSupported = |
|
50 !pbService && !!PrivateBrowsingUtils && is('Firefox'); |
|
51 |
|
52 // checks that per-tab private browsing is implemented |
|
53 let isTabPBSupported = exports.isTabPBSupported = |
|
54 !pbService && !!PrivateBrowsingUtils && is('Fennec') && satisfiesVersion(version, '>=20.0*'); |
|
55 |
|
56 exports.isPermanentPrivateBrowsing = function() { |
|
57 return !!(PrivateBrowsingUtils && PrivateBrowsingUtils.permanentPrivateBrowsing); |
|
58 } |
|
59 |
|
60 function ignoreWindow(window) { |
|
61 return !isPrivateBrowsingSupported && isWindowPrivate(window) && !isGlobalPBSupported; |
|
62 } |
|
63 exports.ignoreWindow = ignoreWindow; |
|
64 |
|
65 function onChange() { |
|
66 // Emit event with in next turn of event loop. |
|
67 deferredEmit(exports, pbService.privateBrowsingEnabled ? 'start' : 'stop'); |
|
68 } |
|
69 |
|
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 } |
|
75 |
|
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. |
|
81 |
|
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 ); |
|
91 |
|
92 let getMode = function getMode(chromeWin) { |
|
93 if (isWindowPrivate(chromeWin)) |
|
94 return true; |
|
95 |
|
96 // default |
|
97 return pbService ? pbService.privateBrowsingEnabled : false; |
|
98 }; |
|
99 exports.getMode = getMode; |
|
100 |
|
101 exports.on = on.bind(null, exports); |
|
102 |
|
103 // Make sure listeners are cleaned up. |
|
104 unload(function() off(exports)); |