tabs-firefox.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. module.metadata = {
  6. "stability": "unstable"
  7. };
  8. const { Trait } = require("../deprecated/traits");
  9. const { List } = require("../deprecated/list");
  10. const { Tab } = require("../tabs/tab");
  11. const { EventEmitter } = require("../deprecated/events");
  12. const { EVENTS } = require("../tabs/events");
  13. const { getOwnerWindow, getActiveTab, getTabs,
  14. openTab } = require("../tabs/utils");
  15. const { Options } = require("../tabs/common");
  16. const { observer: tabsObserver } = require("../tabs/observer");
  17. const { ignoreWindow } = require("../private-browsing/utils");
  18. const TAB_BROWSER = "tabbrowser";
  19. /**
  20. * This is a trait that is used in composition of window wrapper. Trait tracks
  21. * tab related events of the wrapped window in order to keep track of open
  22. * tabs and maintain their wrappers. Every new tab gets wrapped and jetpack
  23. * type event is emitted.
  24. */
  25. const WindowTabTracker = Trait.compose({
  26. /**
  27. * Chrome window whose tabs are tracked.
  28. */
  29. _window: Trait.required,
  30. /**
  31. * Function used to emit events.
  32. */
  33. _emit: EventEmitter.required,
  34. _tabOptions: Trait.required,
  35. /**
  36. * Function to add event listeners.
  37. */
  38. on: EventEmitter.required,
  39. removeListener: EventEmitter.required,
  40. /**
  41. * Initializes tab tracker for a browser window.
  42. */
  43. _initWindowTabTracker: function _initWindowTabTracker() {
  44. // Ugly hack that we have to remove at some point (see Bug 658059). At this
  45. // point it is necessary to invoke lazy `tabs` getter on the windows object
  46. // which creates a `TabList` instance.
  47. this.tabs;
  48. // Binding all methods used as event listeners to the instance.
  49. this._onTabReady = this._emitEvent.bind(this, "ready");
  50. this._onTabLoad = this._emitEvent.bind(this, "load");
  51. this._onTabPageShow = this._emitEvent.bind(this, "pageshow");
  52. this._onTabOpen = this._onTabEvent.bind(this, "open");
  53. this._onTabClose = this._onTabEvent.bind(this, "close");
  54. this._onTabActivate = this._onTabEvent.bind(this, "activate");
  55. this._onTabDeactivate = this._onTabEvent.bind(this, "deactivate");
  56. this._onTabPinned = this._onTabEvent.bind(this, "pinned");
  57. this._onTabUnpinned = this._onTabEvent.bind(this, "unpinned");
  58. for each (let tab in getTabs(this._window)) {
  59. // We emulate "open" events for all open tabs since gecko does not emits
  60. // them on the tabs that new windows are open with. Also this is
  61. // necessary to synchronize tabs lists with an actual state.
  62. this._onTabOpen(tab);
  63. }
  64. // We also emulate "activate" event so that it's picked up by a tab list.
  65. this._onTabActivate(getActiveTab(this._window));
  66. // Setting up event listeners
  67. tabsObserver.on("open", this._onTabOpen);
  68. tabsObserver.on("close", this._onTabClose);
  69. tabsObserver.on("activate", this._onTabActivate);
  70. tabsObserver.on("deactivate", this._onTabDeactivate);
  71. tabsObserver.on("pinned", this._onTabPinned);
  72. tabsObserver.on("unpinned", this._onTabUnpinned);
  73. },
  74. _destroyWindowTabTracker: function _destroyWindowTabTracker() {
  75. // We emulate close events on all tabs, since gecko does not emits such
  76. // events by itself.
  77. for each (let tab in this.tabs)
  78. this._emitEvent("close", tab);
  79. this._tabs._clear();
  80. tabsObserver.removeListener("open", this._onTabOpen);
  81. tabsObserver.removeListener("close", this._onTabClose);
  82. tabsObserver.removeListener("activate", this._onTabActivate);
  83. tabsObserver.removeListener("deactivate", this._onTabDeactivate);
  84. },
  85. _onTabEvent: function _onTabEvent(type, tab) {
  86. // Accept only tabs for the watched window, and ignore private tabs
  87. // if addon doesn't have private permission
  88. if (this._window === getOwnerWindow(tab) && !ignoreWindow(this._window)) {
  89. let options = this._tabOptions.shift() || {};
  90. options.tab = tab;
  91. options.window = this._public;
  92. // Ignore zombie tabs on open that have already been removed
  93. if (type == "open" && !tab.linkedBrowser)
  94. return;
  95. // Create a tab wrapper on open event, otherwise, just fetch existing
  96. // tab object
  97. let wrappedTab = Tab(options, type !== "open");
  98. if (!wrappedTab)
  99. return;
  100. // Setting up an event listener for ready events.
  101. if (type === "open") {
  102. wrappedTab.on("ready", this._onTabReady);
  103. wrappedTab.on("load", this._onTabLoad);
  104. wrappedTab.on("pageshow", this._onTabPageShow);
  105. }
  106. this._emitEvent(type, wrappedTab);
  107. }
  108. },
  109. _emitEvent: function _emitEvent(type, tag) {
  110. // Slices additional arguments and passes them into exposed
  111. // listener like other events (for pageshow)
  112. let args = Array.slice(arguments);
  113. // Notifies combined tab list that tab was added / removed.
  114. tabs._emit.apply(tabs, args);
  115. // Notifies contained tab list that window was added / removed.
  116. this._tabs._emit.apply(this._tabs, args);
  117. }
  118. });
  119. exports.WindowTabTracker = WindowTabTracker;
  120. /**
  121. * This trait is used to create live representation of open tab lists. Each
  122. * window wrapper's tab list is represented by an object created from this
  123. * trait. It is also used to represent list of all the open windows. Trait is
  124. * composed out of `EventEmitter` in order to emit 'TabOpen', 'TabClose' events.
  125. * **Please note** that objects created by this trait can't be exposed outside
  126. * instead you should expose it's `_public` property, see comments in
  127. * constructor for details.
  128. */
  129. const TabList = List.resolve({ constructor: "_init" }).compose(
  130. // This is ugly, but necessary. Will be removed by #596248
  131. EventEmitter.resolve({ toString: null }),
  132. Trait.compose({
  133. on: Trait.required,
  134. _emit: Trait.required,
  135. constructor: function TabList(options) {
  136. this._window = options.window;
  137. // Add new items to the list
  138. this.on(EVENTS.open.name, this._add.bind(this));
  139. // Remove closed items from the list
  140. this.on(EVENTS.close.name, this._remove.bind(this));
  141. // Set value whenever new tab becomes active.
  142. this.on("activate", function onTabActivate(tab) {
  143. this._activeTab = tab;
  144. }.bind(this));
  145. // Initialize list.
  146. this._init();
  147. // This list is not going to emit any events, object holding this list
  148. // will do it instead, to make that possible we return a private API.
  149. return this;
  150. },
  151. get activeTab() this._activeTab,
  152. _activeTab: null,
  153. open: function open(options) {
  154. let window = this._window;
  155. let chromeWindow = window._window;
  156. options = Options(options);
  157. // save the tab options
  158. window._tabOptions.push(options);
  159. // open the tab
  160. let tab = openTab(chromeWindow, options.url, options);
  161. }
  162. // This is ugly, but necessary. Will be removed by #596248
  163. }).resolve({ toString: null })
  164. );
  165. /**
  166. * Combined list of all open tabs on all the windows.
  167. * type {TabList}
  168. */
  169. var tabs = TabList({ window: null });
  170. exports.tabs = tabs._public;
  171. /**
  172. * Trait is a part of composition that represents window wrapper. This trait is
  173. * composed out of `WindowTabTracker` that allows it to keep track of open tabs
  174. * on the window being wrapped.
  175. */
  176. const WindowTabs = Trait.compose(
  177. WindowTabTracker,
  178. Trait.compose({
  179. _window: Trait.required,
  180. /**
  181. * List of tabs
  182. */
  183. get tabs() (this._tabs || (this._tabs = TabList({ window: this })))._public,
  184. _tabs: null,
  185. })
  186. );
  187. exports.WindowTabs = WindowTabs;