Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GH-1566 - Smart blocking should check for Click2Play #388

Merged
merged 9 commits into from Jun 11, 2019

Destructure underscore lib imports.

  • Loading branch information
jsignanini committed Jun 11, 2019
commit 80c917a973341dc55cbf7cae997f4f11101730c0
@@ -18,7 +18,7 @@
/**
* @namespace Background
*/
import _ from 'underscore';
import { debounce, every, size } from 'underscore';
import moment from 'moment/min/moment-with-locales.min';
import cliqz, { prefs } from './classes/Cliqz';
// object class
@@ -1058,11 +1058,11 @@ function onMessageHandler(request, sender, callback) {
*/
function initializeDispatcher() {
dispatcher.on('conf.save.selected_app_ids', (appIds) => {
const num_selected = _.size(appIds);
const num_selected = size(appIds);
const { db } = bugDb;
db.noneSelected = (num_selected === 0);
// can't simply compare num_selected and _.size(db.apps) since apps get removed sometimes
db.allSelected = (!!num_selected && _.every(db.apps, (app, app_id) => appIds.hasOwnProperty(app_id)));
// can't simply compare num_selected and size(db.apps) since apps get removed sometimes
db.allSelected = (!!num_selected && every(db.apps, (app, app_id) => appIds.hasOwnProperty(app_id)));
});
dispatcher.on('conf.save.site_whitelist', () => {
// TODO debounce with below
@@ -1118,7 +1118,7 @@ function initializeDispatcher() {
}
});

dispatcher.on('conf.changed.settings', _.debounce((key) => {
dispatcher.on('conf.changed.settings', debounce((key) => {
log('Conf value changed for a watched user setting:', key);
}, 200));

@@ -13,7 +13,7 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0
*/

import _ from 'underscore';
import { isEqual } from 'underscore';
import normalize from 'json-api-normalizer';
import build from 'redux-object';
import RSVP from 'rsvp';
@@ -542,7 +542,7 @@ class Account {
}
SYNC_SET.forEach((key) => {
if (settings[key] !== undefined &&
!_.isEqual(conf[key], settings[key])) {
!isEqual(conf[key], settings[key])) {
conf[key] = settings[key];
}
});
@@ -14,7 +14,7 @@
/* eslint no-param-reassign: 0 */
/* eslint no-shadow: 0 */

import _ from 'underscore';
import { difference, each, every, keys, reduce, size } from 'underscore';
import conf from './Conf';
import Updatable from './Updatable';
import { defineLazyProperty, flushChromeMemoryCache } from '../utils/utils';
@@ -38,9 +38,9 @@ class BugDb extends Updatable {
updateNewAppIds(new_apps, old_apps) {
log('updating newAppIds...');

const new_app_ids = _.difference(
_.keys(new_apps),
_.keys(old_apps)
const new_app_ids = difference(
keys(new_apps),
keys(old_apps)
).map(Number);

conf.new_app_ids = new_app_ids;
@@ -55,7 +55,7 @@ class BugDb extends Updatable {
if (conf.block_by_default) {
log('applying block-by-default...');
const { selected_app_ids } = conf;
_.each(new_app_ids, (app_id) => {
each(new_app_ids, (app_id) => {
selected_app_ids[app_id] = 1;
});
conf.selected_app_ids = selected_app_ids;
@@ -184,13 +184,13 @@ class BugDb extends Updatable {

log('setting bugdb noneSelected/allSelected...');

const num_selected = _.size(conf.selected_app_ids);
const num_selected = size(conf.selected_app_ids);
db.noneSelected = (num_selected === 0);

// since allSelected is slow to eval, make it lazy
defineLazyProperty(db, 'allSelected', () => {
const num_selected = _.size(conf.selected_app_ids);
return (!!num_selected && _.every(db.apps, (app, app_id) => conf.selected_app_ids.hasOwnProperty(app_id)));
const num_selected = size(conf.selected_app_ids);
return (!!num_selected && every(db.apps, (app, app_id) => conf.selected_app_ids.hasOwnProperty(app_id)));
});

log('processed bugdb...');
@@ -211,7 +211,7 @@ class BugDb extends Updatable {

// pre-trie/legacy db
} else if (old_bugs.hasOwnProperty('bugsVersion') && bugs.version !== old_bugs.bugsVersion) {
const old_apps = _.reduce(old_bugs.bugs, (memo, bug) => {
const old_apps = reduce(old_bugs.bugs, (memo, bug) => {
memo[bug.aid] = true;
return memo;
}, {});
@@ -14,7 +14,7 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0
*/

import _ from 'underscore';
import { map, object, reduce, throttle } from 'underscore';
import bugDb from './BugDb';
import button from './BrowserButton';
import c2pDb from './Click2PlayDb';
@@ -51,7 +51,7 @@ class EventHandlers {
// Use a 1sec interval to limit calls on pages with a large number of requests.
// Don't use tabId with button.update for cases where tab is switched before throttle delay is reached.
// ToDo: Remove this function when there is an event for AdBlocker:foundAd.
this._throttleButtonUpdate = _.throttle((tabId) => {
this._throttleButtonUpdate = throttle((tabId) => {
button.update(tabId);
}, 1000, { leading: false });
}
@@ -238,7 +238,7 @@ class EventHandlers {
if (result) {
utils.sendMessage(
tab_id, 'showUpgradeAlert', {
translations: _.object(_.map(alert_messages, key => [key, chrome.i18n.getMessage(key)])),
translations: object(map(alert_messages, key => [key, chrome.i18n.getMessage(key)])),
language: conf.language,
major_upgrade: globals.JUST_UPGRADED_FROM_7
},
@@ -256,7 +256,7 @@ class EventHandlers {
if (result) {
utils.sendMessage(
tab_id, 'showLibraryUpdateAlert', {
translations: _.object(_.map(alert_messages, key => [key, chrome.i18n.getMessage(key)])),
translations: object(map(alert_messages, key => [key, chrome.i18n.getMessage(key)])),
language: conf.language
},
() => {
@@ -670,7 +670,7 @@ class EventHandlers {
const surrogates = surrogatedb.getForTracker(details.url, appId, bugId, ti.host);

if (surrogates.length > 0) {
code = _.reduce(surrogates, (memo, s) => {
code = reduce(surrogates, (memo, s) => {
memo += s.code; // eslint-disable-line no-param-reassign
return memo;
}, '');
@@ -13,7 +13,7 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0
*/

import _ from 'underscore';
import { isEmpty } from 'underscore';
import foundBugs from './FoundBugs';
/**
* Class for handling request latency data.
@@ -38,7 +38,7 @@ class Latency {
}
// If the latencies object for this request id is empty then this is
// not a tracker. Safe to delete object and return.
if (_.isEmpty(this.latencies[request_id])) {
if (isEmpty(this.latencies[request_id])) {
delete this.latencies[request_id];
return 0;
}
@@ -55,7 +55,7 @@ class Latency {
} = this.latencies[request_id][details.url];

delete this.latencies[request_id][details.url];
if (_.isEmpty(this.latencies[request_id])) {
if (isEmpty(this.latencies[request_id])) {
delete this.latencies[request_id];
}

@@ -14,7 +14,7 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0
*/

import _ from 'underscore';
import { isEqual, throttle } from 'underscore';
import button from './BrowserButton';
import cliqz from './Cliqz';
import conf from './Conf';
@@ -232,7 +232,7 @@ class PanelData {
* Invoked in EventHandlers#onBeforeRequest when a new bug has been found
* Sends updated data to the panel and blocking and/or summary components
*/
updatePanelUI = _.throttle(this._updatePanelUI.bind(this), 600, { leading: true, trailing: true }); // matches donut redraw throttling
updatePanelUI = throttle(this._updatePanelUI.bind(this), 600, { leading: true, trailing: true }); // matches donut redraw throttling
_updatePanelUI() {
if (!this._panelPort || !this._activeTab) { return; }

@@ -616,7 +616,7 @@ class PanelData {
// Set the conf from data
// TODO can this now be replaced by Object.entries?
for (const [key, value] of objectEntries(data)) {
if (conf.hasOwnProperty(key) && !_.isEqual(conf[key], value)) {
if (conf.hasOwnProperty(key) && !isEqual(conf[key], value)) {
conf[key] = value;
syncSetDataChanged = SYNC_SET.has(key) ? true : syncSetDataChanged;
// TODO refactor - this work should probably be the direct responsibility of Globals
@@ -11,7 +11,7 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0
*/

import _ from 'underscore';
import { any, filter, isArray, map } from 'underscore';
import conf from './Conf';
import Updatable from './Updatable';
import { log } from '../utils/common';
@@ -59,14 +59,14 @@ class SurrogateDb extends Updatable {

// convert single values to arrays first
['pattern_id', 'app_id', 'sites', 'match'].forEach((prop) => {
if (s.hasOwnProperty(prop) && !_.isArray(s[prop])) {
if (s.hasOwnProperty(prop) && !isArray(s[prop])) {
s[prop] = [s[prop]];
}
});

// initialize regexes
if (s.hasOwnProperty('match')) {
s.match = _.map(s.match, match => new RegExp(match, ''));
s.match = map(s.match, match => new RegExp(match, ''));
}

if (s.hasOwnProperty('pattern_id') || s.hasOwnProperty('app_id')) {
@@ -108,7 +108,7 @@ class SurrogateDb extends Updatable {
candidates = candidates.concat(this.db.pattern_ids[pattern_id]);
}

return _.filter(candidates, (surrogate) => {
return filter(candidates, (surrogate) => {
// note: does not support *.example.com (exact matches only)
if (surrogate.hasOwnProperty('sites')) { // array of site hosts
if (!surrogate.sites.includes(host_name)) {
@@ -117,7 +117,7 @@ class SurrogateDb extends Updatable {
}

if (surrogate.hasOwnProperty('match')) {
if (!_.any(surrogate.match, match => script_src.match(match))) {
if (!any(surrogate.match, match => script_src.match(match))) {
return false;
}
}
@@ -11,7 +11,7 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0
*/

import _ from 'underscore';
import { bind, isFunction } from 'underscore';
import globals from './Globals';
import conf from './Conf';
import { getJson, fetchLocalJSONResource } from '../utils/utils';
@@ -56,7 +56,7 @@ class Updatable {
callback
};

if (_.isFunction(version)) {
if (isFunction(version)) {
opts.callback = version;
delete opts.version;
}
@@ -166,7 +166,7 @@ class Updatable {
}

// Fetch new bugs list from remote. Bind the callback param from _remoteFetcher() to this anonymous function
this._remoteFetcher(_.bind(function (result, list) {
this._remoteFetcher(bind(function (result, list) {
// if the fetch worked and we have a list returned
if (result && list) {
const data = this.processList(false, list);
@@ -13,7 +13,7 @@

/* eslint no-use-before-define: 0 */

import _ from 'underscore';
import { reject } from 'underscore';
import bugDb from '../classes/BugDb';
import c2pDb from '../classes/Click2PlayDb';
import conf from '../classes/Conf';
@@ -52,7 +52,7 @@ export function buildC2P(details, app_id) {

// click-to-play for social buttons might be disabled
if (!conf.enable_click2play_social) {
c2pApp = _.reject(c2pApp, c2pAppDef => !!c2pAppDef.button);
c2pApp = reject(c2pApp, c2pAppDef => !!c2pAppDef.button);
}

if (!c2pApp.length) {
@@ -18,7 +18,7 @@
/**
* @namespace BackgroundUtils
*/
import _ from 'underscore';
import { debounce } from 'underscore';
import url from 'url';
import tabInfo from '../classes/TabInfo';
import globals from '../classes/Globals';
@@ -114,7 +114,7 @@ export function isValidTopLevelNavigation(details) {
* (can default to 20 if undefined)
* @memberOf BackgroundUtils
*/
export const flushChromeMemoryCache = _.debounce(() => {
export const flushChromeMemoryCache = debounce(() => {
chrome.webRequest.handlerBehaviorChanged();
}, 1000 * 35, true);

ProTip! Use n and p to navigate between commits in a pull request.