This repository was archived by the owner on Dec 11, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 970
/
Copy pathframeStateUtil.js
692 lines (591 loc) · 20.1 KB
/
frameStateUtil.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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
/* 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/. */
const Immutable = require('immutable')
// Constants
const config = require('../constants/config')
const settings = require('../constants/settings')
// Actions
const windowActions = require('../actions/windowActions')
const webviewActions = require('../actions/webviewActions')
const appActions = require('../actions/appActions')
// State
const {makeImmutable} = require('../../app/common/state/immutableUtil')
// Utils
const {getSetting} = require('../settings')
const {isIntermediateAboutPage} = require('../lib/appUrlUtil')
const urlParse = require('../../app/common/urlParse')
let tabPageHoverTimeout
let tabHoverTimeout = null
const comparatorByKeyAsc = (a, b) => a.get('key') > b.get('key')
? 1 : b.get('key') > a.get('key') ? -1 : 0
const matchFrame = (queryInfo, frame) => {
queryInfo = queryInfo.toJS ? queryInfo.toJS() : queryInfo
return !Object.keys(queryInfo).map((queryKey) => (frame.get(queryKey) === queryInfo[queryKey])).includes(false)
}
function query (state, queryInfo) {
return state.get('frames').filter(matchFrame.bind(null, queryInfo))
}
function find (state, queryInfo) {
return state.get('frames').find(matchFrame.bind(null, queryInfo))
}
function isFrameKeyActive (state, frameKey) {
return getActiveFrameKey(state) === frameKey
}
function getFrames (state) {
return state.get('frames')
}
function getSortedFrames (state) {
return state.get('frames').sort(comparatorByKeyAsc)
}
function getPinnedFrames (state) {
return state.get('frames').filter((frame) => frame.get('pinnedLocation'))
}
function getNonPinnedFrames (state) {
return state.get('frames').filter((frame) => !frame.get('pinnedLocation'))
}
function getFrameIndex (state, key) {
return getFramesInternalIndex(state, key)
}
function getActiveFrameIndex (state) {
return getFrameIndex(state, getActiveFrameKey(state))
}
function getActiveFrameTabId (state) {
const activeFrame = getActiveFrame(state)
return activeFrame && activeFrame.get('tabId')
}
function getFrameByIndex (state, i) {
if (i === -1) {
return null
}
return state.getIn(['frames', i])
}
// This will eventually go away fully when we replace frameKey by tabId
function getFrameKeyByTabId (state, tabId) {
let parentFrameKey = null
const openerFrame = getFrameByTabId(state, tabId)
if (openerFrame) {
parentFrameKey = openerFrame.get('key')
}
return parentFrameKey
}
function getFrameKeysByDisplayIndex (state) {
const frames = state.get('frames')
let framesByDisplayIndex = [[], []]
frames.forEach((frame) => {
let key = frame.get('key')
if (frame.get('pinnedLocation')) {
framesByDisplayIndex[0].push(key)
} else {
framesByDisplayIndex[1].push(key)
}
})
return framesByDisplayIndex.reduce(function (a, b) {
return a.concat(b)
}, [])
}
function getFrameKeysByNonPinnedDisplayIndex (state) {
return state.get('frames')
.filter((frame) => !frame.get('pinnedLocation'))
.map((frame) => frame.get('key'))
}
/**
* Obtains the display index for the specified frame key excluding pins
*/
function findNonPinnedDisplayIndexForFrameKey (state, key) {
return getFrameKeysByNonPinnedDisplayIndex(state)
.findIndex((displayKey) => displayKey === key)
}
function getFrameByDisplayIndex (state, i) {
let frames = getFrameKeysByDisplayIndex(state)
let key = frames[i]
return getFrameByKey(state, key)
}
function getFrameByKey (state, key) {
const index = getFrameIndex(state, key)
if (index === -1) {
return null
}
return state.getIn(['frames', index])
}
function isFrameSecure (frame) {
frame = makeImmutable(frame)
if (frame && typeof frame.getIn(['security', 'isSecure']) === 'boolean') {
return frame.getIn(['security', 'isSecure'])
} else {
return false
}
}
function isFrameLoading (frame) {
frame = makeImmutable(frame)
return frame && frame.get('loading')
}
function startLoadTime (frame) {
frame = makeImmutable(frame)
return frame && frame.get('startLoadTime')
}
function endLoadTime (frame) {
frame = makeImmutable(frame)
return frame && frame.get('endLoadTime')
}
function getHistory (frame) {
frame = makeImmutable(frame)
return (frame && frame.get('history')) || Immutable.fromJS([])
}
function isFrameKeyPinned (state, key) {
if (typeof key !== 'number') {
return false
}
const frame = getFrameByKey(state, key)
return frame ? frame.get('pinnedLocation') : false
}
function getNonPinnedFrameCount (state) {
return state.get('frames').filter((frame) => !frame.get('pinnedLocation')).size
}
function getFrameByTabId (state, tabId) {
return getFrameByIndex(state, getFramesInternalIndexByTabId(state, tabId))
}
function getIndexByTabId (state, tabId) {
return getFramesInternalIndexByTabId(state, tabId)
}
function getActiveFrame (state) {
const activeFrameIndex = getActiveFrameIndex(state)
return state.get('frames').get(activeFrameIndex)
}
// Returns the same as the active frame's location, but returns the requested
// URL if it's safe browsing, a cert error page or an error page.
function getLastCommittedURL (frame) {
frame = makeImmutable(frame)
if (!frame) {
return undefined
}
let location = frame.get('location')
const history = getHistory(frame)
if (isIntermediateAboutPage(location)) {
const parsedUrl = urlParse(location)
if (parsedUrl.hash) {
location = parsedUrl.hash.split('#')[1]
} else if (history.size > 0) {
location = history.last()
}
}
return location
}
function getActiveFrameKey (state) {
return state.get('activeFrameKey')
}
function getNextFrame (state) {
const activeFrameIndex = findDisplayIndexForFrameKey(state, getActiveFrameKey(state))
const index = (activeFrameIndex + 1) % state.get('frames').size
return getFrameByDisplayIndex(state, index)
}
function getPreviousFrame (state) {
const activeFrameIndex = findDisplayIndexForFrameKey(state, getActiveFrameKey(state))
const index = (state.get('frames').size + activeFrameIndex - 1) % state.get('frames').size
return getFrameByDisplayIndex(state, index)
}
/**
* Obtains the display index for the specified frame key
*/
function findDisplayIndexForFrameKey (state, key) {
return getFrameKeysByDisplayIndex(state).findIndex((displayKey) => displayKey === key)
}
/**
* Determines if the specified frame was opened from the specified
* ancestorFrameKey.
*
* For example you may go to google.com and open 3 links in new tabs:
* G g1 g2 g3
* Then you may change to g1 and open another tab:
* G g1 g1.1 g2 g3
* But then you may go back to google.com and open another tab.
* It should go like so:
* G g1 g1.1 g2 g3 g4
*/
function isAncestorFrameKey (state, frame, parentFrameKey) {
if (!frame || !frame.get('parentFrameKey')) {
return false
}
if (frame.get('parentFrameKey') === parentFrameKey) {
return true
}
// So there is a parentFrameKey but it isn't the specified one.
// Check recursively for each of the parentFrame's ancestors to see
// if we have a match.
const parentFrameIndex = getFrameIndex(state, frame.get('parentFrameKey'))
const parentFrame = state.getIn(['frames', parentFrameIndex])
if (parentFrameIndex === -1 || !parentFrame.get('parentFrameKey')) {
return false
}
return isAncestorFrameKey(state, parentFrame, parentFrameKey)
}
function getPartitionNumber (partition) {
const regex = /(?:persist:)?partition-(\d+)/
const matches = regex.exec(partition)
return Number((matches && matches[1]) || 0)
}
function isPrivatePartition (partition) {
return partition && !partition.startsWith('persist:')
}
function isSessionPartition (partition) {
return partition && partition.startsWith('persist:partition-')
}
function getPartition (frameOpts) {
return getPartitionFromNumber(frameOpts.get('partitionNumber'), frameOpts.get('isPrivate'))
}
function getPartitionFromNumber (partitionNumber, incognito) {
if (!partitionNumber && !incognito) {
return 'persist:default'
} else if (incognito) {
return 'default'
}
return `persist:partition-${partitionNumber}`
}
const frameOptsFromFrame = (frame) => {
return frame
.delete('key')
.delete('parentFrameKey')
.delete('activeShortcut')
.delete('activeShortcutDetails')
.delete('index')
.deleteIn(['navbar', 'urlbar', 'suggestions'])
}
/**
* Adds a frame specified by frameOpts and newKey and sets the activeFrameKey
* @return Immutable top level application state ready to merge back in
*/
function addFrame (state, frameOpts, newKey, partitionNumber, openInForeground, insertionIndex) {
const frames = state.get('frames')
const location = frameOpts.location // page url
const displayURL = frameOpts.displayURL == null ? location : frameOpts.displayURL
delete frameOpts.displayURL
const rendererInitiated = frameOpts.rendererInitiated
delete frameOpts.rendererInitiated
// Only add pin requests if it's not already added
const isPinned = frameOpts.isPinned
delete frameOpts.isPinned
// TODO: longer term get rid of parentFrameKey completely instead of
// calculating it here.
let parentFrameKey = frameOpts.parentFrameKey
if (frameOpts.openerTabId) {
parentFrameKey = getFrameKeyByTabId(state, frameOpts.openerTabId)
}
const frame = Immutable.fromJS(Object.assign({
zoomLevel: config.zoom.defaultValue,
audioMuted: false, // frame is muted
location,
aboutDetails: undefined,
src: location, // what the iframe src should be
tabId: frameOpts.tabId == null ? -1 : frameOpts.tabId,
loading: frameOpts.rendererInitiated,
startLoadTime: null,
endLoadTime: null,
lastAccessedTime: openInForeground ? new Date().getTime() : null,
isPrivate: false,
partitionNumber,
pinnedLocation: isPinned ? location : undefined,
key: newKey,
navbar: {
urlbar: {
location: rendererInitiated ? location : displayURL,
suggestions: {
selectedIndex: 0,
searchResults: [],
suggestionList: null
},
selected: false,
// URL load-start will focus the webview if it's not newtab.
focused: true,
active: false
}
},
searchDetail: null,
findDetail: {
searchString: '',
caseSensitivity: false
},
security: {
isSecure: null
},
unloaded: frameOpts.unloaded,
parentFrameKey,
history: []
}, frameOpts))
return {
frames: frames.splice(insertionIndex, 0, frame)
}
}
/**
* Removes a frame specified by frameProps
* @return Immutable top level application state ready to merge back in
*/
function removeFrame (state, frameProps, framePropsIndex) {
const frames = state.get('frames')
let closedFrames = state.get('closedFrames')
const newFrames = frames.splice(framePropsIndex, 1)
if (isValidClosedFrame(frameProps)) {
frameProps = frameProps.set('isFullScreen', false)
closedFrames = closedFrames.push(frameProps)
if (frameProps.get('thumbnailBlob')) {
window.URL.revokeObjectURL(frameProps.get('thumbnailBlob'))
}
if (closedFrames.size > config.maxClosedFrames) {
closedFrames = closedFrames.shift()
}
}
return {
closedFrames,
frames: newFrames
}
}
function getFrameTabPageIndex (state, frameKey, tabsPerTabPage = getSetting(settings.TABS_PER_PAGE)) {
const index = findNonPinnedDisplayIndexForFrameKey(state, frameKey)
if (index === -1) {
return -1
}
return Math.floor(index / tabsPerTabPage)
}
function onFindBarHide (frameKey) {
windowActions.setFindbarShown(frameKey, false)
webviewActions.stopFindInPage()
windowActions.setFindDetail(frameKey, Immutable.fromJS({
internalFindStatePresent: false,
numberOfMatches: -1,
activeMatchOrdinal: 0
}))
}
function getTotalBlocks (frame) {
if (!frame) {
return false
}
frame = makeImmutable(frame)
const ads = frame.getIn(['adblock', 'blocked'])
const trackers = frame.getIn(['trackingProtection', 'blocked'])
const scripts = frame.getIn(['noScript', 'blocked'])
const fingerprint = frame.getIn(['fingerprintingProtection', 'blocked'])
const blocked = (ads && ads.size ? ads.size : 0) +
(trackers && trackers.size ? trackers.size : 0) +
(scripts && scripts.size ? scripts.size : 0) +
(fingerprint && fingerprint.size ? fingerprint.size : 0)
return (blocked === 0)
? false
: ((blocked > 99) ? '99+' : blocked)
}
/**
* Check if frame is pinned or not
*/
function isPinned (state, frameKey) {
const frame = getFrameByKey(state, frameKey)
return frame && !!frame.get('pinnedLocation')
}
/**
* Updates the tab page index to the specified frameProps
* @param frameProps Any frame belonging to the page
*/
function updateTabPageIndex (state, frameProps) {
frameProps = makeImmutable(frameProps)
const index = getFrameTabPageIndex(state, frameProps.get('key'))
if (index === -1) {
return state
}
state = state.setIn(['ui', 'tabs', 'tabPageIndex'], index)
state = state.deleteIn(['ui', 'tabs', 'previewTabPageIndex'])
return state
}
const frameStatePath = (state, frameKey) => {
const index = getFrameIndex(state, frameKey)
if (index === -1) {
return null
}
return ['frames', getFrameIndex(state, frameKey)]
}
const activeFrameStatePath = (state) => frameStatePath(state, getActiveFrameKey(state))
const getFramesInternalIndex = (state, frameKey) => {
if (frameKey == null) return -1
const index = state.getIn(['framesInternal', 'index', frameKey.toString()])
return index == null ? -1 : index
}
const getFramesInternalIndexByTabId = (state, tabId) => {
if (tabId == null) return -1
const index = state.getIn(['framesInternal', 'tabIndex', tabId.toString()])
return index == null ? -1 : index
}
const deleteTabInternalIndex = (state, tabId) => {
return state.deleteIn(['framesInternal', 'tabIndex', tabId.toString()])
}
const deleteFrameInternalIndex = (state, frame) => {
if (!frame) {
return state
}
state = state.deleteIn(['framesInternal', 'index', frame.get('key').toString()])
return deleteTabInternalIndex(state, frame.get('tabId'))
}
const updateFramesInternalIndex = (state, fromIndex) => {
let framesInternal = state.get('framesInternal') || Immutable.Map()
state.get('frames').slice(fromIndex).forEach((frame, idx) => {
const realIndex = idx + fromIndex
if (frame.get('key')) {
framesInternal = framesInternal.setIn(['index', frame.get('key').toString()], realIndex)
}
if (frame.get('tabId') !== -1) {
framesInternal = framesInternal.setIn(['tabIndex', frame.get('tabId').toString()], realIndex)
}
appActions.tabIndexChanged(frame.get('tabId'), realIndex)
})
return state.set('framesInternal', framesInternal)
}
const isValidClosedFrame = (frame) => {
const location = frame.get('location')
if (location && (location.indexOf('about:newtab') !== -1 || location.indexOf('about:blank') !== -1)) {
return false
}
return !frame.get('isPrivate')
}
const getTabPageCount = (state) => {
const frames = getNonPinnedFrames(state) || Immutable.List()
const tabsPerPage = Number(getSetting(settings.TABS_PER_PAGE))
return Math.ceil(frames.size / tabsPerPage)
}
const getPreviewFrameKey = (state) => {
return state.get('previewFrameKey')
}
const setPreviewTabPageIndex = (state, index, immediate = false) => {
clearTimeout(tabPageHoverTimeout)
const previewTabs = getSetting(settings.SHOW_TAB_PREVIEWS)
const isActive = state.getIn(['ui', 'tabs', 'tabPageIndex']) === index
let newTabPageIndex = index
if (!previewTabs || state.getIn(['ui', 'tabs', 'hoverTabPageIndex']) !== index || isActive) {
newTabPageIndex = null
}
if (!immediate) {
// if there is an existing preview tab page index then we're already in preview mode
// we use actions here because that is the only way to delay updating the state
const previewMode = state.getIn(['ui', 'tabs', 'previewTabPageIndex']) != null
if (previewMode && newTabPageIndex == null) {
// add a small delay when we are clearing the preview frame key so we don't lose
// previewMode if the user mouses over another tab - see below
tabPageHoverTimeout = setTimeout(windowActions.setPreviewTabPageIndex.bind(null, null), 200)
return state
}
if (!previewMode) {
// If user isn't in previewMode so we add a bit of delay to avoid tab from flashing out
// as reported here: https://github.com/brave/browser-laptop/issues/1434
// using an action here because that is the only way we can do a delayed state update
tabPageHoverTimeout = setTimeout(windowActions.setPreviewTabPageIndex.bind(null, newTabPageIndex), 200)
return state
}
}
return state.setIn(['ui', 'tabs', 'previewTabPageIndex'], newTabPageIndex)
}
const setPreviewFrameKey = (state, frameKey, immediate = false) => {
clearTimeout(tabHoverTimeout)
const frame = getFrameByKey(state, frameKey)
const isActive = isFrameKeyActive(state, frameKey)
const previewTabs = getSetting(settings.SHOW_TAB_PREVIEWS)
let newPreviewFrameKey = frameKey
if (!previewTabs || frame == null || !frame.get('hoverState') || isActive) {
newPreviewFrameKey = null
}
if (!immediate) {
// if there is an existing preview frame key then we're already in preview mode
// we use actions here because that is the only way to delay updating the state
const previewMode = getPreviewFrameKey(state) != null
if (previewMode && newPreviewFrameKey == null) {
// add a small delay when we are clearing the preview frame key so we don't lose
// previewMode if the user mouses over another tab - see below
tabHoverTimeout = setTimeout(windowActions.setPreviewFrame.bind(null, null), 200)
return state
}
if (!previewMode) {
// If user isn't in previewMode so we add a bit of delay to avoid tab from flashing out
// as reported here: https://github.com/brave/browser-laptop/issues/1434
// using an action here because that is the only way we can do a delayed state update
tabHoverTimeout = setTimeout(windowActions.setPreviewFrame.bind(null, newPreviewFrameKey), 200)
return state
}
}
const index = getFrameTabPageIndex(state, frame)
if (index !== -1) {
if (index !== state.getIn(['ui', 'tabs', 'tabPageIndex'])) {
state = state.setIn(['ui', 'tabs', 'previewTabPageIndex'], index)
} else {
state = state.deleteIn(['ui', 'tabs', 'previewTabPageIndex'])
}
}
return state.set('previewFrameKey', newPreviewFrameKey)
}
const setTabPageHoverState = (state, tabPageIndex, hoverState) => {
const currentHoverIndex = state.getIn(['ui', 'tabs', 'hoverTabPageIndex'])
if (!hoverState && currentHoverIndex === tabPageIndex) {
state = state.setIn(['ui', 'tabs', 'hoverTabPageIndex'], null)
} else if (hoverState) {
state = state.setIn(['ui', 'tabs', 'hoverTabPageIndex'], tabPageIndex)
}
state = setPreviewTabPageIndex(state, tabPageIndex)
return state
}
const setTabHoverState = (state, frameKey, hoverState) => {
const frameIndex = getFrameIndex(state, frameKey)
if (frameIndex !== -1) {
state = state.setIn(['frames', frameIndex, 'hoverState'], hoverState)
state = setPreviewFrameKey(state, frameKey)
}
return state
}
module.exports = {
setTabPageHoverState,
setPreviewTabPageIndex,
setTabHoverState,
setPreviewFrameKey,
getPreviewFrameKey,
deleteTabInternalIndex,
deleteFrameInternalIndex,
updateFramesInternalIndex,
query,
find,
isAncestorFrameKey,
isFrameKeyActive,
isFrameSecure,
isFrameLoading,
startLoadTime,
endLoadTime,
getHistory,
isFrameKeyPinned,
getNonPinnedFrameCount,
isPrivatePartition,
isSessionPartition,
getFrames,
getSortedFrames,
getPinnedFrames,
getNonPinnedFrames,
getFrameIndex,
getActiveFrameIndex,
getActiveFrameTabId,
getFrameByIndex,
getFrameByDisplayIndex,
getFrameByKey,
getFrameByTabId,
getIndexByTabId,
getPartitionNumber,
getActiveFrame,
getNextFrame,
getPreviousFrame,
findDisplayIndexForFrameKey,
getFrameKeysByDisplayIndex,
getPartition,
getPartitionFromNumber,
addFrame,
removeFrame,
frameOptsFromFrame,
getFrameKeyByTabId,
getFrameTabPageIndex,
frameStatePath,
activeFrameStatePath,
getLastCommittedURL,
onFindBarHide,
getTotalBlocks,
isPinned,
updateTabPageIndex,
isValidClosedFrame,
getTabPageCount
}