restart.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. const { Cc, Ci, Cu } = require("chrome");
  6. const l10n = require("gcli/l10n");
  7. const Services = require("Services");
  8. const BRAND_SHORT_NAME = Cc["@mozilla.org/intl/stringbundle;1"]
  9. .getService(Ci.nsIStringBundleService)
  10. .createBundle("chrome://branding/locale/brand.properties")
  11. .GetStringFromName("brandShortName");
  12. /**
  13. * Restart command
  14. *
  15. * @param boolean nocache
  16. * Disables loading content from cache upon restart.
  17. *
  18. * Examples :
  19. * >> restart
  20. * - restarts browser immediately
  21. * >> restart --nocache
  22. * - restarts immediately and starts Firefox without using cache
  23. */
  24. exports.items = [
  25. {
  26. item: "command",
  27. runAt: "client",
  28. name: "restart",
  29. description: l10n.lookupFormat("restartBrowserDesc", [ BRAND_SHORT_NAME ]),
  30. params: [{
  31. group: l10n.lookup("restartBrowserGroupOptions"),
  32. params: [
  33. {
  34. name: "nocache",
  35. type: "boolean",
  36. description: l10n.lookup("restartBrowserNocacheDesc")
  37. },
  38. {
  39. name: "safemode",
  40. type: "boolean",
  41. description: l10n.lookup("restartBrowserSafemodeDesc")
  42. }
  43. ]
  44. }],
  45. returnType: "string",
  46. exec: function Restart(args, context) {
  47. let canceled = Cc["@mozilla.org/supports-PRBool;1"]
  48. .createInstance(Ci.nsISupportsPRBool);
  49. Services.obs.notifyObservers(canceled, "quit-application-requested", "restart");
  50. if (canceled.data) {
  51. return l10n.lookup("restartBrowserRequestCancelled");
  52. }
  53. // disable loading content from cache.
  54. if (args.nocache) {
  55. Services.appinfo.invalidateCachesOnRestart();
  56. }
  57. const appStartup = Cc["@mozilla.org/toolkit/app-startup;1"]
  58. .getService(Ci.nsIAppStartup);
  59. if (args.safemode) {
  60. // restart in safemode
  61. appStartup.restartInSafeMode(Ci.nsIAppStartup.eAttemptQuit);
  62. } else {
  63. // restart normally
  64. appStartup.quit(Ci.nsIAppStartup.eAttemptQuit | Ci.nsIAppStartup.eRestart);
  65. }
  66. return l10n.lookupFormat("restartBrowserRestarting", [ BRAND_SHORT_NAME ]);
  67. }
  68. }
  69. ];