Bug 1839316: part 5) Guard the "fetchpriority" attribute behind a pref. r=kershaw...
[gecko.git] / devtools / shared / system.js
blob1a6988d5b82271db29d2afb366c84b5e8a523fc9
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this
3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4 "use strict";
6 loader.lazyRequireGetter(
7   this,
8   "DevToolsServer",
9   "resource://devtools/server/devtools-server.js",
10   true
12 const lazy = {};
13 ChromeUtils.defineESModuleGetters(lazy, {
14   AppConstants: "resource://gre/modules/AppConstants.sys.mjs",
15 });
16 loader.lazyGetter(this, "hostname", () => {
17   try {
18     // On some platforms (Linux according to try), this service does not exist and fails.
19     return Services.dns.myHostName;
20   } catch (e) {
21     return "";
22   }
23 });
24 loader.lazyGetter(this, "endianness", () => {
25   if (new Uint32Array(new Uint8Array([1, 2, 3, 4]).buffer)[0] === 0x04030201) {
26     return "LE";
27   }
28   return "BE";
29 });
31 const APP_MAP = {
32   "{ec8030f7-c20a-464f-9b0e-13a3a9e97384}": "firefox",
33   "{3550f703-e582-4d05-9a08-453d09bdfdc6}": "thunderbird",
34   "{92650c4d-4b8e-4d2a-b7eb-24ecf4f6b63a}": "seamonkey",
35   "{718e30fb-e89b-41dd-9da7-e25a45638b28}": "sunbird",
36   "{aa3c5121-dab2-40e2-81ca-7ea25febc110}": "mobile/android",
39 var CACHED_INFO = null;
41 function getSystemInfo() {
42   if (CACHED_INFO) {
43     return CACHED_INFO;
44   }
46   const appInfo = Services.appinfo;
47   const win = Services.wm.getMostRecentWindow(DevToolsServer.chromeWindowType);
48   const [processor, compiler] = appInfo.XPCOMABI.split("-");
49   let dpi, useragent, width, height, physicalWidth, physicalHeight, brandName;
50   const appid = appInfo.ID;
51   const apptype = APP_MAP[appid];
52   const geckoVersion = appInfo.platformVersion;
53   const hardware = "unknown";
54   let version = "unknown";
56   const os = appInfo.OS;
57   version = appInfo.version;
59   const bundle = Services.strings.createBundle(
60     "chrome://branding/locale/brand.properties"
61   );
62   if (bundle) {
63     brandName = bundle.GetStringFromName("brandFullName");
64   } else {
65     brandName = null;
66   }
68   if (win) {
69     const utils = win.windowUtils;
70     dpi = utils.displayDPI;
71     useragent = win.navigator.userAgent;
72     width = win.screen.width;
73     height = win.screen.height;
74     physicalWidth = win.screen.width * win.devicePixelRatio;
75     physicalHeight = win.screen.height * win.devicePixelRatio;
76   }
78   const info = {
79     /**
80      * Information from nsIXULAppInfo, regarding
81      * the application itself.
82      */
84     // The XUL application's UUID.
85     appid,
87     // Name of the app, "firefox", "thunderbird", etc., listed in APP_MAP
88     apptype,
90     // Mixed-case or empty string of vendor, like "Mozilla"
91     vendor: appInfo.vendor,
93     // Name of the application, like "Firefox", "Thunderbird".
94     name: appInfo.name,
96     // The application's version, for example "0.8.0+" or "3.7a1pre".
97     // Typically, the version of Firefox, for example.
98     // It is different than the version of Gecko or the XULRunner platform.
99     version,
101     // The application's build ID/date, for example "2004051604".
102     appbuildid: appInfo.appBuildID,
104     // The build ID/date of Gecko and the XULRunner platform.
105     platformbuildid: appInfo.platformBuildID,
106     geckobuildid: appInfo.platformBuildID,
108     // The version of Gecko or XULRunner platform, for example "1.8.1.19" or
109     // "1.9.3pre". In "Firefox 3.7 alpha 1" the application version is "3.7a1pre"
110     // while the platform version is "1.9.3pre"
111     platformversion: geckoVersion,
112     geckoversion: geckoVersion,
114     // Locale used in this build
115     locale: Services.locale.appLocaleAsBCP47,
117     /**
118      * Information regarding the operating system.
119      */
121     // Returns the endianness of the architecture: either "LE" or "BE"
122     endianness,
124     // Returns the hostname of the machine
125     hostname,
127     // Name of the OS type. Typically the same as `uname -s`. Possible values:
128     // https://developer.mozilla.org/en/OS_TARGET
129     os,
130     platform: os,
132     // hardware and version info from `deviceinfo.hardware`
133     // and `deviceinfo.os`.
134     hardware,
136     // Device name. This property is only available on Android.
137     // e.g. "Pixel 2"
138     deviceName: getDeviceName(),
140     // Type of process architecture running:
141     // "arm", "ia32", "x86", "x64"
142     // Alias to both `arch` and `processor` for node/deviceactor compat
143     arch: processor,
144     processor,
146     // Name of compiler used for build:
147     // `'msvc', 'n32', 'gcc2', 'gcc3', 'sunc', 'ibmc'...`
148     compiler,
150     // Location for the current profile
151     profile: getProfileLocation(),
153     // Update channel
154     channel: lazy.AppConstants.MOZ_UPDATE_CHANNEL,
156     dpi,
157     useragent,
158     width,
159     height,
160     physicalWidth,
161     physicalHeight,
162     brandName,
163   };
165   CACHED_INFO = info;
166   return info;
169 function getDeviceName() {
170   try {
171     // Will throw on other platforms than Firefox for Android.
172     return Services.sysinfo.getProperty("device");
173   } catch (e) {
174     return null;
175   }
178 function getProfileLocation() {
179   // In child processes, we cannot access the profile location.
180   try {
181     // For some reason this line must come first or in xpcshell tests
182     // nsXREDirProvider never gets initialised and so the profile service
183     // crashes on initialisation.
184     const profd = Services.dirsvc.get("ProfD", Ci.nsIFile);
185     const profservice = Cc["@mozilla.org/toolkit/profile-service;1"].getService(
186       Ci.nsIToolkitProfileService
187     );
188     if (profservice.currentProfile) {
189       return profservice.currentProfile.name;
190     }
192     return profd.leafName;
193   } catch (e) {
194     return "";
195   }
198 exports.getSystemInfo = getSystemInfo;