AppInfo.jsm 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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";
  5. this.EXPORTED_SYMBOLS = [
  6. "newAppInfo",
  7. "getAppInfo",
  8. "updateAppInfo",
  9. ];
  10. const {classes: Cc, interfaces: Ci, results: Cr, utils: Cu} = Components;
  11. Cu.import("resource://gre/modules/XPCOMUtils.jsm");
  12. let origPlatformInfo = Cc["@mozilla.org/xre/app-info;1"]
  13. .getService(Ci.nsIPlatformInfo);
  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. *
  24. * crashReporter: nsICrashReporter interface is implemented if true
  25. * extraProps: extra properties added to XULAppInfo
  26. */
  27. this.newAppInfo = function (options={}) {
  28. let ID = ("ID" in options) ? options.ID : "xpcshell@tests.mozilla.org";
  29. let name = ("name" in options) ? options.name : "xpcshell";
  30. let version = ("version" in options) ? options.version : "1";
  31. let platformVersion
  32. = ("platformVersion" in options) ? options.platformVersion : "p-ver";
  33. let OS = ("OS" in options) ? options.OS : "XPCShell";
  34. let extraProps = ("extraProps" in options) ? options.extraProps : {};
  35. let appInfo = {
  36. // nsIXULAppInfo
  37. vendor: "Mozilla",
  38. name: name,
  39. ID: ID,
  40. version: version,
  41. appBuildID: "20160315",
  42. // nsIPlatformInfo
  43. platformVersion: platformVersion,
  44. platformBuildID: origPlatformInfo.platformBuildID,
  45. // nsIXULRuntime
  46. inSafeMode: false,
  47. logConsoleErrors: true,
  48. OS: OS,
  49. XPCOMABI: "noarch-spidermonkey",
  50. invalidateCachesOnRestart() {},
  51. // nsIWinAppHelper
  52. get userCanElevate() {
  53. return false;
  54. },
  55. };
  56. let interfaces = [Ci.nsIXULAppInfo,
  57. Ci.nsIPlatformInfo,
  58. Ci.nsIXULRuntime];
  59. if ("nsIWinAppHelper" in Ci) {
  60. interfaces.push(Ci.nsIWinAppHelper);
  61. }
  62. if ("crashReporter" in options && options.crashReporter) {
  63. // nsICrashReporter
  64. appInfo.annotations = {};
  65. appInfo.annotateCrashReport = function(key, data) {
  66. this.annotations[key] = data;
  67. };
  68. interfaces.push(Ci.nsICrashReporter);
  69. }
  70. for (let key of Object.keys(extraProps)) {
  71. appInfo.browserTabsRemoteAutostart = extraProps[key];
  72. }
  73. appInfo.QueryInterface = XPCOMUtils.generateQI(interfaces);
  74. return appInfo;
  75. };
  76. var currentAppInfo = newAppInfo();
  77. /**
  78. * Obtain a reference to the current object used to define XULAppInfo.
  79. */
  80. this.getAppInfo = function () { return currentAppInfo; };
  81. /**
  82. * Update the current application info.
  83. *
  84. * See newAppInfo for options.
  85. *
  86. * To change the current XULAppInfo, simply call this function. If there was
  87. * a previously registered app info object, it will be unloaded and replaced.
  88. */
  89. this.updateAppInfo = function (options) {
  90. currentAppInfo = newAppInfo(options);
  91. let id = Components.ID("{fbfae60b-64a4-44ef-a911-08ceb70b9f31}");
  92. let cid = "@mozilla.org/xre/app-info;1";
  93. let registrar = Components.manager.QueryInterface(Ci.nsIComponentRegistrar);
  94. // Unregister an existing factory if one exists.
  95. try {
  96. let existing = Components.manager.getClassObjectByContractID(cid, Ci.nsIFactory);
  97. registrar.unregisterFactory(id, existing);
  98. } catch (ex) {}
  99. let factory = {
  100. createInstance: function (outer, iid) {
  101. if (outer != null) {
  102. throw Cr.NS_ERROR_NO_AGGREGATION;
  103. }
  104. return currentAppInfo.QueryInterface(iid);
  105. },
  106. };
  107. registrar.registerFactory(id, "XULAppInfo", cid, factory);
  108. };