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/. */
10 #include <type_traits>
12 #include "nsISupports.h"
13 #include "ShutdownPhase.h"
17 enum class AppShutdownMode
{
22 enum class AppShutdownReason
{
25 // Normal application shutdown.
27 // The application wants to restart.
29 // The OS is force closing us.
31 // The user is logging off from the OS session, the system may stay alive.
33 // The system is shutting down (and maybe restarting).
39 static ShutdownPhase
GetCurrentShutdownPhase();
40 static bool IsInOrBeyond(ShutdownPhase aPhase
);
43 * Returns the current exit code that the process will be terminated with.
45 static int GetExitCode();
48 * Save environment variables that we might need if the app initiates a
49 * restart later in its lifecycle.
51 static void SaveEnvVarsForPotentialRestart();
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
);
61 * Confirm that we are in fact going to be shutting down.
63 static void OnShutdownConfirmed();
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();
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);
83 * True if the application is currently attempting to shut down in order to
86 static bool IsRestarting();
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));
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
);
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
);
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