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