From fd9cf39715962456bd78c93ba9b5a54aa9a0708f 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 | 466 +++++++++--------- app/renderer/components/main/main.js | 15 +- .../app/common/lib/braveryPanelUtilTest.js | 47 ++ 4 files changed, 312 insertions(+), 250 deletions(-) create mode 100644 app/common/lib/braveryPanelUtil.js create mode 100644 test/unit/app/common/lib/braveryPanelUtilTest.js diff --git a/app/common/lib/braveryPanelUtil.js b/app/common/lib/braveryPanelUtil.js new file mode 100644 index 00000000000..a7f6abb7e20 --- /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 getRedirectedResources = (redirectedResources) => { + let result = new Immutable.List() + if (redirectedResources) { + redirectedResources.forEach((urls) => { + urls.forEach((url) => { + result = result.push(url) + }) + }) + } + return result +} + +module.exports = { + getDisplayHost, + getRedirectedResources +} diff --git a/app/renderer/components/main/braveryPanel.js b/app/renderer/components/main/braveryPanel.js index 55cf870b497..8b79a24e20e 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', Immutable.List()) + + const props = {} + // used in renderer + props.tabId = activeFrame.get('tabId', tabState.TAB_ID_NONE) + props.blockedAds = activeFrame.getIn(['adblock', 'blocked'], Immutable.List()) + props.blockedByTrackingList = activeFrame.getIn(['trackingProtection', 'blocked'], Immutable.List()) + props.blockedScripts = activeFrame.getIn(['noScript', 'blocked'], Immutable.List()) + props.blockedFingerprinting = activeFrame.getIn(['fingerprintingProtection', 'blocked'], Immutable.List()) + props.redirectedResources = braveryUtil.getRedirectedResources(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.size + props.blockedByTrackingList.size + props.scriptsBlockedStat = props.blockedScripts.size + props.fpBlockedStat = props.blockedFingerprinting.size + props.httpsUpgradedResourceStat = props.redirectedResources.size + props.displayHost = braveryUtil.getDisplayHost(lastCommittedURL) + props.isAdvancedExpanded = braveryPanelDetail.get('advancedControls') !== false + props.isBlockedAdsShown = braveryPanelDetail.get('expandAdblock') + props.isBlockingAds = props.blockedAds.size > 0 + props.isBlockingTrackedContent = props.blockedByTrackingList.size > 0 + props.showRedirectingResources = props.redirectedResources.size > 0 + props.isHttpsShown = braveryPanelDetail.get('expandHttpse') + props.isBlockingScripts = props.blockedScripts.size > 0 + props.isBlockedScriptsShown = braveryPanelDetail.get('expandNoScript') + props.isBlockingFingerprinting = props.blockedFingerprinting.size > 0 + props.isFpShown = braveryPanelDetail.get('expandFp') + + // used in other functions + props.lastCommittedURL = lastCommittedURL + props.isPrivate = activeFrame.get('isPrivate') + + 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 +412,18 @@ class BraveryPanel extends ImmutableComponent { : null } { - this.isRedirectingResources && this.isHttpseShown + this.props.showRedirectingResources && this.props.isHttpsShown ?
      { - this.redirectedResourcesSet.map((site) => + this.props.redirectedResources.map((site) =>
    • {site}
    • ) } @@ -437,18 +431,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 +450,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 +471,42 @@ class BraveryPanel extends ImmutableComponent {
          { - this.isAdvancedExpanded + this.props.isAdvancedExpanded ?

          - +
          - +
          @@ -598,16 +592,16 @@ class BraveryPanel extends ImmutableComponent {
          + ? : null } { diff --git a/test/unit/app/common/lib/braveryPanelUtilTest.js b/test/unit/app/common/lib/braveryPanelUtilTest.js new file mode 100644 index 00000000000..ce97cc4f039 --- /dev/null +++ b/test/unit/app/common/lib/braveryPanelUtilTest.js @@ -0,0 +1,47 @@ +/* global describe, it */ +const braveryPanelUtil = require('../../../../../app/common/lib/braveryPanelUtil') +const assert = require('assert') +const Immutable = require('immutable') + +require('../../../braveUnit') + +describe('braveryPanelUtil test', function () { + describe('getDisplayHost', function () { + it('url is http', function () { + const result = braveryPanelUtil.getDisplayHost('http://brave.com') + assert.equal(result, 'brave.com') + }) + + it('url is https', function () { + const result = braveryPanelUtil.getDisplayHost('https://brave.com') + assert.equal(result, 'brave.com') + }) + + it('url is https', function () { + const result = braveryPanelUtil.getDisplayHost('file://brave.text') + assert.equal(result, 'file://brave.text') + }) + }) + + describe('getRedirectedResources', function () { + const data = Immutable.fromJS({ + 'BootstrapCDN.xml': [ + 'http://maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css', + 'http://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css' + ], + 'GoogleAPIs.xml': [ + 'http://fonts.googleapis.com/css?family=Source+Sans+Pro' + ] + }) + + it('returns empty Immutable list if you dont provide resources', function () { + const result = braveryPanelUtil.getRedirectedResources() + assert.equal(result, Immutable.List()) + }) + + it('merge all resources into one list', function () { + const result = braveryPanelUtil.getRedirectedResources(data) + assert.equal(result.size, 3) + }) + }) +})