From bf384e2bca01cd487c993788e38ff0c0ff3b3d2f Mon Sep 17 00:00:00 2001 From: Raymond Hill Date: Wed, 4 Apr 2018 12:42:01 -0400 Subject: [PATCH] better evaluate vAPI.webextFlavor --- platform/chromium/vapi-background.js | 51 +++++++++++----------------- platform/chromium/vapi-common.js | 49 +++++++++++++++++++++++--- platform/webext/vapi-webrequest.js | 25 ++++++-------- src/js/assets.js | 3 +- src/js/static-net-filtering.js | 2 +- src/js/traffic.js | 25 ++++++-------- 6 files changed, 88 insertions(+), 67 deletions(-) diff --git a/platform/chromium/vapi-background.js b/platform/chromium/vapi-background.js index c0f07427f..698fd3654 100644 --- a/platform/chromium/vapi-background.js +++ b/platform/chromium/vapi-background.js @@ -33,26 +33,10 @@ var chrome = self.chrome; var manifest = chrome.runtime.getManifest(); -vAPI.chrome = true; -vAPI.chromiumVersion = (function(){ - var matches = /\bChrom(?:e|ium)\/(\d+)\b/.exec(navigator.userAgent); - return matches !== null ? parseInt(matches[1], 10) : NaN; - })(); - vAPI.cantWebsocket = chrome.webRequest.ResourceType instanceof Object === false || chrome.webRequest.ResourceType.WEBSOCKET !== 'websocket'; -vAPI.webextFlavor = ''; -if ( - self.browser instanceof Object && - typeof self.browser.runtime.getBrowserInfo === 'function' -) { - self.browser.runtime.getBrowserInfo().then(function(info) { - vAPI.webextFlavor = info.vendor + '-' + info.name + '-' + info.version; - }); -} - // https://issues.adblockplus.org/ticket/5695 // - Good idea, adopted: cleaner way to detect user-stylesheet support. vAPI.supportsUserStylesheets = @@ -1147,7 +1131,25 @@ vAPI.cloud = (function() { var maxChunkCountPerItem = Math.floor(512 * 0.75) & ~(chunkCountPerFetch - 1); // Mind chrome.storage.sync.QUOTA_BYTES_PER_ITEM (8192 at time of writing) - var maxChunkSize = chrome.storage.sync.QUOTA_BYTES_PER_ITEM || 8192; + // https://github.com/gorhill/uBlock/issues/3006 + // For Firefox, we will use a lower ratio to allow for more overhead for + // the infrastructure. Unfortunately this leads to less usable space for + // actual data, but all of this is provided for free by browser vendors, + // so we need to accept and deal with these limitations. + var evalMaxChunkSize = function() { + return Math.floor( + (chrome.storage.sync.QUOTA_BYTES_PER_ITEM || 8192) * + (vAPI.webextFlavor.startsWith('Mozilla-Firefox-') ? 0.6 : 0.75) + ); + }; + + var maxChunkSize = evalMaxChunkSize(); + + // The real actual webextFlavor value may not be set in stone, so listen + // for possible future changes. + window.addEventListener('webextFlavor', function() { + maxChunkSize = evalMaxChunkSize(); + }, { once: true }); // Mind chrome.storage.sync.QUOTA_BYTES (128 kB at time of writing) // Firefox: @@ -1155,19 +1157,6 @@ vAPI.cloud = (function() { // > You can store up to 100KB of data using this API/ var maxStorageSize = chrome.storage.sync.QUOTA_BYTES || 102400; - // Flavor-specific handling needs to be done here. Reason: to allow time - // for vAPI.webextFlavor to be properly set. - // https://github.com/gorhill/uBlock/issues/3006 - // For Firefox, we will use a lower ratio to allow for more overhead for - // the infrastructure. Unfortunately this leads to less usable space for - // actual data, but all of this is provided for free by browser vendors, - // so we need to accept and deal with these limitations. - var initialize = function() { - var ratio = vAPI.webextFlavor.startsWith('Mozilla-Firefox-') ? 0.6 : 0.75; - maxChunkSize = Math.floor(maxChunkSize * ratio); - initialize = function(){}; - }; - var options = { defaultDeviceName: window.navigator.platform, deviceName: vAPI.localStorage.getItem('deviceName') || '' @@ -1226,7 +1215,6 @@ vAPI.cloud = (function() { }; var push = function(dataKey, data, callback) { - initialize(); var bin = { 'source': options.deviceName || options.defaultDeviceName, @@ -1271,7 +1259,6 @@ vAPI.cloud = (function() { }; var pull = function(dataKey, callback) { - initialize(); var assembleChunks = function(bin) { if ( chrome.runtime.lastError ) { diff --git a/platform/chromium/vapi-common.js b/platform/chromium/vapi-common.js index 007313dea..36757a48b 100644 --- a/platform/chromium/vapi-common.js +++ b/platform/chromium/vapi-common.js @@ -36,6 +36,50 @@ vAPI.setTimeout = vAPI.setTimeout || self.setTimeout.bind(self); /******************************************************************************/ +vAPI.webextFlavor = (function() { + var ua = navigator.userAgent, + match, reEx; + var dispatch = function() { + window.dispatchEvent(new CustomEvent('webextFlavor')); + }; + + // Order of tests is important! + + // Asynchronous + if ( + self.browser instanceof Object && + typeof self.browser.runtime.getBrowserInfo === 'function' + ) { + self.browser.runtime.getBrowserInfo().then(function(info) { + vAPI.webextFlavor = + info.vendor + '-' + info.name + '-' + info.version; + dispatch(); + }); + match = /Firefox\/([\d.]+)/.exec(ua); + return match !== null ? 'Mozilla-Firefox-' + match[1] : ''; + } + + // Synchronous + /* Don't starve potential listeners: */ vAPI.setTimeout(dispatch, 97); + match = /OPR\/([\d.]+)/.exec(ua); + if ( match !== null ) { + reEx = /Chrom(?:e|ium)\/([\d.]+)/; + if ( reEx.test(ua) ) { match = reEx.exec(ua); } + return 'Opera-Chromium-' + match[1]; + } + match = /Chromium\/([\d.]+)/.exec(ua); + if ( match !== null ) { + return 'Chromium-Chromium-' + match[1]; + } + match = /Chrome\/([\d.]+)/.exec(ua); + if ( match !== null ) { + return 'Google-Chromium-' + match[1]; + } + return ''; +})(); + +/******************************************************************************/ + // http://www.w3.org/International/questions/qa-scripts#directions var setScriptDirection = function(language) { @@ -77,10 +121,7 @@ setScriptDirection(vAPI.i18n('@@ui_locale')); // `window.open('', '_self').close()`. vAPI.closePopup = function() { - if ( - self.browser instanceof Object && - typeof self.browser.runtime.getBrowserInfo === 'function' - ) { + if ( /^Mozilla-Firefox-/.test(vAPI.webextFlavor) ) { window.close(); return; } diff --git a/platform/webext/vapi-webrequest.js b/platform/webext/vapi-webrequest.js index 92eacc332..81607498b 100644 --- a/platform/webext/vapi-webrequest.js +++ b/platform/webext/vapi-webrequest.js @@ -43,20 +43,17 @@ vAPI.net.registerListeners = function() { // https://github.com/gorhill/uBlock/issues/2950 // Firefox 55 does not normalize URLs to ASCII, uBO must do this itself. // https://bugzilla.mozilla.org/show_bug.cgi?id=945240 - let mustPunycode = false; - (function() { - if ( - typeof browser === 'object' && - browser !== null && - browser.runtime instanceof Object && - typeof browser.runtime.getBrowserInfo === 'function' - ) { - browser.runtime.getBrowserInfo().then(info => { - mustPunycode = info.name === 'Firefox' && - /^5[0-6]\./.test(info.version); - }); - } - })(); + let evalMustPunycode = function() { + return /^Mozilla-Firefox-5[0-6]/.test(vAPI.webextFlavor); + }; + + let mustPunycode = evalMustPunycode(); + + // The real actual webextFlavor value may not be set in stone, so listen + // for possible future changes. + window.addEventListener('webextFlavor', function() { + mustPunycode = evalMustPunycode(); + }, { once: true }); let wrApi = browser.webRequest; diff --git a/src/js/assets.js b/src/js/assets.js index 7c0ae6fd7..d940139c9 100644 --- a/src/js/assets.js +++ b/src/js/assets.js @@ -912,8 +912,7 @@ var updateFirst = function() { typeof browser === 'object' && browser.runtime.getManifest(); noRemoteResources = - typeof vAPI.webextFlavor === 'string' && - vAPI.webextFlavor.startsWith('Mozilla-Firefox-') && + /^Mozilla-Firefox-/.test(vAPI.webextFlavor) && manifest instanceof Object && manifest.applications instanceof Object && manifest.applications.gecko instanceof Object && diff --git a/src/js/static-net-filtering.js b/src/js/static-net-filtering.js index 161728950..4a1fad2bb 100644 --- a/src/js/static-net-filtering.js +++ b/src/js/static-net-filtering.js @@ -1605,7 +1605,7 @@ FilterParser.prototype.translate = function() { this.dataStr = "connect-src https: http:"; // https://bugs.chromium.org/p/chromium/issues/detail?id=669086 // TODO: remove when most users are beyond Chromium v56 - if ( vAPI.chromiumVersion < 57 ) { + if ( /-Chromium-(?:4|5[0-6])/.test(vAPI.webextFlavor) ) { this.dataStr += '; frame-src *'; } return; diff --git a/src/js/traffic.js b/src/js/traffic.js index 13627e2d1..6ee787e67 100644 --- a/src/js/traffic.js +++ b/src/js/traffic.js @@ -1126,20 +1126,17 @@ var injectCSP = function(pageStore, details) { // https://github.com/gorhill/uMatrix/issues/967#issuecomment-373002011 // This can be removed once Firefox 60 ESR is released. -var cantMergeCSPHeaders = (function() { - if ( - self.browser instanceof Object && - typeof self.browser.runtime.getBrowserInfo === 'function' - ) { - self.browser.runtime.getBrowserInfo().then(function(info) { - cantMergeCSPHeaders = - info.vendor === 'Mozilla' && - info.name === 'Firefox' && - parseInt(info.version, 10) < 59; - }); - } - return false; -})(); +var evalCantMergeCSPHeaders = function() { + return /^Mozilla-Firefox-5[2-8]/.test(vAPI.webextFlavor); +}; + +var cantMergeCSPHeaders = evalCantMergeCSPHeaders(); + +// The real actual webextFlavor value may not be set in stone, so listen +// for possible future changes. +window.addEventListener('webextFlavor', function() { + cantMergeCSPHeaders = evalCantMergeCSPHeaders(); +}, { once: true }); /******************************************************************************/