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

Commit

Permalink
Fix isResourceEnabled jank using a cache
Browse files Browse the repository at this point in the history
fix #9987
  • Loading branch information
diracdeltas committed Aug 3, 2017
1 parent 00cc603 commit 265a57b
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 15 deletions.
62 changes: 51 additions & 11 deletions app/filtering.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,51 @@ const registeredSessions = {}
*/
const permissionCallbacks = {}

/**
* Cache of URLs mapped their site settings.
* This gets rebuilt when site settings or a bravery setting is changed.
*/
let braverySettingsForUrl = {}

const getBraverySettingsForUrl = (url, appState, isPrivate) => {
if (braverySettingsForUrl[url]) {
return braverySettingsForUrl[url]
}
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)
}
braverySettingsForUrl[url] = braverySettings
return braverySettings
}

const settingsKeys = ['siteSettings', 'temporarySiteSettings', 'settings']
const appStoreChangeCallback = function (diffs) {
if (!diffs) {
return
}
diffs.forEach((diff) => {
if (!diff || !diff.path) {
return
}
const path = diff.path.split('/')
if (path.length < 2) {
// We are looking for paths like ['', 'siteSettings']
return
}

const type = path[1]
if (settingsKeys.includes(type) ||
(path[2] === 'enabled' && appConfig.resourceNames.includes(type))) {
// Rebuild bravery settings cache
braverySettingsForUrl = {}
}
})
}

module.exports.registerBeforeSendHeadersFilteringCB = (filteringFn) => {
beforeSendHeadersFilteringFns.push(filteringFn)
}
Expand Down Expand Up @@ -684,6 +729,7 @@ module.exports.init = (state, action, store) => {
permissionCallbacks[message](buttonIndex, persist)
}
})
appStore.addChangeListener(appStoreChangeCallback)

return state
}
Expand Down Expand Up @@ -716,28 +762,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 @@ -52,17 +52,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

0 comments on commit 265a57b

Please sign in to comment.