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 969
/
Copy pathurlBarReducer.js
110 lines (98 loc) · 3.73 KB
/
urlBarReducer.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
/* 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 windowConstants = require('../../../js/constants/windowConstants')
const {getSourceAboutUrl, getSourceMagnetUrl} = require('../../../js/lib/appUrlUtil')
const {isURL, isPotentialPhishingUrl, getUrlFromInput} = require('../../../js/lib/urlutil')
const {activeFrameStatePath, frameStatePath, frameStatePathForFrame, getActiveFrame, tabStatePath, getFrameByTabId} = require('../../../js/state/frameStateUtil')
const getLocation = (location) => {
location = location.trim()
location = getSourceAboutUrl(location) ||
getSourceMagnetUrl(location) ||
location
if (isURL(location)) {
location = getUrlFromInput(location)
}
return location
}
const updateNavBarInput = (state, loc, framePath) => {
if (framePath === undefined) {
framePath = activeFrameStatePath(state)
}
state = state.setIn(framePath.concat(['navbar', 'urlbar', 'location']), loc)
return state
}
const navigationAborted = (state, action) => {
const frame = getFrameByTabId(state, action.tabId)
if (frame) {
let location = action.location || frame.get('provisionalLocation')
if (location) {
const frameStatePath = frameStatePathForFrame(state, frame)
location = getLocation(location)
state = updateNavBarInput(state, location, frameStatePath)
state = state.mergeIn(frameStatePath, {
location
})
}
}
return state
}
const urlBarReducer = (state, action) => {
switch (action.actionType) {
case windowConstants.WINDOW_SET_NAVBAR_INPUT:
state = updateNavBarInput(state, action.location)
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 || state.get('activeFrameKey')
state = state.mergeIn(frameStatePath(state, key), {
location: action.location
})
state = state.mergeIn(tabStatePath(state, key), {
location: action.location
})
if (!action.isNavigatedInPage) {
state = state.mergeIn(frameStatePath(state, key), {
adblock: {},
audioPlaybackActive: false,
computedThemeColor: undefined,
httpsEverywhere: {},
icon: undefined,
location: action.location,
noScript: {},
themeColor: undefined,
title: '',
trackingProtection: {},
fingerprintingProtection: {}
})
// TODO: This should be moved into a tabs reducer
state = state.mergeIn(tabStatePath(state, key), {
audioPlaybackActive: false,
themeColor: undefined,
location: action.location,
computedThemeColor: undefined,
icon: undefined,
title: ''
})
}
// Update nav bar unless when spawning a new tab. The user might have
// typed in the URL bar while we were navigating -- we should preserve it.
if (!(action.location === 'about:newtab' && !getActiveFrame(state).get('canGoForward'))) {
const key = action.key || state.get('activeFrameKey')
state = updateNavBarInput(state, action.location, frameStatePath(state, key))
}
// For potential phishing pages, show a warning
if (isPotentialPhishingUrl(action.location)) {
state = state.setIn(['ui', 'siteInfo', 'isVisible'], true)
}
break
case windowConstants.WINDOW_SET_NAVIGATION_ABORTED:
state = navigationAborted(state, action)
break
}
return state
}
module.exports = urlBarReducer