autorecovery.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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 file,
  3. * You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. /* Auto-recovery module.
  5. * This module aims to catch fatal browser initialization errors and either
  6. * automatically correct likely causes from them, or automatically restarting
  7. * the browser in safe mode. This is hooked into the browser's "onload"
  8. * event because it can be assumed that at that point, everything must
  9. * have been properly initialized already.
  10. */
  11. var Cc = Components.classes;
  12. var Ci = Components.interfaces;
  13. var Cu = Components.utils;
  14. // Services = object with smart getters for common XPCOM services
  15. Cu.import("resource://gre/modules/Services.jsm");
  16. var browser_autoRecovery = {
  17. onLoad: function() {
  18. var nsIAS = Ci.nsIAppStartup; // Application startup interface
  19. if (typeof gBrowser === "undefined") {
  20. // gBrowser should always be defined at this point, but if it is not, then most likely
  21. // it is due to an incompatible or outdated language pack being installed and selected.
  22. // In this case, we reset "general.useragent.locale" to try to recover browser startup.
  23. if (Services.prefs.prefHasUserValue("general.useragent.locale")) {
  24. // Restart automatically in en-US.
  25. Services.prefs.clearUserPref("general.useragent.locale");
  26. Cc["@mozilla.org/toolkit/app-startup;1"].getService(nsIAS).quit(nsIAS.eRestart | nsIAS.eAttemptQuit);
  27. } else if (!Services.appinfo.inSafeMode) {
  28. // gBrowser isn't defined, and we're not using a custom locale. Most likely
  29. // a user-installed add-on causes issues here, so we restart in Safe Mode.
  30. let RISM = Services.prompt.confirm(null, "Error",
  31. "The Browser didn't start properly!\n"+
  32. "This is usually caused by an add-on or misconfiguration.\n\n"+
  33. "Restart in Safe Mode?");
  34. if (RISM) {
  35. Cc["@mozilla.org/toolkit/app-startup;1"].getService(nsIAS).restartInSafeMode(nsIAS.eRestart | nsIAS.eAttemptQuit);
  36. } else {
  37. // Force quit application
  38. Cc["@mozilla.org/toolkit/app-startup;1"].getService(nsIAS).quit(nsIAS.eForceQuit);
  39. }
  40. }
  41. // Something else caused this issue and we're already in Safe Mode, so we return
  42. // without doing anything else, and let normal error handling take place.
  43. return;
  44. } // gBrowser undefined
  45. // Other checks than gBrowser undefined can go here!
  46. // Remove our listener, since we don't want this to fire on every load.
  47. window.removeEventListener("load", browser_autoRecovery.onLoad, false);
  48. }
  49. };
  50. window.addEventListener("load", browser_autoRecovery.onLoad, false);