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: // TODO: BUG 792670 - remove dependency below michael@0: const { browserWindows: windows } = require('../windows'); michael@0: const { tabs } = require('../windows/tabs-firefox'); michael@0: const { isPrivate } = require('../private-browsing'); michael@0: const { isWindowPBSupported } = require('../private-browsing/utils') michael@0: const { isPrivateBrowsingSupported } = require('sdk/self'); michael@0: michael@0: const supportPrivateTabs = isPrivateBrowsingSupported && isWindowPBSupported; michael@0: michael@0: function newTabWindow(options) { michael@0: // `tabs` option is under review and may be removed. michael@0: return windows.open({ michael@0: tabs: [ options ], michael@0: isPrivate: options.isPrivate michael@0: }); michael@0: } michael@0: michael@0: Object.defineProperties(tabs, { michael@0: open: { value: function open(options) { michael@0: if (options.inNewWindow) { michael@0: newTabWindow(options); michael@0: return undefined; michael@0: } michael@0: michael@0: let activeWindow = windows.activeWindow; michael@0: let privateState = (supportPrivateTabs && (options.isPrivate || isPrivate(activeWindow))) || false; michael@0: michael@0: // if the active window is in the state that we need then use it michael@0: if (activeWindow && (!supportPrivateTabs || privateState === isPrivate(activeWindow))) { michael@0: activeWindow.tabs.open(options); michael@0: } michael@0: else { michael@0: // find a window in the state that we need michael@0: let window = getWindow(privateState); michael@0: if (window) { michael@0: window.tabs.open(options); michael@0: } michael@0: // open a window in the state that we need michael@0: else { michael@0: newTabWindow(options); michael@0: } michael@0: } michael@0: michael@0: return undefined; michael@0: }} michael@0: }); michael@0: michael@0: function getWindow(privateState) { michael@0: for each (let window in windows) { michael@0: if (privateState === isPrivate(window)) { michael@0: return window; michael@0: } michael@0: } michael@0: return null; michael@0: } michael@0: michael@0: // Workaround for bug 674195. Freezing objects from other compartments fail, michael@0: // so we use `Object.freeze` from the same component as objects michael@0: // `hasOwnProperty`. Since `hasOwnProperty` here will be from other component michael@0: // we override it to support our workaround. michael@0: module.exports = Object.create(tabs, { michael@0: isPrototypeOf: { value: Object.prototype.isPrototypeOf } michael@0: });