-
Notifications
You must be signed in to change notification settings - Fork 273
/
handle-new-tabs.js
386 lines (353 loc) · 13.8 KB
/
handle-new-tabs.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
/*
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
'use strict';
import {
log as internalLogger,
dumpTab,
configs
} from '/common/common.js';
import * as Constants from '/common/constants.js';
import * as TabsStore from '/common/tabs-store.js';
import * as TabsInternalOperation from '/common/tabs-internal-operation.js';
import * as TreeBehavior from '/common/tree-behavior.js';
import Tab from '/common/Tab.js';
import * as TabsOpen from './tabs-open.js';
import * as TabsMove from './tabs-move.js';
import * as Tree from './tree.js';
function log(...args) {
internalLogger('background/handle-new-tabs', ...args);
}
// this should return false if the tab is / may be moved while processing
Tab.onCreating.addListener((tab, info = {}) => {
if (info.duplicatedInternally)
return true;
log('Tabs.onCreating ', dumpTab(tab), info);
const possibleOpenerTab = info.activeTab || Tab.getActiveTab(tab.windowId);
const opener = tab.$TST.openerTab;
if (opener) {
tab.$TST.setAttribute(Constants.kPERSISTENT_ORIGINAL_OPENER_TAB_ID, opener.$TST.uniqueId.id);
TabsStore.addToBeGroupedTab(tab);
}
else {
let dontMove = false;
if (!info.maybeOrphan &&
possibleOpenerTab &&
!info.restored) {
let autoAttachBehavior = configs.autoAttachOnNewTabCommand;
if (tab.$TST.nextTab &&
possibleOpenerTab == tab.$TST.previousTab) {
// New tab opened with browser.tabs.insertAfterCurrent=true may have
// next tab. In this case the tab is expected to be placed next to the
// active tab always, so we should change the behavior specially.
// See also:
// https://github.com/piroor/treestyletab/issues/2054
// https://github.com/piroor/treestyletab/issues/2194#issuecomment-505272940
dontMove = true;
switch (autoAttachBehavior) {
case Constants.kNEWTAB_OPEN_AS_ORPHAN:
case Constants.kNEWTAB_OPEN_AS_SIBLING:
case Constants.kNEWTAB_OPEN_AS_NEXT_SIBLING:
if (possibleOpenerTab.$TST.hasChild)
autoAttachBehavior = Constants.kNEWTAB_OPEN_AS_CHILD;
else
autoAttachBehavior = Constants.kNEWTAB_OPEN_AS_NEXT_SIBLING;
break;
case Constants.kNEWTAB_OPEN_AS_CHILD:
default:
break;
}
}
if (tab.$TST.isNewTabCommandTab) {
if (!info.positionedBySelf) {
log('behave as a tab opened by new tab command');
return handleNewTabFromActiveTab(tab, {
activeTab: possibleOpenerTab,
autoAttachBehavior,
dontMove,
inheritContextualIdentityMode: configs.inheritContextualIdentityToChildTabMode
}).then(moved => !moved);
}
return false;
}
else if (possibleOpenerTab != tab) {
tab.$TST.possibleOpenerTab = possibleOpenerTab.id;
}
tab.$TST.isNewTab = !info.fromExternal;
}
if (info.fromExternal) {
log('behave as a tab opened from external application');
// we may need to reopen the tab with loaded URL
if (configs.inheritContextualIdentityToTabsFromExternalMode != Constants.kCONTEXTUAL_IDENTITY_DEFAULT)
tab.$TST.fromExternal = true;
return Tree.behaveAutoAttachedTab(tab, {
baseTab: possibleOpenerTab,
behavior: configs.autoAttachOnOpenedFromExternal,
dontMove,
broadcast: true
}).then(moved => !moved);
}
log('behave as a tab opened with any URL');
tab.$TST.positionedBySelf = info.positionedBySelf;
return true;
}
log(`opener: ${dumpTab(opener)}, positionedBySelf = ${info.positionedBySelf}`);
if (opener && opener.pinned &&
opener.windowId == tab.windowId) {
return handleTabsFromPinnedOpener(tab, opener).then(moved => !moved);
}
else if (!info.maybeOrphan) {
if (info.fromExternal &&
configs.inheritContextualIdentityToTabsFromExternalMode != Constants.kCONTEXTUAL_IDENTITY_DEFAULT)
tab.$TST.fromExternal = true;
return Tree.behaveAutoAttachedTab(tab, {
baseTab: opener,
behavior: info.fromExternal ? configs.autoAttachOnOpenedFromExternal : configs.autoAttachOnOpenedWithOwner,
dontMove: info.positionedBySelf || info.mayBeReplacedWithContainer,
broadcast: true
}).then(moved => !moved);
}
return true;
});
async function handleNewTabFromActiveTab(tab, params = {}) {
const activeTab = params.activeTab;
log('handleNewTabFromActiveTab: activeTab = ', dumpTab(activeTab), params);
if (activeTab &&
activeTab.$TST.ancestors.includes(tab)) {
log(' => ignore restored ancestor tab');
return false;
}
const moved = await Tree.behaveAutoAttachedTab(tab, {
baseTab: activeTab,
behavior: params.autoAttachBehavior,
broadcast: true,
dontMove: params.dontMove || false
});
if (tab.cookieStoreId && tab.cookieStoreId != 'firefox-default') {
log('handleNewTabFromActiveTab: do not reopen tab opened with non-default contextual identity ', tab.cookieStoreId);
return moved;
}
const parent = tab.$TST.parent;
let cookieStoreId = null;
switch (params.inheritContextualIdentityMode) {
case Constants.kCONTEXTUAL_IDENTITY_FROM_PARENT:
if (parent)
cookieStoreId = parent.cookieStoreId;
break;
case Constants.kCONTEXTUAL_IDENTITY_FROM_LAST_ACTIVE:
cookieStoreId = activeTab.cookieStoreId
break;
default:
return moved;
}
if ((tab.cookieStoreId || 'firefox-default') == (cookieStoreId || 'firefox-default')) {
log('handleNewTabFromActiveTab: no need to reopen with inherited contextual identity ', cookieStoreId);
return moved;
}
log('handleNewTabFromActiveTab: reopen with inherited contextual identity ', cookieStoreId);
// We need to prevent grouping of this original tab and the reopened tab
// by the "multiple tab opened in XXX msec" feature.
const window = TabsStore.windows.get(tab.windowId);
window.openedNewTabs.delete(tab.id);
await TabsOpen.openURIInTab(params.url || null, {
windowId: activeTab.windowId,
parent,
insertBefore: tab,
cookieStoreId
});
TabsInternalOperation.removeTab(tab);
return moved;
}
async function handleTabsFromPinnedOpener(tab, opener) {
const parent = Tab.getGroupTabForOpener(opener);
if (parent) {
log('handleTabsFromPinnedOpener: attach to corresponding group tab');
tab.$TST.setAttribute(Constants.kPERSISTENT_ALREADY_GROUPED_FOR_PINNED_OPENER, true);
tab.$TST.alreadyMovedAsOpenedFromPinnedOpener = true;
// it could be updated already...
const lastRelatedTab = opener.$TST.lastRelatedTabId == tab.id ? opener.$TST.previousLastRelatedTab : opener.$TST.lastRelatedTab;
return Tree.attachTabTo(tab, parent, {
lastRelatedTab,
forceExpand: true, // this is required to avoid the group tab itself is active from active tab in collapsed tree
broadcast: true
});
}
if (configs.autoGroupNewTabsFromPinned &&
tab.$TST.needToBeGroupedSiblings.length > 0) {
log('handleTabsFromPinnedOpener: controlled by auto-grouping');
return false;
}
switch (configs.insertNewTabFromPinnedTabAt) {
case Constants.kINSERT_NEXT_TO_LAST_RELATED_TAB: {
const lastRelatedTab = opener.$TST.lastRelatedTab;
if (lastRelatedTab) {
log(`handleTabsFromPinnedOpener: place after last related tab ${dumpTab(lastRelatedTab)}`);
tab.$TST.alreadyMovedAsOpenedFromPinnedOpener = true;
return TabsMove.moveTabAfter(tab, lastRelatedTab.$TST.lastDescendant || lastRelatedTab, {
delayedMove: true,
broadcast: true
});
}
};
case Constants.kINSERT_TOP: {
const lastPinnedTab = Tab.getLastPinnedTab(tab.windowId);
if (lastPinnedTab) {
log(`handleTabsFromPinnedOpener: opened from pinned opener: place after last pinned tab ${dumpTab(lastPinnedTab)}`);
tab.$TST.alreadyMovedAsOpenedFromPinnedOpener = true;
return TabsMove.moveTabAfter(tab, lastPinnedTab, {
delayedMove: true,
broadcast: true
});
}
const firstNormalTab = Tab.getFirstNormalTab(tab.windowId);
if (firstNormalTab) {
log(`handleTabsFromPinnedOpener: opened from pinned opener: place before first pinned tab ${dumpTab(firstNormalTab)}`);
tab.$TST.alreadyMovedAsOpenedFromPinnedOpener = true;
return TabsMove.moveTabBefore(tab, firstNormalTab, {
delayedMove: true,
broadcast: true
});
}
}; break;
case Constants.kINSERT_END: {
const lastTab = Tab.getLastTab(tab.windowId);
log('handleTabsFromPinnedOpener: opened from pinned opener: place after the last tab ', lastTab);
tab.$TST.alreadyMovedAsOpenedFromPinnedOpener = true;
return TabsMove.moveTabAfter(tab, lastTab, {
delayedMove: true,
broadcast: true
});
};
}
return Promise.resolve(false);
}
Tab.onCreated.addListener((tab, info = {}) => {
if (!info.duplicated)
return;
const original = info.originalTab;
log('duplicated ', dumpTab(tab), dumpTab(original));
if (info.duplicatedInternally) {
log('duplicated by internal operation');
tab.$TST.addState(Constants.kTAB_STATE_DUPLICATING, { broadcast: true });
TabsStore.addDuplicatingTab(tab);
}
else {
Tree.behaveAutoAttachedTab(tab, {
baseTab: original,
behavior: configs.autoAttachOnDuplicated,
dontMove: info.positionedBySelf || info.movedBySelfWhileCreation || info.mayBeReplacedWithContainer,
broadcast: true
});
}
});
Tab.onUpdated.addListener((tab, changeInfo) => {
if ('openerTabId' in changeInfo &&
configs.syncParentTabAndOpenerTab &&
!tab.$TST.updatingOpenerTabIds.includes(changeInfo.openerTabId) /* accept only changes from outside of TST */) {
Tab.waitUntilTrackedAll(tab.windowId).then(() => {
const parent = tab.$TST.openerTab;
if (!parent ||
parent.windowId != tab.windowId ||
parent == tab.$TST.parent)
return;
Tree.attachTabTo(tab, parent, {
insertAt: Constants.kINSERT_NEAREST,
forceExpand: tab.active,
broadcast: true
});
});
}
if (tab.$TST.openedCompletely &&
(changeInfo.url || changeInfo.status == 'complete') &&
(tab.$TST.isNewTab || tab.$TST.fromExternal)) {
log('loaded tab ', dumpTab(tab), { isNewTab: tab.$TST.isNewTab, fromExternal: tab.$TST.fromExternal });
delete tab.$TST.isNewTab;
const possibleOpenerTab = Tab.get(tab.$TST.possibleOpenerTab);
delete tab.$TST.possibleOpenerTab;
log('possibleOpenerTab ', dumpTab(possibleOpenerTab));
if (tab.$TST.fromExternal) {
delete tab.$TST.fromExternal;
log('behave as a tab opened from external application (delayed)');
handleNewTabFromActiveTab(tab, {
url: tab.url,
activeTab: possibleOpenerTab,
autoAttachBehavior: configs.autoAttachOnOpenedFromExternal,
inheritContextualIdentityMode: configs.inheritContextualIdentityToTabsFromExternalMode
});
return;
}
const window = TabsStore.windows.get(tab.windowId);
log('window.openedNewTabs ', window.openedNewTabs);
if (tab.$TST.parent ||
!possibleOpenerTab ||
window.openedNewTabs.has(tab.id) ||
tab.$TST.openedWithOthers ||
tab.$TST.positionedBySelf) {
log(' => no need to control');
return;
}
if (tab.$TST.isNewTabCommandTab) {
log('behave as a tab opened by new tab command (delayed)');
handleNewTabFromActiveTab(tab, {
activeTab: possibleOpenerTab,
autoAttachBehavior: configs.autoAttachOnNewTabCommand,
inheritContextualIdentityMode: configs.inheritContextualIdentityToChildTabMode
});
return;
}
const siteMatcher = /^\w+:\/\/([^\/]+)(?:$|\/.*$)/;
const openerTabSite = possibleOpenerTab.url.match(siteMatcher);
const newTabSite = tab.url.match(siteMatcher);
if (openerTabSite && newTabSite && openerTabSite[1] == newTabSite[1]) {
log('behave as a tab opened from same site (delayed)');
handleNewTabFromActiveTab(tab, {
url: tab.url,
activeTab: possibleOpenerTab,
autoAttachBehavior: configs.autoAttachSameSiteOrphan,
inheritContextualIdentityMode: configs.inheritContextualIdentityToSameSiteOrphanMode
});
return;
}
log('checking special openers (delayed)', { opener: possibleOpenerTab.url, child: tab.url });
for (const rule of Constants.kAGGRESSIVE_OPENER_TAB_DETECTION_RULES_WITH_URL) {
if (rule.opener.test(possibleOpenerTab.url) &&
rule.child.test(tab.url)) {
log('behave as a tab opened from special opener (delayed)', { rule });
handleNewTabFromActiveTab(tab, {
url: tab.url,
activeTab: possibleOpenerTab,
autoAttachBehavior: Constants.kNEWTAB_OPEN_AS_CHILD
});
return;
}
}
}
});
Tab.onAttached.addListener(async (tab, info = {}) => {
if (!info.windowId ||
!TreeBehavior.shouldApplyTreeBehavior(info))
return;
log('Tabs.onAttached ', dumpTab(tab), info);
log('descendants of attached tab: ', () => info.descendants.map(dumpTab));
const movedTabs = await Tree.moveTabs(info.descendants, {
destinationWindowId: tab.windowId,
insertAfter: tab
});
log('moved descendants: ', () => movedTabs.map(dumpTab));
if (info.descendants.length == movedTabs.length) {
await Tree.applyTreeStructureToTabs(
[tab, ...movedTabs],
info.structure
);
}
else {
for (const movedTab of movedTabs) {
Tree.attachTabTo(movedTab, tab, {
broadcast: true,
dontMove: true
});
}
}
});