Skip to content

Commit

Permalink
FXVPN-12 Allow manipulation of client settings
Browse files Browse the repository at this point in the history
  • Loading branch information
strseb committed Dec 9, 2024
1 parent e357761 commit ae3a739
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 1 deletion.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"build": "web-ext build --source-dir src --filename mozilla-vpn-extension.xpi",
"start-dev": "web-ext run --verbose --source-dir src --pref=ui.popup.disable_autohide=true",
"start": "web-ext run --verbose --source-dir src",
"test": "node --experimental-vm-modules node_modules/jest/bin/jest.js --collect-coverage --detectOpenHandles"
"test": "node --experimental-vm-modules node_modules/jest/bin/jest.js --collect-coverage --detectOpenHandles --"
},
"repository": {
"type": "git",
Expand Down
5 changes: 5 additions & 0 deletions src/background/vpncontroller/states.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export const REQUEST_TYPES = [
"telemetry",
"session_start",
"session_stop",
"settings",
];

export class VPNState {
Expand Down Expand Up @@ -213,3 +214,7 @@ export class vpnStatusResponse {
version: "2.25.0",
};
}

export class VPNSettings {
extensionTelemetryEnabled = false;
}
18 changes: 18 additions & 0 deletions src/background/vpncontroller/vpncontroller.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
StateVPNClosed,
StateVPNSignedOut,
StateVPNNeedsUpdate,
VPNSettings,
} from "./states.js";

const log = Logger.logger("TabHandler");
Expand All @@ -47,6 +48,7 @@ export class VPNController extends Component {
postToApp: PropertyType.Function,
isolationKey: PropertyType.Bindable,
featureList: PropertyType.Bindable,
settings: PropertyType.Bindable,
};

get state() {
Expand All @@ -62,6 +64,9 @@ export class VPNController extends Component {
get featureList() {
return this.#mFeaturelist;
}
get settings() {
return this.#settings.readOnly;
}

initNativeMessaging() {
log("initNativeMessaging");
Expand Down Expand Up @@ -178,6 +183,17 @@ export class VPNController extends Component {
...new FeatureFlags(),
...response.featurelist,
});
break;
case "settings":
const settings = new VPNSettings();
// Copy over all values that we expect to be in VPNSettings
Object.keys(settings).forEach((k) => {
if (response.settings[k]) {
settings[k] = response.settings[k];
}
});
this.#settings.set(settings);
break;
default:
console.log("Unexpected Message type: " + response.t);
}
Expand All @@ -200,6 +216,7 @@ export class VPNController extends Component {
this.postToApp("status");
this.postToApp("servers");
this.postToApp("disabled_apps");
this.postToApp("settings");
});
return;
}
Expand Down Expand Up @@ -235,6 +252,7 @@ export class VPNController extends Component {
#mFeaturelist = property(new FeatureFlags());

#isExcluded = property(false);
#settings = property(new VPNSettings());
}

export function isSplitTunnled(
Expand Down
34 changes: 34 additions & 0 deletions tests/jest/background/vpncontroller/vpncontroller.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import {
isSplitTunnled,
ServerCity,
ServerCountry,
VPNController,
VPNSettings,
vpnStatusResponse,
} from "../../../../src/background/vpncontroller";

Expand Down Expand Up @@ -178,3 +180,35 @@ describe("fromVPNStatusResponse", () => {
expect(result.state).toBe("NeedsUpdate");
});
});

describe("IPC::Settings", () => {
it("can handle a setting response", async () => {
const target = new VPNController({ registerObserver: () => {} });
// The Value should be the default one.
expect(target.settings.value.extensionTelemetryEnabled).toBe(
new VPNSettings().extensionTelemetryEnabled
);
// The VPN Client may at any point push new data
const message = {
t: "settings",
settings: {
extensionTelemetryEnabled: true,
},
};
await target.handleResponse(message);
expect(target.settings.value.extensionTelemetryEnabled).toBe(
message.settings.extensionTelemetryEnabled
);
});
it("ignores unknown settings", async () => {
const target = new VPNController({ registerObserver: () => {} });
const message = {
t: "settings",
settings: {
thisSettingDoesNotExist: true,
},
};
await target.handleResponse(message);
expect(target.settings.value["thisSettingDoesNotExist"]).toBe(undefined);
});
});

0 comments on commit ae3a739

Please sign in to comment.