1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/addon-sdk/source/lib/sdk/private-browsing.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,93 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 +'use strict'; 1.8 + 1.9 +module.metadata = { 1.10 + "stability": "stable" 1.11 +}; 1.12 + 1.13 +const { Ci } = require('chrome'); 1.14 +const { setMode, getMode, on: onStateChange, isPermanentPrivateBrowsing } = require('./private-browsing/utils'); 1.15 +const { isWindowPrivate } = require('./window/utils'); 1.16 +const { emit, on, once, off } = require('./event/core'); 1.17 +const { when: unload } = require('./system/unload'); 1.18 +const { deprecateUsage, deprecateFunction, deprecateEvent } = require('./util/deprecate'); 1.19 +const { getOwnerWindow } = require('./private-browsing/window/utils'); 1.20 + 1.21 +onStateChange('start', function onStart() { 1.22 + emit(exports, 'start'); 1.23 +}); 1.24 + 1.25 +onStateChange('stop', function onStop() { 1.26 + emit(exports, 'stop'); 1.27 +}); 1.28 + 1.29 +Object.defineProperty(exports, "isActive", { 1.30 + get: deprecateFunction(getMode, 'require("private-browsing").isActive is deprecated.') 1.31 +}); 1.32 + 1.33 +exports.activate = function activate() setMode(true); 1.34 +exports.deactivate = function deactivate() setMode(false); 1.35 + 1.36 +exports.on = deprecateEvents(on.bind(null, exports)); 1.37 +exports.once = deprecateEvents(once.bind(null, exports)); 1.38 +exports.removeListener = deprecateEvents(function removeListener(type, listener) { 1.39 + // Note: We can't just bind `off` as we do it for other methods cause skipping 1.40 + // a listener argument will remove all listeners for the given event type 1.41 + // causing misbehavior. This way we make sure all arguments are passed. 1.42 + off(exports, type, listener); 1.43 +}); 1.44 + 1.45 +exports.isPrivate = function(thing) { 1.46 + // if thing is defined, and we can find a window for it 1.47 + // then check if the window is private 1.48 + if (!!thing) { 1.49 + // if the thing is a window, and the window is private 1.50 + // then return true 1.51 + if (isWindowPrivate(thing)) { 1.52 + return true; 1.53 + } 1.54 + 1.55 + // does the thing have an associated tab? 1.56 + // page-mod instances do.. 1.57 + if (thing.tab) { 1.58 + let tabWindow = getOwnerWindow(thing.tab); 1.59 + if (tabWindow) { 1.60 + let isThingPrivate = isWindowPrivate(tabWindow); 1.61 + if (isThingPrivate) 1.62 + return isThingPrivate; 1.63 + } 1.64 + } 1.65 + 1.66 + // can we find an associated window? 1.67 + let window = getOwnerWindow(thing); 1.68 + if (window) 1.69 + return isWindowPrivate(window); 1.70 + 1.71 + try { 1.72 + let { isChannelPrivate } = thing.QueryInterface(Ci.nsIPrivateBrowsingChannel); 1.73 + if (isChannelPrivate) 1.74 + return true; 1.75 + } catch(e) {} 1.76 + } 1.77 + 1.78 + // check if the post pwpb, global pb service is enabled. 1.79 + if (isPermanentPrivateBrowsing()) 1.80 + return true; 1.81 + 1.82 + // if we get here, and global private browsing 1.83 + // is available, and it is true, then return 1.84 + // true otherwise false is returned here 1.85 + return getMode(); 1.86 +}; 1.87 + 1.88 +function deprecateEvents(func) deprecateEvent( 1.89 + func, 1.90 + 'The require("sdk/private-browsing") module\'s "start" and "stop" events ' + 1.91 + 'are deprecated.', 1.92 + ['start', 'stop'] 1.93 +); 1.94 + 1.95 +// Make sure listeners are cleaned up. 1.96 +unload(function() off(exports));