Skip to content
This repository has been archived by the owner on Dec 11, 2019. It is now read-only.

Commit

Permalink
Merge pull request #10289 from brave/fix/9987
Browse files Browse the repository at this point in the history
Fix isResourceEnabled jank using a cache
  • Loading branch information
bbondy authored Aug 24, 2017
2 parents 0dfd283 + f0b7290 commit 661ef90
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 15 deletions.
24 changes: 24 additions & 0 deletions app/browser/reducers/braverySettingsReducer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/* 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/. */

'use strict'

const appConstants = require('../../../js/constants/appConstants')
const {clearBraverySettingsCache} = require('../../common/cache/braverySettingsCache')
const {makeImmutable} = require('../../common/state/immutableUtil')

const braverySettingsReducer = (state, action, immutableAction) => {
action = immutableAction || makeImmutable(action)
switch (action.get('actionType')) {
case appConstants.APP_SET_RESOURCE_ENABLED:
case appConstants.APP_CHANGE_SITE_SETTING:
case appConstants.APP_REMOVE_SITE_SETTING:
case appConstants.APP_CLEAR_SITE_SETTINGS:
clearBraverySettingsCache()
break
}
return state
}

module.exports = braverySettingsReducer
27 changes: 27 additions & 0 deletions app/common/cache/braverySettingsCache.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/* 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/. */

/**
* Cache of URLs mapped their site settings.
* This gets rebuilt when site settings or a bravery setting is changed.
*/
const currentBraverySettingsCache = new Map()

const clearBraverySettingsCache = () => {
currentBraverySettingsCache.clear()
}

const getBraverySettingsCache = (url) => {
return currentBraverySettingsCache.get(url)
}

const updateBraverySettingsCache = (url, braverySettings) => {
currentBraverySettingsCache.set(url, braverySettings)
}

module.exports = {
clearBraverySettingsCache,
getBraverySettingsCache,
updateBraverySettingsCache
}
33 changes: 22 additions & 11 deletions app/filtering.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const {fullscreenOption} = require('./common/constants/settingsEnums')
const isThirdPartyHost = require('./browser/isThirdPartyHost')
const extensionState = require('./common/state/extensionState')
const {cookieExceptions, refererExceptions} = require('../js/data/siteHacks')
const {getBraverySettingsCache, updateBraverySettingsCache} = require('./common/cache/braverySettingsCache')

let appStore = null

Expand All @@ -58,6 +59,22 @@ const registeredSessions = {}
*/
const permissionCallbacks = {}

const getBraverySettingsForUrl = (url, appState, isPrivate) => {
const cachedBraverySettings = getBraverySettingsCache(url)
if (cachedBraverySettings) {
return cachedBraverySettings
}
const savedSettings = siteSettings.getSiteSettingsForURL(appState.get('siteSettings'), url)
const tempSettings = siteSettings.getSiteSettingsForURL(appState.get('temporarySiteSettings'), url)

let braverySettings = siteSettings.activeSettings(savedSettings, appState, appConfig)
if (isPrivate && tempSettings) {
braverySettings = siteSettings.activeSettings(tempSettings, appState, appConfig)
}
updateBraverySettingsCache(url, braverySettings)
return braverySettings
}

module.exports.registerBeforeSendHeadersFilteringCB = (filteringFn) => {
beforeSendHeadersFilteringFns.push(filteringFn)
}
Expand Down Expand Up @@ -731,28 +748,22 @@ module.exports.isResourceEnabled = (resourceName, url, isPrivate) => {
if (resourceName === 'flash') {
return true
}
const appState = appStore.getState()
const settingsState = appState.get('settings')

if (resourceName === 'pdfjs') {
return getSetting(settings.PDFJS_ENABLED)
return getSetting(settings.PDFJS_ENABLED, settingsState)
}
if (resourceName === 'webtorrent') {
return getSetting(settings.TORRENT_VIEWER_ENABLED)
return getSetting(settings.TORRENT_VIEWER_ENABLED, settingsState)
}

const appState = appStore.getState()

if (resourceName === 'webtorrent') {
const extension = extensionState.getExtensionById(appState, config.torrentExtensionId)
return extension !== undefined ? extension.get('enabled') : false
}

const savedSettings = siteSettings.getSiteSettingsForURL(appState.get('siteSettings'), url)
const tempSettings = siteSettings.getSiteSettingsForURL(appState.get('temporarySiteSettings'), url)

let braverySettings = siteSettings.activeSettings(savedSettings, appState, appConfig)
if (isPrivate && tempSettings) {
braverySettings = siteSettings.activeSettings(tempSettings, appState, appConfig)
}
const braverySettings = getBraverySettingsForUrl(url, appState, isPrivate)

// If full shields are down never enable extra protection
if (braverySettings.shieldsUp === false) {
Expand Down
8 changes: 4 additions & 4 deletions js/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,17 @@ const getDefaultSetting = (settingKey, settingsCollection) => {
}

const resolveValue = (settingKey, settingsCollection) => {
const appStore = (process.type === 'browser'
? require('./stores/appStore').getState()
: require('./stores/appStoreRenderer').state) || Immutable.Map()
const appSettings = appStore.get('settings') || Immutable.Map()
if (settingsCollection && settingsCollection.constructor === Immutable.Map &&
settingsCollection.get(settingKey) !== undefined) {
return settingsCollection.get(settingKey)
}
if (settingsCollection && settingsCollection[settingKey] !== undefined) {
return settingsCollection[settingKey]
}
const appStore = (process.type === 'browser'
? require('./stores/appStore').getState()
: require('./stores/appStoreRenderer').state) || Immutable.Map()
const appSettings = appStore.get('settings') || Immutable.Map()
return appSettings.get(settingKey) !== undefined ? appSettings.get(settingKey) : appConfig.defaultSettings[settingKey]
}

Expand Down
1 change: 1 addition & 0 deletions js/stores/appStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ const handleAppAction = (action) => {
require('../../app/browser/reducers/shareReducer'),
require('../../app/browser/reducers/updatesReducer'),
require('../../app/browser/reducers/topSitesReducer'),
require('../../app/browser/reducers/braverySettingsReducer'),
require('../../app/ledger').doAction,
require('../../app/browser/menu')
]
Expand Down

0 comments on commit 661ef90

Please sign in to comment.