Skip to content

Commit

Permalink
Enable reporting of MV2 to MV3 extension. (#1371)
Browse files Browse the repository at this point in the history
* Enable reporting of MV2 to MV3 extension.

Reuse onBeforeRequest reporting flow for MV3, this means we need to retain feature parity.

It was deemed that the reporting APIs provided by MV3 were not suitable for our needs due to the cadence they come in.

* Add tracker reporting verification
  • Loading branch information
jonathanKingston committed Sep 1, 2022
1 parent e442e7a commit 3bf36d5
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 13 deletions.
34 changes: 34 additions & 0 deletions integration-test/background/request-blocking.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ describe('Test request blocking', () => {
pageRequests,
({ url }) => url.hostname === 'bad.third-party.site'
)
const manifestVersion = await bgPage.evaluate(() => {
return globalThis.dbg.browserWrapper.getManifestVersion()
})

// Initiate the test requests and wait until they have all completed.
// Note:
Expand Down Expand Up @@ -72,6 +75,37 @@ describe('Test request blocking', () => {
expect(status).withContext(description).not.toEqual('loaded')
}

// Test the tracker reporting matches the expected outcomes.
const trackers = await bgPage.evaluate(async () => {
const currentTab = await globalThis.dbg.utils.getCurrentTab()
return globalThis.dbg.tabManager.get({ tabId: currentTab.id }).trackers
})
// TODO fix manifest v3 blocking service workers (https://app.asana.com/0/1200940319964997/1202895557146471/f)
// The count is higher for MV2 as we permit a load that loads more requests.
const count = manifestVersion === 2 ? 20 : 19

// Test the tabs tracker objects match the expected snapshot.
const trackerSnapshot = {
'Test Site for Tracker Blocking': {
displayName: 'Bad Third Party Site',
prevalence: 0.1,
urls: {
'bad.third-party.site': {
block: {
action: 'block',
reason: 'default block',
categories: [],
isBlocked: true,
isSameEntity: false,
isSameBaseDomain: false
}
}
},
count
}
}
expect(trackers).toEqual(trackerSnapshot)

await page.close()
})
})
File renamed without changes.
1 change: 1 addition & 0 deletions shared/js/background/debug.es6.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ self.dbg = {
atb,
https,
tds,
browserWrapper,
utils
}

Expand Down
18 changes: 10 additions & 8 deletions shared/js/background/events.es6.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,19 +167,21 @@ if (browserName === 'chrome') {
* REQUESTS
*/

const redirect = require('./redirect.es6')
const beforeRequest = require('./before-request.es6')
const tabManager = require('./tab-manager.es6')
const https = require('./https.es6')

let additionalOptions = []
if (manifestVersion === 2) {
browser.webRequest.onBeforeRequest.addListener(
redirect.handleRequest,
{
urls: ['<all_urls>']
},
['blocking']
)
additionalOptions = ['blocking']
}
browser.webRequest.onBeforeRequest.addListener(
beforeRequest.handleRequest,
{
urls: ['<all_urls>']
},
additionalOptions
)

if (manifestVersion === 2) {
const extraInfoSpec = ['blocking', 'responseHeaders']
Expand Down
8 changes: 4 additions & 4 deletions unit-test/background/click-to-load-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const chromeWrapper = require('../../shared/js/background/wrapper.es6.js')
const tds = require('../../shared/js/background/trackers.es6')
const tdsStorage = require('../../shared/js/background/storage/tds.es6')
const tdsStorageStub = require('./../helpers/tds.es6')
const redirect = require('../../shared/js/background/redirect.es6')
const beforeRequest = require('../../shared/js/background/before-request.es6')
const Tab = require('../../shared/js/background/classes/tab.es6')
const tabManager = require('../../shared/js/background/tab-manager.es6')
const settings = require('../../shared/js/background/settings.es6')
Expand Down Expand Up @@ -57,7 +57,7 @@ describe('Tracker Utilities', () => {
for (const tracker of socialTrackers) {
requestData.url = tracker
settings.ready().then(() => {
expect(redirect.handleRequest(requestData)).withContext(`URL: ${tracker}`).toEqual({ cancel: true })
expect(beforeRequest.handleRequest(requestData)).withContext(`URL: ${tracker}`).toEqual({ cancel: true })
})
}
})
Expand All @@ -73,7 +73,7 @@ describe('Tracker Utilities', () => {
for (const url of sdkURLs) {
requestData.url = url
settings.ready().then(() => {
expect(redirect.handleRequest(requestData).redirectUrl).withContext(`URL: ${url}`).toBeDefined()
expect(beforeRequest.handleRequest(requestData).redirectUrl).withContext(`URL: ${url}`).toBeDefined()
})
}
})
Expand All @@ -86,7 +86,7 @@ describe('Tracker Utilities', () => {
for (const url of socialTrackers) {
requestData.url = url
settings.ready().then(() => {
expect(redirect.handleRequest(requestData)).toEqual(undefined)
expect(beforeRequest.handleRequest(requestData)).toEqual(undefined)
})
}
// Reset to empty for other tests
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const tds = require('../../../shared/js/background/trackers.es6')
const tdsStorageStub = require('../../helpers/tds.es6')
const tdsStorage = require('../../../shared/js/background/storage/tds.es6')

const { handleRequest } = require('../../../shared/js/background/redirect.es6')
const { handleRequest } = require('../../../shared/js/background/before-request.es6')
const tabManager = require('../../../shared/js/background/tab-manager.es6')
const browserWrapper = require('../../../shared/js/background/wrapper.es6')
const getArgumentsObject = require('../../../shared/js/background/helpers/arguments-object')
Expand Down

0 comments on commit 3bf36d5

Please sign in to comment.