Skip to content

Commit

Permalink
TDS Override via contentBlocking subfeature
Browse files Browse the repository at this point in the history
  • Loading branch information
sammacbeth committed Jan 23, 2025
1 parent fe78142 commit 8cc4a81
Showing 1 changed file with 43 additions and 1 deletion.
44 changes: 43 additions & 1 deletion shared/js/background/components/tds.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import constants from '../../../data/constants';
* @typedef {import('./remote-config.js').default} RemoteConfig
*/

const TDS_OVERRIDE_SETTINGS_KEY = 'tdsOverride';
const CONTENT_BLOCKING = 'contentBlocking';

export default class TDSStorage {
/**
* @param {{
Expand All @@ -14,6 +17,7 @@ export default class TDSStorage {
* }} opts
*/
constructor({ settings, remoteConfig }) {
this.settings = settings;
this.remoteConfig = remoteConfig;
/** @deprecated config is an alias of remoteConfig */
this.config = this.remoteConfig;
Expand All @@ -28,14 +32,52 @@ export default class TDSStorage {
this.tds = new ResourceLoader(
{
name: 'tds',
remoteUrl: constants.tdsLists[1].url,
remoteUrl: this.getTDSUrl.bind(this),
updateIntervalMinutes: 15,
},
{ settings },
);
this.remoteConfig.onUpdate(this.checkShouldOverrideTDS.bind(this));
}

ready() {
return Promise.all([this.tds.ready, this.surrogates.ready, this.remoteConfig.ready]);
}

async getTDSUrl() {
await this.config.ready;
const overridePath = this.settings.getSetting(TDS_OVERRIDE_SETTINGS_KEY);
if (overridePath) {
return `https://staticcdn.duckduckgo.com/trackerblocking/${overridePath}`;
}
return constants.tdsLists[1].url;
}

checkShouldOverrideTDS() {
const contentBlockingSubFeatures = this.config.config?.features[CONTENT_BLOCKING].features || {};
const enabledBlocklistOverrides = Object.keys(contentBlockingSubFeatures).filter(
(k) => k.startsWith('TDS') && this.config.isSubFeatureEnabled(CONTENT_BLOCKING, k),
);
if (enabledBlocklistOverrides.length > 0) {
const subFeatureName = enabledBlocklistOverrides[0]
const overrideSubFeature = contentBlockingSubFeatures[subFeatureName]
// If this is enabled via an experiment, the override URL is defined as `${cohortName}Url`, otherwise, for a normal rollout use `nextUrl`.
const settingsKey = `${this.remoteConfig.getCohortName(CONTENT_BLOCKING, subFeatureName) || 'next'}Url`
const overridePath = overrideSubFeature.settings && overrideSubFeature.settings[settingsKey]
if (!overridePath) {
console.warn(`Couldn't find TDS override path in subfeature settings.`)
return
}
if (overridePath !== this.settings.getSetting(TDS_OVERRIDE_SETTINGS_KEY)) {
console.log('TDS URL override changed to ', overridePath);
this.settings.updateSetting(TDS_OVERRIDE_SETTINGS_KEY, overridePath);
this.tds.checkForUpdates(true);
}
this.remoteConfig.markExperimentEnrolled(CONTENT_BLOCKING, subFeatureName)
} else if (this.settings.getSetting(TDS_OVERRIDE_SETTINGS_KEY)) {
// User removed from experiment/rollout, reset TDS override and fetch default list
this.settings.removeSetting(TDS_OVERRIDE_SETTINGS_KEY)
this.tds.checkForUpdates(true);
}
}
}

0 comments on commit 8cc4a81

Please sign in to comment.