Skip to content

Commit

Permalink
Moves eventStore into appStore
Browse files Browse the repository at this point in the history
Resolves brave#11009

Auditors:

Test Plan:
  • Loading branch information
NejcZdovc committed Oct 2, 2017
1 parent e357db4 commit e8436ec
Show file tree
Hide file tree
Showing 14 changed files with 1,240 additions and 264 deletions.
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
2 changes: 1 addition & 1 deletion app/browser/tabs.js
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,7 @@ const api = {

tab.on('did-get-response-details', (evt, status, newURL, originalURL, httpResponseCode, requestMethod, referrer, headers, resourceType) => {
if (resourceType === 'mainFrame') {
windowActions.gotResponseDetails(tabId, {status, newURL, originalURL, httpResponseCode, requestMethod, referrer, headers, resourceType})
windowActions.gotResponseDetails(tabId, {status, newURL, originalURL, httpResponseCode, requestMethod, referrer, resourceType})
}
})
})
Expand Down
62 changes: 41 additions & 21 deletions app/common/lib/ledgerUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,44 +4,56 @@

'use strict'

const {responseHasContent} = require('./httpUtil')
const moment = require('moment')

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

/**
* Is page an actual page being viewed by the user? (not an error page, etc)
* If the page is invalid, we don't want to collect usage info.
* @param {Object} view - an entry from page_view (from EventStore)
* @param {Object} responseList - full page_response array (from EventStore)
* @param {Map} view - an entry from ['pageData', 'view']
* @param {List} responseList - full ['pageData', 'load'] List
* @return {boolean} true if page should have usage collected, false if not
*/
module.exports.shouldTrackView = (view, responseList) => {
if (!view || !view.url || !view.tabId) {
const shouldTrackView = (view, responseList) => {
view = makeImmutable(view)

if (view == null) {
return false
}
if (!responseList || !Array.isArray(responseList) || !responseList.length) {

const tabId = view.get('tabId')
const url = view.get('url')

if (!url || !tabId) {
return false
}

const tabId = view.tabId
const url = view.url
responseList = makeImmutable(responseList)
if (!responseList || responseList.size === 0) {
return false
}

for (let i = responseList.length; i > -1; i--) {
const response = responseList[i]
for (let i = (responseList.size - 1); i > -1; i--) {
const response = responseList.get(i)

if (!response) continue
if (!response) {
continue
}

const responseUrl = response && response.details
? response.details.newURL
: null
const responseUrl = response.getIn(['details', 'newURL'], null)

if (url === responseUrl && response.tabId === tabId) {
return responseHasContent(response.details.httpResponseCode)
if (url === responseUrl && response.get('tabId') === tabId) {
return responseHasContent(response.getIn(['details', 'httpResponseCode']))
}
}

return false
}

module.exports.btcToCurrencyString = (btc, ledgerData) => {
const btcToCurrencyString = (btc, ledgerData) => {
const balance = Number(btc || 0)
const currency = ledgerData.get('currency') || 'USD'

Expand Down Expand Up @@ -69,17 +81,17 @@ module.exports.btcToCurrencyString = (btc, ledgerData) => {
return `${balance} BTC`
}

module.exports.formattedTimeFromNow = (timestamp) => {
const formattedTimeFromNow = (timestamp) => {
moment.locale(navigator.language)
return moment(new Date(timestamp)).fromNow()
}

module.exports.formattedDateFromTimestamp = (timestamp, format) => {
const formattedDateFromTimestamp = (timestamp, format) => {
moment.locale(navigator.language)
return moment(new Date(timestamp)).format(format)
}

module.exports.walletStatus = (ledgerData) => {
const walletStatus = (ledgerData) => {
let status = {}

if (ledgerData.get('error')) {
Expand All @@ -93,7 +105,7 @@ module.exports.walletStatus = (ledgerData) => {
status.id = 'insufficientFundsStatus'
} else if (pendingFunds > 0) {
status.id = 'pendingFundsStatus'
status.args = {funds: module.exports.btcToCurrencyString(pendingFunds, ledgerData)}
status.args = {funds: btcToCurrencyString(pendingFunds, ledgerData)}
} else if (transactions && transactions.size > 0) {
status.id = 'defaultWalletStatus'
} else {
Expand All @@ -106,3 +118,11 @@ module.exports.walletStatus = (ledgerData) => {
}
return status
}

module.exports = {
shouldTrackView,
btcToCurrencyString,
formattedTimeFromNow,
formattedDateFromTimestamp,
walletStatus
}
17 changes: 17 additions & 0 deletions app/common/lib/pageDataUtil.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@

const urlFormat = require('url').format
const _ = require('underscore')

const urlParse = require('../../common/urlParse')

const getInfoKey = (url) => {
if (typeof url !== 'string') {
return null
}

return urlFormat(_.pick(urlParse(url), [ 'protocol', 'host', 'hostname', 'port', 'pathname' ]))
}

module.exports = {
getInfoKey
}
98 changes: 98 additions & 0 deletions app/common/state/pageDataState.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/* 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 pageDataUtil = require('../lib/pageDataUtil')
const {getWebContents} = require('../../browser/webContentsCache')
const {isSourceAboutUrl} = require('../../../js/lib/appUrlUtil')
const {makeImmutable} = require('./immutableUtil')

const pageDataState = {
addView: (state, url = null, tabId = null) => {
const tab = getWebContents(tabId)
const isPrivate = !tab ||
tab.isDestroyed() ||
!tab.session.partition.startsWith('persist:')

state = pageDataState.setLastActiveTabId(state, tabId)

if ((url && isSourceAboutUrl(url)) || isPrivate) {
url = null
}

const lastView = pageDataState.getView(state)
if (lastView.get('url') === url) {
return state
}

let pageViewEvent = makeImmutable({
timestamp: new Date().getTime(),
url,
tabId
})
return state.setIn(['pageData', 'view'], pageViewEvent)
},

addInfo: (state, data) => {
data = makeImmutable(data)

if (data == null) {
return state
}

const key = pageDataUtil.getInfoKey(data.get('url'))

data = data.set('key', key)
state = state.setIn(['pageData', 'last', 'info'], key)
return state.setIn(['pageData', 'info', key], data)
},

addLoad: (state, data) => {
if (data == null) {
return state
}

// select only last 100 loads
const newLoad = state.getIn(['pageData', 'load'], Immutable.List()).slice(-100).push(data)
return state.setIn(['pageData', 'load'], newLoad)
},

getView: (state) => {
return state.getIn(['pageData', 'view']) || Immutable.Map()
},

getLastInfo: (state) => {
const key = state.getIn(['pageData', 'last', 'info'])

if (key == null) {
Immutable.Map()
}

return state.getIn(['pageData', 'info', key], Immutable.Map())
},

getLoad: (state) => {
return state.getIn(['pageData', 'load'], Immutable.List())
},

getLastActiveTabId: (state) => {
return state.getIn(['pageData', 'last', 'tabId'])
},

setLastActiveTabId: (state, tabId) => {
return state.setIn(['pageData', 'last', 'tabId'], tabId)
},

setPublisher: (state, key, publisher) => {
if (key == null) {
return state
}

return state.setIn(['pageData', 'info', key, 'publisher'], publisher)
}
}

module.exports = pageDataState
Loading

0 comments on commit e8436ec

Please sign in to comment.