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

Commit

Permalink
Regional adblock support
Browse files Browse the repository at this point in the history
Fix #1357
  • Loading branch information
bbondy committed Oct 4, 2016
1 parent b27f6ad commit 97a191f
Show file tree
Hide file tree
Showing 19 changed files with 246 additions and 90 deletions.
48 changes: 41 additions & 7 deletions app/adBlock.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ const ABPFilterParser = ABPFilterParserLib.ABPFilterParser
const FilterOptions = ABPFilterParserLib.FilterOptions
const DataFile = require('./dataFile')
const Filtering = require('./filtering')
const appConfig = require('../js/constants/appConfig')
// Maintains a map between a resource uuid and an adblock instance
const adblockInstances = new Map()
const defaultAdblock = new ABPFilterParser()
const defaultSafeBrowsing = new ABPFilterParser()
const regions = require('abp-filter-parser-cpp/lib/regions')
const getSetting = require('../js/settings').getSetting

module.exports.adBlockResourceName = 'adblock'
module.exports.safeBrowsingResourceName = 'safeBrowsing'

Expand Down Expand Up @@ -54,13 +62,39 @@ const startAdBlocking = (adblock, resourceName, shouldCheckMainFrame) => {
})
}

module.exports.initInstance = (resourceName, shouldCheckMainFrame) => {
let adblock = new ABPFilterParser()
DataFile.init(resourceName, startAdBlocking.bind(null, adblock, resourceName, shouldCheckMainFrame),
(data) => adblock.deserialize(data))
module.exports.initInstance = (parser, resourceName, shouldCheckMainFrame) => {
DataFile.init(resourceName, startAdBlocking.bind(null, parser, resourceName, shouldCheckMainFrame),
(data) => parser.deserialize(data))
return module.exports
}

module.exports.init = () => module.exports
.initInstance(module.exports.adBlockResourceName, false)
.initInstance(module.exports.safeBrowsingResourceName, true)
module.exports.init = () => {
module.exports
.initInstance(defaultAdblock, module.exports.adBlockResourceName, false)
.initInstance(defaultSafeBrowsing, module.exports.safeBrowsingResourceName, true)
// Initialize the regional adblock files that are enabled
regions
.filter((region) => getSetting(`adblock.${region.uuid}.enabled`))
.forEach((region) => module.exports.updateAdblockDataFiles(region.uuid, true))
}

/**
* Adds an additional adblock resource to download and initialize
* @param uuid - The uuid of the adblock datafile resource
* @param forAdblock - true if main frame URLs should be blocked
*/
module.exports.updateAdblockDataFiles = (uuid, enabled, version = 1, shouldCheckMainFrame = false) => {
appConfig[uuid] = {
resourceType: module.exports.adBlockResourceName,
enabled,
msBetweenRechecks: 1000 * 60 * 60 * 24,
url: appConfig.adblock.alternateDataFiles.replace('{uuid}', uuid),
version
}

if (!adblockInstances.has(uuid)) {
const parser = new ABPFilterParser()
adblockInstances.set(uuid, parser)
module.exports.initInstance(parser, uuid, shouldCheckMainFrame)
}
}
Empty file added app/browser/resourcesManager.js
Empty file.
3 changes: 1 addition & 2 deletions app/extensions.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const browserActions = require('./browser/extensions/browserActions')
const extensionActions = require('./common/actions/extensionActions')
const AppStore = require('../js/stores/appStore')
const config = require('../js/constants/config')
const {fileUrl} = require('../js/lib/appUrlUtil')
const {getAppUrl, getExtensionsPath, getIndexHTML} = require('../js/lib/appUrlUtil')
Expand Down Expand Up @@ -308,5 +307,5 @@ module.exports.init = () => {
}

registerExtensions()
AppStore.addChangeListener(registerExtensions)
appStore.addChangeListener(registerExtensions)
}
2 changes: 1 addition & 1 deletion app/extensions/brave/about-adblock.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<meta charset="utf-8">
<meta name="availableLanguages" content="">
<meta name="defaultLanguage" content="en-US">
<meta name='theme-color' content='#035500'>
<meta name='theme-color' content='#4B0082'>
<link rel="shortcut icon"type="image/x-icon" href="data:image/x-icon;,">
<title data-l10n-id="adblockTitle"></title>
<script src='js/about.js'></script>
Expand Down
4 changes: 4 additions & 0 deletions app/extensions/brave/locales/en-US/adblock.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,7 @@ adblock=Ad Block
lastUpdateCheckDateLabel=Last update check:
lastCheckETagLabel=Last check ETag:
blockedCountLabel=Blocked count:
additionalFilterLists=Additional filter lists
customFilters=Custom filters
customFilterDescription=One per line, a filter is described in Adblock Plus filter syntax
adblockTooManyListsWarning=Warning: Turning on too many lists will degrade performance
28 changes: 20 additions & 8 deletions app/filtering.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const electron = require('electron')
const session = electron.session
const BrowserWindow = electron.BrowserWindow
const webContents = electron.webContents
const AppStore = require('../js/stores/appStore')
const appStore = require('../js/stores/appStore')
const appActions = require('../js/actions/appActions')
const appConfig = require('../js/constants/appConfig')
const downloadStates = require('../js/constants/downloadStates')
Expand All @@ -31,6 +31,7 @@ const app = electron.app
const uuid = require('node-uuid')
const path = require('path')
const getOrigin = require('../js/state/siteUtil').getOrigin
const {adBlockResourceName} = require('./adBlock')

const beforeSendHeadersFilteringFns = []
const beforeRequestFilteringFns = []
Expand Down Expand Up @@ -104,7 +105,7 @@ function registerForBeforeRequest (session) {

for (let i = 0; i < beforeRequestFilteringFns.length; i++) {
let results = beforeRequestFilteringFns[i](details)
const isAdBlock = results.resourceName === appConfig.resourceNames.ADBLOCK
const isAdBlock = results.resourceName === appConfig.resourceNames.ADBLOCK || appConfig[results.resourceName] && appConfig[results.resourceName].resourceType === adBlockResourceName
const isHttpsEverywhere = results.resourceName === appConfig.resourceNames.HTTPS_EVERYWHERE
const isTracker = results.resourceName === appConfig.resourceNames.TRACKING_PROTECTION

Expand All @@ -123,8 +124,16 @@ function registerForBeforeRequest (session) {
appActions.addResourceCount(results.resourceName, 1)
}

let parentResourceName = results.resourceName
// Adblock can have many different resource names for each alternate
// data file. But we always want the per level reporting to report
// it into the window adblock stats.
if (isAdBlock) {
parentResourceName = appConfig.resourceNames.ADBLOCK
}

BrowserWindow.getAllWindows().forEach((wnd) =>
wnd.webContents.send(message, results.resourceName, details))
wnd.webContents.send(message, parentResourceName, details))
if (details.resourceType === 'image') {
cb({ redirectURL: transparent1pxGif })
} else {
Expand Down Expand Up @@ -338,7 +347,7 @@ function registerPermissionHandler (session, partition) {
}

// Check whether there is a persistent site setting for this host
const appState = AppStore.getState()
const appState = appStore.getState()
let settings
let tempSettings
if (mainFrameUrl === appUrlUtil.getIndexHTML() || origin.startsWith('chrome-extension://' + config.braveExtensionId)) {
Expand Down Expand Up @@ -432,7 +441,7 @@ function updateDownloadState (downloadId, item, state) {
return
}

const downloadItemStartTime = AppStore.getState().getIn(['downloads', downloadId, 'startTime'])
const downloadItemStartTime = appStore.getState().getIn(['downloads', downloadId, 'startTime'])
appActions.mergeDownloadDetail(downloadId, {
startTime: downloadItemStartTime || new Date().getTime(),
savePath: item.getSavePath(),
Expand Down Expand Up @@ -464,7 +473,7 @@ function registerForDownloadListener (session) {
}
item.on('updated', function () {
let state = downloadStates.IN_PROGRESS
const downloadItem = AppStore.getState().getIn(['downloads', downloadId])
const downloadItem = appStore.getState().getIn(['downloads', downloadId])
if (downloadItem && downloadItem.get('state') === downloadStates.PAUSED) {
state = downloadStates.PAUSED
}
Expand Down Expand Up @@ -561,7 +570,7 @@ module.exports.isResourceEnabled = (resourceName, url) => {
return true
}

const appState = AppStore.getState()
const appState = appStore.getState()
const settings = siteSettings.getSiteSettingsForURL(appState.get('siteSettings'), url)
const braverySettings = siteSettings.activeSettings(settings, appState, appConfig)

Expand All @@ -571,7 +580,10 @@ module.exports.isResourceEnabled = (resourceName, url) => {
}

if ((resourceName === appConfig.resourceNames.ADBLOCK ||
resourceName === appConfig.resourceNames.TRACKING_PROTECTION)) {
appConfig[resourceName] &&
appConfig[resourceName].enabled &&
appConfig[resourceName].resourceType === adBlockResourceName ||
resourceName === appConfig.resourceNames.TRACKING_PROTECTION)) {
// Check the resource vs the ad control setting
if (braverySettings.adControl === 'allowAdsAndTracking') {
return false
Expand Down
27 changes: 5 additions & 22 deletions docs/state.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,33 +94,16 @@ AppStore
authTag: string, // AES-GCM authentication data, binary-encoded
iv: string // AES-GCM initialization vector, binary-encoded
}],
adblock: {
// resourceIdentifier is one of: `adblock`, `safeBrowsing`, `trackingProtection`,
// `httpsEverywhere`, or another additional resource by name such as
// adblock regional resource files.
[resourceIdentifier]: {
etag: string, // last downloaded data file etag
lastCheckVersion: string, // last checked data file version
lastCheckDate: number, // last checked data file date.getTime()
enabled: boolean, // Enable adblocking
enabled: boolean, // Enable the resoruce
count: number // Number of blocked ads application wide
},
safeBrowsing: {
etag: string, // last downloaded data file etag
lastCheckVersion: string, // last checked data file version
lastCheckDate: number, // last checked data file date.getTime()
enabled: boolean // Enable adblocking
},
trackingProtection: {
etag: string, // last downloaded data file etag
lastCheckVersion: string, // last checked data file version
lastCheckDate: number, // last checked data file date.getTime()
enabled: boolean, // Enable tracking protection
count: number // Number of blocked trackers application wide
},
httpsEverywhere: {
etag: string, // last downloaded data file etag
lastCheckVersion: string, // last checked data file version
lastCheckDate: number, // last checked data file date.getTime()
enabled: boolean, // Enable HTTPS Everywhere
count: number // Number of HTTPS Everywhere upgrades application wide
},
adInsertion: {
enabled: boolean // Enable ad insertion
},
Expand Down
Loading

0 comments on commit 97a191f

Please sign in to comment.