michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: 'use strict'; michael@0: michael@0: module.metadata = { michael@0: "stability": "stable" michael@0: }; michael@0: michael@0: const { Ci } = require('chrome'); michael@0: const { setMode, getMode, on: onStateChange, isPermanentPrivateBrowsing } = require('./private-browsing/utils'); michael@0: const { isWindowPrivate } = require('./window/utils'); michael@0: const { emit, on, once, off } = require('./event/core'); michael@0: const { when: unload } = require('./system/unload'); michael@0: const { deprecateUsage, deprecateFunction, deprecateEvent } = require('./util/deprecate'); michael@0: const { getOwnerWindow } = require('./private-browsing/window/utils'); michael@0: michael@0: onStateChange('start', function onStart() { michael@0: emit(exports, 'start'); michael@0: }); michael@0: michael@0: onStateChange('stop', function onStop() { michael@0: emit(exports, 'stop'); michael@0: }); michael@0: michael@0: Object.defineProperty(exports, "isActive", { michael@0: get: deprecateFunction(getMode, 'require("private-browsing").isActive is deprecated.') michael@0: }); michael@0: michael@0: exports.activate = function activate() setMode(true); michael@0: exports.deactivate = function deactivate() setMode(false); michael@0: michael@0: exports.on = deprecateEvents(on.bind(null, exports)); michael@0: exports.once = deprecateEvents(once.bind(null, exports)); michael@0: exports.removeListener = deprecateEvents(function removeListener(type, listener) { michael@0: // Note: We can't just bind `off` as we do it for other methods cause skipping michael@0: // a listener argument will remove all listeners for the given event type michael@0: // causing misbehavior. This way we make sure all arguments are passed. michael@0: off(exports, type, listener); michael@0: }); michael@0: michael@0: exports.isPrivate = function(thing) { michael@0: // if thing is defined, and we can find a window for it michael@0: // then check if the window is private michael@0: if (!!thing) { michael@0: // if the thing is a window, and the window is private michael@0: // then return true michael@0: if (isWindowPrivate(thing)) { michael@0: return true; michael@0: } michael@0: michael@0: // does the thing have an associated tab? michael@0: // page-mod instances do.. michael@0: if (thing.tab) { michael@0: let tabWindow = getOwnerWindow(thing.tab); michael@0: if (tabWindow) { michael@0: let isThingPrivate = isWindowPrivate(tabWindow); michael@0: if (isThingPrivate) michael@0: return isThingPrivate; michael@0: } michael@0: } michael@0: michael@0: // can we find an associated window? michael@0: let window = getOwnerWindow(thing); michael@0: if (window) michael@0: return isWindowPrivate(window); michael@0: michael@0: try { michael@0: let { isChannelPrivate } = thing.QueryInterface(Ci.nsIPrivateBrowsingChannel); michael@0: if (isChannelPrivate) michael@0: return true; michael@0: } catch(e) {} michael@0: } michael@0: michael@0: // check if the post pwpb, global pb service is enabled. michael@0: if (isPermanentPrivateBrowsing()) michael@0: return true; michael@0: michael@0: // if we get here, and global private browsing michael@0: // is available, and it is true, then return michael@0: // true otherwise false is returned here michael@0: return getMode(); michael@0: }; michael@0: michael@0: function deprecateEvents(func) deprecateEvent( michael@0: func, michael@0: 'The require("sdk/private-browsing") module\'s "start" and "stop" events ' + michael@0: 'are deprecated.', michael@0: ['start', 'stop'] michael@0: ); michael@0: michael@0: // Make sure listeners are cleaned up. michael@0: unload(function() off(exports));