addon-sdk/source/lib/sdk/tabs/tabs-firefox.js

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4 'use strict';
michael@0 5
michael@0 6 // TODO: BUG 792670 - remove dependency below
michael@0 7 const { browserWindows: windows } = require('../windows');
michael@0 8 const { tabs } = require('../windows/tabs-firefox');
michael@0 9 const { isPrivate } = require('../private-browsing');
michael@0 10 const { isWindowPBSupported } = require('../private-browsing/utils')
michael@0 11 const { isPrivateBrowsingSupported } = require('sdk/self');
michael@0 12
michael@0 13 const supportPrivateTabs = isPrivateBrowsingSupported && isWindowPBSupported;
michael@0 14
michael@0 15 function newTabWindow(options) {
michael@0 16 // `tabs` option is under review and may be removed.
michael@0 17 return windows.open({
michael@0 18 tabs: [ options ],
michael@0 19 isPrivate: options.isPrivate
michael@0 20 });
michael@0 21 }
michael@0 22
michael@0 23 Object.defineProperties(tabs, {
michael@0 24 open: { value: function open(options) {
michael@0 25 if (options.inNewWindow) {
michael@0 26 newTabWindow(options);
michael@0 27 return undefined;
michael@0 28 }
michael@0 29
michael@0 30 let activeWindow = windows.activeWindow;
michael@0 31 let privateState = (supportPrivateTabs && (options.isPrivate || isPrivate(activeWindow))) || false;
michael@0 32
michael@0 33 // if the active window is in the state that we need then use it
michael@0 34 if (activeWindow && (!supportPrivateTabs || privateState === isPrivate(activeWindow))) {
michael@0 35 activeWindow.tabs.open(options);
michael@0 36 }
michael@0 37 else {
michael@0 38 // find a window in the state that we need
michael@0 39 let window = getWindow(privateState);
michael@0 40 if (window) {
michael@0 41 window.tabs.open(options);
michael@0 42 }
michael@0 43 // open a window in the state that we need
michael@0 44 else {
michael@0 45 newTabWindow(options);
michael@0 46 }
michael@0 47 }
michael@0 48
michael@0 49 return undefined;
michael@0 50 }}
michael@0 51 });
michael@0 52
michael@0 53 function getWindow(privateState) {
michael@0 54 for each (let window in windows) {
michael@0 55 if (privateState === isPrivate(window)) {
michael@0 56 return window;
michael@0 57 }
michael@0 58 }
michael@0 59 return null;
michael@0 60 }
michael@0 61
michael@0 62 // Workaround for bug 674195. Freezing objects from other compartments fail,
michael@0 63 // so we use `Object.freeze` from the same component as objects
michael@0 64 // `hasOwnProperty`. Since `hasOwnProperty` here will be from other component
michael@0 65 // we override it to support our workaround.
michael@0 66 module.exports = Object.create(tabs, {
michael@0 67 isPrototypeOf: { value: Object.prototype.isPrototypeOf }
michael@0 68 });

mercurial