[mv3] Various fixes and code review

Fixed trusted sites not being excluded from declarative
scripting.

Assign "uBOL_"-prefixed name to anonymous scripting functions
so that they can be easily found in performance profiler results
in dev tools.

Imrpove spread of chunks of filters across declarative scripting
files.
This commit is contained in:
Raymond Hill 2022-09-30 14:55:36 -04:00
parent 2cace64ed4
commit 2a40e67577
No known key found for this signature in database
GPG Key ID: 25E1490B761470C2
10 changed files with 40 additions and 29 deletions

View File

@ -51,7 +51,7 @@ function getScriptingDetails() {
/******************************************************************************/ /******************************************************************************/
const toRegisterable = (fname, hostnames) => { const toRegisterable = (fname, hostnames, trustedSites) => {
const directive = { const directive = {
id: fname, id: fname,
allFrames: true, allFrames: true,
@ -62,6 +62,12 @@ const toRegisterable = (fname, hostnames) => {
} else { } else {
directive.matches = [ '<all_urls>' ]; directive.matches = [ '<all_urls>' ];
} }
if (
directive.matches.length === 1 &&
directive.matches[0] === '<all_urls>'
) {
directive.excludeMatches = ut.matchesFromHostnames(trustedSites);
}
directive.js = [ `/rulesets/js/${fname.slice(0,2)}/${fname.slice(2)}.js` ]; directive.js = [ `/rulesets/js/${fname.slice(0,2)}/${fname.slice(2)}.js` ];
if ( (ut.fidFromFileName(fname) & RUN_AT_END_BIT) !== 0 ) { if ( (ut.fidFromFileName(fname) & RUN_AT_END_BIT) !== 0 ) {
directive.runAt = 'document_end'; directive.runAt = 'document_end';
@ -92,10 +98,19 @@ const arrayEq = (a, b) => {
return true; return true;
}; };
const shouldUpdate = (registered, candidateHostnames) => { const shouldUpdate = (registered, afterHostnames, afterExcludeHostnames) => {
const registeredHostnames = registered.matches && if ( afterHostnames.length === 1 && afterHostnames[0] === '*' ) {
ut.hostnamesFromMatches(registered.matches); const beforeExcludeHostnames = registered.excludeMatches &&
return arrayEq(registeredHostnames, candidateHostnames) === false; ut.hostnamesFromMatches(registered.excludeMatches) ||
[];
if ( arrayEq(beforeExcludeHostnames, afterExcludeHostnames) === false ) {
return true;
}
}
const beforeHostnames = registered.matches &&
ut.hostnamesFromMatches(registered.matches) ||
[];
return arrayEq(beforeHostnames, afterHostnames) === false;
}; };
const isTrustedHostname = (trustedSites, hn) => { const isTrustedHostname = (trustedSites, hn) => {
@ -152,6 +167,7 @@ function registerSomeInjectables(args) {
} = args; } = args;
const toRegisterMap = new Map(); const toRegisterMap = new Map();
const trustedSitesSet = new Set(trustedSites);
const checkMatches = (hostnamesToFidsMap, hn) => { const checkMatches = (hostnamesToFidsMap, hn) => {
let fids = hostnamesToFidsMap.get(hn); let fids = hostnamesToFidsMap.get(hn);
@ -177,7 +193,7 @@ function registerSomeInjectables(args) {
const hostnamesToFidsMap = scriptingDetails.get(rulesetId); const hostnamesToFidsMap = scriptingDetails.get(rulesetId);
if ( hostnamesToFidsMap === undefined ) { continue; } if ( hostnamesToFidsMap === undefined ) { continue; }
for ( let hn of hostnamesSet ) { for ( let hn of hostnamesSet ) {
if ( isTrustedHostname(trustedSites, hn) ) { continue; } if ( isTrustedHostname(trustedSitesSet, hn) ) { continue; }
while ( hn ) { while ( hn ) {
checkMatches(hostnamesToFidsMap, hn); checkMatches(hostnamesToFidsMap, hn);
hn = ut.toBroaderHostname(hn); hn = ut.toBroaderHostname(hn);
@ -196,12 +212,13 @@ function registerAllInjectables(args) {
} = args; } = args;
const toRegisterMap = new Map(); const toRegisterMap = new Map();
const trustedSitesSet = new Set(trustedSites);
for ( const rulesetId of rulesetIds ) { for ( const rulesetId of rulesetIds ) {
const hostnamesToFidsMap = scriptingDetails.get(rulesetId); const hostnamesToFidsMap = scriptingDetails.get(rulesetId);
if ( hostnamesToFidsMap === undefined ) { continue; } if ( hostnamesToFidsMap === undefined ) { continue; }
for ( let [ hn, fids ] of hostnamesToFidsMap ) { for ( let [ hn, fids ] of hostnamesToFidsMap ) {
if ( isTrustedHostname(trustedSites, hn) ) { continue; } if ( isTrustedHostname(trustedSitesSet, hn) ) { continue; }
if ( typeof fids === 'number' ) { fids = [ fids ]; } if ( typeof fids === 'number' ) { fids = [ fids ]; }
for ( const fid of fids ) { for ( const fid of fids ) {
const fname = ut.fnameFromFileId(fid); const fname = ut.fnameFromFileId(fid);
@ -244,7 +261,6 @@ async function registerInjectables(origins) {
browser.scripting.getRegisteredContentScripts(), browser.scripting.getRegisteredContentScripts(),
]).then(results => { ]).then(results => {
results[0] = new Set(ut.hostnamesFromMatches(results[0].origins)); results[0] = new Set(ut.hostnamesFromMatches(results[0].origins));
results[1] = new Set(results[1]);
return results; return results;
}); });
@ -267,11 +283,11 @@ async function registerInjectables(origins) {
const toUpdate = []; const toUpdate = [];
for ( const [ fname, hostnames ] of toRegisterMap ) { for ( const [ fname, hostnames ] of toRegisterMap ) {
if ( before.has(fname) === false ) { if ( before.has(fname) === false ) {
toAdd.push(toRegisterable(fname, hostnames)); toAdd.push(toRegisterable(fname, hostnames, trustedSites));
continue; continue;
} }
if ( shouldUpdate(before.get(fname), hostnames) ) { if ( shouldUpdate(before.get(fname), hostnames, trustedSites) ) {
toUpdate.push(toRegisterable(fname, hostnames)); toUpdate.push(toRegisterable(fname, hostnames, trustedSites));
} }
} }

View File

@ -390,8 +390,7 @@ const pathFromFileName = fname => `${scriptletDir}/${fname.slice(0,2)}/${fname.s
/******************************************************************************/ /******************************************************************************/
const COSMETIC_FILES_PER_RULESET = 12; const MAX_COSMETIC_FILTERS_PER_FILE = 128;
const PROCEDURAL_FILES_PER_RULESET = 4;
// This merges selectors which are used by the same hostnames // This merges selectors which are used by the same hostnames
@ -494,8 +493,6 @@ async function processCosmeticFilters(assetDetails, mapin) {
const contentArray = groupCosmeticBySelectors( const contentArray = groupCosmeticBySelectors(
groupCosmeticByHostnames(mapin) groupCosmeticByHostnames(mapin)
); );
const contentPerFile =
Math.ceil(contentArray.length / COSMETIC_FILES_PER_RULESET);
// We do not want more than n CSS files per subscription, so we will // We do not want more than n CSS files per subscription, so we will
// group multiple unrelated selectors in the same file, and distinct // group multiple unrelated selectors in the same file, and distinct
@ -508,8 +505,8 @@ async function processCosmeticFilters(assetDetails, mapin) {
const originalScriptletMap = await loadAllSourceScriptlets(); const originalScriptletMap = await loadAllSourceScriptlets();
const generatedFiles = []; const generatedFiles = [];
for ( let i = 0; i < contentArray.length; i += contentPerFile ) { for ( let i = 0; i < contentArray.length; i += MAX_COSMETIC_FILTERS_PER_FILE ) {
const slice = contentArray.slice(i, i + contentPerFile); const slice = contentArray.slice(i, i + MAX_COSMETIC_FILTERS_PER_FILE);
const argsMap = slice.map(entry => [ const argsMap = slice.map(entry => [
entry[0], entry[0],
{ {
@ -562,8 +559,6 @@ async function processProceduralCosmeticFilters(assetDetails, mapin) {
const contentArray = groupCosmeticBySelectors( const contentArray = groupCosmeticBySelectors(
groupCosmeticByHostnames(mapin) groupCosmeticByHostnames(mapin)
); );
const contentPerFile =
Math.ceil(contentArray.length / PROCEDURAL_FILES_PER_RULESET);
// We do not want more than n CSS files per subscription, so we will // We do not want more than n CSS files per subscription, so we will
// group multiple unrelated selectors in the same file, and distinct // group multiple unrelated selectors in the same file, and distinct
@ -576,8 +571,8 @@ async function processProceduralCosmeticFilters(assetDetails, mapin) {
const originalScriptletMap = await loadAllSourceScriptlets(); const originalScriptletMap = await loadAllSourceScriptlets();
const generatedFiles = []; const generatedFiles = [];
for ( let i = 0; i < contentArray.length; i += contentPerFile ) { for ( let i = 0; i < contentArray.length; i += MAX_COSMETIC_FILTERS_PER_FILE ) {
const slice = contentArray.slice(i, i + contentPerFile); const slice = contentArray.slice(i, i + MAX_COSMETIC_FILTERS_PER_FILE);
const argsMap = slice.map(entry => [ const argsMap = slice.map(entry => [
entry[0], entry[0],
{ {

View File

@ -37,7 +37,7 @@
// Important! // Important!
// Isolate from global scope // Isolate from global scope
(function() { (function uBOL_abortCurrentScript() {
/******************************************************************************/ /******************************************************************************/

View File

@ -35,7 +35,7 @@
// Important! // Important!
// Isolate from global scope // Isolate from global scope
(function() { (function uBOL_abortOnPropertyRead() {
/******************************************************************************/ /******************************************************************************/

View File

@ -35,7 +35,7 @@
// Important! // Important!
// Isolate from global scope // Isolate from global scope
(function() { (function uBOL_abortOnPropertyWrite() {
/******************************************************************************/ /******************************************************************************/

View File

@ -31,7 +31,7 @@
// Important! // Important!
// Isolate from global scope // Isolate from global scope
(function() { (function uBOL_cssSpecificProcedural() {
/******************************************************************************/ /******************************************************************************/

View File

@ -31,7 +31,7 @@
// Important! // Important!
// Isolate from global scope // Isolate from global scope
(function() { (function uBOL_cssSpecific() {
/******************************************************************************/ /******************************************************************************/

View File

@ -34,7 +34,7 @@
// Important! // Important!
// Isolate from global scope // Isolate from global scope
(function() { (function uBOL_jsonPrune() {
/******************************************************************************/ /******************************************************************************/

View File

@ -35,7 +35,7 @@
// Important! // Important!
// Isolate from global scope // Isolate from global scope
(function() { (function uBOL_noSetimeoutIf() {
/******************************************************************************/ /******************************************************************************/

View File

@ -35,7 +35,7 @@
// Important! // Important!
// Isolate from global scope // Isolate from global scope
(function() { (function uBOL_setConstant() {
/******************************************************************************/ /******************************************************************************/