From aadf4a6427fa2960b4fb9e20cfbaab66e2642d81 Mon Sep 17 00:00:00 2001 From: gorhill Date: Thu, 26 Jan 2017 10:17:38 -0500 Subject: [PATCH] fix #2340 --- src/js/assets.js | 182 ++++++++++++++++++++++---------------------- src/js/messaging.js | 19 ++--- src/js/start.js | 14 +++- src/js/storage.js | 81 +++++++++----------- 4 files changed, 147 insertions(+), 149 deletions(-) diff --git a/src/js/assets.js b/src/js/assets.js index ba83f5d14..1e13848cf 100644 --- a/src/js/assets.js +++ b/src/js/assets.js @@ -137,72 +137,74 @@ var getTextFileFromURL = function(url, onLoad, onError) { **/ api.listKeyAliases = { - "assets/thirdparties/publicsuffix.org/list/effective_tld_names.dat": "public_suffix_list.dat", - "assets/user/filters.txt": "user-filters", - "assets/ublock/resources.txt": "ublock-resources", - "assets/ublock/filters.txt": "ublock-filters", - "assets/ublock/privacy.txt": "ublock-privacy", - "assets/ublock/unbreak.txt": "ublock-unbreak", - "assets/ublock/badware.txt": "ublock-badware", - "assets/ublock/experimental.txt": "ublock-experimental", - "https://easylist-downloads.adblockplus.org/easylistchina.txt": "CHN-0", - "https://raw.githubusercontent.com/cjx82630/cjxlist/master/cjxlist.txt": "CHN-1", - "https://raw.githubusercontent.com/cjx82630/cjxlist/master/cjx-annoyance.txt": "CHN-2", - "https://easylist-downloads.adblockplus.org/easylistgermany.txt": "DEU-0", - "https://adblock.dk/block.csv": "DNK-0", - "assets/thirdparties/easylist-downloads.adblockplus.org/easylist.txt": "easylist", - "https://easylist-downloads.adblockplus.org/easylist_noelemhide.txt": "easylist-nocosmetic", - "assets/thirdparties/easylist-downloads.adblockplus.org/easyprivacy.txt": "easyprivacy", - "https://easylist-downloads.adblockplus.org/fanboy-annoyance.txt": "fanboy-annoyance", - "https://easylist-downloads.adblockplus.org/fanboy-social.txt": "fanboy-social", - "https://easylist-downloads.adblockplus.org/liste_fr.txt": "FRA-0", - "http://adblock.gardar.net/is.abp.txt": "ISL-0", - "https://easylist-downloads.adblockplus.org/easylistitaly.txt": "ITA-0", - "https://dl.dropboxusercontent.com/u/1289327/abpxfiles/filtri.txt": "ITA-1", - "https://easylist-downloads.adblockplus.org/advblock.txt": "RUS-0", - "https://easylist-downloads.adblockplus.org/bitblock.txt": "RUS-1", - "https://filters.adtidy.org/extension/chromium/filters/1.txt": "RUS-2", - "https://adguard.com/en/filter-rules.html?id=1": "RUS-2", - "https://easylist-downloads.adblockplus.org/easylistdutch.txt": "NLD-0", - "https://notabug.org/latvian-list/adblock-latvian/raw/master/lists/latvian-list.txt": "LVA-0", - "http://hosts-file.net/.%5Cad_servers.txt": "hphosts", - "http://adblock.ee/list.php": "EST-0", - "https://s3.amazonaws.com/lists.disconnect.me/simple_malvertising.txt": "disconnect-malvertising", - "https://s3.amazonaws.com/lists.disconnect.me/simple_malware.txt": "disconnect-malware", - "https://s3.amazonaws.com/lists.disconnect.me/simple_tracking.txt": "disconnect-tracking", - "https://www.certyficate.it/adblock/adblock.txt": "POL-0", - "https://easylist-downloads.adblockplus.org/antiadblockfilters.txt": "awrl-0", - "http://adb.juvander.net/Finland_adb.txt": "FIN-0", - "https://raw.githubusercontent.com/gfmaster/adblock-korea-contrib/master/filter.txt": "KOR-0", - "https://raw.githubusercontent.com/yous/YousList/master/youslist.txt": "KOR-1", - "https://www.fanboy.co.nz/fanboy-korean.txt": "KOR-2", - "https://raw.githubusercontent.com/heradhis/indonesianadblockrules/master/subscriptions/abpindo.txt": "IDN-0", - "https://raw.githubusercontent.com/k2jp/abp-japanese-filters/master/abpjf.txt": "JPN-0", - "https://raw.githubusercontent.com/liamja/Prebake/master/obtrusive.txt": "EU-prebake", - "https://easylist-downloads.adblockplus.org/Liste_AR.txt": "ara-0", - "http://margevicius.lt/easylistlithuania.txt": "LTU-0", - "assets/thirdparties/www.malwaredomainlist.com/hostslist/hosts.txt": "malware-0", - "assets/thirdparties/mirror1.malwaredomains.com/files/justdomains": "malware-1", - "http://malwaredomains.lehigh.edu/files/immortal_domains.txt": "malware-2", - "assets/thirdparties/pgl.yoyo.org/as/serverlist": "plowe-0", - "https://raw.githubusercontent.com/easylist/EasyListHebrew/master/EasyListHebrew.txt": "ISR-0", - "https://raw.githubusercontent.com/reek/anti-adblock-killer/master/anti-adblock-killer-filters.txt": "reek-0", - "https://raw.githubusercontent.com/szpeter80/hufilter/master/hufilter.txt": "HUN-0", - "https://raw.githubusercontent.com/tomasko126/easylistczechandslovak/master/filters.txt": "CZE-0", - "http://someonewhocares.org/hosts/hosts": "dpollock-0", - "https://raw.githubusercontent.com/Dawsey21/Lists/master/adblock-list.txt": "spam404-0", - "http://stanev.org/abp/adblock_bg.txt": "BGR-0", - "http://winhelp2002.mvps.org/hosts.txt": "mvps-0", - "https://www.fanboy.co.nz/enhancedstats.txt": "fanboy-enhanced", - "https://www.fanboy.co.nz/fanboy-antifacebook.txt": "fanboy-thirdparty_social", - "https://easylist-downloads.adblockplus.org/easylistspanish.txt": "spa-0", - "https://www.fanboy.co.nz/fanboy-swedish.txt": "SWE-0", - "https://www.fanboy.co.nz/r/fanboy-ultimate.txt": "fanboy-ultimate", - "https://filters.adtidy.org/extension/chromium/filters/13.txt": "TUR-0", - "https://adguard.com/filter-rules.html?id=13": "TUR-0", - "https://www.fanboy.co.nz/fanboy-vietnam.txt": "VIE-0", - "https://www.void.gr/kargig/void-gr-filters.txt": "GRC-0", - "https://raw.githubusercontent.com/betterwebleon/slovenian-list/master/filters.txt": "SVN-0" + "assets/thirdparties/publicsuffix.org/list/effective_tld_names.dat": "public_suffix_list.dat", + "assets/user/filters.txt": "user-filters", + "assets/ublock/resources.txt": "ublock-resources", + "assets/ublock/filters.txt": "ublock-filters", + "assets/ublock/privacy.txt": "ublock-privacy", + "assets/ublock/unbreak.txt": "ublock-unbreak", + "assets/ublock/badware.txt": "ublock-badware", + "assets/ublock/experimental.txt": "ublock-experimental", + "https://easylist-downloads.adblockplus.org/easylistchina.txt": "CHN-0", + "https://raw.githubusercontent.com/cjx82630/cjxlist/master/cjxlist.txt": "CHN-1", + "https://raw.githubusercontent.com/cjx82630/cjxlist/master/cjx-annoyance.txt": "CHN-2", + "https://easylist-downloads.adblockplus.org/easylistgermany.txt": "DEU-0", + "https://adblock.dk/block.csv": "DNK-0", + "assets/thirdparties/easylist-downloads.adblockplus.org/easylist.txt": "easylist", + "https://easylist-downloads.adblockplus.org/easylist_noelemhide.txt": "easylist-nocosmetic", + "assets/thirdparties/easylist-downloads.adblockplus.org/easyprivacy.txt": "easyprivacy", + "https://easylist-downloads.adblockplus.org/fanboy-annoyance.txt": "fanboy-annoyance", + "https://easylist-downloads.adblockplus.org/fanboy-social.txt": "fanboy-social", + "https://easylist-downloads.adblockplus.org/liste_fr.txt": "FRA-0", + "http://adblock.gardar.net/is.abp.txt": "ISL-0", + "https://easylist-downloads.adblockplus.org/easylistitaly.txt": "ITA-0", + "https://dl.dropboxusercontent.com/u/1289327/abpxfiles/filtri.txt": "ITA-1", + "https://easylist-downloads.adblockplus.org/advblock.txt": "RUS-0", + "https://easylist-downloads.adblockplus.org/bitblock.txt": "RUS-1", + "https://filters.adtidy.org/extension/chromium/filters/1.txt": "RUS-2", + "https://adguard.com/en/filter-rules.html?id=1": "RUS-2", + "https://easylist-downloads.adblockplus.org/easylistdutch.txt": "NLD-0", + "https://notabug.org/latvian-list/adblock-latvian/raw/master/lists/latvian-list.txt": "LVA-0", + "http://hosts-file.net/.%5Cad_servers.txt": "hphosts", + "http://adblock.ee/list.php": "EST-0", + "https://s3.amazonaws.com/lists.disconnect.me/simple_malvertising.txt": "disconnect-malvertising", + "https://s3.amazonaws.com/lists.disconnect.me/simple_malware.txt": "disconnect-malware", + "https://s3.amazonaws.com/lists.disconnect.me/simple_tracking.txt": "disconnect-tracking", + "https://www.certyficate.it/adblock/adblock.txt": "POL-0", + "https://raw.githubusercontent.com/MajkiIT/polish-ads-filter/master/polish-adblock-filters/adblock.txt": "POL-0", + "https://easylist-downloads.adblockplus.org/antiadblockfilters.txt": "awrl-0", + "http://adb.juvander.net/Finland_adb.txt": "FIN-0", + "https://raw.githubusercontent.com/gfmaster/adblock-korea-contrib/master/filter.txt": "KOR-0", + "https://raw.githubusercontent.com/yous/YousList/master/youslist.txt": "KOR-1", + "https://www.fanboy.co.nz/fanboy-korean.txt": "KOR-2", + "https://raw.githubusercontent.com/heradhis/indonesianadblockrules/master/subscriptions/abpindo.txt": "IDN-0", + "https://raw.githubusercontent.com/ABPindo/indonesianadblockrules/master/subscriptions/abpindo.txt": "IDN-0", + "https://raw.githubusercontent.com/k2jp/abp-japanese-filters/master/abpjf.txt": "JPN-0", + "https://raw.githubusercontent.com/liamja/Prebake/master/obtrusive.txt": "EU-prebake", + "https://easylist-downloads.adblockplus.org/Liste_AR.txt": "ara-0", + "http://margevicius.lt/easylistlithuania.txt": "LTU-0", + "assets/thirdparties/www.malwaredomainlist.com/hostslist/hosts.txt": "malware-0", + "assets/thirdparties/mirror1.malwaredomains.com/files/justdomains": "malware-1", + "http://malwaredomains.lehigh.edu/files/immortal_domains.txt": "malware-2", + "assets/thirdparties/pgl.yoyo.org/as/serverlist": "plowe-0", + "https://raw.githubusercontent.com/easylist/EasyListHebrew/master/EasyListHebrew.txt": "ISR-0", + "https://raw.githubusercontent.com/reek/anti-adblock-killer/master/anti-adblock-killer-filters.txt": "reek-0", + "https://raw.githubusercontent.com/szpeter80/hufilter/master/hufilter.txt": "HUN-0", + "https://raw.githubusercontent.com/tomasko126/easylistczechandslovak/master/filters.txt": "CZE-0", + "http://someonewhocares.org/hosts/hosts": "dpollock-0", + "https://raw.githubusercontent.com/Dawsey21/Lists/master/adblock-list.txt": "spam404-0", + "http://stanev.org/abp/adblock_bg.txt": "BGR-0", + "http://winhelp2002.mvps.org/hosts.txt": "mvps-0", + "https://www.fanboy.co.nz/enhancedstats.txt": "fanboy-enhanced", + "https://www.fanboy.co.nz/fanboy-antifacebook.txt": "fanboy-thirdparty_social", + "https://easylist-downloads.adblockplus.org/easylistspanish.txt": "spa-0", + "https://www.fanboy.co.nz/fanboy-swedish.txt": "SWE-0", + "https://www.fanboy.co.nz/r/fanboy-ultimate.txt": "fanboy-ultimate", + "https://filters.adtidy.org/extension/chromium/filters/13.txt": "TUR-0", + "https://adguard.com/filter-rules.html?id=13": "TUR-0", + "https://www.fanboy.co.nz/fanboy-vietnam.txt": "VIE-0", + "https://www.void.gr/kargig/void-gr-filters.txt": "GRC-0", + "https://raw.githubusercontent.com/betterwebleon/slovenian-list/master/filters.txt": "SVN-0" }; var migrate = function(callback) { @@ -343,7 +345,7 @@ var saveAssetSourceRegistry = (function() { }; })(); -var updateAssetSourceRegistry = function(json) { +var updateAssetSourceRegistry = function(json, silent) { var newDict; try { newDict = JSON.parse(json); @@ -351,29 +353,29 @@ var updateAssetSourceRegistry = function(json) { } if ( newDict instanceof Object === false ) { return; } - getAssetSourceRegistry(function(oldDict) { - var assetKey; - // Remove obsolete entries (only those which were built-in). - for ( assetKey in oldDict ) { - if ( - newDict[assetKey] === undefined && - oldDict[assetKey].submitter === undefined - ) { - unregisterAssetSource(assetKey); - } + var oldDict = assetSourceRegistry, + assetKey; + + // Remove obsolete entries (only those which were built-in). + for ( assetKey in oldDict ) { + if ( + newDict[assetKey] === undefined && + oldDict[assetKey].submitter === undefined + ) { + unregisterAssetSource(assetKey); } - // Add/update existing entries. Notify of new asset sources. - for ( assetKey in newDict ) { - if ( oldDict[assetKey] === undefined ) { - fireNotification( - 'builtin-asset-source-added', - { assetKey: assetKey, entry: newDict[assetKey] } - ); - } - registerAssetSource(assetKey, newDict[assetKey]); + } + // Add/update existing entries. Notify of new asset sources. + for ( assetKey in newDict ) { + if ( oldDict[assetKey] === undefined && !silent ) { + fireNotification( + 'builtin-asset-source-added', + { assetKey: assetKey, entry: newDict[assetKey] } + ); } - saveAssetSourceRegistry(); - }); + registerAssetSource(assetKey, newDict[assetKey]); + } + saveAssetSourceRegistry(); }; var getAssetSourceRegistry = function(callback) { @@ -406,7 +408,7 @@ var getAssetSourceRegistry = function(callback) { getTextFileFromURL( µBlock.assetsBootstrapLocation || 'assets/assets.json', function() { - updateAssetSourceRegistry(this.responseText); + updateAssetSourceRegistry(this.responseText, true); registryReady(); } ); @@ -899,7 +901,7 @@ api.metadata = function(callback) { }); getAssetCacheRegistry(function() { - cacheRegistryReady = assetCacheRegistry; + cacheRegistryReady = true; if ( assetRegistryReady ) { onReady(); } }); }; diff --git a/src/js/messaging.js b/src/js/messaging.js index 201ddbf28..4549d6c5b 100644 --- a/src/js/messaging.js +++ b/src/js/messaging.js @@ -754,27 +754,25 @@ var backupUserData = function(callback) { timeStamp: Date.now(), version: vAPI.app.version, userSettings: µb.userSettings, - selectedFilterLists: [], + selectedFilterLists: µb.selectedFilterLists, hiddenSettingsString: µb.stringFromHiddenSettings(), netWhitelist: µb.stringFromWhitelist(µb.netWhitelist), dynamicFilteringString: µb.permanentFirewall.toString(), urlFilteringString: µb.permanentURLFiltering.toString(), hostnameSwitchesString: µb.hnSwitches.toString(), - userFilters: '' - }; - - var onSelectedListsReady = function(selectedFilterLists) { - userData.selectedFilterLists = selectedFilterLists; - + userFilters: '', // TODO(seamless migration): // The following is strictly for convenience, to be minimally // forward-compatible. This will definitely be removed in the // short term, as I do not expect the need to install an older // version of uBO to ever be needed beyond the short term. // >>>>>>>> - userData.filterLists = µb.oldDataFromNewListKeys(selectedFilterLists); + filterLists: µb.oldDataFromNewListKeys(µb.selectedFilterLists) // <<<<<<<< + }; + var onUserFiltersReady = function(details) { + userData.userFilters = details.content; var filename = vAPI.i18n('aboutBackupFilename') .replace('{{datetime}}', µb.dateNowToSensibleString()) .replace(/ +/g, '_'); @@ -789,11 +787,6 @@ var backupUserData = function(callback) { getLocalData(callback); }; - var onUserFiltersReady = function(details) { - userData.userFilters = details.content; - µb.loadSelectedFilterLists(onSelectedListsReady); - }; - µb.assets.get(µb.userFiltersPath, onUserFiltersReady); }; diff --git a/src/js/start.js b/src/js/start.js index 2ccd4fc8d..8c37217f9 100644 --- a/src/js/start.js +++ b/src/js/start.js @@ -241,7 +241,7 @@ var fromFetch = function(to, fetched) { /******************************************************************************/ -var onAdminSettingsRestored = function() { +var onSelectedFilterListsLoaded = function() { var fetchableProps = { 'compiledMagic': '', 'dynamicFilteringString': 'behind-the-scene * 3p noop\nbehind-the-scene * 3p-frame noop', @@ -266,6 +266,18 @@ var onAdminSettingsRestored = function() { /******************************************************************************/ +// TODO(seamless migration): +// Eventually selected filter list keys will be loaded as a fetchable +// property. Until then we need to handle backward and forward +// compatibility, this means a special asynchronous call to load selected +// filter lists. + +var onAdminSettingsRestored = function() { + µb.loadSelectedFilterLists(onSelectedFilterListsLoaded); +}; + +/******************************************************************************/ + µb.hiddenSettings = (function() { var out = objectAssign({}, µb.hiddenSettingsDefault), json = vAPI.localStorage.getItem('hiddenSettings'); diff --git a/src/js/storage.js b/src/js/storage.js index 7d394f52d..f914c0452 100644 --- a/src/js/storage.js +++ b/src/js/storage.js @@ -170,7 +170,12 @@ var µb = this; vAPI.storage.get([ 'selectedFilterLists', 'remoteBlacklists' ], function(bin) { if ( !bin || !bin.selectedFilterLists && !bin.remoteBlacklists ) { - return callback(); + // Select default filter lists if first-time launch. + µb.assets.metadata(function(availableLists) { + µb.saveSelectedFilterLists(µb.autoSelectRegionalFilterLists(availableLists)); + callback(); + }); + return; } var listKeys = []; if ( bin.selectedFilterLists ) { @@ -186,33 +191,30 @@ // Uncomment when all have moved to v1.11 and beyond. //vAPI.storage.remove('remoteBlacklists'); } - µb.selectedFilterLists = listKeys.slice(); - callback(listKeys); + µb.selectedFilterLists = listKeys; + callback(); }); }; µBlock.saveSelectedFilterLists = function(newKeys, append) { - var µb = this; - this.loadSelectedFilterLists(function(oldKeys) { - oldKeys = oldKeys || []; - if ( append ) { - newKeys = newKeys.concat(oldKeys); + var oldKeys = this.selectedFilterLists.slice(); + if ( append ) { + newKeys = newKeys.concat(oldKeys); + } + var newSet = new Set(newKeys); + // Purge unused filter lists from cache. + for ( var i = 0, n = oldKeys.length; i < n; i++ ) { + if ( newSet.has(oldKeys[i]) === false ) { + this.removeFilterList(oldKeys[i]); } - var newSet = new Set(newKeys); - // Purge unused filter lists from cache. - for ( var i = 0, n = oldKeys.length; i < n; i++ ) { - if ( newSet.has(oldKeys[i]) === false ) { - µb.removeFilterList(oldKeys[i]); - } - } - newKeys = µb.setToArray(newSet); - var bin = { - selectedFilterLists: newKeys, - remoteBlacklists: µb.oldDataFromNewListKeys(newKeys) - }; - µb.selectedFilterLists = newKeys; - vAPI.storage.set(bin); - }); + } + newKeys = this.setToArray(newSet); + var bin = { + selectedFilterLists: newKeys, + remoteBlacklists: this.oldDataFromNewListKeys(newKeys) + }; + this.selectedFilterLists = newKeys; + vAPI.storage.set(bin); }; // TODO(seamless migration): @@ -429,7 +431,7 @@ µBlock.autoSelectRegionalFilterLists = function(lists) { var lang = self.navigator.language.slice(0, 2), - selectedListKeys = [], + selectedListKeys = [ this.userFiltersPath ], list; for ( var key in lists ) { if ( lists.hasOwnProperty(key) === false ) { continue; } @@ -522,26 +524,7 @@ } }; - // Selected lists. - var onSelectedListsLoaded = function(keys) { - var listKey; - // No user lists data means use default settings. - if ( Array.isArray(keys) ) { - var listKeySet = new Set(keys); - for ( listKey in newAvailableLists ) { - if ( newAvailableLists.hasOwnProperty(listKey) ) { - newAvailableLists[listKey].off = !listKeySet.has(listKey); - } - } - } else if ( µb.firstInstall ) { - µb.saveSelectedFilterLists(µb.autoSelectRegionalFilterLists(newAvailableLists)); - } - - finalize(); - callback(newAvailableLists); - }; - - // Built-in filter lists. + // Built-in filter lists loaded. var onBuiltinListsLoaded = function(entries) { for ( var assetKey in entries ) { if ( entries.hasOwnProperty(assetKey) === false ) { continue; } @@ -551,7 +534,15 @@ } // Load set of currently selected filter lists. - µb.loadSelectedFilterLists(onSelectedListsLoaded); + var listKeySet = new Set(µb.selectedFilterLists); + for ( listKey in newAvailableLists ) { + if ( newAvailableLists.hasOwnProperty(listKey) ) { + newAvailableLists[listKey].off = !listKeySet.has(listKey); + } + } + + finalize(); + callback(newAvailableLists); }; // Available lists previously computed.