1
0
mirror of https://github.com/gorhill/uBlock.git synced 2024-07-05 11:37:01 +02:00

[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 = {
id: fname,
allFrames: true,
@ -62,6 +62,12 @@ const toRegisterable = (fname, hostnames) => {
} else {
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` ];
if ( (ut.fidFromFileName(fname) & RUN_AT_END_BIT) !== 0 ) {
directive.runAt = 'document_end';
@ -92,10 +98,19 @@ const arrayEq = (a, b) => {
return true;
};
const shouldUpdate = (registered, candidateHostnames) => {
const registeredHostnames = registered.matches &&
ut.hostnamesFromMatches(registered.matches);
return arrayEq(registeredHostnames, candidateHostnames) === false;
const shouldUpdate = (registered, afterHostnames, afterExcludeHostnames) => {
if ( afterHostnames.length === 1 && afterHostnames[0] === '*' ) {
const beforeExcludeHostnames = registered.excludeMatches &&
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) => {
@ -152,6 +167,7 @@ function registerSomeInjectables(args) {
} = args;
const toRegisterMap = new Map();
const trustedSitesSet = new Set(trustedSites);
const checkMatches = (hostnamesToFidsMap, hn) => {
let fids = hostnamesToFidsMap.get(hn);
@ -177,7 +193,7 @@ function registerSomeInjectables(args) {
const hostnamesToFidsMap = scriptingDetails.get(rulesetId);
if ( hostnamesToFidsMap === undefined ) { continue; }
for ( let hn of hostnamesSet ) {
if ( isTrustedHostname(trustedSites, hn) ) { continue; }
if ( isTrustedHostname(trustedSitesSet, hn) ) { continue; }
while ( hn ) {
checkMatches(hostnamesToFidsMap, hn);
hn = ut.toBroaderHostname(hn);
@ -196,12 +212,13 @@ function registerAllInjectables(args) {
} = args;
const toRegisterMap = new Map();
const trustedSitesSet = new Set(trustedSites);
for ( const rulesetId of rulesetIds ) {
const hostnamesToFidsMap = scriptingDetails.get(rulesetId);
if ( hostnamesToFidsMap === undefined ) { continue; }
for ( let [ hn, fids ] of hostnamesToFidsMap ) {
if ( isTrustedHostname(trustedSites, hn) ) { continue; }
if ( isTrustedHostname(trustedSitesSet, hn) ) { continue; }
if ( typeof fids === 'number' ) { fids = [ fids ]; }
for ( const fid of fids ) {
const fname = ut.fnameFromFileId(fid);
@ -244,7 +261,6 @@ async function registerInjectables(origins) {
browser.scripting.getRegisteredContentScripts(),
]).then(results => {
results[0] = new Set(ut.hostnamesFromMatches(results[0].origins));
results[1] = new Set(results[1]);
return results;
});
@ -267,11 +283,11 @@ async function registerInjectables(origins) {
const toUpdate = [];
for ( const [ fname, hostnames ] of toRegisterMap ) {
if ( before.has(fname) === false ) {
toAdd.push(toRegisterable(fname, hostnames));
toAdd.push(toRegisterable(fname, hostnames, trustedSites));
continue;
}
if ( shouldUpdate(before.get(fname), hostnames) ) {
toUpdate.push(toRegisterable(fname, hostnames));
if ( shouldUpdate(before.get(fname), hostnames, trustedSites) ) {
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 PROCEDURAL_FILES_PER_RULESET = 4;
const MAX_COSMETIC_FILTERS_PER_FILE = 128;
// This merges selectors which are used by the same hostnames
@ -494,8 +493,6 @@ async function processCosmeticFilters(assetDetails, mapin) {
const contentArray = groupCosmeticBySelectors(
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
// group multiple unrelated selectors in the same file, and distinct
@ -508,8 +505,8 @@ async function processCosmeticFilters(assetDetails, mapin) {
const originalScriptletMap = await loadAllSourceScriptlets();
const generatedFiles = [];
for ( let i = 0; i < contentArray.length; i += contentPerFile ) {
const slice = contentArray.slice(i, i + contentPerFile);
for ( let i = 0; i < contentArray.length; i += MAX_COSMETIC_FILTERS_PER_FILE ) {
const slice = contentArray.slice(i, i + MAX_COSMETIC_FILTERS_PER_FILE);
const argsMap = slice.map(entry => [
entry[0],
{
@ -562,8 +559,6 @@ async function processProceduralCosmeticFilters(assetDetails, mapin) {
const contentArray = groupCosmeticBySelectors(
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
// group multiple unrelated selectors in the same file, and distinct
@ -576,8 +571,8 @@ async function processProceduralCosmeticFilters(assetDetails, mapin) {
const originalScriptletMap = await loadAllSourceScriptlets();
const generatedFiles = [];
for ( let i = 0; i < contentArray.length; i += contentPerFile ) {
const slice = contentArray.slice(i, i + contentPerFile);
for ( let i = 0; i < contentArray.length; i += MAX_COSMETIC_FILTERS_PER_FILE ) {
const slice = contentArray.slice(i, i + MAX_COSMETIC_FILTERS_PER_FILE);
const argsMap = slice.map(entry => [
entry[0],
{

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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