Blame toolkit/modules/ResetProfile.jsm

Packit f0b94e
/* This Source Code Form is subject to the terms of the Mozilla Public
Packit f0b94e
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
Packit f0b94e
 * You can obtain one at http://mozilla.org/MPL/2.0/. */
Packit f0b94e
Packit f0b94e
"use strict";
Packit f0b94e
Packit f0b94e
var EXPORTED_SYMBOLS = ["ResetProfile"];
Packit f0b94e
Packit f0b94e
ChromeUtils.import("resource://gre/modules/Services.jsm");
Packit f0b94e
ChromeUtils.import("resource://gre/modules/AppConstants.jsm");
Packit f0b94e
Packit f0b94e
const MOZ_APP_NAME = AppConstants.MOZ_APP_NAME;
Packit f0b94e
const MOZ_BUILD_APP = AppConstants.MOZ_BUILD_APP;
Packit f0b94e
Packit f0b94e
var ResetProfile = {
Packit f0b94e
  /**
Packit f0b94e
   * Check if reset is supported for the currently running profile.
Packit f0b94e
   *
Packit f0b94e
   * @return boolean whether reset is supported.
Packit f0b94e
   */
Packit f0b94e
  resetSupported() {
Packit f0b94e
    if (Services.policies && !Services.policies.isAllowed("profileRefresh")) {
Packit f0b94e
      return false;
Packit f0b94e
    }
Packit f0b94e
    // Reset is only supported if the self-migrator used for reset exists.
Packit f0b94e
    let migrator = "@mozilla.org/profile/migrator;1?app=" + MOZ_BUILD_APP +
Packit f0b94e
                   "&type=" + MOZ_APP_NAME;
Packit f0b94e
    if (!(migrator in Cc)) {
Packit f0b94e
      return false;
Packit f0b94e
    }
Packit f0b94e
    // We also need to be using a profile the profile manager knows about.
Packit f0b94e
    let profileService = Cc["@mozilla.org/toolkit/profile-service;1"].
Packit f0b94e
                         getService(Ci.nsIToolkitProfileService);
Packit f0b94e
    let currentProfileDir = Services.dirsvc.get("ProfD", Ci.nsIFile);
Packit f0b94e
    let profileEnumerator = profileService.profiles;
Packit f0b94e
    while (profileEnumerator.hasMoreElements()) {
Packit f0b94e
      let profile = profileEnumerator.getNext().QueryInterface(Ci.nsIToolkitProfile);
Packit f0b94e
      if (profile.rootDir && profile.rootDir.equals(currentProfileDir)) {
Packit f0b94e
        return true;
Packit f0b94e
      }
Packit f0b94e
    }
Packit f0b94e
    return false;
Packit f0b94e
  },
Packit f0b94e
Packit f0b94e
  /**
Packit f0b94e
   * Ask the user if they wish to restart the application to reset the profile.
Packit f0b94e
   */
Packit f0b94e
  openConfirmationDialog(window) {
Packit f0b94e
    // Prompt the user to confirm.
Packit f0b94e
    let params = {
Packit f0b94e
      reset: false,
Packit f0b94e
    };
Packit f0b94e
    window.openDialog("chrome://global/content/resetProfile.xul", null,
Packit f0b94e
                      "chrome,modal,centerscreen,titlebar,dialog=yes", params);
Packit f0b94e
    if (!params.reset)
Packit f0b94e
      return;
Packit f0b94e
Packit f0b94e
    // Set the reset profile environment variable.
Packit f0b94e
    let env = Cc["@mozilla.org/process/environment;1"]
Packit f0b94e
                .getService(Ci.nsIEnvironment);
Packit f0b94e
    env.set("MOZ_RESET_PROFILE_RESTART", "1");
Packit f0b94e
Packit f0b94e
    Services.startup.quit(Ci.nsIAppStartup.eForceQuit | Ci.nsIAppStartup.eRestart);
Packit f0b94e
  },
Packit f0b94e
};