Bug 1883706: part 3) Implement `createHTML`, `createScript` and `createScriptURL...
[gecko.git] / testing / modules / AppInfo.sys.mjs
blob56d074e380555e44c6684d0e8e03386595b64fb9
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/. */
5 let origPlatformInfo = Cc["@mozilla.org/xre/app-info;1"].getService(
6   Ci.nsIPlatformInfo
7 );
9 // eslint-disable-next-line mozilla/use-services
10 let origRuntime = Cc["@mozilla.org/xre/app-info;1"].getService(
11   Ci.nsIXULRuntime
14 /**
15  * Create new XULAppInfo instance with specified options.
16  *
17  * options is a object with following keys:
18  *   ID:              nsIXULAppInfo.ID
19  *   name:            nsIXULAppInfo.name
20  *   version:         nsIXULAppInfo.version
21  *   platformVersion: nsIXULAppInfo.platformVersion
22  *   OS:              nsIXULRuntime.OS
23  *   appBuildID:      nsIXULRuntime.appBuildID
24  *   lastAppBuildID:  nsIXULRuntime.lastAppBuildID
25  *   lastAppVersion:  nsIXULRuntime.lastAppVersion
26  *
27  *   crashReporter:   nsICrashReporter interface is implemented if true
28  */
29 export var newAppInfo = function (options = {}) {
30   let appInfo = {
31     // nsIXULAppInfo
32     vendor: "Mozilla",
33     name: options.name ?? "xpcshell",
34     ID: options.ID ?? "xpcshell@tests.mozilla.org",
35     version: options.version ?? "1",
36     appBuildID: options.appBuildID ?? "20160315",
38     // nsIPlatformInfo
39     platformVersion: options.platformVersion ?? "p-ver",
40     platformBuildID: origPlatformInfo.platformBuildID,
42     // nsIXULRuntime
43     ...Ci.nsIXULRuntime,
44     inSafeMode: false,
45     logConsoleErrors: true,
46     OS: options.OS ?? "XPCShell",
47     XPCOMABI: "noarch-spidermonkey",
48     invalidateCachesOnRestart() {},
49     processType: origRuntime.processType,
50     uniqueProcessID: origRuntime.uniqueProcessID,
52     fissionAutostart: origRuntime.fissionAutostart,
53     sessionHistoryInParent: origRuntime.sessionHistoryInParent,
54     browserTabsRemoteAutostart: origRuntime.browserTabsRemoteAutostart,
55     get maxWebProcessCount() {
56       return origRuntime.maxWebProcessCount;
57     },
58     get launcherProcessState() {
59       return origRuntime.launcherProcessState;
60     },
62     // nsIWinAppHelper
63     get userCanElevate() {
64       return false;
65     },
66   };
68   appInfo.lastAppBuildID = options.lastAppBuildID ?? appInfo.appBuildID;
69   appInfo.lastAppVersion = options.lastAppVersion ?? appInfo.version;
71   let interfaces = [
72     Ci.nsIXULAppInfo,
73     Ci.nsIPlatformInfo,
74     Ci.nsIXULRuntime,
75     Ci.nsICrashReporter,
76   ];
77   if ("nsIWinAppHelper" in Ci) {
78     interfaces.push(Ci.nsIWinAppHelper);
79   }
81   // nsICrashReporter
82   appInfo.annotations = {};
83   appInfo.annotateCrashReport = function (key, data) {
84     if (options.crashReporter) {
85       this.annotations[key] = data;
86     } else {
87       throw Components.Exception("", Cr.NS_ERROR_NOT_INITIALIZED);
88     }
89   };
91   appInfo.QueryInterface = ChromeUtils.generateQI(interfaces);
93   return appInfo;
96 var currentAppInfo = newAppInfo();
98 /**
99  * Obtain a reference to the current object used to define XULAppInfo.
100  */
101 export var getAppInfo = function () {
102   return currentAppInfo;
106  * Update the current application info.
108  * See newAppInfo for options.
110  * To change the current XULAppInfo, simply call this function. If there was
111  * a previously registered app info object, it will be unloaded and replaced.
112  */
113 export var updateAppInfo = function (options) {
114   currentAppInfo = newAppInfo(options);
116   let id = Components.ID("{fbfae60b-64a4-44ef-a911-08ceb70b9f31}");
117   let contractid = "@mozilla.org/xre/app-info;1";
118   let registrar = Components.manager.QueryInterface(Ci.nsIComponentRegistrar);
120   // Unregister an existing factory if one exists.
121   try {
122     let existing = Components.manager.getClassObjectByContractID(
123       contractid,
124       Ci.nsIFactory
125     );
126     registrar.unregisterFactory(id, existing);
127   } catch (ex) {}
129   let factory = {
130     createInstance(iid) {
131       return currentAppInfo.QueryInterface(iid);
132     },
133   };
135   Services.appinfo = currentAppInfo;
137   registrar.registerFactory(id, "XULAppInfo", contractid, factory);