tabs-fennec.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. const { Class } = require('../core/heritage');
  6. const { Tab } = require('../tabs/tab');
  7. const { browserWindows } = require('./fennec');
  8. const { windowNS } = require('../window/namespace');
  9. const { tabsNS, tabNS } = require('../tabs/namespace');
  10. const { openTab, getTabs, getSelectedTab, getTabForBrowser: getRawTabForBrowser,
  11. getTabContentWindow } = require('../tabs/utils');
  12. const { Options } = require('../tabs/common');
  13. const { getTabForBrowser, getTabForRawTab } = require('../tabs/helpers');
  14. const { on, once, off, emit } = require('../event/core');
  15. const { method } = require('../lang/functional');
  16. const { EVENTS } = require('../tabs/events');
  17. const { EventTarget } = require('../event/target');
  18. const { when: unload } = require('../system/unload');
  19. const { windowIterator } = require('../deprecated/window-utils');
  20. const { List, addListItem, removeListItem } = require('../util/list');
  21. const { isPrivateBrowsingSupported } = require('../self');
  22. const { isTabPBSupported, ignoreWindow } = require('../private-browsing/utils');
  23. const mainWindow = windowNS(browserWindows.activeWindow).window;
  24. const ERR_FENNEC_MSG = 'This method is not yet supported by Fennec';
  25. const supportPrivateTabs = isPrivateBrowsingSupported && isTabPBSupported;
  26. const Tabs = Class({
  27. implements: [ List ],
  28. extends: EventTarget,
  29. initialize: function initialize(options) {
  30. let tabsInternals = tabsNS(this);
  31. let window = tabsNS(this).window = options.window || mainWindow;
  32. EventTarget.prototype.initialize.call(this, options);
  33. List.prototype.initialize.apply(this, getTabs(window).map(Tab));
  34. // TabOpen event
  35. window.BrowserApp.deck.addEventListener(EVENTS.open.dom, onTabOpen, false);
  36. // TabSelect
  37. window.BrowserApp.deck.addEventListener(EVENTS.activate.dom, onTabSelect, false);
  38. },
  39. get activeTab() {
  40. return getTabForRawTab(getSelectedTab(tabsNS(this).window));
  41. },
  42. open: function(options) {
  43. options = Options(options);
  44. let activeWin = browserWindows.activeWindow;
  45. if (options.isPinned) {
  46. console.error(ERR_FENNEC_MSG); // TODO
  47. }
  48. let rawTab = openTab(windowNS(activeWin).window, options.url, {
  49. inBackground: options.inBackground,
  50. isPrivate: supportPrivateTabs && options.isPrivate
  51. });
  52. // by now the tab has been created
  53. let tab = getTabForRawTab(rawTab);
  54. if (options.onClose)
  55. tab.on('close', options.onClose);
  56. if (options.onOpen) {
  57. // NOTE: on Fennec this will be true
  58. if (tabNS(tab).opened)
  59. options.onOpen(tab);
  60. tab.on('open', options.onOpen);
  61. }
  62. if (options.onReady)
  63. tab.on('ready', options.onReady);
  64. if (options.onPageShow)
  65. tab.on('pageshow', options.onPageShow);
  66. if (options.onActivate)
  67. tab.on('activate', options.onActivate);
  68. return tab;
  69. }
  70. });
  71. let gTabs = exports.tabs = Tabs(mainWindow);
  72. function tabsUnloader(event, window) {
  73. window = window || (event && event.target);
  74. if (!(window && window.BrowserApp))
  75. return;
  76. window.BrowserApp.deck.removeEventListener(EVENTS.open.dom, onTabOpen, false);
  77. window.BrowserApp.deck.removeEventListener(EVENTS.activate.dom, onTabSelect, false);
  78. }
  79. // unload handler
  80. unload(function() {
  81. for (let window in windowIterator()) {
  82. tabsUnloader(null, window);
  83. }
  84. });
  85. function addTab(tab) {
  86. addListItem(gTabs, tab);
  87. return tab;
  88. }
  89. function removeTab(tab) {
  90. removeListItem(gTabs, tab);
  91. return tab;
  92. }
  93. // TabOpen
  94. function onTabOpen(event) {
  95. let browser = event.target;
  96. // Eventually ignore private tabs
  97. if (ignoreWindow(browser.contentWindow))
  98. return;
  99. let tab = getTabForBrowser(browser);
  100. if (tab === null) {
  101. let rawTab = getRawTabForBrowser(browser);
  102. // create a Tab instance for this new tab
  103. tab = addTab(Tab(rawTab));
  104. }
  105. tabNS(tab).opened = true;
  106. tab.on('ready', function() emit(gTabs, 'ready', tab));
  107. tab.once('close', onTabClose);
  108. tab.on('pageshow', function(_tab, persisted)
  109. emit(gTabs, 'pageshow', tab, persisted));
  110. emit(tab, 'open', tab);
  111. emit(gTabs, 'open', tab);
  112. }
  113. // TabSelect
  114. function onTabSelect(event) {
  115. let browser = event.target;
  116. // Eventually ignore private tabs
  117. if (ignoreWindow(browser.contentWindow))
  118. return;
  119. // Set value whenever new tab becomes active.
  120. let tab = getTabForBrowser(browser);
  121. emit(tab, 'activate', tab);
  122. emit(gTabs, 'activate', tab);
  123. for each (let t in gTabs) {
  124. if (t === tab) continue;
  125. emit(t, 'deactivate', t);
  126. emit(gTabs, 'deactivate', t);
  127. }
  128. }
  129. // TabClose
  130. function onTabClose(tab) {
  131. removeTab(tab);
  132. emit(gTabs, EVENTS.close.name, tab);
  133. }