Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ATB parameter in MV3 #1585

Merged
merged 11 commits into from
Dec 7, 2022
14 changes: 14 additions & 0 deletions integration-test/background/atb.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ const pageWait = require('../helpers/pageWait')
// eslint-disable-next-line no-shadow
const fetch = (...args) => import('node-fetch').then(({ default: fetch }) => fetch(...args))

const manifestVersion = harness.getManifestVersion()

let browser
let bgPage
let requests
Expand Down Expand Up @@ -68,6 +70,10 @@ describe('install workflow', () => {
})

it('should get its ATB param from atb.js when there\'s no install success page', async () => {
if (manifestVersion === 3) {
kzar marked this conversation as resolved.
Show resolved Hide resolved
return
}

// try get ATB params
await bgPage.evaluate(() => globalThis.dbg.atb.updateATBValues())
await backgroundWait.forSetting(bgPage, 'extiSent')
Expand Down Expand Up @@ -97,6 +103,10 @@ describe('install workflow', () => {
expect(numExtiCalled).toEqual(1)
})
it('should get its ATB param from the success page when one is present', async () => {
if (manifestVersion === 3) {
return
}

// open a success page and wait for it to have finished loading
const successPage = await browser.newPage()
await pageWait.forGoto(successPage, 'https://duckduckgo.com/?natb=v123-4ab&cp=atbhc')
Expand Down Expand Up @@ -141,6 +151,10 @@ describe('install workflow', () => {
})

it('should retreive stored ATB value on reload', async () => {
if (manifestVersion === 3) {
kzar marked this conversation as resolved.
Show resolved Hide resolved
return
}

// set an ATB value from the past
const pastATBValue = 'v123-1'
await bgPage.evaluate((pagePastATBValue) => globalThis.dbg.settings.updateSetting('atb', pagePastATBValue), pastATBValue)
Expand Down
1 change: 1 addition & 0 deletions integration-test/config-mv3.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"background/test-fingerprint.js",
"background/storage.js",
"background/click-attribution.js",
"background/atb.js",
"content-scripts/gpc.js"
]
}
44 changes: 44 additions & 0 deletions shared/js/background/atb.es6.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,18 @@ const settings = require('./settings.es6')
const parseUserAgentString = require('../shared-utils/parse-user-agent-string.es6')
const load = require('./load.es6')
const browserWrapper = require('./wrapper.es6')
const { ATB_PARAM_RULE_ID } = require('./declarative-net-request')
const { ATB_PARAM_PRIORITY } = require('@duckduckgo/ddg2dnr/lib/rulePriorities')
const { generateDNRRule } = require('@duckduckgo/ddg2dnr/lib/utils')

const ATB_ERROR_COHORT = 'v1-1'
const ATB_FORMAT_RE = /(v\d+-\d(?:[a-z_]{2})?)$/

// list of accepted params in ATB url
const ACCEPTED_URL_PARAMS = ['natb', 'cp', 'npi']

const manifestVersion = browserWrapper.getManifestVersion()

const ATB = (() => {
// regex to match ddg urls to add atb params to.
// Matching subdomains, searches, and newsletter page
Expand All @@ -23,6 +28,7 @@ const ATB = (() => {
const ddgAtbURL = 'https://duckduckgo.com/atb.js?'

return {

shouldUpdateSetAtb (request) {
return matchPage.test(request.url)
},
Expand All @@ -40,6 +46,7 @@ const ATB = (() => {
if (!atbSetting) {
atbSetting = ATB_ERROR_COHORT
settings.updateSetting('atb', ATB_ERROR_COHORT)
ATB.setOrUpdateATBdnrRule(ATB_ERROR_COHORT)
errorParam = '&e=1'
}

Expand All @@ -52,8 +59,10 @@ const ATB = (() => {

if (res.data.updateVersion) {
settings.updateSetting('atb', res.data.updateVersion)
ATB.setOrUpdateATBdnrRule(res.data.updateVersion)
} else if (atbSetting === ATB_ERROR_COHORT) {
settings.updateSetting('atb', res.data.version)
ATB.setOrUpdateATBdnrRule(res.data.version)
}
})
},
Expand Down Expand Up @@ -92,6 +101,7 @@ const ATB = (() => {
const url = ddgAtbURL + randomValue + '&browser=' + parseUserAgentString().browser
return load.JSONfromExternalFile(url).then((res) => {
settings.updateSetting('atb', res.data.version)
ATB.setOrUpdateATBdnrRule(res.data.version)
}, () => {
console.log('couldn\'t reach atb.js for initial server call, trying again')
numTries += 1
Expand Down Expand Up @@ -164,6 +174,7 @@ const ATB = (() => {

if (atb) {
settings.updateSetting('atb', atb)
ATB.setOrUpdateATBdnrRule(atb)
}

ATB.finalizeATB(params)
Expand Down Expand Up @@ -199,6 +210,39 @@ const ATB = (() => {
!domain.match(regExpSoftwarePage)
},

/**
* Creates a DNR rule for ATB parameters
* @param {string} atb
*/
setOrUpdateATBdnrRule: (atb) => {
if (!(manifestVersion === 3 && atb)) {
jdorweiler marked this conversation as resolved.
Show resolved Hide resolved
return
}

const atbRule = generateDNRRule({
id: ATB_PARAM_RULE_ID,
priority: ATB_PARAM_PRIORITY,
actionType: 'redirect',
redirect: {
transform: {
queryTransform: {
addOrReplaceParams: [{ key: 'atb', value: atb }]
}
}
},
resourceTypes: ['main_frame'],
requestDomains: ['duckduckgo.com'],
regexFilter: regExpAboutPage.source
})

if (atbRule?.id === ATB_PARAM_RULE_ID) {
chrome.declarativeNetRequest.updateDynamicRules({
removeRuleIds: [atbRule.id],
addRules: [atbRule]
})
}
},

async getSurveyURL () {
let url = ddgAtbURL + Math.ceil(Math.random() * 1e7) + '&uninstall=1&action=survey'
const atb = settings.getSetting('atb')
Expand Down
7 changes: 7 additions & 0 deletions shared/js/background/declarative-net-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const ruleIdRangeByConfigName = {
// only require one declarativeNetRequest rule, so hardcode the rule IDs here.
export const USER_ALLOWLIST_RULE_ID = 20001
export const SERVICE_WORKER_INITIATED_ALLOWING_RULE_ID = 20002
export const ATB_PARAM_RULE_ID = 20003

/**
* A dummy etag rule is saved with the declarativeNetRequest rules generated for
Expand Down Expand Up @@ -341,6 +342,12 @@ export async function getMatchDetails (ruleId) {
}
}

if (ruleId === ATB_PARAM_RULE_ID) {
return {
type: 'atbParam'
}
}

for (const [configName, [ruleIdStart, ruleIdEnd]]
of Object.entries(ruleIdRangeByConfigName)) {
if (ruleId >= ruleIdStart && ruleId <= ruleIdEnd) {
Expand Down
4 changes: 4 additions & 0 deletions shared/js/background/events.es6.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,14 @@ async function onInstalled (details) {
}
await ATB.updateATBValues()
await ATB.openPostInstallPage()

if (browserName === 'chrome') {
experiment.setActiveExperiment()
}
} else if (details.reason.match(/update/) && browserName === 'chrome') {
if (manifestVersion === 3) {
ATB.setOrUpdateATBdnrRule(settings.getSetting('atb'))
}
experiment.setActiveExperiment()
}

Expand Down