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 pathframeReducer.js
197 lines (169 loc) · 6.51 KB
/
frameReducer.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
/* 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'
const Immutable = require('immutable')
// Constants
const appConstants = require('../../../js/constants/appConstants')
const windowConstants = require('../../../js/constants/windowConstants')
const config = require('../../../js/constants/config')
// Actions
const appActions = require('../../../js/actions/appActions')
// Utils
const frameStateUtil = require('../../../js/state/frameStateUtil')
const {getCurrentWindowId} = require('../currentWindow')
const {getSourceAboutUrl, getSourceMagnetUrl} = require('../../../js/lib/appUrlUtil')
const {isURL, isPotentialPhishingUrl, getUrlFromInput} = require('../../../js/lib/urlutil')
const setFullScreen = (state, action) => {
const index = frameStateUtil.getFrameIndex(state, action.frameProps.get('key'))
return state.mergeIn(['frames', index], {
isFullScreen: action.isFullScreen !== undefined ? action.isFullScreen : state.getIn(['frames', index].concat('isFullScreen')),
showFullScreenWarning: action.showFullScreenWarning
})
}
const closeFrame = (state, action) => {
const index = frameStateUtil.getFrameIndex(state, action.frameKey)
if (index === -1) {
return state
}
const frameProps = frameStateUtil.getFrameByKey(state, action.frameKey)
const hoverState = state.getIn(['frames', index, 'hoverState'])
state = state.merge(frameStateUtil.removeFrame(
state,
frameProps.set('closedAtIndex', index),
index
))
state = frameStateUtil.deleteFrameInternalIndex(state, frameProps)
state = frameStateUtil.updateFramesInternalIndex(state, index)
if (state.get('frames', Immutable.List()).size === 0) {
appActions.closeWindow(getCurrentWindowId())
}
const nextFrame = frameStateUtil.getFrameByIndex(state, index)
if (nextFrame) {
// Copy the hover state if tab closed with mouse as long as we have a next frame
// This allow us to have closeTab button visible for sequential frames closing,
// until onMouseLeave event happens.
if (hoverState) {
state = frameStateUtil.setTabHoverState(state, nextFrame.get('key'), hoverState)
}
} else if (hoverState && frameStateUtil.getPreviewFrameKey(state) === action.frameKey) {
state = frameStateUtil.setPreviewFrameKey(state, null)
}
return state
}
const getLocation = (location) => {
location = location.trim()
location = getSourceAboutUrl(location) ||
getSourceMagnetUrl(location) ||
location
if (isURL(location)) {
location = getUrlFromInput(location)
}
return location
}
const frameReducer = (state, action, immutableAction) => {
switch (action.actionType) {
case appConstants.APP_TAB_UPDATED:
const tab = immutableAction.get('tabValue')
if (!tab) {
break
}
const tabId = tab.get('tabId')
const frame = frameStateUtil.getFrameByTabId(state, tabId)
if (!frame) {
break
}
const index = frameStateUtil.getIndexByTabId(state, tabId)
const pinned = immutableAction.getIn(['changeInfo', 'pinned'])
if (pinned != null) {
if (pinned) {
state = state.setIn(['frames', index, 'pinnedLocation'], tab.get('url'))
} else {
state = state.deleteIn(['frames', index, 'pinnedLocation'])
}
}
// handle pinned tabs that are created as pinned
const url = immutableAction.getIn(['changeInfo', 'url'])
if (url != null && tab.get('pinned') === true) {
const pinnedLocation = state.getIn(['frames', index, 'pinnedLocation'])
if (!pinnedLocation || pinnedLocation === 'about:blank' || pinnedLocation === '') {
state = state.setIn(['frames', index, 'pinnedLocation'], tab.get('url'))
}
}
// TODO fix race condition in Muon more info in #9000
const title = immutableAction.getIn(['tabValue', 'title'])
if (title != null) {
state = state.setIn(['frames', index, 'title'], title)
}
// TODO fix race condition in Muon more info in #9000
const active = immutableAction.getIn(['tabValue', 'active'])
if (active != null) {
if (active) {
state = state.set('activeFrameKey', frame.get('key'))
if (frame.get('hoverState')) {
state = state.set('previewFrameKey', null)
}
if (frame.getIn(['ui', 'tabs', 'hoverTabPageIndex']) == null) {
state = state.deleteIn(['ui', 'tabs', 'previewTabPageIndex'])
}
state = state.setIn(['frames', index, 'lastAccessedTime'], new Date().getTime())
state = frameStateUtil.updateTabPageIndex(state, frame)
}
}
break
case windowConstants.WINDOW_SET_NAVIGATED:
// For about: URLs, make sure we store the URL as about:something
// and not what we map to.
action.location = getLocation(action.location)
const key = action.key
const framePath = frameStateUtil.frameStatePath(state, key)
if (!framePath) {
break
}
state = state.mergeIn(framePath, {
location: action.location
})
if (!action.isNavigatedInPage) {
state = state.mergeIn(framePath, {
adblock: {},
audioPlaybackActive: false,
computedThemeColor: undefined,
httpsEverywhere: {},
icon: undefined,
location: action.location,
noScript: {},
themeColor: undefined,
title: '',
trackingProtection: {},
fingerprintingProtection: {}
})
}
// For potential phishing pages, show a warning
if (isPotentialPhishingUrl(action.location)) {
state = state.setIn(['ui', 'siteInfo', 'isVisible'], true)
}
break
case windowConstants.WINDOW_CLOSE_FRAMES:
let closedFrames = new Immutable.List()
action.framePropsList.forEach((frameProps) => {
if (frameStateUtil.isValidClosedFrame(frameProps)) {
closedFrames = closedFrames.push(frameProps)
if (closedFrames.size > config.maxClosedFrames) {
closedFrames = closedFrames.shift()
}
}
})
closedFrames.forEach((frame) => {
appActions.tabCloseRequested(frame.get('tabId'))
})
break
case windowConstants.WINDOW_CLOSE_FRAME:
state = closeFrame(state, action)
break
case windowConstants.WINDOW_SET_FULL_SCREEN:
state = setFullScreen(state, action)
break
}
return state
}
module.exports = frameReducer