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