Skip to content

Commit

Permalink
Add support to urlskip= media resources
Browse files Browse the repository at this point in the history
  • Loading branch information
gorhill committed Oct 12, 2024
1 parent 2c60bb3 commit ce9fc5d
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 26 deletions.
20 changes: 12 additions & 8 deletions src/js/benchmarks.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,11 +166,11 @@ export async function benchmarkStaticNetFiltering(options = {}) {
let allowCount = 0;
let redirectCount = 0;
let removeparamCount = 0;
let urlskipCount = 0;
let cspCount = 0;
let permissionsCount = 0;
let replaceCount = 0;
for ( let i = 0; i < requests.length; i++ ) {
const request = requests[i];
for ( const request of requests ) {
fctxt.setURL(request.url);
if ( fctxt.getIPAddress() === '' ) {
fctxt.setIPAddress('93.184.215.14\n2606:2800:21f:cb07:6820:80da:af6b:8b2c');
Expand All @@ -186,12 +186,15 @@ export async function benchmarkStaticNetFiltering(options = {}) {
if ( sfne.transformRequest(fctxt) ) {
redirectCount += 1;
}
if ( fctxt.redirectURL !== undefined && sfne.hasQuery(fctxt) ) {
if ( sfne.filterQuery(fctxt, 'removeparam') ) {
if ( sfne.hasQuery(fctxt) ) {
if ( sfne.filterQuery(fctxt) ) {
removeparamCount += 1;
}
}
if ( fctxt.type === 'main_frame' || fctxt.type === 'sub_frame' ) {
if ( sfne.urlSkip(fctxt) ) {
urlskipCount += 1;
}
if ( fctxt.isDocument() ) {
if ( sfne.matchAndFetchModifiers(fctxt, 'csp') ) {
cspCount += 1;
}
Expand All @@ -207,9 +210,9 @@ export async function benchmarkStaticNetFiltering(options = {}) {
if ( sfne.redirectRequest(redirectEngine, fctxt) ) {
redirectCount += 1;
}
}
if ( fctxt.type === 'main_frame' ) {
sfne.matchAndFetchModifiers(fctxt, 'urlskip');
if ( fctxt.isRootDocument() && sfne.urlSkip(fctxt) ) {
urlskipCount += 1;
}
}
}
const t1 = performance.now();
Expand All @@ -224,6 +227,7 @@ export async function benchmarkStaticNetFiltering(options = {}) {
`\tUnblocked: ${allowCount}`,
`\tredirect=: ${redirectCount}`,
`\tremoveparam=: ${removeparamCount}`,
`\turlskip=: ${urlskipCount}`,
`\tcsp=: ${cspCount}`,
`\tpermissions=: ${permissionsCount}`,
`\treplace=: ${replaceCount}`,
Expand Down
41 changes: 26 additions & 15 deletions src/js/pagestore.js
Original file line number Diff line number Diff line change
Expand Up @@ -942,6 +942,9 @@ const PageStore = class {
if ( staticNetFilteringEngine.hasQuery(fctxt) ) {
staticNetFilteringEngine.filterQuery(fctxt, directives);
}
if ( this.urlSkippableResources.has(fctxt.itype) ) {
staticNetFilteringEngine.urlSkip(fctxt, directives);
}
if ( directives.length === 0 ) { return; }
if ( logger.enabled !== true ) { return; }
fctxt.pushFilters(directives.map(a => a.logData()));
Expand Down Expand Up @@ -1132,22 +1135,30 @@ const PageStore = class {
response.blockedResources =
this.netFilteringCache.lookupAllBlocked(fctxt.getDocHostname());
}
};

PageStore.prototype.cacheableResults = new Set([
µb.FilteringContext.SUB_FRAME,
]);

PageStore.prototype.collapsibleResources = new Set([
µb.FilteringContext.IMAGE,
µb.FilteringContext.MEDIA,
µb.FilteringContext.OBJECT,
µb.FilteringContext.SUB_FRAME,
]);

// To mitigate memory churning
PageStore.junkyard = [];
PageStore.junkyardMax = 10;
cacheableResults = new Set([
µb.FilteringContext.SUB_FRAME
]);

collapsibleResources = new Set([
µb.FilteringContext.IMAGE,
µb.FilteringContext.MEDIA,
µb.FilteringContext.OBJECT,
µb.FilteringContext.SUB_FRAME,
]);

urlSkippableResources = new Set([
µb.FilteringContext.IMAGE,
µb.FilteringContext.MAIN_FRAME,
µb.FilteringContext.MEDIA,
µb.FilteringContext.OBJECT,
µb.FilteringContext.SUB_FRAME,
]);

// To mitigate memory churning
static junkyard = [];
static junkyardMax = 10;
};

/******************************************************************************/

Expand Down
4 changes: 2 additions & 2 deletions src/js/static-net-filtering.js
Original file line number Diff line number Diff line change
Expand Up @@ -5449,9 +5449,9 @@ StaticNetFilteringEngine.prototype.urlSkip = function(fctxt, out = []) {
return out;
};

function urlSkip(directive, urlin, steps) {
function urlSkip(directive, url, steps) {
try {
let urlout = urlin;
let urlout = url;
for ( const step of steps ) {
const urlin = urlout;
const c0 = step.charCodeAt(0);
Expand Down
3 changes: 2 additions & 1 deletion src/js/traffic.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,9 @@ const onBeforeRootFrameRequest = function(fctxt) {
if ( trusted === false && pageStore !== null ) {
if ( result !== 1 ) {
pageStore.redirectNonBlockedRequest(fctxt);
} else {
pageStore.skipMainDocument(fctxt);
}
pageStore.skipMainDocument(fctxt);
}

if ( logger.enabled ) {
Expand Down

0 comments on commit ce9fc5d

Please sign in to comment.