From 3b0dd8d58adc10fc0c4653548ba4aac0862d2d20 Mon Sep 17 00:00:00 2001 From: NejcZdovc Date: Tue, 11 Apr 2017 13:49:35 +0200 Subject: [PATCH] Frame -> redux component --- app/common/state/aboutHistoryState.js | 15 ++++ app/common/state/siteState.js | 45 +++++++++++ js/components/frame.js | 105 ++++++++++++++++++++++-- js/components/main.js | 110 +------------------------- 4 files changed, 161 insertions(+), 114 deletions(-) create mode 100644 app/common/state/siteState.js diff --git a/app/common/state/aboutHistoryState.js b/app/common/state/aboutHistoryState.js index 6f9048993e6..dd4f95bf675 100644 --- a/app/common/state/aboutHistoryState.js +++ b/app/common/state/aboutHistoryState.js @@ -4,17 +4,32 @@ const {makeImmutable} = require('./immutableUtil') const historyUtil = require('../lib/historyUtil') +const appActions = require('../../../js/actions/appActions') const aboutHistoryState = { getHistory: (state) => { state = makeImmutable(state) return state.getIn(['about', 'history']) }, + setHistory: (state) => { state = makeImmutable(state) state = state.setIn(['about', 'history', 'entries'], historyUtil.getHistory(state.get('sites'))) return state.setIn(['about', 'history', 'updatedStamp'], new Date().getTime()) + }, + + buildHistory: (state, frame) => { + if (frame.get('location') === 'about:history') { + const history = this.getHistory(state) + if (history) { + return history + } + + appActions.populateHistory() + } + + return null } } diff --git a/app/common/state/siteState.js b/app/common/state/siteState.js new file mode 100644 index 00000000000..33d8d89358f --- /dev/null +++ b/app/common/state/siteState.js @@ -0,0 +1,45 @@ +const assert = require('assert') +const Immutable = require('immutable') +const { makeImmutable, isMap, isList } = require('./immutableUtil') +const siteTags = require('../../../js/constants/siteTags') +const siteSettings = require('../../../js/state/siteSettings') +const appConfig = require('../../../js//constants/appConfig') + +const validateState = function (state) { + state = makeImmutable(state) + assert.ok(isMap(state), 'state must be an Immutable.Map') + assert.ok(isList(state.get('sites')), 'state must contain an Immutable.List of sites') + return state +} + +const siteState = { + getSites: (state) => { + state = validateState(state) + return state.get('sites') + }, + + getBookmarkFolders: (state) => { + const sites = this.getSites(state) + return sites.filter((site) => site.get('tags') + .includes(siteTags.BOOKMARK_FOLDER)) || new Immutable.Map() + }, + + getBookmarks: (state) => { + const sites = this.getSites(state) + return sites.filter((site) => site.get('tags') + .includes(siteTags.BOOKMARK)) || new Immutable.Map() + }, + + getAllSiteSettings: (state, frame) => { + if (frame && frame.get('isPrivate')) { + return state.get('siteSettings').mergeDeep(state.get('temporarySiteSettings')) + } + return state.get('siteSettings') + }, + + enabledNoScript (state, settings) { + return siteSettings.activeSettings(settings, state, appConfig).noScript === true + } +} + +module.exports = siteState diff --git a/js/components/frame.js b/js/components/frame.js index b73e602f092..5af540f0067 100644 --- a/js/components/frame.js +++ b/js/components/frame.js @@ -3,6 +3,7 @@ * You can obtain one at http://mozilla.org/MPL/2.0/. */ const React = require('react') +const ReduxComponent = require('../../app/renderer/components/reduxComponent') const urlParse = require('../../app/common/urlParse') const windowActions = require('../actions/windowActions') const appActions = require('../actions/appActions') @@ -25,7 +26,6 @@ const {aboutUrls, isSourceMagnetUrl, isSourceAboutUrl, isTargetAboutUrl, getTarg const {isFrameError, isAborted} = require('../../app/common/lib/httpUtil') const locale = require('../l10n') const appConfig = require('../constants/appConfig') -const {getSiteSettingsForHostPattern} = require('../state/siteSettings') const {currentWindowWebContents, isFocused} = require('../../app/renderer/currentWindow') const windowStore = require('../stores/windowStore') const appStoreRenderer = require('../stores/appStoreRenderer') @@ -33,6 +33,9 @@ const siteSettings = require('../state/siteSettings') const {newTabMode} = require('../../app/common/constants/settingsEnums') const imageUtil = require('../lib/imageUtil') const MessageBox = require('../../app/renderer/components/messageBox') +const siteState = require('../../app/common/state/siteState') +const tabState = require('../../app/common/state/tabState') +const aboutHistoryState = require('../../app/common/state/aboutHistoryState') const WEBRTC_DEFAULT = 'default' const WEBRTC_DISABLE_NON_PROXY = 'disable_non_proxied_udp' @@ -245,7 +248,7 @@ class Frame extends ImmutableComponent { } runInsecureContent () { - const activeSiteSettings = getSiteSettingsForHostPattern(this.props.allSiteSettings, this.origin) + const activeSiteSettings = siteSettings.getSiteSettingsForHostPattern(this.props.allSiteSettings, this.origin) return activeSiteSettings === undefined ? false : activeSiteSettings.get('runInsecureContent') } @@ -262,7 +265,7 @@ class Frame extends ImmutableComponent { if (!this.props.allSiteSettings) { return false } - const activeSiteSettings = getSiteSettingsForHostPattern(this.props.allSiteSettings, + const activeSiteSettings = siteSettings.getSiteSettingsForHostPattern(this.props.allSiteSettings, origin) if (activeSiteSettings && typeof activeSiteSettings.get('widevine') === 'number') { return true @@ -273,7 +276,7 @@ class Frame extends ImmutableComponent { expireContentSettings (origin) { // Expired Flash settings should be deleted when the webview is // navigated or closed. Same for NoScript's allow-once option. - const activeSiteSettings = getSiteSettingsForHostPattern(this.props.allSiteSettings, + const activeSiteSettings = siteSettings.getSiteSettingsForHostPattern(this.props.allSiteSettings, origin) if (!activeSiteSettings) { return @@ -1128,7 +1131,7 @@ class Frame extends ImmutableComponent { onFindAgain (forward) { if (!this.props.findbarShown) { - windowActions.setFindbarShown(this.frame, true) + windowActions.setFindbarShown(this.props.frame, true) } const searchString = this.props.findDetail && this.props.findDetail.get('searchString') if (searchString) { @@ -1164,10 +1167,96 @@ class Frame extends ImmutableComponent { } } + mergeProps (state, dispatchProps, ownProps) { + const currentWindow = state.get('currentWindow') + const frame = frameStateUtil.getFrameByKey(currentWindow, ownProps.frameKey) + const activeFrame = frameStateUtil.getActiveFrame(currentWindow) + const emptyMap = new Immutable.Map() + const allSiteSettings = siteState.getAllSiteSettings(state, activeFrame) + const frameSiteSettings = frame.get('location') + ? siteSettings.getSiteSettingsForURL(allSiteSettings, frame.get('location')) + : undefined + const baseUrl = getBaseUrl(frame.get('location')) + + const props = { + frame, + tabIndex: frameStateUtil.getFrameIndex(currentWindow, frame.get('key')), + tabData: frame.get('tabId') ? tabState.getByTabId(state, frame.get('tabId')) : null, + urlBarFocused: activeFrame && activeFrame.getIn(['navbar', 'urlbar', 'focused']), + contextMenuDetail: currentWindow.get('contextMenuDetail'), + partition: frameStateUtil.getPartition(frame), + settings: ['about:preferences', 'about:history', 'about:adblock'].includes(baseUrl) + ? state.get('settings') || emptyMap + : null, + bookmarks: frame.get('location') === 'about:bookmarks' + ? siteState.getBookmarks(state) + : null, + history: aboutHistoryState.buildHistory(state, frame), + extensions: ['about:extensions', 'about:preferences'].includes(baseUrl) + ? state.get('extensions') || emptyMap + : null, + preferencesData: frame.get('location') === 'about:preferences#payments' + ? state.getIn(['about', 'preferences']) || emptyMap + : null, + downloads: state.get('downloads') || emptyMap, + bookmarkFolders: frame.get('location') === 'about:bookmarks' + ? siteState.getBookmarkFolders(state) + : null, + isFullScreen: frame.get('isFullScreen'), + isSecure: frame.getIn(['security', 'isSecure']), + showFullScreenWarning: frame.get('showFullScreenWarning'), + findbarShown: frame.get('findbarShown'), + findDetail: frame.get('findDetail'), + hrefPreview: frame.get('hrefPreview'), + showOnRight: frame.get('showOnRight'), + location: frame.get('location'), + isPrivate: frame.get('isPrivate'), + partitionNumber: frame.get('partitionNumber'), + activeShortcut: frame.get('activeShortcut'), + activeShortcutDetails: frame.get('activeShortcutDetails'), + provisionalLocation: frame.get('provisionalLocation'), + pinnedLocation: frame.get('pinnedLocation'), + src: frame.get('src'), + guestInstanceId: frame.get('guestInstanceId'), + tabId: frame.get('tabId'), + aboutDetails: frame.get('aboutDetails'), + unloaded: frame.get('unloaded'), + audioMuted: frame.get('audioMuted'), + passwords: state.get('passwords'), + adblock: state.get('adblock'), + safeBrowsing: state.get('safeBrowsing'), + httpsEverywhere: state.get('httpsEverywhere'), + trackingProtection: state.get('trackingProtection'), + adInsertion: state.get('adInsertion'), + noScript: state.get('noScript'), + flash: state.get('flash'), + widevine: state.get('widevine'), + cookieblock: state.get('cookieblock'), + allSiteSettings: allSiteSettings, + sync: state.get('sync') || new Immutable.Map(), + ledgerInfo: state.get('ledgerInfo') || new Immutable.Map(), + publisherInfo: state.get('publisherInfo') || new Immutable.Map(), + versionInformation: state.getIn(['about', 'brave', 'versionInformation']), + braveryDefaults: Immutable.fromJS(siteSettings.braveryDefaults(state, appConfig)), + isPreview: frame.get('key') === currentWindow.get('previewFrameKey'), + isActive: frameStateUtil.isFrameKeyActive(currentWindow, frame.get('key')), + autofillCreditCards: state.getIn(['autofill', 'creditCards']), + autofillAddresses: state.getIn(['autofill', 'addresses']), + adblockCount: state.getIn(['adblock', 'count']), + trackedBlockersCount: state.getIn(['trackingProtection', 'count']), + httpsUpgradedCount: state.getIn(['httpsEverywhere', 'count']), + newTabDetail: frame.get('location') === 'about:newtab' ? state.getIn(['about', 'newtab']) : null, + frameSiteSettings: frameSiteSettings, + enableNoScript: siteState.enabledNoScript(state, frameSiteSettings) + } + + return Object.assign({}, ownProps, props) + } + render () { const messageBoxDetail = this.tab && this.tab.get('messageBoxDetail') return
: null } @@ -1205,4 +1294,4 @@ class Frame extends ImmutableComponent { } } -module.exports = Frame +module.exports = ReduxComponent.connect(Frame) diff --git a/js/components/main.js b/js/components/main.js index 5395c3bb040..7c773e04e47 100644 --- a/js/components/main.js +++ b/js/components/main.js @@ -51,7 +51,6 @@ const CheckDefaultBrowserDialog = require('../../app/renderer/components/checkDe const appConfig = require('../constants/appConfig') const messages = require('../constants/messages') const settings = require('../constants/settings') -const siteTags = require('../constants/siteTags') const dragTypes = require('../constants/dragTypes') const keyCodes = require('../../app/common/constants/keyCodes') const keyLocations = require('../../app/common/constants/keyLocations') @@ -61,11 +60,11 @@ const {bookmarksToolbarMode} = require('../../app/common/constants/settingsEnums // State handling const basicAuthState = require('../../app/common/state/basicAuthState') const extensionState = require('../../app/common/state/extensionState') -const aboutHistoryState = require('../../app/common/state/aboutHistoryState') const frameStateUtil = require('../state/frameStateUtil') const siteUtil = require('../state/siteUtil') const searchProviders = require('../data/searchProviders') const defaultBrowserState = require('../../app/common/state/defaultBrowserState') +const siteState = require('../../app/common/state/siteState') // Util const _ = require('underscore') @@ -76,7 +75,6 @@ const siteSettings = require('../state/siteSettings') const urlParse = require('../../app/common/urlParse') const debounce = require('../lib/debounce') const {currentWindow, isMaximized, isFocused, isFullScreen} = require('../../app/renderer/currentWindow') -const emptyMap = new Immutable.Map() const emptyList = new Immutable.List() const {makeImmutable} = require('../../app/common/state/immutableUtil') const platformUtil = require('../../app/common/lib/platformUtil') @@ -701,10 +699,6 @@ class Main extends ImmutableComponent { windowActions.setModalDialogDetail('checkDefaultBrowserDialog') } - enableNoScript (settings) { - return siteSettings.activeSettings(settings, this.props.appState, appConfig).noScript === true - } - onCloseFrame (activeFrameProps, forceClose = false) { windowActions.closeFrame(frameStateUtil.getFrames(this.props.windowState), activeFrameProps, forceClose) } @@ -803,10 +797,7 @@ class Main extends ImmutableComponent { get allSiteSettings () { const activeFrame = frameStateUtil.getActiveFrame(this.props.windowState) - if (activeFrame && activeFrame.get('isPrivate')) { - return this.props.appState.get('siteSettings').mergeDeep(this.props.appState.get('temporarySiteSettings')) - } - return this.props.appState.get('siteSettings') + return siteState.getAllSiteSettings(this.props.appState, activeFrame) } frameSiteSettings (location) { @@ -816,12 +807,6 @@ class Main extends ImmutableComponent { return siteSettings.getSiteSettingsForURL(this.allSiteSettings, location) } - frameBraverySettings (location) { - return Immutable.fromJS(siteSettings.activeSettings(this.frameSiteSettings(location), - this.props.appState, - appConfig)) - } - get activeTabId () { const activeFrame = frameStateUtil.getActiveFrame(this.props.windowState) return activeFrame && activeFrame.get('tabId') @@ -879,17 +864,6 @@ class Main extends ImmutableComponent { } } - bindHistory (frame) { - if (frame.get('location') === 'about:history') { - const history = aboutHistoryState.getHistory(this.props.appState) - if (history) { - return history - } - appActions.populateHistory() - } - return null - } - getTotalBlocks (frames) { if (!frames) { return false @@ -921,7 +895,6 @@ class Main extends ImmutableComponent { const sortedFrames = frameStateUtil.getSortedFrames(this.props.windowState) const activeFrame = frameStateUtil.getActiveFrame(this.props.windowState) this.frames = {} - const allSiteSettings = this.allSiteSettings const activeSiteSettings = this.activeSiteSettings const nonPinnedFrames = frameStateUtil.getNonPinnedFrames(this.props.windowState) const tabsPerPage = Number(getSetting(settings.TABS_PER_PAGE)) @@ -946,8 +919,6 @@ class Main extends ImmutableComponent { const braverySettings = siteSettings.activeSettings(activeSiteSettings, this.props.appState, appConfig) const loginRequiredDetail = activeFrame ? basicAuthState.getLoginRequiredDetail(this.props.appState, activeFrame.get('tabId')) : null const customTitlebar = this.customTitlebar - const versionInformation = this.props.appState.getIn(['about', 'brave', 'versionInformation']) - const braveryDefaults = Immutable.fromJS(siteSettings.braveryDefaults(this.props.appState, appConfig)) const contextMenuDetail = this.props.windowState.get('contextMenuDetail') const shouldAllowWindowDrag = !contextMenuDetail && !this.props.windowState.get('bookmarkDetail') && @@ -1063,7 +1034,7 @@ class Main extends ImmutableComponent { bookmarkDetail={this.props.windowState.get('bookmarkDetail')} mouseInTitlebar={this.props.windowState.getIn(['ui', 'mouseInTitlebar'])} searchDetail={this.props.windowState.get('searchDetail')} - enableNoScript={this.enableNoScript(activeSiteSettings)} + enableNoScript={siteState.enabledNoScript(this.props.appState, activeSiteSettings)} settings={this.props.appState.get('settings')} noScriptIsVisible={noScriptIsVisible} menubarVisible={customTitlebar.menubarVisible} @@ -1292,83 +1263,10 @@ class Main extends ImmutableComponent { sortedFrames.map((frame) => { this.frames[frame.get('key')] = node }} - tabData={this.props.appState.get('tabs').find((tab) => tab.get('tabId') === frame.get('tabId'))} - urlBarFocused={activeFrame && activeFrame.getIn(['navbar', 'urlbar', 'focused'])} - tabIndex={frameStateUtil.getFrameIndex(this.props.windowState, frame.get('key'))} - prefOpenInForeground={getSetting(settings.SWITCH_TO_NEW_TABS)} - onCloseFrame={this.onCloseFrame} frameKey={frame.get('key')} - contextMenuDetail={contextMenuDetail} - partition={frameStateUtil.getPartition(frame)} key={frame.get('key')} - settings={['about:preferences', 'about:history', 'about:adblock'].includes(getBaseUrl(frame.get('location'))) - ? this.props.appState.get('settings') || emptyMap - : null} - bookmarks={frame.get('location') === 'about:bookmarks' - ? appStateSites - .filter((site) => site.get('tags') - .includes(siteTags.BOOKMARK)) || emptyMap - : null} - history={this.bindHistory(frame)} - extensions={['about:extensions', 'about:preferences'].includes(getBaseUrl(frame.get('location'))) - ? this.props.appState.get('extensions') || emptyMap - : null} - preferencesData={frame.get('location') === 'about:preferences#payments' - ? this.props.appState.getIn(['about', 'preferences']) || emptyMap - : null} - downloads={this.props.appState.get('downloads') || emptyMap} - bookmarkFolders={frame.get('location') === 'about:bookmarks' - ? appStateSites - .filter((site) => site.get('tags') - .includes(siteTags.BOOKMARK_FOLDER)) || emptyMap - : null} - isFullScreen={frame.get('isFullScreen')} - isSecure={frame.getIn(['security', 'isSecure'])} - showFullScreenWarning={frame.get('showFullScreenWarning')} - findbarShown={frame.get('findbarShown')} - findDetail={frame.get('findDetail')} - hrefPreview={frame.get('hrefPreview')} - showOnRight={frame.get('showOnRight')} - location={frame.get('location')} - isPrivate={frame.get('isPrivate')} - partitionNumber={frame.get('partitionNumber')} - activeShortcut={frame.get('activeShortcut')} - activeShortcutDetails={frame.get('activeShortcutDetails')} - provisionalLocation={frame.get('provisionalLocation')} - pinnedLocation={frame.get('pinnedLocation')} - src={frame.get('src')} - guestInstanceId={frame.get('guestInstanceId')} - tabId={frame.get('tabId')} - aboutDetails={frame.get('aboutDetails')} - unloaded={frame.get('unloaded')} - audioMuted={frame.get('audioMuted')} - passwords={this.props.appState.get('passwords')} - adblock={this.props.appState.get('adblock')} - safeBrowsing={this.props.appState.get('safeBrowsing')} - httpsEverywhere={this.props.appState.get('httpsEverywhere')} - trackingProtection={this.props.appState.get('trackingProtection')} - adInsertion={this.props.appState.get('adInsertion')} - noScript={this.props.appState.get('noScript')} - flash={this.props.appState.get('flash')} - widevine={this.props.appState.get('widevine')} - cookieblock={this.props.appState.get('cookieblock')} - allSiteSettings={allSiteSettings} - sync={this.props.appState.get('sync') || new Immutable.Map()} - ledgerInfo={this.props.appState.get('ledgerInfo') || new Immutable.Map()} - publisherInfo={this.props.appState.get('publisherInfo') || new Immutable.Map()} - frameSiteSettings={this.frameSiteSettings(frame.get('location'))} + onCloseFrame={this.onCloseFrame} onFindHide={this.onFindHide} - enableNoScript={this.enableNoScript(this.frameSiteSettings(frame.get('location')))} - versionInformation={versionInformation} - braveryDefaults={braveryDefaults} - isPreview={frame.get('key') === this.props.windowState.get('previewFrameKey')} - isActive={frameStateUtil.isFrameKeyActive(this.props.windowState, frame.get('key'))} - autofillCreditCards={this.props.appState.getIn(['autofill', 'creditCards'])} - autofillAddresses={this.props.appState.getIn(['autofill', 'addresses'])} - adblockCount={this.props.appState.getIn(['adblock', 'count'])} - trackedBlockersCount={this.props.appState.getIn(['trackingProtection', 'count'])} - httpsUpgradedCount={this.props.appState.getIn(['httpsEverywhere', 'count'])} - newTabDetail={frame.get('location') === 'about:newtab' ? this.props.appState.getIn(['about', 'newtab']) : null} />) }