This repository has been archived by the owner on Jun 6, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
/
shieldsPanelReducer.ts
332 lines (321 loc) · 11.6 KB
/
shieldsPanelReducer.ts
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
/* 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/. */
import * as shieldsPanelTypes from '../../constants/shieldsPanelTypes'
import * as windowTypes from '../../constants/windowTypes'
import * as tabTypes from '../../constants/tabTypes'
import * as webNavigationTypes from '../../constants/webNavigationTypes'
import {
setAllowBraveShields,
setAllowAds,
setAllowTrackers,
setAllowHTTPUpgradableResources,
setAllowJavaScript,
setAllowFingerprinting,
setAllowCookies,
toggleShieldsValue,
requestShieldPanelData,
setAllowScriptOriginsOnce
} from '../api/shieldsAPI'
import { setBadgeText, setIcon } from '../api/browserActionAPI'
import { reloadTab } from '../api/tabsAPI'
import * as shieldsPanelState from '../../state/shieldsPanelState'
import { State, Tab } from '../../types/state/shieldsPannelState'
import { Actions } from '../../types/actions/index'
import { getTotalResourcesBlocked } from '../../helpers/shieldsUtils'
const updateShieldsIconBadgeText = (state: State) => {
const tabId: number = shieldsPanelState.getActiveTabId(state)
const tab: Tab = state.tabs[tabId]
if (tab) {
const total = getTotalResourcesBlocked(tab)
// do not show any badge if there are no blocked items
setBadgeText(tabId, total > 99 ? '99+' : total > 0 ? total.toString() : '')
}
}
const updateShieldsIconImage = (state: State) => {
const tabId: number = shieldsPanelState.getActiveTabId(state)
const tab: Tab = state.tabs[tabId]
if (tab) {
const url: string = tab.url
const isShieldsActive: boolean = state.tabs[tabId].braveShields !== 'block'
setIcon(url, tabId, isShieldsActive)
}
}
const updateShieldsIcon = (state: State) => {
updateShieldsIconBadgeText(state)
updateShieldsIconImage(state)
}
const focusedWindowChanged = (state: State, windowId: number): State => {
if (windowId !== -1) {
state = shieldsPanelState.updateFocusedWindow(state, windowId)
if (shieldsPanelState.getActiveTabId(state)) {
requestShieldPanelData(shieldsPanelState.getActiveTabId(state))
updateShieldsIcon(state)
} else {
console.warn('no tab id so cannot request shield data from window focus change!')
}
}
return state
}
const updateActiveTab = (state: State, windowId: number, tabId: number): State => {
requestShieldPanelData(tabId)
return shieldsPanelState.updateActiveTab(state, windowId, tabId)
}
export default function shieldsPanelReducer (state: State = { tabs: {}, windows: {}, currentWindowId: -1 }, action: Actions) {
switch (action.type) {
case webNavigationTypes.ON_COMMITTED:
{
if (action.isMainFrame) {
state = shieldsPanelState.resetBlockingStats(state, action.tabId)
state = shieldsPanelState.resetBlockingResources(state, action.tabId)
state = shieldsPanelState.resetNoScriptInfo(state, action.tabId, new window.URL(action.url).origin)
}
break
}
case windowTypes.WINDOW_REMOVED:
{
state = shieldsPanelState.removeWindowInfo(state, action.windowId)
break
}
case windowTypes.WINDOW_CREATED:
{
if (action.window.focused || Object.keys(state.windows).length === 0) {
state = focusedWindowChanged(state, action.window.id)
}
break
}
case windowTypes.WINDOW_FOCUS_CHANGED:
{
state = focusedWindowChanged(state, action.windowId)
break
}
case tabTypes.ACTIVE_TAB_CHANGED:
{
const windowId: number = action.windowId
const tabId: number = action.tabId
state = updateActiveTab(state, windowId, tabId)
updateShieldsIcon(state)
break
}
case tabTypes.TAB_DATA_CHANGED:
{
const tab: chrome.tabs.Tab = action.tab
if (tab.active && tab.id) {
state = updateActiveTab(state, tab.windowId, tab.id)
updateShieldsIcon(state)
}
break
}
case tabTypes.TAB_CREATED:
{
const tab: chrome.tabs.Tab = action.tab
if (!tab) {
break
}
if (tab.active && tab.id) {
state = updateActiveTab(state, tab.windowId, tab.id)
updateShieldsIcon(state)
}
break
}
case shieldsPanelTypes.SHIELDS_PANEL_DATA_UPDATED:
{
state = shieldsPanelState.updateTabShieldsData(state, action.details.id, action.details)
updateShieldsIcon(state)
break
}
case shieldsPanelTypes.SHIELDS_TOGGLED:
{
const tabId: number = shieldsPanelState.getActiveTabId(state)
const tabData = shieldsPanelState.getActiveTabData(state)
if (!tabData) {
console.error('Active tab not found')
break
}
setAllowBraveShields(tabData.origin, action.setting)
.then(() => {
reloadTab(tabId, true).catch(() => {
console.error('Tab reload was not successful')
})
requestShieldPanelData(shieldsPanelState.getActiveTabId(state))
})
.catch(() => {
console.error('Could not set shields')
})
state = shieldsPanelState
.updateTabShieldsData(state, tabId, { braveShields: action.setting })
break
}
case shieldsPanelTypes.HTTPS_EVERYWHERE_TOGGLED:
{
const tabData = shieldsPanelState.getActiveTabData(state)
if (!tabData) {
console.error('Active tab not found')
break
}
setAllowHTTPUpgradableResources(tabData.origin, toggleShieldsValue(action.setting))
.then(() => {
requestShieldPanelData(shieldsPanelState.getActiveTabId(state))
reloadTab(tabData.id, true).catch(() => {
console.error('Tab reload was not successful')
})
})
.catch(() => {
console.error('Could not set HTTPS Everywhere setting')
})
break
}
case shieldsPanelTypes.JAVASCRIPT_TOGGLED:
{
const tabData = shieldsPanelState.getActiveTabData(state)
if (!tabData) {
console.error('Active tab not found')
break
}
setAllowJavaScript(tabData.origin, toggleShieldsValue(tabData.javascript))
.then(() => {
requestShieldPanelData(shieldsPanelState.getActiveTabId(state))
reloadTab(tabData.id, true).catch(() => {
console.error('Tab reload was not successful')
})
})
.catch(() => {
console.error('Could not set JavaScript setting')
})
break
}
case shieldsPanelTypes.RESOURCE_BLOCKED:
{
const tabId: number = action.details.tabId
const currentTabId: number = shieldsPanelState.getActiveTabId(state)
state = shieldsPanelState.updateResourceBlocked(
state, tabId, action.details.blockType, action.details.subresource)
if (tabId === currentTabId) {
updateShieldsIconBadgeText(state)
}
break
}
case shieldsPanelTypes.BLOCK_ADS_TRACKERS:
{
const tabId: number = shieldsPanelState.getActiveTabId(state)
const tabData = shieldsPanelState.getActiveTabData(state)
if (!tabData) {
console.error('Active tab not found')
break
}
const setting = toggleShieldsValue(action.setting)
const p1 = setAllowAds(tabData.origin, setting)
.catch(() => {
console.error('Could not set ad block setting')
})
const p2 = setAllowTrackers(tabData.origin, setting)
.catch(() => {
console.error('Could not set tracking protection setting')
})
Promise.all([p1, p2])
.then(() => {
reloadTab(tabId, true).catch(() => {
console.error('Tab reload was not successful')
})
requestShieldPanelData(shieldsPanelState.getActiveTabId(state))
})
.catch(() => {
console.error('Could not set blockers for tracking')
})
break
}
case shieldsPanelTypes.CONTROLS_TOGGLED:
{
const tabId: number = shieldsPanelState.getActiveTabId(state)
state = shieldsPanelState
.updateTabShieldsData(state, tabId, { controlsOpen: action.setting })
break
}
case shieldsPanelTypes.BLOCK_FINGERPRINTING:
{
const tabData = shieldsPanelState.getActiveTabData(state)
if (!tabData) {
console.error('Active tab not found')
break
}
setAllowFingerprinting(tabData.origin, action.setting)
.then(() => {
requestShieldPanelData(shieldsPanelState.getActiveTabId(state))
reloadTab(tabData.id, true).catch(() => {
console.error('Tab reload was not successful')
})
})
.catch(() => {
console.error('Could not set fingerprinting setting')
})
break
}
case shieldsPanelTypes.BLOCK_COOKIES:
{
const tabData = shieldsPanelState.getActiveTabData(state)
if (!tabData) {
console.error('Active tab not found')
break
}
setAllowCookies(tabData.origin, action.setting)
.then(() => {
if (action.setting === 'block') {
chrome.cookies.getAll({ domain: tabData.origin },
function (cookies) {
cookies.forEach(function (cookie) {
chrome.cookies.remove({ 'url': 'http://' + cookie.domain + cookie.path, 'name': cookie.name })
chrome.cookies.remove({ 'url': 'https://' + cookie.domain + cookie.path, 'name': cookie.name })
})
}
)
chrome.tabs.executeScript(tabData.id, {
code: 'try { window.sessionStorage.clear(); } catch(e) {}'})
// clearing localStorage may fail with SecurityError if third-
// party cookies are already blocked, but that's okay
chrome.tabs.executeScript(tabData.id, {
code: 'try { window.localStorage.clear(); } catch(e) {}'})
}
requestShieldPanelData(shieldsPanelState.getActiveTabId(state))
reloadTab(tabData.id, true).catch(() => {
console.error('Tab reload was not successful')
})
})
.catch(() => {
console.error('Could not set cookies setting')
})
break
}
case shieldsPanelTypes.ALLOW_SCRIPT_ORIGINS_ONCE:
{
const tabData = shieldsPanelState.getActiveTabData(state)
if (!tabData) {
console.error('Active tab not found')
break
}
setAllowScriptOriginsOnce(action.origins, tabData.id)
.then(() => {
requestShieldPanelData(shieldsPanelState.getActiveTabId(state))
reloadTab(tabData.id, true).catch(() => {
console.error('Tab reload was not successful')
})
})
.catch(() => {
console.error('Could not set allow script origins once')
})
break
}
case shieldsPanelTypes.CHANGE_NO_SCRIPT_SETTINGS:
{
const tabId: number = shieldsPanelState.getActiveTabId(state)
state = shieldsPanelState.changeNoScriptSettings(state, tabId, action.origin)
break
}
case shieldsPanelTypes.CHANGE_ALL_NO_SCRIPT_SETTINGS:
{
const tabId: number = shieldsPanelState.getActiveTabId(state)
state = shieldsPanelState.changeAllNoScriptSettings(state, tabId, action.origin, action.shouldBlock)
break
}
}
return state
}