From 71d58218cd1d7cda002766bf50269f11fd1cebc0 Mon Sep 17 00:00:00 2001 From: NejcZdovc Date: Wed, 14 Jun 2017 13:12:27 +0200 Subject: [PATCH] Converts BraveryPanel into redux component Resolves #9454 Auditors: @bsclifton @bridiver Test Plan: --- app/common/lib/braveryPanelUtil.js | 34 ++ app/renderer/components/main/braveryPanel.js | 467 +++++++++---------- app/renderer/components/main/main.js | 15 +- 3 files changed, 266 insertions(+), 250 deletions(-) create mode 100644 app/common/lib/braveryPanelUtil.js diff --git a/app/common/lib/braveryPanelUtil.js b/app/common/lib/braveryPanelUtil.js new file mode 100644 index 00000000000..d0123e43fa5 --- /dev/null +++ b/app/common/lib/braveryPanelUtil.js @@ -0,0 +1,34 @@ +/* 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/. */ + +const Immutable = require('immutable') + +// Utils +const urlParse = require('../urlParse') + +const getDisplayHost = (lastCommittedURL) => { + const parsedUrl = urlParse(lastCommittedURL) + if (parsedUrl.protocol === 'https:' || parsedUrl.protocol === 'http:') { + return parsedUrl.host + } + + return lastCommittedURL +} + +const getRedirectedResourcesSet = (redirectedResources) => { + let result = new Immutable.List() + if (redirectedResources) { + redirectedResources.forEach((urls) => { + if (urls) { + result = result.push(urls) + } + }) + } + return result +} + +module.exports = { + getDisplayHost, + getRedirectedResourcesSet +} diff --git a/app/renderer/components/main/braveryPanel.js b/app/renderer/components/main/braveryPanel.js index 55cf870b497..246feb24f12 100644 --- a/app/renderer/components/main/braveryPanel.js +++ b/app/renderer/components/main/braveryPanel.js @@ -4,9 +4,10 @@ const React = require('react') const Immutable = require('immutable') +const {StyleSheet, css} = require('aphrodite/no-important') // Components -const ImmutableComponent = require('../immutableComponent') +const ReduxComponent = require('../reduxComponent') const Dialog = require('../common/dialog') const BrowserButton = require('../common/browserButton') const SwitchControl = require('../common/switchControl') @@ -15,29 +16,36 @@ const {BraveryPanelDropdown} = require('../common/dropdown') // Constants const config = require('../../../../js/constants/config') const settings = require('../../../../js/constants/settings') +const appConfig = require('../../../../js/constants/appConfig') // Actions const windowActions = require('../../../../js/actions/windowActions') const appActions = require('../../../../js/actions/appActions') +// State +const siteSettingsState = require('../../../common/state/siteSettingsState') +const siteSettings = require('../../../../js/state/siteSettings') +const tabState = require('../../../common/state/tabState') + // Utils const urlParse = require('../../../common/urlParse') const cx = require('../../../../js/lib/classSet') const siteUtil = require('../../../../js/state/siteUtil') -const getSetting = require('../../../../js/settings').getSetting +const {getSetting} = require('../../../../js/settings') +const frameStateUtil = require('../../../../js/state/frameStateUtil') +const braveryUtil = require('../../../common/lib/braveryPanelUtil') -const {StyleSheet, css} = require('aphrodite/no-important') +// Styles const globalStyles = require('../styles/global') const commonStyles = require('../styles/commonStyles') - const closeButton = require('../../../../img/toolbar/braveryPanel_btn.svg') -class BraveryPanel extends ImmutableComponent { +class BraveryPanel extends React.Component { constructor () { super() this.onToggleSiteSetting = this.onToggleSiteSetting.bind(this) this.onToggleAdsAndTracking = this.onToggleAdsAndTracking.bind(this) - this.onToggleHttpseList = this.onToggleHttpseList.bind(this) + this.onToggleHttpsList = this.onToggleHttpsList.bind(this) this.onToggleNoScriptList = this.onToggleNoScriptList.bind(this) this.onToggleFpList = this.onToggleFpList.bind(this) this.onToggleAdvanced = this.onToggleAdvanced.bind(this) @@ -52,134 +60,82 @@ class BraveryPanel extends ImmutableComponent { this.onEditGlobal = this.onEditGlobal.bind(this) this.onInfoClick = this.onInfoClick.bind(this) } - get isBlockingTrackedContent () { - return this.blockedByTrackingList && this.blockedByTrackingList.size > 0 - } - get blockedByTrackingList () { - return this.props.frameProps.getIn(['trackingProtection', 'blocked']) - } - get isAdvancedExpanded () { - return this.props.braveryPanelDetail.get('advancedControls') !== false - } - get blockedAds () { - return this.props.frameProps.getIn(['adblock', 'blocked']) - } - get isBlockingAds () { - return this.blockedAds && this.blockedAds.size > 0 - } - get isBlockedAdsShown () { - return this.props.braveryPanelDetail.get('expandAdblock') - } - get blockedScripts () { - return this.props.frameProps.getIn(['noScript', 'blocked']) - } - get isBlockingScripts () { - return this.blockedScripts && this.blockedScripts.size > 0 - } - get isBlockedScriptsShown () { - return this.props.braveryPanelDetail.get('expandNoScript') - } - get isBlockingFingerprinting () { - return this.blockedFingerprinting && this.blockedFingerprinting.size > 0 - } - get blockedFingerprinting () { - return this.props.frameProps.getIn(['fingerprintingProtection', 'blocked']) - } - get isHttpseShown () { - return this.props.braveryPanelDetail.get('expandHttpse') - } - get isFpShown () { - return this.props.braveryPanelDetail.get('expandFp') - } - get isPrivate () { - return this.props.frameProps.getIn(['isPrivate']) - } - get redirectedResources () { - return this.props.frameProps.get('httpsEverywhere') - } - get redirectedResourcesSet () { - let result = new Immutable.Set([]) - if (this.redirectedResources) { - this.redirectedResources.forEach((urls) => { - if (urls) { - result = result.union(urls) - } - }) - } - return result - } - get isRedirectingResources () { - return this.redirectedResources && this.redirectedResources.size > 0 - } + onToggleAdsAndTracking (e) { windowActions.setBraveryPanelDetail({ - expandAdblock: !this.isBlockedAdsShown + expandAdblock: !this.props.isBlockedAdsShown }) e.stopPropagation() } - onToggleHttpseList (e) { - if (!this.isHttpseShown && this.redirectedResources && - this.redirectedResources.size) { - // Display full list of rulesets in console for debugging - console.log('httpse rulesets', JSON.stringify(this.redirectedResources.toJS())) + + onToggleHttpsList (e) { + if (!this.props.isHttpsShown && this.props.redirectedResources && + this.props.redirectedResources.size) { + // Display full list of rule sets in console for debugging + console.log('https rule sets', JSON.stringify(this.props.redirectedResources.toJS())) } windowActions.setBraveryPanelDetail({ - expandHttpse: !this.isHttpseShown + expandHttpse: !this.props.isHttpsShown }) e.stopPropagation() } + onToggleFpList (e) { windowActions.setBraveryPanelDetail({ - expandFp: !this.isFpShown + expandFp: !this.props.isFpShown }) e.stopPropagation() } + onToggleNoScriptList (e) { windowActions.setBraveryPanelDetail({ - expandNoScript: !this.isBlockedScriptsShown + expandNoScript: !this.props.isBlockedScriptsShown }) e.stopPropagation() } + onToggleAdvanced () { windowActions.setBraveryPanelDetail({ - advancedControls: !this.isAdvancedExpanded + advancedControls: !this.props.isAdvancedExpanded }) } + onReload () { - appActions.loadURLRequested(this.props.frameProps.get('tabId'), this.props.lastCommittedURL) + appActions.loadURLRequested(this.props.tabId, this.props.lastCommittedURL) } + onEditGlobal () { appActions.createTabRequested({ url: 'about:preferences#shields' }) } + onInfoClick () { + this.onHide() appActions.createTabRequested({ url: config.fingerprintingInfoUrl }) } + + onHide () { + windowActions.setBraveryPanelDetail() + } + onToggleSiteSetting (setting, e) { - if (setting !== 'shieldsUp' && !this.props.braverySettings.shieldsUp) { + if (setting !== 'shieldsUp' && !this.props.shieldsUp) { return } + let ruleKey = siteUtil.getOrigin(this.props.lastCommittedURL) const parsedUrl = urlParse(this.props.lastCommittedURL) if (setting !== 'noScript' && (parsedUrl.protocol === 'https:' || parsedUrl.protocol === 'http:')) { ruleKey = `https?://${parsedUrl.host}` } - appActions.changeSiteSetting(ruleKey, setting, e.target.value, this.isPrivate) + appActions.changeSiteSetting(ruleKey, setting, e.target.value, this.props.isPrivate) this.onReload() } - get displayHost () { - const parsedUrl = urlParse(this.props.lastCommittedURL) - if (parsedUrl.protocol === 'https:' || parsedUrl.protocol === 'http:') { - return parsedUrl.host - } - return this.props.lastCommittedURL - } get compactBraveryPanelHeader () { - const shieldsUp = this.props.braverySettings.shieldsUp return
@@ -208,17 +164,16 @@ class BraveryPanel extends ImmutableComponent { styles.braveryPanel_compact__header__bottom )}>
-
{this.displayHost}
+
{this.props.displayHost}
} get defaultBraveryPanelHeader () { - const shieldsUp = this.props.braverySettings.shieldsUp return
-
{this.displayHost}
+
{this.props.displayHost}
} + mergeProps (state, ownProps) { + const currentWindow = state.get('currentWindow') + const activeFrame = frameStateUtil.getActiveFrame(currentWindow) || Immutable.Map() + const lastCommittedURL = frameStateUtil.getLastCommittedURL(activeFrame) + const allSiteSettings = siteSettingsState.getAllSiteSettings(state, activeFrame.get('isPrivate')) + const activeSiteSettings = siteSettings.getSiteSettingsForURL(allSiteSettings, lastCommittedURL) + const braverySettings = siteSettings.activeSettings(activeSiteSettings, state, appConfig) + const braveryPanelDetail = currentWindow.get('braveryPanelDetail', Immutable.Map()) + const redirectedResources = activeFrame.get('httpsEverywhere') + + const props = {} + // used in renderer + props.tabId = activeFrame.get('tabId', tabState.TAB_ID_NONE) + props.blockedAds = activeFrame.getIn(['adblock', 'blocked']) + props.blockedByTrackingList = activeFrame.getIn(['trackingProtection', 'blocked']) + props.blockedScripts = activeFrame.getIn(['noScript', 'blocked']) + props.blockedFingerprinting = activeFrame.getIn(['fingerprintingProtection', 'blocked']) + props.redirectedResourcesSet = braveryUtil.getRedirectedResourcesSet(redirectedResources) + props.shieldsUp = braverySettings.shieldsUp + props.noScriptEnabled = braverySettings.noScript + props.httpsEnabled = braverySettings.httpsEverywhere + props.adControl = braverySettings.adControl + props.isFpEnabled = braverySettings.fingerprintingProtection + props.cookieControl = braverySettings.cookieControl + props.safeBrowsing = braverySettings.safeBrowsing + props.isCompactBraveryPanel = getSetting(settings.COMPACT_BRAVERY_PANEL) + props.adsBlockedStat = (props.blockedAds ? props.blockedAds.size : 0) + (props.blockedByTrackingList ? props.blockedByTrackingList.size : 0) + props.scriptsBlockedStat = props.blockedScripts ? props.blockedScripts.size : 0 + props.fpBlockedStat = props.blockedFingerprinting ? props.blockedFingerprinting.size : 0 + props.httpsUpgradedResourceStat = props.redirectedResourcesSet ? props.redirectedResourcesSet.size : 0 + props.displayHost = braveryUtil.getDisplayHost(lastCommittedURL) + props.isAdvancedExpanded = braveryPanelDetail.get('advancedControls') !== false + props.isBlockedAdsShown = braveryPanelDetail.get('expandAdblock') + props.isBlockingAds = props.blockedAds && props.blockedAds.size > 0 + props.isBlockingTrackedContent = props.blockedByTrackingList && props.blockedByTrackingList.size > 0 + props.isRedirectingResources = redirectedResources && redirectedResources.size > 0 + props.isHttpsShown = braveryPanelDetail.get('expandHttps') + props.isBlockingScripts = props.blockedScripts && props.blockedScripts.size > 0 + props.isBlockedScriptsShown = braveryPanelDetail.get('expandNoScript') + props.isBlockingFingerprinting = props.blockedFingerprinting && props.blockedFingerprinting.size > 0 + props.isFpShown = braveryPanelDetail.get('expandFp') + + // used in other functions + props.lastCommittedURL = lastCommittedURL + props.isPrivate = activeFrame.get('isPrivate') + props.redirectedResources = redirectedResources // TODO (nejc) only primitives + + return props + } + render () { - const shieldsUp = this.props.braverySettings.shieldsUp - const noScriptEnabled = this.props.braverySettings.noScript - const httpseEnabled = this.props.braverySettings.httpsEverywhere - const adControl = this.props.braverySettings.adControl - const fpEnabled = this.props.braverySettings.fingerprintingProtection - const adsBlockedStat = (this.blockedAds ? this.blockedAds.size : 0) + (this.blockedByTrackingList ? this.blockedByTrackingList.size : 0) - const scriptsBlockedStat = this.blockedScripts ? this.blockedScripts.size : 0 - const fpBlockedStat = this.blockedFingerprinting ? this.blockedFingerprinting.size : 0 - const httpsUpgradedResourceStat = this.redirectedResourcesSet.size || 0 const l10nArgs = JSON.stringify({ - blockedAdCount: adsBlockedStat, - httpsUpgradeCount: httpsUpgradedResourceStat, - blockedScriptCount: scriptsBlockedStat, - blockedFingerprintCount: fpBlockedStat + blockedAdCount: this.props.adsBlockedStat, + httpsUpgradeCount: this.props.httpsUpgradedResourceStat, + blockedScriptCount: this.props.scriptsBlockedStat, + blockedFingerprintCount: this.props.fpBlockedStat }) - const compactBraveryPanel = getSetting(settings.COMPACT_BRAVERY_PANEL) - return + return
e.stopPropagation()} - data-test-id={compactBraveryPanel ? 'braveryPanelCompact' : 'braveryPanel'}> + data-test-id={this.props.isCompactBraveryPanel ? 'braveryPanelCompact' : 'braveryPanel'}> { - compactBraveryPanel + this.props.isCompactBraveryPanel ? this.compactBraveryPanelHeader : this.defaultBraveryPanelHeader }
- {adsBlockedStat} + >{this.props.adsBlockedStat}
- {httpsUpgradedResourceStat} + >{this.props.httpsUpgradedResourceStat}
- {scriptsBlockedStat} + >{this.props.scriptsBlockedStat}
- {fpBlockedStat} + >{this.props.fpBlockedStat}
{ - (this.isBlockedAdsShown && (this.isBlockingAds || this.isBlockingTrackedContent)) + (this.props.isBlockedAdsShown && (this.props.isBlockingAds || this.props.isBlockingTrackedContent)) ?
    { - this.isBlockingAds - ? this.blockedAds.map(site => + this.props.isBlockingAds + ? this.props.blockedAds.map(site =>
  • {site}
  • ) : null } { - this.isBlockingTrackedContent - ? this.blockedByTrackingList.map(site => + this.props.isBlockingTrackedContent + ? this.props.blockedByTrackingList.map(site =>
  • {site}
  • ) : null @@ -418,18 +413,18 @@ class BraveryPanel extends ImmutableComponent { : null } { - this.isRedirectingResources && this.isHttpseShown + this.props.isRedirectingResources && this.props.isHttpsShown ?
      { - this.redirectedResourcesSet.map((site) => + this.props.redirectedResourcesSet.map((site) =>
    • {site}
    • ) } @@ -437,18 +432,18 @@ class BraveryPanel extends ImmutableComponent { : null } { - this.isBlockingScripts && this.isBlockedScriptsShown + this.props.isBlockingScripts && this.props.isBlockedScriptsShown ?
        { - this.blockedScripts.map((site) => + this.props.blockedScripts.map((site) =>
      • {site}
      • ) } @@ -456,18 +451,18 @@ class BraveryPanel extends ImmutableComponent { : null } { - this.isBlockingFingerprinting && this.isFpShown + this.props.isBlockingFingerprinting && this.props.isFpShown ?
          { - this.blockedFingerprinting.map((site) => + this.props.blockedFingerprinting.map((site) =>
        • {site}
        • ) } @@ -477,42 +472,42 @@ class BraveryPanel extends ImmutableComponent {
          { - this.isAdvancedExpanded + this.props.isAdvancedExpanded ?

          - +
          - +
          @@ -598,16 +593,16 @@ class BraveryPanel extends ImmutableComponent {
          + ? : null } {