From 966e40341299f6e9912a80e35273ff864fde0dfd Mon Sep 17 00:00:00 2001 From: NejcZdovc Date: Thu, 15 Jun 2017 15:12:58 +0200 Subject: [PATCH] Convert NoScriptInfo into redux Resolves #9447 Auditors: @bsclifton @bridiver Test Plan: --- app/renderer/components/main/main.js | 12 +- app/renderer/components/main/noScriptInfo.js | 158 ++++++++++++------- 2 files changed, 101 insertions(+), 69 deletions(-) diff --git a/app/renderer/components/main/main.js b/app/renderer/components/main/main.js index fe99991f648..f19ed110021 100644 --- a/app/renderer/components/main/main.js +++ b/app/renderer/components/main/main.js @@ -82,7 +82,6 @@ class Main extends ImmutableComponent { this.onHideWidevinePanel = this.onHideWidevinePanel.bind(this) this.onHideAutofillAddressPanel = this.onHideAutofillAddressPanel.bind(this) this.onHideAutofillCreditCardPanel = this.onHideAutofillCreditCardPanel.bind(this) - this.onHideNoScript = this.onHideNoScript.bind(this) this.onHideReleaseNotes = this.onHideReleaseNotes.bind(this) this.onHideCheckDefaultBrowserDialog = this.onHideCheckDefaultBrowserDialog.bind(this) this.onTabContextMenu = this.onTabContextMenu.bind(this) @@ -560,10 +559,6 @@ class Main extends ImmutableComponent { windowActions.setAutofillCreditCardDetail() } - onHideNoScript () { - windowActions.setNoScriptVisible(false) - } - onHideReleaseNotes () { windowActions.setReleaseNotesVisible(false) } @@ -679,7 +674,8 @@ class Main extends ImmutableComponent { const widevinePanelIsVisible = this.props.windowState.getIn(['widevinePanelDetail', 'shown']) const autofillAddressPanelIsVisible = this.props.windowState.get('autofillAddressDetail') const autofillCreditCardPanelIsVisible = this.props.windowState.get('autofillCreditCardDetail') - const noScriptIsVisible = this.props.windowState.getIn(['ui', 'noScriptInfo', 'isVisible']) + const noScriptIsVisible = this.props.windowState.getIn(['ui', 'noScriptInfo', 'isVisible']) && + siteUtil.getOrigin(activeFrame.get('location')) const releaseNotesIsVisible = this.props.windowState.getIn(['ui', 'releaseNotes', 'isVisible']) const checkDefaultBrowserDialogIsVisible = isFocused() && defaultBrowserState.shouldDisplayDialog(this.props.appState) @@ -785,9 +781,7 @@ class Main extends ImmutableComponent { } { noScriptIsVisible - ? + ? : null } { diff --git a/app/renderer/components/main/noScriptInfo.js b/app/renderer/components/main/noScriptInfo.js index 9cd01afa42a..bd6ea1965b6 100644 --- a/app/renderer/components/main/noScriptInfo.js +++ b/app/renderer/components/main/noScriptInfo.js @@ -3,11 +3,11 @@ * You can obtain one at http://mozilla.org/MPL/2.0/. */ const React = require('react') -const PropTypes = require('prop-types') const Immutable = require('immutable') const urlParse = require('../../../common/urlParse') // Components +const ReduxComponent = require('../reduxComponent') const ImmutableComponent = require('../immutableComponent') const Dialog = require('../common/dialog') const BrowserButton = require('../common/browserButton') @@ -15,14 +15,19 @@ const BrowserButton = require('../common/browserButton') // Actions const appActions = require('../../../../js/actions/appActions') const tabActions = require('../../../common/actions/tabActions') +const windowActions = require('../../../../js/actions/windowActions') + +// State +const tabState = require('../../../common/state/tabState') // Utils const siteUtil = require('../../../../js/state/siteUtil') +const frameStateUtil = require('../../../../js/state/frameStateUtil') class NoScriptCheckbox extends ImmutableComponent { toggleCheckbox (e) { - this.checkbox.checked = !this.checkbox.checked e.stopPropagation() + this.checkbox.checked = !this.checkbox.checked } get id () { @@ -30,32 +35,27 @@ class NoScriptCheckbox extends ImmutableComponent { } render () { - return
- { e.stopPropagation() }} - ref={(node) => { this.checkbox = node }} defaultChecked - origin={this.props.origin} /> + return
+ { e.stopPropagation() }} + ref={(node) => { this.checkbox = node }} + defaultChecked + origin={this.props.origin} + />
} } -class NoScriptInfo extends ImmutableComponent { - get blockedOrigins () { - const blocked = this.props.frameProps.getIn(['noScript', 'blocked']) - if (blocked && blocked.size) { - return new Immutable.Set(blocked.map(siteUtil.getOrigin)) - } else { - return new Immutable.Set() - } - } - - get origin () { - return siteUtil.getOrigin(this.props.frameProps.get('location')) - } - - get isPrivate () { - return this.props.frameProps.get('isPrivate') +class NoScriptInfo extends React.Component { + constructor (props) { + super(props) + this.unselectAll = this.unselectAll.bind(this) } onClickInner (e) { @@ -65,22 +65,23 @@ class NoScriptInfo extends ImmutableComponent { unselectAll (e) { e.stopPropagation() let checkboxes = this.checkboxes.querySelectorAll('input') - if (!checkboxes) { - return + + if (checkboxes) { + checkboxes.forEach((box) => { + box.checked = false + }) } - checkboxes.forEach((box) => { - box.checked = false - }) } reload () { - tabActions.reload(this.props.frameProps.get('tabId')) + tabActions.reload(this.props.activeTabId) } - onAllow (setting, e) { - if (!this.origin) { + onAllow (setting) { + if (!this.props.origin) { return } + let checkedOrigins = new Immutable.Map() this.checkboxes.querySelectorAll('input').forEach((box) => { const origin = box.getAttribute('origin') @@ -88,53 +89,90 @@ class NoScriptInfo extends ImmutableComponent { checkedOrigins = checkedOrigins.set(origin, box.checked ? setting : false) } }) + if (checkedOrigins.filter((value) => value !== false).size) { - appActions.noScriptExceptionsAdded(this.origin, checkedOrigins, this.isPrivate) + appActions.noScriptExceptionsAdded(this.props.origin, checkedOrigins, this.props.isPrivate) this.reload() - this.props.onHide() + this.onHide() } } - get buttons () { - return
- - {this.isPrivate - ? null - : - } -
+ onHide () { + windowActions.setNoScriptVisible(false) } - render () { - if (!this.origin) { - return null + mergeProps (state, ownProps) { + const currentWindow = state.get('currentWindow') + const activeFrame = frameStateUtil.getActiveFrame(currentWindow) || Immutable.Map() + const blocked = activeFrame.getIn(['noScript', 'blocked']) + let blockedOrigins = Immutable.List() + + if (blocked && blocked.size) { + const originsSet = Immutable.Set(blocked.map(siteUtil.getOrigin)) + blockedOrigins = Immutable.List(originsSet.toJS()) } + + const props = {} + // used in renderer + props.siteHost = urlParse(activeFrame.get('location')).host + props.showBlocks = blocked && blockedOrigins.size + props.blockedOrigins = blockedOrigins + props.isPrivate = activeFrame.get('isPrivate') + + // Used in other function + props.activeTabId = activeFrame.get('tabId', tabState.TAB_ID_NONE) + props.origin = siteUtil.getOrigin(activeFrame.get('location')) + + return props + } + + render () { const l10nArgs = { - site: urlParse(this.props.frameProps.get('location')).host + site: this.props.siteHost } - return + + return
-
- {this.blockedOrigins.size +
+ { + this.props.showBlocks ?
{ this.checkboxes = node }} className='blockedOriginsList'> - {this.blockedOrigins.map((origin) => )} + { + this.props.blockedOrigins.map((origin) => ) + }
-
- {this.buttons} + onClick={this.unselectAll} + /> +
+ + { + !this.props.isPrivate + ? + : null + } +
- : null} + : null + }
} } -NoScriptInfo.propTypes = { - frameProps: PropTypes.object, - onHide: PropTypes.func -} - -module.exports = NoScriptInfo +module.exports = ReduxComponent.connect(NoScriptInfo)