Skip to content

Commit

Permalink
Handle no active tab in state and no cosmetic filter list in storage
Browse files Browse the repository at this point in the history
Fix brave/brave-browser#845
Fix brave/brave-browser#846

[state].GetActiveTabData can return undefined from state yet the type did not reflect this even though some calls, but not all calls, handled it.

cosemtic filter list was being read before it was set in storage.
  • Loading branch information
petemill committed Aug 27, 2018
1 parent 27c815c commit cf068d4
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 11 deletions.
4 changes: 4 additions & 0 deletions app/background/api/cosmeticFilterAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ export const removeSiteFilter = (origin: string) => {

export const applySiteFilters = (hostname: string) => {
chrome.storage.local.get('cosmeticFilterList', (storeData = {}) => {
if (!storeData.cosmeticFilterList) {
console.info('applySiteFilters: no cosmetic filter store yet')
return
}
if (storeData.cosmeticFilterList[hostname] !== undefined) {
storeData.cosmeticFilterList[hostname].map((rule: string) => {
console.log('applying rule', rule)
Expand Down
11 changes: 8 additions & 3 deletions app/background/reducers/cosmeticFilterReducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
} from '../api/shieldsAPI'
import { reloadTab } from '../api/tabsAPI'
import * as shieldsPanelState from '../../state/shieldsPanelState'
import { State, Tab } from '../../types/state/shieldsPannelState'
import { State } from '../../types/state/shieldsPannelState'
import { Actions } from '../../types/actions/index'
import * as cosmeticFilterTypes from '../../constants/cosmeticFilterTypes'
import {
Expand Down Expand Up @@ -55,7 +55,11 @@ export default function cosmeticFilterReducer (state: State = {
}
case webNavigationTypes.ON_COMMITTED:
{
const tabData: Tab = shieldsPanelState.getActiveTabData(state)
const tabData = shieldsPanelState.getActiveTabData(state)
if (!tabData) {
console.error('Active tab not found')
break
}
applySiteFilters(tabData.hostname)
break
}
Expand Down Expand Up @@ -111,8 +115,9 @@ export default function cosmeticFilterReducer (state: State = {
case shieldsPanelTypes.SHIELDS_TOGGLED:
{
const tabId: number = shieldsPanelState.getActiveTabId(state)
const tabData: Tab = shieldsPanelState.getActiveTabData(state)
const tabData = shieldsPanelState.getActiveTabData(state)
if (!tabData) {
console.error('Active tab not found')
break
}
setAllowBraveShields(tabData.origin, action.setting)
Expand Down
33 changes: 26 additions & 7 deletions app/background/reducers/shieldsPanelReducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,9 @@ export default function shieldsPanelReducer (state: State = { tabs: {}, windows:
case shieldsPanelTypes.SHIELDS_TOGGLED:
{
const tabId: number = shieldsPanelState.getActiveTabId(state)
const tabData: Tab = shieldsPanelState.getActiveTabData(state)
const tabData = shieldsPanelState.getActiveTabData(state)
if (!tabData) {
console.error('Active tab not found')
break
}
setAllowBraveShields(tabData.origin, action.setting)
Expand All @@ -152,8 +153,9 @@ export default function shieldsPanelReducer (state: State = { tabs: {}, windows:
}
case shieldsPanelTypes.HTTPS_EVERYWHERE_TOGGLED:
{
const tabData: Tab = shieldsPanelState.getActiveTabData(state)
const tabData = shieldsPanelState.getActiveTabData(state)
if (!tabData) {
console.error('Active tab not found')
break
}

Expand All @@ -171,7 +173,11 @@ export default function shieldsPanelReducer (state: State = { tabs: {}, windows:
}
case shieldsPanelTypes.JAVASCRIPT_TOGGLED:
{
const tabData: Tab = shieldsPanelState.getActiveTabData(state)
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))
Expand All @@ -198,9 +204,10 @@ export default function shieldsPanelReducer (state: State = { tabs: {}, windows:
case shieldsPanelTypes.BLOCK_ADS_TRACKERS:
{
const tabId: number = shieldsPanelState.getActiveTabId(state)
const tabData: Tab = shieldsPanelState.getActiveTabData(state)
const tabData = shieldsPanelState.getActiveTabData(state)

if (!tabData) {
console.error('Active tab not found')
break
}

Expand Down Expand Up @@ -234,7 +241,11 @@ export default function shieldsPanelReducer (state: State = { tabs: {}, windows:
}
case shieldsPanelTypes.BLOCK_FINGERPRINTING:
{
const tabData: Tab = shieldsPanelState.getActiveTabData(state)
const tabData = shieldsPanelState.getActiveTabData(state)
if (!tabData) {
console.error('Active tab not found')
break
}
setAllowFingerprinting(tabData.origin, action.setting)
.then(() => {
requestShieldPanelData(shieldsPanelState.getActiveTabId(state))
Expand All @@ -249,7 +260,11 @@ export default function shieldsPanelReducer (state: State = { tabs: {}, windows:
}
case shieldsPanelTypes.BLOCK_COOKIES:
{
const tabData: Tab = shieldsPanelState.getActiveTabData(state)
const tabData = shieldsPanelState.getActiveTabData(state)
if (!tabData) {
console.error('Active tab not found')
break
}
setAllowCookies(tabData.origin, action.setting)
.then(() => {
requestShieldPanelData(shieldsPanelState.getActiveTabId(state))
Expand All @@ -264,7 +279,11 @@ export default function shieldsPanelReducer (state: State = { tabs: {}, windows:
}
case shieldsPanelTypes.ALLOW_SCRIPT_ORIGINS_ONCE:
{
const tabData: Tab = shieldsPanelState.getActiveTabData(state)
const tabData = shieldsPanelState.getActiveTabData(state)
if (!tabData) {
console.error('Active tab not found')
break
}
setAllowScriptOriginsOnce(action.origins, tabData.id)
.then(() => {
requestShieldPanelData(shieldsPanelState.getActiveTabId(state))
Expand Down
2 changes: 1 addition & 1 deletion app/types/state/shieldsPannelState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export interface GetActiveTabId {
}

export interface GetActiveTabData {
(state: State): Tab
(state: State): Tab | undefined
}

export interface UpdateActiveTab {
Expand Down

0 comments on commit cf068d4

Please sign in to comment.