Skip to content

Commit

Permalink
Merge pull request #3150 from brave/ca-3121
Browse files Browse the repository at this point in the history
[brave-extension] add settings option to hide Shields activity count
  • Loading branch information
cezaraugusto authored Mar 5, 2020
2 parents 9c1f108 + a692ec9 commit 0985439
Show file tree
Hide file tree
Showing 33 changed files with 302 additions and 40 deletions.
10 changes: 8 additions & 2 deletions app/brave_generated_resources.grd
Original file line number Diff line number Diff line change
Expand Up @@ -353,8 +353,14 @@ By installing this extension, you are agreeing to the Google Widevine Terms of U
<message name="IDS_SETTINGS_BRAVE_SHIELDS_NO_SCRIPT_CONTROL_LABEL" desc="Default Brave script blocking control setting label">
Block scripts
</message>
<message name="IDS_SETTINGS_BRAVE_SHIELDS_GOOGLE_LOGINS_LABEL" desc="Label for a switch control which allows Google logins">
Allow Google logins
<message name="IDS_SETTINGS_BRAVE_SHIELDS_LOOK_AND_FEEL_TITLE" desc="The title the settings section with interface options such as showing the resources blocked badge">
Look &amp; Feel
</message>
<message name="IDS_SETTINGS_BRAVE_SHIELDS_SHOW_STATS_BLOCKED_BADGE_LABEL" desc="Default Brave show stats blocked badge control setting label">
Show the number of blocked items on the Shields icon
</message>
<message name="IDS_SETTINGS_BRAVE_SHIELDS_GOOGLE_LOGIN_LABEL" desc="Label for a switch control which allows Google social buttons to be enabled/disabled">
Allow Google login buttons on third party sites
</message>
<message name="IDS_SETTINGS_BRAVE_SHIELDS_FACEBOOK_EMBEDDED_POSTS_LABEL" desc="Label for a switch control which allows Facebook embedded posts">
Allow Facebook logins and embedded posts
Expand Down
3 changes: 1 addition & 2 deletions browser/brave_profile_prefs.cc
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,7 @@ void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry) {

registry->RegisterBooleanPref(kShieldsAdvancedViewEnabled,
is_new_user == false);

// Google-oauth should work by default
registry->RegisterBooleanPref(kShieldsStatsBadgeVisible, true);
registry->RegisterBooleanPref(kGoogleLoginControlType, true);
registry->RegisterBooleanPref(kFBEmbedControlType, true);
registry->RegisterBooleanPref(kTwitterEmbedControlType, true);
Expand Down
3 changes: 3 additions & 0 deletions browser/brave_profile_prefs_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ IN_PROC_BROWSER_TEST_F(BraveProfilePrefsBrowserTest, MiscBravePrefs) {
kHTTPSEVerywhereControlType));
EXPECT_FALSE(
browser()->profile()->GetPrefs()->GetBoolean(kNoScriptControlType));
EXPECT_TRUE(
browser()->profile()->GetPrefs()->GetBoolean(
kShieldsStatsBadgeVisible));
EXPECT_TRUE(
browser()->profile()->GetPrefs()->GetBoolean(kAdControlType));
EXPECT_TRUE(
Expand Down
2 changes: 2 additions & 0 deletions browser/extensions/api/settings_private/brave_prefs_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ const PrefsUtil::TypedPrefMap& BravePrefsUtil::GetWhitelistedKeys() {
// Default Brave shields
(*s_brave_whitelist)[kShieldsAdvancedViewEnabled] =
settings_api::PrefType::PREF_TYPE_BOOLEAN;
(*s_brave_whitelist)[kShieldsStatsBadgeVisible] =
settings_api::PrefType::PREF_TYPE_BOOLEAN;
(*s_brave_whitelist)[kAdControlType] =
settings_api::PrefType::PREF_TYPE_BOOLEAN;
(*s_brave_whitelist)[kHTTPSEVerywhereControlType] =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,13 @@
</template>
</select>
</div>
<div class="settings-box">
<div class="label shields-primary-title">$i18n{shieldsLookFeelTitle}</div>
</div>
<settings-toggle-button
class="continuation"
pref="{{prefs.brave.shields.stats_badge_visible}}"
label="$i18n{showStatsBlockedBadgeLabel}">
</template>
<script src="default_brave_shields_page.js"></script>
</dom-module>
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,13 @@ void BraveAddCommonStrings(content::WebUIDataSource* html_source,
IDS_SETTINGS_BRAVE_SHIELDS_HTTPS_EVERYWHERE_CONTROL_LABEL},
{"noScriptControlLabel",
IDS_SETTINGS_BRAVE_SHIELDS_NO_SCRIPT_CONTROL_LABEL},
{"googleLoginControlLabel",
IDS_SETTINGS_BRAVE_SHIELDS_GOOGLE_LOGINS_LABEL},
{"fbEmbedControlLabel",
{"shieldsLookFeelTitle",
IDS_SETTINGS_BRAVE_SHIELDS_LOOK_AND_FEEL_TITLE},
{"showStatsBlockedBadgeLabel",
IDS_SETTINGS_BRAVE_SHIELDS_SHOW_STATS_BLOCKED_BADGE_LABEL},
{"googleLoginControlLabel",
IDS_SETTINGS_BRAVE_SHIELDS_GOOGLE_LOGIN_LABEL},
{"fbEmbedControlLabel",
IDS_SETTINGS_BRAVE_SHIELDS_FACEBOOK_EMBEDDED_POSTS_LABEL},
{"twitterEmbedControlLabel",
IDS_SETTINGS_BRAVE_SHIELDS_TWITTER_EMBEDDED_TWEETS_LABEL},
Expand Down
2 changes: 2 additions & 0 deletions common/pref_names.cc
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ const char kHTTPSEVerywhereControlType[] = "brave.https_everywhere_default";
const char kNoScriptControlType[] = "brave.no_script_default";
const char kShieldsAdvancedViewEnabled[] =
"brave.shields.advanced_view_enabled";
const char kShieldsStatsBadgeVisible[] =
"brave.shields.stats_badge_visible";
const char kAdControlType[] = "brave.ad_default";
const char kGoogleLoginControlType[] = "brave.google_login_default";
const char kFBEmbedControlType[] = "brave.fb_embed_default";
Expand Down
1 change: 1 addition & 0 deletions common/pref_names.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ extern const char kReferralAndroidFirstRunTimestamp[];
extern const char kHTTPSEVerywhereControlType[];
extern const char kNoScriptControlType[];
extern const char kShieldsAdvancedViewEnabled[];
extern const char kShieldsStatsBadgeVisible[];
extern const char kAdControlType[];
extern const char kGoogleLoginControlType[];
extern const char kFBEmbedControlType[];
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/* 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 types from '../constants/settingsTypes'
import * as actions from '../types/actions/settingsActions'
import { SettingsOptions, GeneratedSettingsKey, SettingsData } from '../types/other/settingsTypes'

// Helpers
import * as shieldsAPI from '../background/api/shieldsAPI'
import * as settingsUtils from '../helpers/settingsUtils'
import { areObjectsEqual } from '../helpers/objectUtils'
import { Dispatch } from 'redux'
import { State } from '../types/state/mainState'
/**
* Inform the store that settings have changed. This action is used only
* for storing values in Redux and does not tell which brave settings have changed.
*/
export const setStoreSettingsChange: actions.SetStoreSettingsChange = (settingsData) => {
return {
type: types.SET_STORE_SETTINGS_CHANGE,
settingsData
}
}

/**
* Perform an update in settings both in brave://settings and Shields store whenever a setting change.
* This action is bounded to the settings listener and should not be used outside this scope.
*/
export const settingsDidChange: actions.SettingsDidChange = (settings) => {
const settingsOptions: SettingsOptions = settingsUtils.settingsOptions
const currentSetting: Partial<GeneratedSettingsKey> = settingsOptions[settings.key]
return setStoreSettingsChange({ [currentSetting]: settings.value })
}

/**
* Get a list of settings values from brave://settings and update if comparison
* against settings values from store deosn't match.
*/
interface FetchAndDispatchSettings {
(): (dispatch: Dispatch, getState: () => State) => void
}

export const fetchAndDispatchSettings: FetchAndDispatchSettings = () => {
return (dispatch, getState) => {
const settingsDataFromStore: SettingsData = getState().shieldsPanel.settingsData
shieldsAPI.getViewPreferences()
.then(
(settingsData: SettingsData) => {
if (!areObjectsEqual(settingsDataFromStore, settingsData)) {
dispatch(setStoreSettingsChange(settingsData))
}
},
error => console.error('[Shields] error updating settings data', error)
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ require('./actions/webNavigationActions')
require('./actions/runtimeActions')
require('./actions/tabActions')
require('./actions/windowActions')
require('./actions/settingsActions')
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/* 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 { bindActionCreators } from 'redux'
import store from '../store'
import * as settingsActions from '../../actions/settingsActions'
export default bindActionCreators(settingsActions, store.dispatch)
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ require('./api/cosmeticFilterAPI')
require('./api/localeAPI')
require('./api/shieldsAPI')
require('./api/tabsAPI')
require('./api/storageAPI')
Original file line number Diff line number Diff line change
Expand Up @@ -175,23 +175,33 @@ export const reportBrokenSite = (tabId: number) =>

export type GetViewPreferencesData = {
showAdvancedView: boolean
statsBadgeVisible: boolean
}

const settingsKeys = {
showAdvancedView: { key: 'brave.shields.advanced_view_enabled', type: chrome.settingsPrivate.PrefType.BOOLEAN }
showAdvancedView: { key: 'brave.shields.advanced_view_enabled', type: chrome.settingsPrivate.PrefType.BOOLEAN },
statsBadgeVisible: { key: 'brave.shields.stats_badge_visible', type: chrome.settingsPrivate.PrefType.BOOLEAN }
}
export async function getViewPreferences (): Promise<GetViewPreferencesData> {
const showAdvancedViewPref = await SettingsPrivate.getPreference(settingsKeys.showAdvancedView.key)
if (showAdvancedViewPref.type !== settingsKeys.showAdvancedView.type) {
throw new Error(`Unexpected settings type received for "${settingsKeys.showAdvancedView.key}". Expected: ${settingsKeys.showAdvancedView.type}, Received: ${showAdvancedViewPref.type}`)
}
return {
showAdvancedView: showAdvancedViewPref.value
}
let newSettings = {} as GetViewPreferencesData
await Promise.all(
Object.keys(settingsKeys).map(async (name) => {
// Get setting by internal key
const pref = await SettingsPrivate.getPreference(settingsKeys[name].key)
// Validate setting type
if (pref.type !== settingsKeys[name].type) {
throw new Error(`Unexpected settings type received for "${settingsKeys[name].key}". Expected: ${settingsKeys[name].type}, Received: ${pref.type}`)
}
// Valid
newSettings[name] = pref.value
})
)
return newSettings
}

export type SetViewPreferencesData = {
showAdvancedView?: boolean
statsBadgeVisible?: boolean
}
export async function setViewPreferences (preferences: SetViewPreferencesData): Promise<void> {
const setOps = []
Expand All @@ -200,6 +210,11 @@ export async function setViewPreferences (preferences: SetViewPreferencesData):
SettingsPrivate.setPreference(settingsKeys.showAdvancedView.key, preferences.showAdvancedView)
)
}
if (preferences.statsBadgeVisible !== undefined) {
setOps.push(
SettingsPrivate.setPreference(settingsKeys.statsBadgeVisible.key, preferences.statsBadgeVisible)
)
}
await Promise.all(setOps)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +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/. */

import { debounce } from '../../../../../common/debounce'
// Types
import * as Shields from '../../types/state/shieldsPannelState'
import { SettingsData } from '../../types/other/settingsTypes'

// Helpers
import { debounce } from '../../../../../common/debounce'
const keyName = 'shields-persistent-data'

export const initialSettingsData: SettingsData = {
showAdvancedView: false,
statsBadgeVisible: true
}

export const defaultPersistentData: Shields.PersistentData = {
isFirstAccess: true
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require('./events/shieldsEvents')
require('./events/runtimeEvents')
require('./events/webNavigationEvents')
require('./events/cosmeticFilterEvents')
require('./events/settingsEvents')
// Only do detection events if the wallet API is available
if (chrome.braveWallet) {
require('./events/dappDetectionEvents')
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/* 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 settingsActions from '../actions/settingsActions'
import { settingsKeyList } from '../../helpers/settingsUtils'
import { SettingsKey } from '../../types/other/settingsTypes'

chrome.settingsPrivate.onPrefsChanged.addListener(function (settings) {
const settingsKey = settings[0].key as SettingsKey
// only call the store update if the settings change is something we care about
if (settingsKeyList.includes(settingsKey)) {
settingsActions.settingsDidChange(settings[0])
}
})
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,24 @@
* 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/. */

// Actions
import actions from '../actions/webNavigationActions'
import settingsActions from '../actions/settingsActions'

chrome.webNavigation.onBeforeNavigate.addListener(function ({ tabId, url, frameId }: chrome.webNavigation.WebNavigationParentedCallbackDetails) {
const isMainFrame: boolean = frameId === 0
actions.onBeforeNavigate(tabId, url, isMainFrame)
})

let shouldRequestSettingsData = true
chrome.webNavigation.onCommitted.addListener(function ({ tabId, url, frameId }: chrome.webNavigation.WebNavigationTransitionCallbackDetails) {
const isMainFrame: boolean = frameId === 0
actions.onCommitted(tabId, url, isMainFrame)
if (shouldRequestSettingsData) {
// check whether or not the settings store should update based on settings changes.
// this action is needed in the onCommitted phase for edge cases such as when after Brave is re-launched
settingsActions.fetchAndDispatchSettings()
// this request only needs to perform once
shouldRequestSettingsData = false
}
})
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ import * as storageAPI from '../api/storageAPI'
import * as shieldsPanelTypes from '../../constants/shieldsPanelTypes'
import * as windowTypes from '../../constants/windowTypes'
import * as tabTypes from '../../constants/tabTypes'
import * as settingsTypes from '../../constants/settingsTypes'
import * as webNavigationTypes from '../../constants/webNavigationTypes'
import { State, PersistentData } from '../../types/state/shieldsPannelState'
import { Actions } from '../../types/actions/index'
import { SettingsData } from '../../types/other/settingsTypes'

// State helpers
import * as shieldsPanelState from '../../state/shieldsPanelState'
Expand All @@ -28,9 +30,9 @@ import {
setAllowCookies,
toggleShieldsValue,
requestShieldPanelData,
setAllowScriptOriginsOnce,
onShieldsPanelShown,
reportBrokenSite
reportBrokenSite,
setAllowScriptOriginsOnce
} from '../api/shieldsAPI'
import { reloadTab } from '../api/tabsAPI'
import {
Expand All @@ -47,6 +49,7 @@ import { getHostname } from '../../helpers/urlUtils'
export default function shieldsPanelReducer (
state: State = {
persistentData: storageAPI.loadPersistentData(),
settingsData: storageAPI.initialSettingsData,
tabs: {},
windows: {},
currentWindowId: -1
Expand Down Expand Up @@ -299,6 +302,11 @@ export default function shieldsPanelReducer (
})
break
}
case settingsTypes.SET_STORE_SETTINGS_CHANGE: {
const settingsData: Partial<SettingsData> = action.settingsData
state = { ...state, settingsData: { ...state.settingsData, ...settingsData } }
break
}
// 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:
// ....................................................................................
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ Promise.all([
mountNode
)
})
.catch((e) => {
.catch((error: any) => {
console.error('Problem mounting brave shields')
console.error(e)
console.error(error)
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/* 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/. */

export const SET_STORE_SETTINGS_CHANGE = 'SET_STORE_SETTINGS_CHANGE'
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import { bindActionCreators } from 'redux'
import { connect } from 'react-redux'
import * as shieldsPanelActions from '../actions/shieldsPanelActions'
import * as settingsActions from '../actions/settingsActions'
import * as shieldsPanelState from '../state/shieldsPanelState'
import BraveShields from '../containers/braveShields'
import { State } from '../types/state/mainState'
Expand All @@ -18,12 +19,16 @@ const mapStateToProps = (
return ({
shieldsPanelTabData: shieldsPanelState.getActiveTabData(state.shieldsPanel),
persistentData: shieldsPanelState.getPersistentData(state.shieldsPanel),
settings: ownProps.settings
settingsData: shieldsPanelState.mergeSettingsData(state.shieldsPanel, ownProps.settings)
})
}

// combine all actions used outside background pages.
// for background action bindings, refer to background/index.js
const actions = Object.assign({}, shieldsPanelActions, settingsActions)

const mapDispatchToProps = (dispatch: any) => ({
actions: bindActionCreators(shieldsPanelActions, dispatch)
actions: bindActionCreators(actions, dispatch)
})

export default connect(
Expand Down
Loading

0 comments on commit 0985439

Please sign in to comment.