no bug - Bumping Firefox l10n changesets r=release a=l10n-bump DONTBUILD CLOSED TREE
[gecko.git] / toolkit / modules / OsEnvironment.sys.mjs
blobc16cbf31873143c1c4bcea6ddd3a73f8aa15ca03
1 /* -*- js-indent-level: 2; indent-tabs-mode: nil -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3  * License, v. 2.0. If a copy of the MPL was not distributed with this
4  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 import { AppConstants } from "resource://gre/modules/AppConstants.sys.mjs";
8 const lazy = {};
10 ChromeUtils.defineESModuleGetters(lazy, {
11   WindowsRegistry: "resource://gre/modules/WindowsRegistry.sys.mjs",
12   WindowsVersionInfo:
13     "resource://gre/modules/components-utils/WindowsVersionInfo.sys.mjs",
14 });
16 export let OsEnvironment = {
17   /**
18    * This is a policy object used to override behavior for testing.
19    */
20   Policy: {
21     getAllowedAppSources: () =>
22       lazy.WindowsRegistry.readRegKey(
23         Ci.nsIWindowsRegKey.ROOT_KEY_LOCAL_MACHINE,
24         "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer",
25         "AicEnabled"
26       ),
27     windowsVersionHasAppSourcesFeature: () => {
28       let windowsVersion = parseFloat(Services.sysinfo.getProperty("version"));
29       if (isNaN(windowsVersion)) {
30         throw new Error("Unable to parse Windows version");
31       }
32       if (windowsVersion < 10) {
33         return false;
34       }
36       // The App Sources feature was added in Windows 10, build 15063.
37       const { buildNumber } = lazy.WindowsVersionInfo.get();
38       return buildNumber >= 15063;
39     },
40   },
42   reportAllowedAppSources() {
43     if (AppConstants.platform != "win") {
44       // This is currently a windows-only feature.
45       return;
46     }
48     const appSourceScalar = "os.environment.allowed_app_sources";
50     let haveAppSourcesFeature;
51     try {
52       haveAppSourcesFeature =
53         OsEnvironment.Policy.windowsVersionHasAppSourcesFeature();
54     } catch (ex) {
55       console.error(ex);
56       Services.telemetry.scalarSet(appSourceScalar, "Error");
57       return;
58     }
59     if (!haveAppSourcesFeature) {
60       Services.telemetry.scalarSet(appSourceScalar, "NoSuchFeature");
61       return;
62     }
64     let allowedAppSources;
65     try {
66       allowedAppSources = OsEnvironment.Policy.getAllowedAppSources();
67     } catch (ex) {
68       console.error(ex);
69       Services.telemetry.scalarSet(appSourceScalar, "Error");
70       return;
71     }
72     if (allowedAppSources === undefined) {
73       // A return value of undefined means that the registry value didn't
74       // exist. Windows treats a missing registry entry the same as if the
75       // value is "Anywhere". Make sure to differentiate a missing registry
76       // entry from one containing an empty string, since it is unclear how
77       // Windows would treat such a setting, but it may not be the same as
78       // if the value is missing.
79       allowedAppSources = "Anywhere";
80     }
82     const expectedValues = [
83       "Anywhere",
84       "Recommendations",
85       "PreferStore",
86       "StoreOnly",
87     ];
88     if (!expectedValues.includes(allowedAppSources)) {
89       allowedAppSources = "Error";
90     }
92     Services.telemetry.scalarSet(appSourceScalar, allowedAppSources);
93   },