Bug 1932613 - temporarily disable browser_ml_end_to_end.js for permanent failures...
[gecko.git] / xpcom / base / AppShutdown.h
blobf8cd6609264f1205ea716504199a1394007318e7
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef AppShutdown_h
8 #define AppShutdown_h
10 #include <type_traits>
11 #include "nsCOMPtr.h"
12 #include "nsISupports.h"
13 #include "ShutdownPhase.h"
15 namespace mozilla {
17 enum class AppShutdownMode {
18 Normal,
19 Restart,
22 enum class AppShutdownReason {
23 // No reason.
24 Unknown,
25 // Normal application shutdown.
26 AppClose,
27 // The application wants to restart.
28 AppRestart,
29 // The OS is force closing us.
30 OSForceClose,
31 // The user is logging off from the OS session, the system may stay alive.
32 OSSessionEnd,
33 // The system is shutting down (and maybe restarting).
34 OSShutdown,
37 class AppShutdown {
38 public:
39 static ShutdownPhase GetCurrentShutdownPhase();
40 static bool IsInOrBeyond(ShutdownPhase aPhase);
42 /**
43 * Returns the current exit code that the process will be terminated with.
45 static int GetExitCode();
47 /**
48 * Save environment variables that we might need if the app initiates a
49 * restart later in its lifecycle.
51 static void SaveEnvVarsForPotentialRestart();
53 /**
54 * Init the shutdown with the requested shutdown mode, exit code and optional
55 * a reason (if missing it will be derived from aMode).
57 static void Init(AppShutdownMode aMode, int aExitCode,
58 AppShutdownReason aReason);
60 /**
61 * Confirm that we are in fact going to be shutting down.
63 static void OnShutdownConfirmed();
65 /**
66 * If we've attempted to initiate a restart, this call will set up the
67 * necessary environment variables and launch the new process.
69 static void MaybeDoRestart();
71 /**
72 * The _exit() call is not a safe way to terminate your own process on
73 * Windows, because _exit runs DLL detach callbacks which run static
74 * destructors for xul.dll.
76 * This method terminates the current process without those issues.
78 * Optionally a custom exit code can be supplied.
80 static void DoImmediateExit(int aExitCode = 0);
82 /**
83 * True if the application is currently attempting to shut down in order to
84 * restart.
86 static bool IsRestarting();
88 /**
89 * Wrapper for shutdown notifications that informs the terminator before
90 * we notify other observers. Calls MaybeFastShutdown.
92 static void AdvanceShutdownPhase(
93 ShutdownPhase aPhase, const char16_t* aNotificationData = nullptr,
94 const nsCOMPtr<nsISupports>& aNotificationSubject =
95 nsCOMPtr<nsISupports>(nullptr));
97 /**
98 * XXX: Before tackling bug 1697745 we need the
99 * possibility to advance the phase without notification
100 * in the content process.
102 static void AdvanceShutdownPhaseWithoutNotify(ShutdownPhase aPhase);
105 * Map shutdown phase to observer key
107 static const char* GetObserverKey(ShutdownPhase aPhase);
110 * Map shutdown phase to readable name
112 static const char* GetShutdownPhaseName(ShutdownPhase aPhase);
115 * Map observer topic key to shutdown phase
117 static ShutdownPhase GetShutdownPhaseFromTopic(const char* aTopic);
119 #ifdef DEBUG
121 * Check, if we are allowed to send a shutdown notification.
122 * Shutdown specific topics are only allowed during calls to
123 * AdvanceShutdownPhase itself.
125 static bool IsNoOrLegalShutdownTopic(const char* aTopic);
126 #endif
128 private:
130 * Set the shutdown reason annotation.
132 static void AnnotateShutdownReason(AppShutdownReason aReason);
135 * This will perform a fast shutdown via _exit(0) or similar if the user's
136 * prefs are configured to do so at this phase.
138 static void MaybeFastShutdown(ShutdownPhase aPhase);
141 * Internal helper function, uses MaybeFastShutdown.
143 static void AdvanceShutdownPhaseInternal(
144 ShutdownPhase aPhase, bool doNotify, const char16_t* aNotificationData,
145 const nsCOMPtr<nsISupports>& aNotificationSubject);
148 } // namespace mozilla
150 #endif // AppShutdown_h