Bug 1883449 - [wpt-sync] Update web-platform-tests to bb67daef8e6a384ead5da4c991c12f8...
[gecko.git] / devtools / shared / system.js
blob2f602a5318290ab49dca9e00548c79d9abf679f0
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(
14   lazy,
15   {
16     AppConstants: "resource://gre/modules/AppConstants.sys.mjs",
17   },
18   { global: "contextual" }
20 loader.lazyGetter(this, "hostname", () => {
21   try {
22     // On some platforms (Linux according to try), this service does not exist and fails.
23     return Services.dns.myHostName;
24   } catch (e) {
25     return "";
26   }
27 });
28 loader.lazyGetter(this, "endianness", () => {
29   if (new Uint32Array(new Uint8Array([1, 2, 3, 4]).buffer)[0] === 0x04030201) {
30     return "LE";
31   }
32   return "BE";
33 });
35 const APP_MAP = {
36   "{ec8030f7-c20a-464f-9b0e-13a3a9e97384}": "firefox",
37   "{3550f703-e582-4d05-9a08-453d09bdfdc6}": "thunderbird",
38   "{92650c4d-4b8e-4d2a-b7eb-24ecf4f6b63a}": "seamonkey",
39   "{718e30fb-e89b-41dd-9da7-e25a45638b28}": "sunbird",
40   "{aa3c5121-dab2-40e2-81ca-7ea25febc110}": "mobile/android",
43 var CACHED_INFO = null;
45 function getSystemInfo() {
46   if (CACHED_INFO) {
47     return CACHED_INFO;
48   }
50   const appInfo = Services.appinfo;
51   const win = Services.wm.getMostRecentWindow(DevToolsServer.chromeWindowType);
52   const [processor, compiler] = appInfo.XPCOMABI.split("-");
53   let dpi, useragent, width, height, physicalWidth, physicalHeight, brandName;
54   const appid = appInfo.ID;
55   const apptype = APP_MAP[appid];
56   const geckoVersion = appInfo.platformVersion;
57   const hardware = "unknown";
58   let version = "unknown";
60   const os = appInfo.OS;
61   version = appInfo.version;
63   const bundle = Services.strings.createBundle(
64     "chrome://branding/locale/brand.properties"
65   );
66   if (bundle) {
67     brandName = bundle.GetStringFromName("brandFullName");
68   } else {
69     brandName = null;
70   }
72   if (win) {
73     const utils = win.windowUtils;
74     dpi = utils.displayDPI;
75     useragent = win.navigator.userAgent;
76     width = win.screen.width;
77     height = win.screen.height;
78     physicalWidth = win.screen.width * win.devicePixelRatio;
79     physicalHeight = win.screen.height * win.devicePixelRatio;
80   }
82   const info = {
83     /**
84      * Information from nsIXULAppInfo, regarding
85      * the application itself.
86      */
88     // The XUL application's UUID.
89     appid,
91     // Name of the app, "firefox", "thunderbird", etc., listed in APP_MAP
92     apptype,
94     // Mixed-case or empty string of vendor, like "Mozilla"
95     vendor: appInfo.vendor,
97     // Name of the application, like "Firefox", "Thunderbird".
98     name: appInfo.name,
100     // The application's version, for example "0.8.0+" or "3.7a1pre".
101     // Typically, the version of Firefox, for example.
102     // It is different than the version of Gecko or the XULRunner platform.
103     version,
105     // The application's build ID/date, for example "2004051604".
106     appbuildid: appInfo.appBuildID,
108     // The build ID/date of Gecko and the XULRunner platform.
109     platformbuildid: appInfo.platformBuildID,
110     geckobuildid: appInfo.platformBuildID,
112     // The version of Gecko or XULRunner platform, for example "1.8.1.19" or
113     // "1.9.3pre". In "Firefox 3.7 alpha 1" the application version is "3.7a1pre"
114     // while the platform version is "1.9.3pre"
115     platformversion: geckoVersion,
116     geckoversion: geckoVersion,
118     // Locale used in this build
119     locale: Services.locale.appLocaleAsBCP47,
121     /**
122      * Information regarding the operating system.
123      */
125     // Returns the endianness of the architecture: either "LE" or "BE"
126     endianness,
128     // Returns the hostname of the machine
129     hostname,
131     // Name of the OS type. Typically the same as `uname -s`. Possible values:
132     // https://developer.mozilla.org/en/OS_TARGET
133     os,
134     platform: os,
136     // hardware and version info from `deviceinfo.hardware`
137     // and `deviceinfo.os`.
138     hardware,
140     // Device name. This property is only available on Android.
141     // e.g. "Pixel 2"
142     deviceName: getDeviceName(),
144     // Type of process architecture running:
145     // "arm", "ia32", "x86", "x64"
146     // Alias to both `arch` and `processor` for node/deviceactor compat
147     arch: processor,
148     processor,
150     // Name of compiler used for build:
151     // `'msvc', 'n32', 'gcc2', 'gcc3', 'sunc', 'ibmc'...`
152     compiler,
154     // Location for the current profile
155     profile: getProfileLocation(),
157     // Update channel
158     channel: lazy.AppConstants.MOZ_UPDATE_CHANNEL,
160     dpi,
161     useragent,
162     width,
163     height,
164     physicalWidth,
165     physicalHeight,
166     brandName,
167   };
169   CACHED_INFO = info;
170   return info;
173 function getDeviceName() {
174   try {
175     // Will throw on other platforms than Firefox for Android.
176     return Services.sysinfo.getProperty("device");
177   } catch (e) {
178     return null;
179   }
182 function getProfileLocation() {
183   // In child processes, we cannot access the profile location.
184   try {
185     // For some reason this line must come first or in xpcshell tests
186     // nsXREDirProvider never gets initialised and so the profile service
187     // crashes on initialisation.
188     const profd = Services.dirsvc.get("ProfD", Ci.nsIFile);
189     const profservice = Cc["@mozilla.org/toolkit/profile-service;1"].getService(
190       Ci.nsIToolkitProfileService
191     );
192     if (profservice.currentProfile) {
193       return profservice.currentProfile.name;
194     }
196     return profd.leafName;
197   } catch (e) {
198     return "";
199   }
202 exports.getSystemInfo = getSystemInfo;