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