tabs-firefox.js 2.1 KB

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