Skip to content

Commit b4966af

Browse files
committed
Bug 1859608 - Change the user's DoH region when that changes r=necko-reviewers,sunil,kershaw
The DoHConfig now listens for region change notification and saves the new region in a pref. On the next initialization of the DoH config we will use the saved pref to update the doh region even if the pref was already set. This should also work for testing (manually setting the value of the pref) as long as no region changes happen. Differential Revision: https://phabricator.services.mozilla.com/D195023
1 parent e475b72 commit b4966af

File tree

3 files changed

+81
-0
lines changed

3 files changed

+81
-0
lines changed

browser/components/doh/DoHConfig.sys.mjs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,18 @@ export const DoHConfigController = {
166166
// until the region is available.
167167
async loadRegion() {
168168
await new Promise(resolve => {
169+
// If the region has changed since it was last set, update the pref.
170+
let homeRegionOverride = lazy.Preferences.get(
171+
`${kGlobalPrefBranch}.home-region-override`
172+
);
173+
if (homeRegionOverride) {
174+
lazy.Preferences.set(
175+
`${kGlobalPrefBranch}.home-region`,
176+
homeRegionOverride
177+
);
178+
lazy.Preferences.reset(`${kGlobalPrefBranch}.home-region-override`);
179+
}
180+
169181
let homeRegion = lazy.Preferences.get(`${kGlobalPrefBranch}.home-region`);
170182
if (homeRegion) {
171183
kRegionPrefBranch = `${kGlobalPrefBranch}.${homeRegion.toLowerCase()}`;
@@ -201,6 +213,7 @@ export const DoHConfigController = {
201213
await this.loadRegion();
202214

203215
Services.prefs.addObserver(`${kGlobalPrefBranch}.`, this, true);
216+
Services.obs.addObserver(this, lazy.Region.REGION_TOPIC);
204217

205218
gProvidersCollection.on("sync", this.updateFromRemoteSettings);
206219
gConfigCollection.on("sync", this.updateFromRemoteSettings);
@@ -213,6 +226,7 @@ export const DoHConfigController = {
213226
await this.initComplete;
214227

215228
Services.prefs.removeObserver(`${kGlobalPrefBranch}`, this);
229+
Services.obs.removeObserver(this, lazy.Region.REGION_TOPIC);
216230

217231
gProvidersCollection.off("sync", this.updateFromRemoteSettings);
218232
gConfigCollection.off("sync", this.updateFromRemoteSettings);
@@ -240,6 +254,12 @@ export const DoHConfigController = {
240254
}
241255
this.notifyNewConfig();
242256
break;
257+
case lazy.Region.REGION_TOPIC:
258+
lazy.Preferences.set(
259+
`${kGlobalPrefBranch}.home-region-override`,
260+
lazy.Region.home
261+
);
262+
break;
243263
}
244264
},
245265

browser/components/doh/test/browser/browser.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,5 @@ skip-if = ["os == 'win' && bits == 32"] # Bug 1713464
2929
["browser_trrSelection_disable.js"]
3030

3131
["browser_userInterference.js"]
32+
33+
["browser_doh_region.js"]
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/* Any copyright is dedicated to the Public Domain.
2+
* http://creativecommons.org/publicdomain/zero/1.0/
3+
*/
4+
5+
"use strict";
6+
7+
add_task(setup);
8+
add_task(setupRegion); // Sets region to DE
9+
10+
add_task(async function testPrefFirstRollout() {
11+
registerCleanupFunction(setupRegion); // Reset region
12+
13+
let defaults = Services.prefs.getDefaultBranch("");
14+
15+
setPassingHeuristics();
16+
17+
is(
18+
DoHConfigController.currentConfig.enabled,
19+
false,
20+
"Rollout should not be enabled"
21+
);
22+
23+
let configFlushedPromise = DoHTestUtils.waitForConfigFlush();
24+
defaults.setBoolPref(`${kRegionalPrefNamespace}.enabled`, true);
25+
await configFlushedPromise;
26+
27+
is(
28+
DoHConfigController.currentConfig.enabled,
29+
true,
30+
"Rollout should be enabled"
31+
);
32+
await ensureTRRMode(2);
33+
34+
is(
35+
Preferences.get("doh-rollout.home-region"),
36+
"DE",
37+
"Initial region should be DE"
38+
);
39+
Region._setHomeRegion("UK");
40+
await ensureTRRMode(2); // Mode shouldn't change.
41+
42+
is(Preferences.get("doh-rollout.home-region-override"), "UK");
43+
44+
await DoHController._uninit();
45+
await DoHConfigController._uninit();
46+
47+
// Check after controller gets reinitialized (or restart)
48+
// that the region gets set to UK
49+
await DoHConfigController.init();
50+
await DoHController.init();
51+
is(Preferences.get("doh-rollout.home-region"), "UK");
52+
53+
is(
54+
DoHConfigController.currentConfig.enabled,
55+
false,
56+
"Rollout should not be enabled for new region"
57+
);
58+
await ensureTRRMode(undefined); // restart of the controller should change the region.
59+
});

0 commit comments

Comments
 (0)