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

branch
TOR_BUG_3246
changeset 7
129ffea94266
equal deleted inserted replaced
-1:000000000000 0:6a999883e0dc
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 // TODO: BUG 792670 - remove dependency below
7 const { browserWindows: windows } = require('../windows');
8 const { tabs } = require('../windows/tabs-firefox');
9 const { isPrivate } = require('../private-browsing');
10 const { isWindowPBSupported } = require('../private-browsing/utils')
11 const { isPrivateBrowsingSupported } = require('sdk/self');
12
13 const supportPrivateTabs = isPrivateBrowsingSupported && isWindowPBSupported;
14
15 function newTabWindow(options) {
16 // `tabs` option is under review and may be removed.
17 return windows.open({
18 tabs: [ options ],
19 isPrivate: options.isPrivate
20 });
21 }
22
23 Object.defineProperties(tabs, {
24 open: { value: function open(options) {
25 if (options.inNewWindow) {
26 newTabWindow(options);
27 return undefined;
28 }
29
30 let activeWindow = windows.activeWindow;
31 let privateState = (supportPrivateTabs && (options.isPrivate || isPrivate(activeWindow))) || false;
32
33 // if the active window is in the state that we need then use it
34 if (activeWindow && (!supportPrivateTabs || privateState === isPrivate(activeWindow))) {
35 activeWindow.tabs.open(options);
36 }
37 else {
38 // find a window in the state that we need
39 let window = getWindow(privateState);
40 if (window) {
41 window.tabs.open(options);
42 }
43 // open a window in the state that we need
44 else {
45 newTabWindow(options);
46 }
47 }
48
49 return undefined;
50 }}
51 });
52
53 function getWindow(privateState) {
54 for each (let window in windows) {
55 if (privateState === isPrivate(window)) {
56 return window;
57 }
58 }
59 return null;
60 }
61
62 // Workaround for bug 674195. Freezing objects from other compartments fail,
63 // so we use `Object.freeze` from the same component as objects
64 // `hasOwnProperty`. Since `hasOwnProperty` here will be from other component
65 // we override it to support our workaround.
66 module.exports = Object.create(tabs, {
67 isPrototypeOf: { value: Object.prototype.isPrototypeOf }
68 });

mercurial