Skip to content

Commit

Permalink
Adblocker: Do not report requests from previous page
Browse files Browse the repository at this point in the history
  • Loading branch information
chrmod committed Oct 12, 2023
1 parent 9b3b06c commit fdadc27
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 9 deletions.
6 changes: 4 additions & 2 deletions modules/adblocker/sources/adblocker.es
Original file line number Diff line number Diff line change
Expand Up @@ -290,13 +290,15 @@ export default class Adblocker {
* engine decides.
*/
onBeforeRequest(context, response) {
if (context.frameId === 0 && context.isMainFrame) {
this.stats.addNewPage(context);
}

if (this.shouldProcessRequest(context, response) === false) {
return false;
}

if (context.isMainFrame) {
this.stats.addNewPage(context);

// HTML filtering is a feature from the adblocker which is able to
// intercept streaming responses from main documents before they are
// parsed by the browser. This means that we have a chance to remove
Expand Down
38 changes: 31 additions & 7 deletions modules/adblocker/sources/statistics.es
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import { nextTick } from '../core/decorators';
import TrackerCounter from '../core/helpers/tracker-counter';
import events from '../core/events';
import pacemaker from '../core/services/pacemaker';
import { parse } from '../core/url';
import { browser } from '../platform/globals';

const FIRSTPARTY = 'First party';

Expand All @@ -20,6 +22,7 @@ class PageStats {
this.tabUrl = context.tabUrl;
this.hostGD = context.tabUrlParts.generalDomain;
this.blockedRequests = [];
this.createdAt = Date.now();

// Lazily computed once a report is requested
this._counter = new TrackerCounter();
Expand Down Expand Up @@ -138,30 +141,51 @@ export default class AdbStats {
}

init() {
browser.webNavigation.onBeforeNavigate.addListener(this.onBeforeNavigate);
this.clearInterval = pacemaker.everyFewMinutes(() => { this.clearStats(); });
}

unload() {
browser.webNavigation.onBeforeNavigate.removeListener(this.onBeforeNavigate);
pacemaker.clearTimeout(this.clearInterval);
}

onBeforeNavigate = (event) => {
if (event.frameId !== 0 || event.frameType === 'outermost_frame') {
return;
}
const urlParts = parse(event.url);
this.addNewPage({
tabId: event.tabId,
tabUrl: event.url,
tabUrlParts: urlParts,
});
};

addBlockedUrl(context) {
// Check if we already have a context for this tab
let page = this.tabs.get(context.tabId);
if (page === undefined) {
page = this.addNewPage(context);
}

// If it's a new url in an existing tab
if (context.tabUrl !== page.tabUrl) {
if (!page) {
page = this.addNewPage(context);
}

page.addBlockedUrl(context);
// ignore requests that may related to previous page
if (context.tabUrl === page.tabUrl) {
page.addBlockedUrl(context);
}
}

addNewPage(context) {
const existingPage = this.tabs.get(context.tabId);

if (
existingPage
&& existingPage.tabUrl === context.tabUrl
&& existingPage.createdAt + 200 > Date.now()
) {
return existingPage;
}

if (existingPage !== undefined) {
// Emit summary of stats from previous page
nextTick(() => {
Expand Down

0 comments on commit fdadc27

Please sign in to comment.