From 54b5286e10b52eb807f05b79221ae08f24eb56bd Mon Sep 17 00:00:00 2001 From: Serge Zarembsky Date: Tue, 20 Mar 2018 15:49:35 -0400 Subject: [PATCH 01/10] Adding return after rejects in a few places. --- src/background.js | 1 + src/classes/Metrics.js | 8 ++------ src/utils/accounts.js | 2 ++ 3 files changed, 5 insertions(+), 6 deletions(-) diff --git a/src/background.js b/src/background.js index 14041e8c4..8bb823f76 100644 --- a/src/background.js +++ b/src/background.js @@ -200,6 +200,7 @@ function getSiteData() { if (!tab) { reject(new Error('Tab not found. Cannot gather page data')); + return; } resolve({ diff --git a/src/classes/Metrics.js b/src/classes/Metrics.js index 062a955cd..ffc0ca65e 100644 --- a/src/classes/Metrics.js +++ b/src/classes/Metrics.js @@ -72,9 +72,7 @@ class Metrics { prefsSet({ utm_source: this.utm_source, utm_campaign: this.utm_campaign - }) - .then(prefs => resolve(prefs)) - .catch(err => reject(err)); + }); }); resolve(); }); @@ -95,9 +93,7 @@ class Metrics { prefsSet({ utm_source: this.utm_source, utm_campaign: this.utm_campaign - }) - .then(reject) - .catch(reject); + }); } }); resolve(); diff --git a/src/utils/accounts.js b/src/utils/accounts.js index e8d8134cf..18c1e5176 100644 --- a/src/utils/accounts.js +++ b/src/utils/accounts.js @@ -534,11 +534,13 @@ function _refreshToken() { return new Promise((resolve, reject) => { if (!conf.login_info.logged_in) { resolve('User not logged in'); + return; } const decoded_user_token = conf.login_info.decoded_user_token; if (!decoded_user_token || !decoded_user_token.exp) { reject('User token is corrupted or null'); + return; } const currentTime = (new Date()).getTime(); From 60783575e889594f690ee8b6f32302df95fbb0a7 Mon Sep 17 00:00:00 2001 From: Serge Zarembsky Date: Thu, 22 Mar 2018 12:28:38 -0400 Subject: [PATCH 02/10] Implementing flag for site-specific unblocks. --- src/classes/EventHandlers.js | 20 ++++++++++++++------ src/classes/Policy.js | 29 ++++++++++++++++++----------- 2 files changed, 32 insertions(+), 17 deletions(-) diff --git a/src/classes/EventHandlers.js b/src/classes/EventHandlers.js index 8bbbe2e70..3f7f75fd4 100644 --- a/src/classes/EventHandlers.js +++ b/src/classes/EventHandlers.js @@ -378,15 +378,21 @@ class EventHandlers { let tab_host; let fromRedirect; let block; + let ss_unblock; if (bug_id) { app_id = bugDb.db.bugs[bug_id].aid; cat_id = bugDb.db.apps[app_id].cat; incognito = tabInfo.getTabInfo(tab_id, 'incognito'); tab_host = tabInfo.getTabInfo(tab_id, 'host'); fromRedirect = globals.REDIRECT_MAP.has(request_id); - block = this._checkBlocking(app_id, cat_id, tab_id, tab_host, page_url, request_id); + const retVal = this._checkBlocking(app_id, cat_id, tab_id, tab_host, page_url, request_id); + block = retVal.block; + /* eslint prefer-destructuring: 0 */ + if (retVal.ss_unblock) { + // The way to pass this flag to Cliqz handlers + details.ghosteryWhitelisted = true; + } } - // Latency initialization needs to be synchronous to avoid race condition with onCompleted, etc. // TODO can URLs repeat within a redirect chain? what are the cases of repeating URLs (trackers only, ...)? if (block === false) { @@ -400,6 +406,8 @@ class EventHandlers { page_url, incognito }; + + // Find out if block false because of site_specific_unblock } // process the tracker asynchronously @@ -767,10 +775,10 @@ class EventHandlers { * @param {number} app_id tracker id * @param {number} cat_id tracker category id * @param {number} tab_id tab id - * @param {string} tab_host tab url host - * @param {string} page_url full tab url + * @param {string} tab_host tab url host + * @param {string} page_url full tab url * @param {number} request_id request id - * @return {boolean} + * @return {Object} {block, ss_unblock} */ _checkBlocking(app_id, cat_id, tab_id, tab_host, page_url, request_id) { const fromRedirect = globals.REDIRECT_MAP.has(request_id); @@ -779,7 +787,7 @@ class EventHandlers { // If we let page-level c2p trackers through, we don't want to block it // along with all subsequent top-level redirects. if (fromRedirect && globals.LET_REDIRECTS_THROUGH) { - block = false; + block = { block: false }; } else { block = this.policy.shouldBlock(app_id, cat_id, tab_id, tab_host, page_url); } diff --git a/src/classes/Policy.js b/src/classes/Policy.js index e58020738..cc8be6116 100644 --- a/src/classes/Policy.js +++ b/src/classes/Policy.js @@ -100,35 +100,42 @@ class Policy { * @param {number} tab_id tab id * @param {string} tab_host tab url host * @param {string} tab_url tab url - * @return {boolean} + * @return {Object} {block, ss_unblock} */ shouldBlock(app_id, cat_id, tab_id, tab_host, tab_url) { if (globals.SESSION.paused_blocking) { - return false; + return { block: false }; + } + + const ss_unblock = conf.toggle_individual_trackers && conf.site_specific_unblocks.hasOwnProperty(tab_host) && conf.site_specific_unblocks[tab_host].includes(+app_id); + + if (ss_unblock && !this.blacklisted(tab_url) && !this.whitelisted(tab_url)) { + return { block: false, ss_unblock: true }; } if (conf.selected_app_ids.hasOwnProperty(app_id)) { - if (conf.toggle_individual_trackers && conf.site_specific_unblocks.hasOwnProperty(tab_host) && conf.site_specific_unblocks[tab_host].includes(+app_id)) { + if (ss_unblock) { if (this.blacklisted(tab_url)) { - return !c2pDb.allowedOnce(tab_id, app_id); + return { block: !c2pDb.allowedOnce(tab_id, app_id) }; } - return false; + return { block: false }; } if (this.whitelisted(tab_url)) { - return false; + return { block: false }; } - return !c2pDb.allowedOnce(tab_id, app_id); + return { block: !c2pDb.allowedOnce(tab_id, app_id) }; } + // We get here when app_id is not selected for blocking if (conf.toggle_individual_trackers && conf.site_specific_blocks.hasOwnProperty(tab_host) && conf.site_specific_blocks[tab_host].includes(+app_id)) { if (this.whitelisted(tab_url)) { - return false; + return { block: false }; } - return !c2pDb.allowedOnce(tab_id, app_id); + return { block: !c2pDb.allowedOnce(tab_id, app_id) }; } if (this.blacklisted(tab_url)) { - return !c2pDb.allowedOnce(tab_id, app_id); + return { block: !c2pDb.allowedOnce(tab_id, app_id) }; } - return false; + return { block: false }; } } From c81bd4153e52553802db20fa61c7ebc617d1102b Mon Sep 17 00:00:00 2001 From: Serge Zarembsky Date: Thu, 22 Mar 2018 14:13:19 -0400 Subject: [PATCH 03/10] Using abbreviations for seconds, removing unnecessary comment. --- _locales/es/messages.json | 2 +- _locales/fr/messages.json | 2 +- src/classes/EventHandlers.js | 2 -- 3 files changed, 2 insertions(+), 4 deletions(-) diff --git a/_locales/es/messages.json b/_locales/es/messages.json index b3a744cc7..ab12e4f9f 100644 --- a/_locales/es/messages.json +++ b/_locales/es/messages.json @@ -856,7 +856,7 @@ "message": "Nunca" }, "settings_seconds": { - "message": "segundos" + "message": "s" }, "settings_hide_alert_trusted": { "message": "Ocultar el cuadro púrpura en sitios web de confianza" diff --git a/_locales/fr/messages.json b/_locales/fr/messages.json index df21b0cd8..5d3fd348f 100644 --- a/_locales/fr/messages.json +++ b/_locales/fr/messages.json @@ -856,7 +856,7 @@ "message": "Jamais" }, "settings_seconds": { - "message": "secondes" + "message": "s" }, "settings_hide_alert_trusted": { "message": "Masquer la case violette sur les sites de confiance" diff --git a/src/classes/EventHandlers.js b/src/classes/EventHandlers.js index 3f7f75fd4..235c4e508 100644 --- a/src/classes/EventHandlers.js +++ b/src/classes/EventHandlers.js @@ -406,8 +406,6 @@ class EventHandlers { page_url, incognito }; - - // Find out if block false because of site_specific_unblock } // process the tracker asynchronously From 0745205448c440d3e2e37c1215eea03355663b75 Mon Sep 17 00:00:00 2001 From: Serge Zarembsky Date: Fri, 23 Mar 2018 10:52:30 -0400 Subject: [PATCH 04/10] Requested changes. --- src/background.js | 2 +- src/classes/EventHandlers.js | 8 +++----- src/classes/Policy.js | 18 +++++++++--------- 3 files changed, 13 insertions(+), 15 deletions(-) diff --git a/src/background.js b/src/background.js index 8bb823f76..61fee0dd0 100644 --- a/src/background.js +++ b/src/background.js @@ -1008,7 +1008,7 @@ messageCenter.on('enabled', () => { * } * } */ - log('GOT OFFER!!!!!!!!!!!!!!!!!!!!!!'); + log('GOT OFFER', msg); // first check that the message is from core and is the one we expect if (msg.origin === 'offers-core' && msg.type === 'push-offer' && diff --git a/src/classes/EventHandlers.js b/src/classes/EventHandlers.js index 235c4e508..cde11d3f0 100644 --- a/src/classes/EventHandlers.js +++ b/src/classes/EventHandlers.js @@ -378,17 +378,15 @@ class EventHandlers { let tab_host; let fromRedirect; let block; - let ss_unblock; if (bug_id) { app_id = bugDb.db.bugs[bug_id].aid; cat_id = bugDb.db.apps[app_id].cat; incognito = tabInfo.getTabInfo(tab_id, 'incognito'); tab_host = tabInfo.getTabInfo(tab_id, 'host'); fromRedirect = globals.REDIRECT_MAP.has(request_id); - const retVal = this._checkBlocking(app_id, cat_id, tab_id, tab_host, page_url, request_id); - block = retVal.block; - /* eslint prefer-destructuring: 0 */ - if (retVal.ss_unblock) { + const { block1, ss_unblock } = this._checkBlocking(app_id, cat_id, tab_id, tab_host, page_url, request_id); + block = block1; + if (ss_unblock) { // The way to pass this flag to Cliqz handlers details.ghosteryWhitelisted = true; } diff --git a/src/classes/Policy.js b/src/classes/Policy.js index cc8be6116..267750ef2 100644 --- a/src/classes/Policy.js +++ b/src/classes/Policy.js @@ -104,7 +104,7 @@ class Policy { */ shouldBlock(app_id, cat_id, tab_id, tab_host, tab_url) { if (globals.SESSION.paused_blocking) { - return { block: false }; + return { block: false, ss_unblock: false }; } const ss_unblock = conf.toggle_individual_trackers && conf.site_specific_unblocks.hasOwnProperty(tab_host) && conf.site_specific_unblocks[tab_host].includes(+app_id); @@ -116,26 +116,26 @@ class Policy { if (conf.selected_app_ids.hasOwnProperty(app_id)) { if (ss_unblock) { if (this.blacklisted(tab_url)) { - return { block: !c2pDb.allowedOnce(tab_id, app_id) }; + return { block: !c2pDb.allowedOnce(tab_id, app_id), ss_unblock: false }; } - return { block: false }; + return { block: false, ss_unblock: false }; } if (this.whitelisted(tab_url)) { - return { block: false }; + return { block: false, ss_unblock: false }; } - return { block: !c2pDb.allowedOnce(tab_id, app_id) }; + return { block: !c2pDb.allowedOnce(tab_id, app_id), ss_unblock: false }; } // We get here when app_id is not selected for blocking if (conf.toggle_individual_trackers && conf.site_specific_blocks.hasOwnProperty(tab_host) && conf.site_specific_blocks[tab_host].includes(+app_id)) { if (this.whitelisted(tab_url)) { - return { block: false }; + return { block: false, ss_unblock: false }; } - return { block: !c2pDb.allowedOnce(tab_id, app_id) }; + return { block: !c2pDb.allowedOnce(tab_id, app_id), ss_unblock: false }; } if (this.blacklisted(tab_url)) { - return { block: !c2pDb.allowedOnce(tab_id, app_id) }; + return { block: !c2pDb.allowedOnce(tab_id, app_id), ss_unblock: false }; } - return { block: false }; + return { block: false, ss_unblock: false }; } } From 61a1e5c88c9e6bcd48cf76eaef3023ef59541f2c Mon Sep 17 00:00:00 2001 From: Serge Zarembsky Date: Fri, 23 Mar 2018 13:34:32 -0400 Subject: [PATCH 05/10] Implementing suggested changes. --- src/classes/EventHandlers.js | 29 +++++++++--------------- src/classes/Policy.js | 43 ++++++++++++++++++++---------------- 2 files changed, 34 insertions(+), 38 deletions(-) diff --git a/src/classes/EventHandlers.js b/src/classes/EventHandlers.js index cde11d3f0..71457d10b 100644 --- a/src/classes/EventHandlers.js +++ b/src/classes/EventHandlers.js @@ -23,7 +23,7 @@ import conf from './Conf'; import foundBugs from './FoundBugs'; import globals from './Globals'; import latency from './Latency'; -import Policy from './Policy'; +import Policy, { BLOCK_REASON_SS_UNBLOCK } from './Policy'; import PolicySmartBlock from './PolicySmartBlock'; import PurpleBox from './PurpleBox'; import surrogatedb from './SurrogateDb'; @@ -372,24 +372,15 @@ class EventHandlers { return { cancel: false }; } - let app_id; - let cat_id; - let incognito; - let tab_host; - let fromRedirect; - let block; - if (bug_id) { - app_id = bugDb.db.bugs[bug_id].aid; - cat_id = bugDb.db.apps[app_id].cat; - incognito = tabInfo.getTabInfo(tab_id, 'incognito'); - tab_host = tabInfo.getTabInfo(tab_id, 'host'); - fromRedirect = globals.REDIRECT_MAP.has(request_id); - const { block1, ss_unblock } = this._checkBlocking(app_id, cat_id, tab_id, tab_host, page_url, request_id); - block = block1; - if (ss_unblock) { - // The way to pass this flag to Cliqz handlers - details.ghosteryWhitelisted = true; - } + const app_id = bugDb.db.bugs[bug_id].aid; + const cat_id = bugDb.db.apps[app_id].cat; + const incognito = tabInfo.getTabInfo(tab_id, 'incognito'); + const tab_host = tabInfo.getTabInfo(tab_id, 'host'); + const fromRedirect = globals.REDIRECT_MAP.has(request_id); + const { block, reason } = this._checkBlocking(app_id, cat_id, tab_id, tab_host, page_url, request_id); + if (!block && reason === BLOCK_REASON_SS_UNBLOCK) { + // The way to pass this flag to Cliqz handlers + details.ghosteryWhitelisted = true; } // Latency initialization needs to be synchronous to avoid race condition with onCompleted, etc. // TODO can URLs repeat within a redirect chain? what are the cases of repeating URLs (trackers only, ...)? diff --git a/src/classes/Policy.js b/src/classes/Policy.js index 267750ef2..32d371509 100644 --- a/src/classes/Policy.js +++ b/src/classes/Policy.js @@ -20,9 +20,18 @@ import conf from './Conf'; import { processUrl } from '../utils/utils'; import globals from './Globals'; -const POLICY_BLOCK_NOTHING = 'POLICY_BLOCK_NOTHING'; -const POLICY_BLOCK_EVERYTHING = 'POLICY_BLOCK_EVERYTHING'; -const POLICY_BLOCK_ADS = 'POLICY_BLOCK_ADS'; +/** + * Enum for reasons returned by shouldBlock + * @type {string} + * TBD: See if we can do with integer values for performance. + */ +export const BLOCK_REASON_PAUSED = 'BLOCK_REASON_PAUSED'; +export const BLOCK_REASON_ALLOW_ONCE = 'BLOCK_REASON_ALLOW_ONCE'; +export const BLOCK_REASON_BLACKLISTED = 'BLOCK_REASON_BLACKLISTED'; +export const BLOCK_REASON_SS_UNBLOCK = 'BLOCK_REASON_SS_UNBLOCK'; +export const BLOCK_REASON_WHITELISTED = 'BLOCK_REASON_WHITELISTED'; +export const BLOCK_REASON_GLOBAL_BLOCKING = 'BLOCK_REASON_GLOBAL_BLOCKING'; +export const BLOCK_REASON_SS_BLOCKED = 'BLOCK_REASON_SS_BLOCKED'; /** * Class for handling site policy. * @memberOf BackgroundClasses @@ -104,39 +113,35 @@ class Policy { */ shouldBlock(app_id, cat_id, tab_id, tab_host, tab_url) { if (globals.SESSION.paused_blocking) { - return { block: false, ss_unblock: false }; - } - - const ss_unblock = conf.toggle_individual_trackers && conf.site_specific_unblocks.hasOwnProperty(tab_host) && conf.site_specific_unblocks[tab_host].includes(+app_id); - - if (ss_unblock && !this.blacklisted(tab_url) && !this.whitelisted(tab_url)) { - return { block: false, ss_unblock: true }; + return { block: false, reason: BLOCK_REASON_PAUSED }; } + const allowedOnce = c2pDb.allowedOnce(tab_id, app_id); if (conf.selected_app_ids.hasOwnProperty(app_id)) { - if (ss_unblock) { + if (conf.toggle_individual_trackers && conf.site_specific_unblocks.hasOwnProperty(tab_host) && conf.site_specific_unblocks[tab_host].includes(+app_id)) { if (this.blacklisted(tab_url)) { - return { block: !c2pDb.allowedOnce(tab_id, app_id), ss_unblock: false }; + return { block: !allowedOnce, reason: allowedOnce ? BLOCK_REASON_ALLOW_ONCE : BLOCK_REASON_BLACKLISTED }; } - return { block: false, ss_unblock: false }; + return { block: false, reason: BLOCK_REASON_SS_UNBLOCK }; } if (this.whitelisted(tab_url)) { - return { block: false, ss_unblock: false }; + return { block: false, reason: BLOCK_REASON_WHITELISTED }; } - return { block: !c2pDb.allowedOnce(tab_id, app_id), ss_unblock: false }; + return { block: !allowedOnce, reason: allowedOnce ? BLOCK_REASON_ALLOW_ONCE : BLOCK_REASON_GLOBAL_BLOCKING }; } // We get here when app_id is not selected for blocking if (conf.toggle_individual_trackers && conf.site_specific_blocks.hasOwnProperty(tab_host) && conf.site_specific_blocks[tab_host].includes(+app_id)) { if (this.whitelisted(tab_url)) { - return { block: false, ss_unblock: false }; + return { block: false, reason: BLOCK_REASON_WHITELISTED }; } - return { block: !c2pDb.allowedOnce(tab_id, app_id), ss_unblock: false }; + return { block: !allowedOnce, reason: allowedOnce ? BLOCK_REASON_ALLOW_ONCE : BLOCK_REASON_SS_BLOCKED }; } if (this.blacklisted(tab_url)) { - return { block: !c2pDb.allowedOnce(tab_id, app_id), ss_unblock: false }; + return { block: !allowedOnce, reason: allowedOnce ? BLOCK_REASON_ALLOW_ONCE : BLOCK_REASON_BLACKLISTED }; } - return { block: false, ss_unblock: false }; + return { block: false, reason: BLOCK_REASON_GLOBAL_BLOCKING }; } } export default Policy; + From 40c1d482ee6ea023ca0783e86c1d274f3ca036b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Mar=C3=ADa=20Signanini?= Date: Fri, 23 Mar 2018 14:33:14 -0400 Subject: [PATCH 06/10] Update policy block reason name. --- src/classes/EventHandlers.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/classes/EventHandlers.js b/src/classes/EventHandlers.js index 71457d10b..d5a4d2556 100644 --- a/src/classes/EventHandlers.js +++ b/src/classes/EventHandlers.js @@ -23,7 +23,7 @@ import conf from './Conf'; import foundBugs from './FoundBugs'; import globals from './Globals'; import latency from './Latency'; -import Policy, { BLOCK_REASON_SS_UNBLOCK } from './Policy'; +import Policy, { BLOCK_REASON_SS_UNBLOCKED } from './Policy'; import PolicySmartBlock from './PolicySmartBlock'; import PurpleBox from './PurpleBox'; import surrogatedb from './SurrogateDb'; @@ -378,7 +378,7 @@ class EventHandlers { const tab_host = tabInfo.getTabInfo(tab_id, 'host'); const fromRedirect = globals.REDIRECT_MAP.has(request_id); const { block, reason } = this._checkBlocking(app_id, cat_id, tab_id, tab_host, page_url, request_id); - if (!block && reason === BLOCK_REASON_SS_UNBLOCK) { + if (!block && reason === BLOCK_REASON_SS_UNBLOCKED) { // The way to pass this flag to Cliqz handlers details.ghosteryWhitelisted = true; } From 6bf01a9b2ec1cba99cec70a6221a72bb65c1310c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Mar=C3=ADa=20Signanini?= Date: Fri, 23 Mar 2018 14:34:36 -0400 Subject: [PATCH 07/10] Update block reason names. Call allowedOnce check only when needed. --- src/classes/Policy.js | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/src/classes/Policy.js b/src/classes/Policy.js index 32d371509..f20e5698c 100644 --- a/src/classes/Policy.js +++ b/src/classes/Policy.js @@ -25,13 +25,13 @@ import globals from './Globals'; * @type {string} * TBD: See if we can do with integer values for performance. */ -export const BLOCK_REASON_PAUSED = 'BLOCK_REASON_PAUSED'; -export const BLOCK_REASON_ALLOW_ONCE = 'BLOCK_REASON_ALLOW_ONCE'; -export const BLOCK_REASON_BLACKLISTED = 'BLOCK_REASON_BLACKLISTED'; -export const BLOCK_REASON_SS_UNBLOCK = 'BLOCK_REASON_SS_UNBLOCK'; +export const BLOCK_REASON_BLOCK_PAUSED = 'BLOCK_REASON_BLOCK_PAUSED'; +export const BLOCK_REASON_GLOBAL_BLOCKED = 'BLOCK_REASON_GLOBAL_BLOCKED'; export const BLOCK_REASON_WHITELISTED = 'BLOCK_REASON_WHITELISTED'; -export const BLOCK_REASON_GLOBAL_BLOCKING = 'BLOCK_REASON_GLOBAL_BLOCKING'; +export const BLOCK_REASON_BLACKLISTED = 'BLOCK_REASON_BLACKLISTED'; +export const BLOCK_REASON_SS_UNBLOCKED = 'BLOCK_REASON_SS_UNBLOCKED'; export const BLOCK_REASON_SS_BLOCKED = 'BLOCK_REASON_SS_BLOCKED'; +export const BLOCK_REASON_ALLOWED_ONCE = 'BLOCK_REASON_ALLOWED_ONCE'; /** * Class for handling site policy. * @memberOf BackgroundClasses @@ -113,33 +113,36 @@ class Policy { */ shouldBlock(app_id, cat_id, tab_id, tab_host, tab_url) { if (globals.SESSION.paused_blocking) { - return { block: false, reason: BLOCK_REASON_PAUSED }; + return { block: false, reason: BLOCK_REASON_BLOCK_PAUSED }; } - const allowedOnce = c2pDb.allowedOnce(tab_id, app_id); if (conf.selected_app_ids.hasOwnProperty(app_id)) { if (conf.toggle_individual_trackers && conf.site_specific_unblocks.hasOwnProperty(tab_host) && conf.site_specific_unblocks[tab_host].includes(+app_id)) { if (this.blacklisted(tab_url)) { - return { block: !allowedOnce, reason: allowedOnce ? BLOCK_REASON_ALLOW_ONCE : BLOCK_REASON_BLACKLISTED }; + const allowedOnce = c2pDb.allowedOnce(tab_id, app_id); + return { block: !allowedOnce, reason: allowedOnce ? BLOCK_REASON_ALLOWED_ONCE : BLOCK_REASON_BLACKLISTED }; } - return { block: false, reason: BLOCK_REASON_SS_UNBLOCK }; + return { block: false, reason: BLOCK_REASON_SS_UNBLOCKED }; } if (this.whitelisted(tab_url)) { return { block: false, reason: BLOCK_REASON_WHITELISTED }; } - return { block: !allowedOnce, reason: allowedOnce ? BLOCK_REASON_ALLOW_ONCE : BLOCK_REASON_GLOBAL_BLOCKING }; + const allowedOnce = c2pDb.allowedOnce(tab_id, app_id); + return { block: !allowedOnce, reason: allowedOnce ? BLOCK_REASON_ALLOWED_ONCE : BLOCK_REASON_GLOBAL_BLOCKED }; } // We get here when app_id is not selected for blocking if (conf.toggle_individual_trackers && conf.site_specific_blocks.hasOwnProperty(tab_host) && conf.site_specific_blocks[tab_host].includes(+app_id)) { if (this.whitelisted(tab_url)) { return { block: false, reason: BLOCK_REASON_WHITELISTED }; } - return { block: !allowedOnce, reason: allowedOnce ? BLOCK_REASON_ALLOW_ONCE : BLOCK_REASON_SS_BLOCKED }; + const allowedOnce = c2pDb.allowedOnce(tab_id, app_id); + return { block: !allowedOnce, reason: allowedOnce ? BLOCK_REASON_ALLOWED_ONCE : BLOCK_REASON_SS_BLOCKED }; } if (this.blacklisted(tab_url)) { - return { block: !allowedOnce, reason: allowedOnce ? BLOCK_REASON_ALLOW_ONCE : BLOCK_REASON_BLACKLISTED }; + const allowedOnce = c2pDb.allowedOnce(tab_id, app_id); + return { block: !allowedOnce, reason: allowedOnce ? BLOCK_REASON_ALLOWED_ONCE : BLOCK_REASON_BLACKLISTED }; } - return { block: false, reason: BLOCK_REASON_GLOBAL_BLOCKING }; + return { block: false, reason: BLOCK_REASON_GLOBAL_BLOCKED }; } } From 6ee32ae249a5acc239a6f65560b8199f5effb487 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Mar=C3=ADa=20Signanini?= Date: Fri, 23 Mar 2018 14:53:08 -0400 Subject: [PATCH 08/10] Update JSDocs and add missing block reason. --- src/classes/EventHandlers.js | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/classes/EventHandlers.js b/src/classes/EventHandlers.js index d5a4d2556..05ce074f2 100644 --- a/src/classes/EventHandlers.js +++ b/src/classes/EventHandlers.js @@ -23,7 +23,7 @@ import conf from './Conf'; import foundBugs from './FoundBugs'; import globals from './Globals'; import latency from './Latency'; -import Policy, { BLOCK_REASON_SS_UNBLOCKED } from './Policy'; +import Policy, { BLOCK_REASON_SS_UNBLOCKED, BLOCK_REASON_C2P_ALLOWED_THROUGH } from './Policy'; import PolicySmartBlock from './PolicySmartBlock'; import PurpleBox from './PurpleBox'; import surrogatedb from './SurrogateDb'; @@ -754,6 +754,12 @@ class EventHandlers { return true; } + /** + * @typedef {Object} BlockWithReason + * @property {boolean} block indicates if the tracker should be blocked. + * @property {string} reason indicates the reason for the block result. + */ + /** * Determine whether this request should be blocked * @@ -765,7 +771,7 @@ class EventHandlers { * @param {string} tab_host tab url host * @param {string} page_url full tab url * @param {number} request_id request id - * @return {Object} {block, ss_unblock} + * @return {BlockWithReason} block result with reason */ _checkBlocking(app_id, cat_id, tab_id, tab_host, page_url, request_id) { const fromRedirect = globals.REDIRECT_MAP.has(request_id); @@ -774,7 +780,7 @@ class EventHandlers { // If we let page-level c2p trackers through, we don't want to block it // along with all subsequent top-level redirects. if (fromRedirect && globals.LET_REDIRECTS_THROUGH) { - block = { block: false }; + block = { block: false, reason: BLOCK_REASON_C2P_ALLOWED_THROUGH }; } else { block = this.policy.shouldBlock(app_id, cat_id, tab_id, tab_host, page_url); } From b523b46a5e188ff5ca12c933dde65ea0641416c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Mar=C3=ADa=20Signanini?= Date: Fri, 23 Mar 2018 14:54:30 -0400 Subject: [PATCH 09/10] Update JSDocs and update block reason c2p names. --- src/classes/Policy.js | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/src/classes/Policy.js b/src/classes/Policy.js index f20e5698c..ae60602f3 100644 --- a/src/classes/Policy.js +++ b/src/classes/Policy.js @@ -31,7 +31,9 @@ export const BLOCK_REASON_WHITELISTED = 'BLOCK_REASON_WHITELISTED'; export const BLOCK_REASON_BLACKLISTED = 'BLOCK_REASON_BLACKLISTED'; export const BLOCK_REASON_SS_UNBLOCKED = 'BLOCK_REASON_SS_UNBLOCKED'; export const BLOCK_REASON_SS_BLOCKED = 'BLOCK_REASON_SS_BLOCKED'; -export const BLOCK_REASON_ALLOWED_ONCE = 'BLOCK_REASON_ALLOWED_ONCE'; +export const BLOCK_REASON_C2P_ALLOWED_ONCE = 'BLOCK_REASON_C2P_ALLOWED_ONCE'; +export const BLOCK_REASON_C2P_ALLOWED_THROUGH = 'BLOCK_REASON_C2P_ALLOWed_THROUGH'; + /** * Class for handling site policy. * @memberOf BackgroundClasses @@ -101,6 +103,12 @@ class Policy { return false; } + /** + * @typedef {Object} BlockWithReason + * @property {boolean} block indicates if the tracker should be blocked. + * @property {string} reason indicates the reason for the block result. + */ + /** * Check the users blocking settings (selected_app_ids and site_specific_blocks/unblocks) * to determine whether a tracker should be blocked @@ -109,7 +117,7 @@ class Policy { * @param {number} tab_id tab id * @param {string} tab_host tab url host * @param {string} tab_url tab url - * @return {Object} {block, ss_unblock} + * @return {BlockWithReason} block result with reason */ shouldBlock(app_id, cat_id, tab_id, tab_host, tab_url) { if (globals.SESSION.paused_blocking) { @@ -120,7 +128,7 @@ class Policy { if (conf.toggle_individual_trackers && conf.site_specific_unblocks.hasOwnProperty(tab_host) && conf.site_specific_unblocks[tab_host].includes(+app_id)) { if (this.blacklisted(tab_url)) { const allowedOnce = c2pDb.allowedOnce(tab_id, app_id); - return { block: !allowedOnce, reason: allowedOnce ? BLOCK_REASON_ALLOWED_ONCE : BLOCK_REASON_BLACKLISTED }; + return { block: !allowedOnce, reason: allowedOnce ? BLOCK_REASON_C2P_ALLOWED_ONCE : BLOCK_REASON_BLACKLISTED }; } return { block: false, reason: BLOCK_REASON_SS_UNBLOCKED }; } @@ -128,7 +136,7 @@ class Policy { return { block: false, reason: BLOCK_REASON_WHITELISTED }; } const allowedOnce = c2pDb.allowedOnce(tab_id, app_id); - return { block: !allowedOnce, reason: allowedOnce ? BLOCK_REASON_ALLOWED_ONCE : BLOCK_REASON_GLOBAL_BLOCKED }; + return { block: !allowedOnce, reason: allowedOnce ? BLOCK_REASON_C2P_ALLOWED_ONCE : BLOCK_REASON_GLOBAL_BLOCKED }; } // We get here when app_id is not selected for blocking if (conf.toggle_individual_trackers && conf.site_specific_blocks.hasOwnProperty(tab_host) && conf.site_specific_blocks[tab_host].includes(+app_id)) { @@ -136,11 +144,11 @@ class Policy { return { block: false, reason: BLOCK_REASON_WHITELISTED }; } const allowedOnce = c2pDb.allowedOnce(tab_id, app_id); - return { block: !allowedOnce, reason: allowedOnce ? BLOCK_REASON_ALLOWED_ONCE : BLOCK_REASON_SS_BLOCKED }; + return { block: !allowedOnce, reason: allowedOnce ? BLOCK_REASON_C2P_ALLOWED_ONCE : BLOCK_REASON_SS_BLOCKED }; } if (this.blacklisted(tab_url)) { const allowedOnce = c2pDb.allowedOnce(tab_id, app_id); - return { block: !allowedOnce, reason: allowedOnce ? BLOCK_REASON_ALLOWED_ONCE : BLOCK_REASON_BLACKLISTED }; + return { block: !allowedOnce, reason: allowedOnce ? BLOCK_REASON_C2P_ALLOWED_ONCE : BLOCK_REASON_BLACKLISTED }; } return { block: false, reason: BLOCK_REASON_GLOBAL_BLOCKED }; } From 73cc0eb9dd5ba098c837aba85a76d405d61715f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Mar=C3=ADa=20Signanini?= Date: Fri, 23 Mar 2018 14:55:54 -0400 Subject: [PATCH 10/10] Fix typo. --- src/classes/Policy.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/classes/Policy.js b/src/classes/Policy.js index ae60602f3..d5213e830 100644 --- a/src/classes/Policy.js +++ b/src/classes/Policy.js @@ -32,7 +32,7 @@ export const BLOCK_REASON_BLACKLISTED = 'BLOCK_REASON_BLACKLISTED'; export const BLOCK_REASON_SS_UNBLOCKED = 'BLOCK_REASON_SS_UNBLOCKED'; export const BLOCK_REASON_SS_BLOCKED = 'BLOCK_REASON_SS_BLOCKED'; export const BLOCK_REASON_C2P_ALLOWED_ONCE = 'BLOCK_REASON_C2P_ALLOWED_ONCE'; -export const BLOCK_REASON_C2P_ALLOWED_THROUGH = 'BLOCK_REASON_C2P_ALLOWed_THROUGH'; +export const BLOCK_REASON_C2P_ALLOWED_THROUGH = 'BLOCK_REASON_C2P_ALLOWED_THROUGH'; /** * Class for handling site policy.