Skip to content

Commit

Permalink
Merge pull request #2485 from brave/noscriptinfo
Browse files Browse the repository at this point in the history
Update NoScript interface
  • Loading branch information
cezaraugusto authored Jun 6, 2019
2 parents 1738528 + 8a5a061 commit 93c58e0
Show file tree
Hide file tree
Showing 29 changed files with 1,470 additions and 521 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,18 @@
"message": "Allow",
"description": "Message for the resources blocked *allow* option"
},
"blockedOnce": {
"message": "Blocked once",
"description": "Message for the resources blocked *blocked once* option"
},
"allowedOnce": {
"message": "Allowed once",
"description": "Message for the resources blocked *allowed once* option"
},
"allowScriptsOnce": {
"message": "Allow scripts once",
"description": "Message for the shortcut in the main interface to allow all scripts once"
},
"cancel": {
"message": "Cancel",
"description": "Message for the button inside the static list of resources blocked to cancel the operation"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,23 +68,54 @@ export const blockCookies: actions.BlockCookies = (setting) => {
}
}

export const allowScriptOriginsOnce: actions.AllowScriptOriginsOnce = (origins) => {
export const allowScriptOriginsOnce: actions.AllowScriptOriginsOnce = () => {
return {
type: types.ALLOW_SCRIPT_ORIGINS_ONCE,
origins
type: types.ALLOW_SCRIPT_ORIGINS_ONCE
}
}

export const changeNoScriptSettings: actions.ChangeNoScriptSettings = (origin) => {
/**
* Set a given script resource state to be in the allowed/blocked list
* @param {string} url - The resource URL
* @param {boolean} maybeBlock - Whether or not the resource should be blocked
*/
export const setScriptBlockedCurrentState: actions.SetScriptBlockedCurrentState = (url) => {
return {
type: types.CHANGE_NO_SCRIPT_SETTINGS,
origin
type: types.SET_SCRIPT_BLOCKED_ONCE_CURRENT_STATE,
url
}
}

export const changeAllNoScriptSettings: actions.ChangeAllNoScriptSettings = (shouldBlock) => {
/**
* Set all child resources of a given hostname to be in the allowed/blocked list
* @param {string} origin - The blocked resource hostname
* @param {boolean} maybeBlock - Whether or not the resource should be blocked
*/
export const setGroupedScriptsBlockedCurrentState: actions.SetGroupedScriptsBlockedCurrentState = (origin, maybeBlock) => {
return {
type: types.CHANGE_ALL_NO_SCRIPT_SETTINGS,
shouldBlock
type: types.SET_GROUPED_SCRIPTS_BLOCKED_ONCE_CURRENT_STATE,
origin,
maybeBlock
}
}

/**
* Set all resources in blocked/allowed state to be in the allowed/blocked list
* @param {boolean} maybeBlock - Whether or not the resource should be blocked
*/
export const setAllScriptsBlockedCurrentState: actions.SetAllScriptsBlockedCurrentState = (maybeBlock) => {
return {
type: types.SET_ALL_SCRIPTS_BLOCKED_ONCE_CURRENT_STATE,
maybeBlock
}
}

/**
* Set the final state to all resources so they could be stored persistently in
* the blocked/allowed list
*/
export const setFinalScriptsBlockedState: actions.SetFinalScriptsBlockedState = () => {
return {
type: types.SET_FINAL_SCRIPTS_BLOCKED_ONCE_STATE
}
}
Original file line number Diff line number Diff line change
@@ -1,27 +1,31 @@
// /* 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/. */
/* 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/. */

// Types
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,
requestShieldPanelData
} from '../api/shieldsAPI'
import { reloadTab } from '../api/tabsAPI'
import * as shieldsPanelState from '../../state/shieldsPanelState'
import * as cosmeticFilterTypes from '../../constants/cosmeticFilterTypes'
import { State } from '../../types/state/shieldsPannelState'
import { Actions } from '../../types/actions/index'
import * as cosmeticFilterTypes from '../../constants/cosmeticFilterTypes'

// APIs
import { setAllowBraveShields, requestShieldPanelData } from '../api/shieldsAPI'
import { reloadTab } from '../api/tabsAPI'
import {
removeSiteFilter,
addSiteCosmeticFilter,
applySiteFilters,
removeAllFilters
} from '../api/cosmeticFilterAPI'

// State helpers
import * as shieldsPanelState from '../../state/shieldsPanelState'
import * as noScriptState from '../../state/noScriptState'
import { getOrigin } from '../../helpers/urlUtils'

const focusedWindowChanged = (state: State, windowId: number): State => {
if (windowId !== -1) {
state = shieldsPanelState.updateFocusedWindow(state, windowId)
Expand Down Expand Up @@ -54,7 +58,7 @@ export default function cosmeticFilterReducer (state: State = {
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)
state = noScriptState.resetNoScriptInfo(state, action.tabId, getOrigin(action.url))
}
applySiteFilters(tabData.hostname)
break
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,19 @@
* 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/. */

// Types
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 { State } from '../../types/state/shieldsPannelState'
import { Actions } from '../../types/actions/index'

// State helpers
import * as shieldsPanelState from '../../state/shieldsPanelState'
import * as noScriptState from '../../state/noScriptState'

// APIs
import {
setAllowBraveShields,
setAllowAds,
Expand All @@ -19,17 +28,17 @@ import {
setAllowScriptOriginsOnce
} from '../api/shieldsAPI'
import { reloadTab } from '../api/tabsAPI'
import * as shieldsPanelState from '../../state/shieldsPanelState'
import { Actions } from '../../types/actions/index'
import { State } from '../../types/state/shieldsPannelState'

// Helpers
import { getAllowedScriptsOrigins } from '../../helpers/noScriptUtils'

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)
state = noScriptState.resetNoScriptInfo(state, action.tabId, new window.URL(action.url).origin)
}
break
}
Expand Down Expand Up @@ -244,7 +253,7 @@ export default function shieldsPanelReducer (state: State = { tabs: {}, windows:
console.error('Active tab not found')
break
}
setAllowScriptOriginsOnce(action.origins, tabData.id)
setAllowScriptOriginsOnce(getAllowedScriptsOrigins(tabData.noScriptInfo), tabData.id)
.then(() => {
requestShieldPanelData(shieldsPanelState.getActiveTabId(state))
reloadTab(tabData.id, true).catch(() => {
Expand All @@ -256,16 +265,42 @@ export default function shieldsPanelReducer (state: State = { tabs: {}, windows:
})
break
}
case shieldsPanelTypes.CHANGE_NO_SCRIPT_SETTINGS: {
const tabId: number = shieldsPanelState.getActiveTabId(state)
state = shieldsPanelState.changeNoScriptSettings(state, tabId, action.origin)
// NoScriptInfo is the name we call for the list of scripts that are either
// blocked or allowed by the user. Each script have three properties:
// ....................................................................................
// `actuallyBlocked`:
// ....................................................................................
// When set to `true` it blocks the script immediatelly. This is the initial state
// when the user toggle scripts blocked in the main panel screen and also the initial state
// for when users toggle `block/allow` or `block all/allow all`
// ....................................................................................
// `willBlock`:
// ....................................................................................
// When set to `true` it moves the script to its respective list. This is the final state
// when the user choose to close Shields either by clicking `cancel`, moving back to the
// main screen, or closing Shields browser action. This state is triggered only after those actions
// and its state inherit the state of `actuallyBlocked`.
// ....................................................................................
// `userInteracted`:
// ....................................................................................
// This property is for display only. With this we can tell whether or not the user have
// interacted with the script which can change the button state to allow/block (no user interaction)
// or blocked once/allowed once (user has interacted).
case shieldsPanelTypes.SET_SCRIPT_BLOCKED_ONCE_CURRENT_STATE: {
state = noScriptState.setScriptBlockedCurrentState(state, action.url)
break
}
case shieldsPanelTypes.CHANGE_ALL_NO_SCRIPT_SETTINGS: {
const tabId: number = shieldsPanelState.getActiveTabId(state)
state = shieldsPanelState.changeAllNoScriptSettings(state, tabId, action.shouldBlock)
case shieldsPanelTypes.SET_GROUPED_SCRIPTS_BLOCKED_ONCE_CURRENT_STATE: {
state = noScriptState.setGroupedScriptsBlockedCurrentState(state, action.origin, action.maybeBlock)
break
}
case shieldsPanelTypes.SET_ALL_SCRIPTS_BLOCKED_ONCE_CURRENT_STATE: {
state = noScriptState.setAllScriptsBlockedCurrentState(state, action.maybeBlock)
break
}
case shieldsPanelTypes.SET_FINAL_SCRIPTS_BLOCKED_ONCE_STATE: {
state = noScriptState.setFinalScriptsBlockedState(state)
}
}
return state
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@ import {
BlockFingerprinting,
BlockCookies,
AllowScriptOriginsOnce,
ChangeNoScriptSettings,
ChangeAllNoScriptSettings
SetScriptBlockedCurrentState,
SetGroupedScriptsBlockedCurrentState,
SetAllScriptsBlockedCurrentState,
SetFinalScriptsBlockedState
} from '../types/actions/shieldsPanelActions'

interface Props {
Expand All @@ -40,8 +42,10 @@ interface Props {
blockFingerprinting: BlockFingerprinting
blockCookies: BlockCookies
allowScriptOriginsOnce: AllowScriptOriginsOnce
changeNoScriptSettings: ChangeNoScriptSettings
changeAllNoScriptSettings: ChangeAllNoScriptSettings
setScriptBlockedCurrentState: SetScriptBlockedCurrentState
setGroupedScriptsBlockedCurrentState: SetGroupedScriptsBlockedCurrentState
setAllScriptsBlockedCurrentState: SetAllScriptsBlockedCurrentState
setFinalScriptsBlockedState: SetFinalScriptsBlockedState
}
shieldsPanelTabData: Tab
}
Expand Down Expand Up @@ -126,10 +130,12 @@ export default class Shields extends React.PureComponent<Props, State> {
javascript={shieldsPanelTabData.javascript}
javascriptBlocked={shieldsPanelTabData.javascriptBlocked}
noScriptInfo={shieldsPanelTabData.noScriptInfo}
changeNoScriptSettings={actions.changeNoScriptSettings}
blockJavaScript={actions.blockJavaScript}
changeAllNoScriptSettings={actions.changeAllNoScriptSettings}
allowScriptOriginsOnce={actions.allowScriptOriginsOnce}
setScriptBlockedCurrentState={actions.setScriptBlockedCurrentState}
setGroupedScriptsBlockedCurrentState={actions.setGroupedScriptsBlockedCurrentState}
setAllScriptsBlockedCurrentState={actions.setAllScriptsBlockedCurrentState}
setFinalScriptsBlockedState={actions.setFinalScriptsBlockedState}
// Cookies
blockCookies={actions.blockCookies}
cookies={shieldsPanelTabData.cookies}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@ import {
ArrowDownIcon,
BlockedInfoRowStats,
BlockedInfoRowText,
LinkAction,
Toggle
} from 'brave-ui/features/shields'

// Group Components
import DynamicList from '../list/dynamic'
import NoScript from '../list/noScript'

// Locale
import { getLocale } from '../../background/api/localeAPI'
Expand All @@ -33,10 +34,12 @@ import {
import { BlockJSOptions } from '../../types/other/blockTypes'
import { NoScriptInfo } from '../../types/other/noScriptInfo'
import {
ChangeNoScriptSettings,
BlockJavaScript,
ChangeAllNoScriptSettings,
AllowScriptOriginsOnce
AllowScriptOriginsOnce,
SetScriptBlockedCurrentState,
SetGroupedScriptsBlockedCurrentState,
SetAllScriptsBlockedCurrentState,
SetFinalScriptsBlockedState
} from '../../types/actions/shieldsPanelActions'

interface CommonProps {
Expand All @@ -51,10 +54,12 @@ interface JavaScriptProps {
javascript: BlockJSOptions
javascriptBlocked: number
noScriptInfo: NoScriptInfo
changeNoScriptSettings: ChangeNoScriptSettings
blockJavaScript: BlockJavaScript
changeAllNoScriptSettings: ChangeAllNoScriptSettings
allowScriptOriginsOnce: AllowScriptOriginsOnce
setScriptBlockedCurrentState: SetScriptBlockedCurrentState
setGroupedScriptsBlockedCurrentState: SetGroupedScriptsBlockedCurrentState
setAllScriptsBlockedCurrentState: SetAllScriptsBlockedCurrentState
setFinalScriptsBlockedState: SetFinalScriptsBlockedState
}

export type Props = CommonProps & JavaScriptProps
Expand Down Expand Up @@ -114,20 +119,28 @@ export default class ScriptsControls extends React.PureComponent<Props, State> {
this.props.blockJavaScript(shouldBlockJavaScript)
}

onClickAllowScriptsOnce = () => {
this.props.setAllScriptsBlockedCurrentState(false)
this.props.setFinalScriptsBlockedState()
this.props.allowScriptOriginsOnce()
}

render () {
const {
favicon,
hostname,
isBlockedListOpen,
allowScriptOriginsOnce,
changeNoScriptSettings,
changeAllNoScriptSettings,
noScriptInfo
noScriptInfo,
setScriptBlockedCurrentState,
setGroupedScriptsBlockedCurrentState,
setAllScriptsBlockedCurrentState,
setFinalScriptsBlockedState
} = this.props
const { scriptsBlockedOpen } = this.state
return (
<>
<BlockedInfoRow id='scriptsControl'>
<BlockedInfoRow id='scriptsControl' extraColumn={true}>
<BlockedInfoRowData
disabled={this.maybeDisableResourcesRow}
tabIndex={this.tabIndex}
Expand All @@ -138,6 +151,17 @@ export default class ScriptsControls extends React.PureComponent<Props, State> {
<BlockedInfoRowStats id='blockScriptsStat'>{this.javascriptBlockedDisplay}</BlockedInfoRowStats>
<BlockedInfoRowText>{getLocale('scriptsBlocked')}</BlockedInfoRowText>
</BlockedInfoRowData>
{
this.maybeDisableResourcesRow === false
&& (
<LinkAction
size='small'
onClick={this.onClickAllowScriptsOnce}
>
{getLocale('allowScriptsOnce')}
</LinkAction>
)
}
<Toggle
id='blockScripts'
size='small'
Expand All @@ -148,16 +172,16 @@ export default class ScriptsControls extends React.PureComponent<Props, State> {
</BlockedInfoRow>
{
scriptsBlockedOpen &&
<DynamicList
<NoScript
favicon={favicon}
hostname={hostname}
origin={origin}
name={getLocale('scriptsOnThisSite')}
list={noScriptInfo}
noScriptInfo={noScriptInfo}
onClose={this.onOpenScriptsBlocked}
allowScriptOriginsOnce={allowScriptOriginsOnce}
changeNoScriptSettings={changeNoScriptSettings}
changeAllNoScriptSettings={changeAllNoScriptSettings}
setScriptBlockedCurrentState={setScriptBlockedCurrentState}
setGroupedScriptsBlockedCurrentState={setGroupedScriptsBlockedCurrentState}
setAllScriptsBlockedCurrentState={setAllScriptsBlockedCurrentState}
setFinalScriptsBlockedState={setFinalScriptsBlockedState}
/>
}
</>
Expand Down
Loading

0 comments on commit 93c58e0

Please sign in to comment.