Skip to content

Commit

Permalink
Moves eventStore into appStore
Browse files Browse the repository at this point in the history
Resolves brave#11009
  • Loading branch information
NejcZdovc authored and syuan100 committed Nov 9, 2017
1 parent f1ad3d1 commit 562ed05
Show file tree
Hide file tree
Showing 20 changed files with 3,551 additions and 2,265 deletions.
221 changes: 221 additions & 0 deletions app/browser/reducers/ledgerReducer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,221 @@
/* This SourceCode 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')
const underscore = require('underscore')

// Constants
const appConstants = require('../../../js/constants/appConstants')
const windowConstants = require('../../../js/constants/windowConstants')
const settings = require('../../../js/constants/settings')

// State
const ledgerState = require('../../common/state/ledgerState')

// Utils
const ledgerUtil = require('../../common/lib/ledgerUtil')
const {makeImmutable} = require('../../common/state/immutableUtil')
const getSetting = require('../../../js/settings').getSetting

const ledgerReducer = (state, action, immutableAction) => {
action = immutableAction || makeImmutable(action)
switch (action.get('actionType')) {
case appConstants.APP_UPDATE_LEDGER_INFO:
{
state = state.setIn(['ledger', 'info'], action.get('ledgerInfo'))
break
}
// TODO refactor
case appConstants.APP_UPDATE_LOCATION_INFO:
{
state = state.setIn(['ledger', 'locations'], action.get('locationInfo'))
break
}
case appConstants.APP_LEDGER_RECOVERY_STATUS_CHANGED:
{
state = ledgerState.setRecoveryStatus(state, action.get('recoverySucceeded'))
break
}
case appConstants.APP_SET_STATE:
{
state = ledgerUtil.init(state)
break
}
case appConstants.APP_BACKUP_KEYS:
{
ledgerUtil.backupKeys(state, action.get('backupAction'))
break
}
case appConstants.APP_RECOVER_WALLET:
{
state = ledgerUtil.recoverKeys(
state,
action.get('useRecoveryKeyFile'),
action.get('firstRecoveryKey'),
action.get('secondRecoveryKey')
)
break
}
case appConstants.APP_SHUTTING_DOWN:
{
state = ledgerUtil.quit(state)
break
}
case appConstants.APP_ON_CLEAR_BROWSING_DATA:
{
const defaults = state.get('clearBrowsingDataDefaults')
const temp = state.get('tempClearBrowsingData', Immutable.Map())
const clearData = defaults ? defaults.merge(temp) : temp
if (clearData.get('browserHistory') && !getSetting(settings.PAYMENTS_ENABLED)) {
state = ledgerState.resetSynopsis(state)
}
break
}
// TODO not sure that we use APP_IDLE_STATE_CHANGED anymore
case appConstants.APP_IDLE_STATE_CHANGED:
{
state = ledgerUtil.pageDataChanged(state)
ledgerUtil.addVisit('NOOP', underscore.now(), null)
break
}
case appConstants.APP_CHANGE_SETTING:
{
switch (action.get('key')) {
case settings.PAYMENTS_ENABLED:
{
state = ledgerUtil.initialize(state, action.get('value'))
break
}
case settings.PAYMENTS_CONTRIBUTION_AMOUNT:
{
ledgerUtil.setPaymentInfo(action.get('value'))
break
}
case settings.PAYMENTS_MINIMUM_VISIT_TIME:
{
const value = action.get('value')
if (value <= 0) break
ledgerUtil.synopsis.options.minPublisherDuration = action.value
state = ledgerState.setSynopsisOption(state, 'minPublisherDuration', value)
break
}
case settings.PAYMENTS_MINIMUM_VISITS:
{
const value = action.get('value')
if (value <= 0) break

ledgerUtil.synopsis.options.minPublisherVisits = value
state = ledgerState.setSynopsisOption(state, 'minPublisherVisits', value)
break
}

case settings.PAYMENTS_ALLOW_NON_VERIFIED:
{
const value = action.get('value')
ledgerUtil.synopsis.options.showOnlyVerified = value
state = ledgerState.setSynopsisOption(state, 'showOnlyVerified', value)
break
}
}
break
}
case appConstants.APP_CHANGE_SITE_SETTING:
{
const pattern = action.get('hostPattern')
if (!pattern) {
console.warn('Changing site settings should always have a hostPattern')
break
}
const i = pattern.indexOf('://')
if (i === -1) break

const publisherKey = pattern.substr(i + 3)
switch (action.get('key')) {
case 'ledgerPaymentsShown':
{
if (action.get('value') === false) {
delete ledgerUtil.synopsis.publishers[publisherKey]
state = ledgerState.deletePublishers(state, publisherKey)
state = ledgerUtil.updatePublisherInfo(state)
}
break
}
case 'ledgerPayments':
{
const publisher = ledgerState.getPublisher(state, publisherKey)
if (publisher.isEmpty()) {
break
}
state = ledgerUtil.updatePublisherInfo(state)
state = ledgerUtil.verifiedP(state, publisherKey)
break
}
case 'ledgerPinPercentage':
{
const publisher = ledgerState.getPublisher(state, publisherKey)
if (publisher.isEmpty()) {
break
}

ledgerUtil.synopsis.publishers[publisherKey].pinPercentage = action.get('value')
state = ledgerUtil.updatePublisherInfo(state, publisherKey)
break
}
}
break
}
case appConstants.APP_REMOVE_SITE_SETTING:
{
const pattern = action.get('hostPattern')
if (!pattern) {
console.warn('Changing site settings should always have a hostPattern')
break
}

const i = pattern.indexOf('://')
if (i === -1) break

const publisherKey = pattern.substr(i + 3)
if (action.get('key') === 'ledgerPayments') {
const publisher = ledgerState.getPublisher(state, publisherKey)
if (publisher.isEmpty()) {
break
}
state = ledgerUtil.updatePublisherInfo(state)
}
break
}
case appConstants.APP_NETWORK_CONNECTED:
{
setTimeout((state) => {
ledgerUtil.networkConnected(state)
}, 1000, state)
break
}
case appConstants.APP_NAVIGATOR_HANDLER_REGISTERED:
{
const hasBitcoinHandler = (action.get('protocol') === 'bitcoin')
state = ledgerState.setInfoProp(state, 'hasBitcoinHandler', hasBitcoinHandler)
break
}
case appConstants.APP_NAVIGATOR_HANDLER_UNREGISTERED:
{
const hasBitcoinHandler = false
state = ledgerState.setInfoProp(state, 'hasBitcoinHandler', hasBitcoinHandler)
break
}
case 'event-set-page-info':
case appConstants.APP_WINDOW_BLURRED:
case appConstants.APP_CLOSE_WINDOW:
case windowConstants.WINDOW_SET_FOCUSED_FRAME:
case windowConstants.WINDOW_GOT_RESPONSE_DETAILS:
{
state = ledgerUtil.pageDataChanged(state)
break
}
}
return state
}

module.exports = ledgerReducer
90 changes: 90 additions & 0 deletions app/browser/reducers/pageDataReducer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/* 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 electron = require('electron')
const BrowserWindow = electron.BrowserWindow

// Constants
const appConstants = require('../../../js/constants/appConstants')
const windowConstants = require('../../../js/constants/windowConstants')

// State
const pageDataState = require('../../common/state/pageDataState')

// Utils
const {makeImmutable} = require('../../common/state/immutableUtil')
const {isSourceAboutUrl} = require('../../../js/lib/appUrlUtil')
const {responseHasContent} = require('../../common/lib/httpUtil')

const pageDataReducer = (state, action, immutableAction) => {
action = immutableAction || makeImmutable(action)
switch (action.get('actionType')) {
case windowConstants.WINDOW_SET_FOCUSED_FRAME:
{
if (action.get('location')) {
state = pageDataState.addView(state, action.get('location'), action.get('tabId'))
}
break
}
case appConstants.APP_WINDOW_BLURRED:
{
let windowCount = BrowserWindow.getAllWindows().filter((win) => win.isFocused()).length
if (windowCount === 0) {
state = pageDataState.addView(state)
}
break
}
// TODO check if this is used anymore
case appConstants.APP_IDLE_STATE_CHANGED:
{
if (action.has('idleState') && action.get('idleState') !== 'active') {
state = pageDataState.addView(state)
}
break
}
case appConstants.APP_WINDOW_CLOSED:
{
state = pageDataState.addView(state)
break
}
case 'event-set-page-info':
{
// retains all past pages, not really sure that's needed... [MTR]
state = pageDataState.addInfo(state, action.get('pageInfo'))
break
}
case windowConstants.WINDOW_GOT_RESPONSE_DETAILS:
{
// Only capture response for the page (not subresources, like images, JavaScript, etc)
if (action.getIn(['details', 'resourceType']) === 'mainFrame') {
const pageUrl = action.getIn(['details', 'newURL'])

// create a page view event if this is a page load on the active tabId
const lastActiveTabId = pageDataState.getLastActiveTabId(state)
const tabId = action.get('tabId')
if (!lastActiveTabId || tabId === lastActiveTabId) {
state = pageDataState.addView(state, pageUrl, tabId)
}

const responseCode = action.getIn(['details', 'httpResponseCode'])
if (isSourceAboutUrl(pageUrl) || !responseHasContent(responseCode)) {
break
}

const pageLoadEvent = makeImmutable({
timestamp: new Date().getTime(),
url: pageUrl,
tabId: tabId,
details: action.get('details')
})
state = pageDataState.addLoad(state, pageLoadEvent)
}
break
}
}

return state
}

module.exports = pageDataReducer
86 changes: 86 additions & 0 deletions app/browser/reducers/siteSettingsReducer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/* This SourceCode 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/. */

'use strict'
const Immutable = require('immutable')
const appConstants = require('../../../js/constants/appConstants')
const siteSettings = require('../../../js/state/siteSettings')
const urlUtil = require('../../../js/lib/urlutil')
const {makeImmutable} = require('../../common/state/immutableUtil')

const siteSettingsReducer = (state, action, immutableAction) => {
action = immutableAction || makeImmutable(action)
switch (action.get('actionType')) {
case appConstants.APP_ALLOW_FLASH_ONCE:
{
const propertyName = action.get('isPrivate') ? 'temporarySiteSettings' : 'siteSettings'
state = state.set(propertyName,
siteSettings.mergeSiteSetting(state.get(propertyName), urlUtil.getOrigin(action.get('url')), 'flash', 1))
break
}
case appConstants.APP_ALLOW_FLASH_ALWAYS:
{
const propertyName = action.get('isPrivate') ? 'temporarySiteSettings' : 'siteSettings'
const expirationTime = Date.now() + (7 * 24 * 3600 * 1000)
state = state.set(propertyName,
siteSettings.mergeSiteSetting(state.get(propertyName), urlUtil.getOrigin(action.get('url')), 'flash', expirationTime))
break
}
case appConstants.APP_CHANGE_SITE_SETTING:
{
let propertyName = action.get('temporary') ? 'temporarySiteSettings' : 'siteSettings'
let newSiteSettings = siteSettings.mergeSiteSetting(state.get(propertyName), action.get('hostPattern'), action.get('key'), action.get('value'))
if (action.get('skipSync')) {
newSiteSettings = newSiteSettings.setIn([action.get('hostPattern'), 'skipSync'], true)
}
state = state.set(propertyName, newSiteSettings)
break
}
case appConstants.APP_REMOVE_SITE_SETTING:
{
let propertyName = action.get('temporary') ? 'temporarySiteSettings' : 'siteSettings'
let newSiteSettings = siteSettings.removeSiteSetting(state.get(propertyName),
action.get('hostPattern'), action.get('key'))
if (action.get('skipSync')) {
newSiteSettings = newSiteSettings.setIn([action.get('hostPattern'), 'skipSync'], true)
}
state = state.set(propertyName, newSiteSettings)
break
}
case appConstants.APP_CLEAR_SITE_SETTINGS:
{
let propertyName = action.get('temporary') ? 'temporarySiteSettings' : 'siteSettings'
let newSiteSettings = new Immutable.Map()
state.get(propertyName).map((entry, hostPattern) => {
let newEntry = entry.delete(action.get('key'))
if (action.get('skipSync')) {
newEntry = newEntry.set('skipSync', true)
}
newSiteSettings = newSiteSettings.set(hostPattern, newEntry)
})
state = state.set(propertyName, newSiteSettings)
break
}
case appConstants.APP_ADD_NOSCRIPT_EXCEPTIONS:
{
const origin = action.get('origins')
const hostPattern = action.get('hostPattern')
const propertyName = action.get('temporary') ? 'temporarySiteSettings' : 'siteSettings'
// Note that this is always cleared on restart or reload, so should not
// be synced or persisted.
const key = 'noScriptExceptions'
if (!origin || !origin.size) {
// Clear the exceptions
state = state.setIn([propertyName, hostPattern, key], new Immutable.Map())
} else {
const currentExceptions = state.getIn([propertyName, hostPattern, key]) || new Immutable.Map()
state = state.setIn([propertyName, hostPattern, key], currentExceptions.merge(origin))
}
break
}
}
return state
}

module.exports = siteSettingsReducer
Loading

0 comments on commit 562ed05

Please sign in to comment.