Bug 1922722 - Remove default argument from WebRenderBridgeParent::FlushRendering...
[gecko.git] / toolkit / modules / ResetProfile.sys.mjs
blob333ee7c0cfb12a25a71b27e648a0b179349a1d76
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/. */
5 import { AppConstants } from "resource://gre/modules/AppConstants.sys.mjs";
7 const lazy = {};
9 ChromeUtils.defineLazyGetter(lazy, "MigrationUtils", () => {
10   // MigrationUtils is currently only available in browser builds.
11   if (AppConstants.MOZ_BUILD_APP != "browser") {
12     return undefined;
13   }
15   try {
16     let { MigrationUtils } = ChromeUtils.importESModule(
17       "resource:///modules/MigrationUtils.sys.mjs"
18     );
19     return MigrationUtils;
20   } catch (e) {
21     console.error(`Unable to load MigrationUtils.sys.mjs: ${e}`);
22   }
23   return undefined;
24 });
26 const MOZ_APP_NAME = AppConstants.MOZ_APP_NAME;
28 export var ResetProfile = {
29   /**
30    * Check if reset is supported for the currently running profile.
31    *
32    * @return boolean whether reset is supported.
33    */
34   resetSupported() {
35     if (Services.policies && !Services.policies.isAllowed("profileRefresh")) {
36       return false;
37     }
39     // Reset is only supported if the self-migrator used for reset exists.
40     if (
41       !lazy.MigrationUtils ||
42       !lazy.MigrationUtils.migratorExists(MOZ_APP_NAME)
43     ) {
44       return false;
45     }
47     // We also need to be using a profile the profile manager knows about.
48     let profileService = Cc[
49       "@mozilla.org/toolkit/profile-service;1"
50     ].getService(Ci.nsIToolkitProfileService);
51     let currentProfileDir = Services.dirsvc.get("ProfD", Ci.nsIFile);
52     for (let profile of profileService.profiles) {
53       if (profile.rootDir && profile.rootDir.equals(currentProfileDir)) {
54         return true;
55       }
56     }
57     return false;
58   },
60   /**
61    * Ask the user if they wish to restart the application to reset the profile.
62    */
63   async openConfirmationDialog(window) {
64     let win = window;
65     // If we are, for instance, on an about page, get the chrome window to
66     // access its gDialogBox.
67     if (win.docShell.chromeEventHandler) {
68       win = win.browsingContext?.topChromeWindow;
69     }
71     let params = {
72       learnMore: false,
73       reset: false,
74     };
76     if (win.gDialogBox) {
77       await win.gDialogBox.open(
78         "chrome://global/content/resetProfile.xhtml",
79         params
80       );
81     } else {
82       win.openDialog(
83         "chrome://global/content/resetProfile.xhtml",
84         null,
85         "modal,centerscreen,titlebar",
86         params
87       );
88     }
90     if (params.learnMore) {
91       win.openTrustedLinkIn(
92         "https://support.mozilla.org/kb/refresh-firefox-reset-add-ons-and-settings",
93         "tab"
94       );
95       return;
96     }
98     if (!params.reset) {
99       return;
100     }
102     // Set the reset profile environment variable.
103     Services.env.set("MOZ_RESET_PROFILE_RESTART", "1");
105     Services.startup.quit(
106       Ci.nsIAppStartup.eForceQuit | Ci.nsIAppStartup.eRestart
107     );
108   },