Skip to content

Commit ca044aa

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 e54b37d commit ca044aa

File tree

3 files changed

+76
-0
lines changed

3 files changed

+76
-0
lines changed

browser/components/doh/DoHConfig.sys.mjs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,15 @@ 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 homeRegionChanged = lazy.Preferences.get(
171+
`${kGlobalPrefBranch}.home-region-changed`
172+
);
173+
if (homeRegionChanged) {
174+
lazy.Preferences.reset(`${kGlobalPrefBranch}.home-region-changed`);
175+
lazy.Preferences.reset(`${kGlobalPrefBranch}.home-region`);
176+
}
177+
169178
let homeRegion = lazy.Preferences.get(`${kGlobalPrefBranch}.home-region`);
170179
if (homeRegion) {
171180
kRegionPrefBranch = `${kGlobalPrefBranch}.${homeRegion.toLowerCase()}`;
@@ -201,6 +210,7 @@ export const DoHConfigController = {
201210
await this.loadRegion();
202211

203212
Services.prefs.addObserver(`${kGlobalPrefBranch}.`, this, true);
213+
Services.obs.addObserver(this, lazy.Region.REGION_TOPIC);
204214

205215
gProvidersCollection.on("sync", this.updateFromRemoteSettings);
206216
gConfigCollection.on("sync", this.updateFromRemoteSettings);
@@ -213,6 +223,7 @@ export const DoHConfigController = {
213223
await this.initComplete;
214224

215225
Services.prefs.removeObserver(`${kGlobalPrefBranch}`, this);
226+
Services.obs.removeObserver(this, lazy.Region.REGION_TOPIC);
216227

217228
gProvidersCollection.off("sync", this.updateFromRemoteSettings);
218229
gConfigCollection.off("sync", this.updateFromRemoteSettings);
@@ -240,6 +251,9 @@ export const DoHConfigController = {
240251
}
241252
this.notifyNewConfig();
242253
break;
254+
case lazy.Region.REGION_TOPIC:
255+
lazy.Preferences.set(`${kGlobalPrefBranch}.home-region-changed`, true);
256+
break;
243257
}
244258
},
245259

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

0 commit comments

Comments
 (0)