forked from brave/browser-laptop
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Resolves brave#11009 Auditors: Test Plan:
- Loading branch information
Showing
14 changed files
with
1,240 additions
and
264 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.