From f0b729073ef13bc828ed7885178eef7fdaf262cd Mon Sep 17 00:00:00 2001 From: yan Date: Thu, 3 Aug 2017 23:17:56 +0000 Subject: [PATCH] Fix isResourceEnabled jank using a cache fix https://github.com/brave/browser-laptop/issues/9987 --- .../reducers/braverySettingsReducer.js | 24 ++++++++++++++ app/common/cache/braverySettingsCache.js | 27 +++++++++++++++ app/filtering.js | 33 ++++++++++++------- js/settings.js | 8 ++--- js/stores/appStore.js | 1 + 5 files changed, 78 insertions(+), 15 deletions(-) create mode 100644 app/browser/reducers/braverySettingsReducer.js create mode 100644 app/common/cache/braverySettingsCache.js diff --git a/app/browser/reducers/braverySettingsReducer.js b/app/browser/reducers/braverySettingsReducer.js new file mode 100644 index 00000000000..1d156216a17 --- /dev/null +++ b/app/browser/reducers/braverySettingsReducer.js @@ -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 diff --git a/app/common/cache/braverySettingsCache.js b/app/common/cache/braverySettingsCache.js new file mode 100644 index 00000000000..afec1b9debf --- /dev/null +++ b/app/common/cache/braverySettingsCache.js @@ -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 +} diff --git a/app/filtering.js b/app/filtering.js index 154a6b299dd..0b901c1ccbb 100644 --- a/app/filtering.js +++ b/app/filtering.js @@ -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 @@ -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) } @@ -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) { diff --git a/js/settings.js b/js/settings.js index 1adf31151ba..58c8636531d 100644 --- a/js/settings.js +++ b/js/settings.js @@ -50,10 +50,6 @@ 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) @@ -61,6 +57,10 @@ const resolveValue = (settingKey, settingsCollection) => { 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] } diff --git a/js/stores/appStore.js b/js/stores/appStore.js index 6a45e84c168..7c769630d0f 100644 --- a/js/stores/appStore.js +++ b/js/stores/appStore.js @@ -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') ]