Bug 603903: register with restart service, restore session if forced to shutdown
MozReview-Commit-ID: Ah4R2W2eojN
--- a/toolkit/xre/nsAppRunner.cpp
+++ b/toolkit/xre/nsAppRunner.cpp
@@ -1879,17 +1879,51 @@ XRE_GetBinaryPath(nsIFile* *aResult)
return mozilla::BinaryPath::GetFile(aResult);
}
#ifdef XP_WIN
#include "nsWindowsRestart.cpp"
#include <shellapi.h>
typedef BOOL (WINAPI* SetProcessDEPPolicyFunc)(DWORD dwFlags);
-#endif
+
+#define PREF_WIN_REGISTER_APPLICATION_RESTART "toolkit.winRegisterApplicationRestart"
+
+static void
+RegisterApplicationRestartChanged(const char* aPref, void* aData) {
+ if (Preferences::GetBool(PREF_WIN_REGISTER_APPLICATION_RESTART, true)) {
+ // Make the command line to use when restarting
+ wchar_t* restartCommandLine = nullptr;
+ if (gRestartArgc > 1) {
+ // excludes argv[0] because RegisterApplicationRestart adds the
+ // executable name
+ wchar_t** restartArgvConverted =
+ AllocConvertUTF8toUTF16Strings(gRestartArgc - 1, gRestartArgv + 1);
+
+ if (restartArgvConverted) {
+ restartCommandLine = MakeCommandLine(gRestartArgc - 1, restartArgvConverted);
+ FreeAllocStrings(gRestartArgc - 1, restartArgvConverted);
+ }
+ } else {
+ restartCommandLine = (wchar_t*) malloc(sizeof(wchar_t));
+ restartCommandLine[0] = L'\0';
+ }
+
+ if (restartCommandLine) {
+ // Flags RESTART_NO_PATCH and RESTART_NO_REBOOT are not set, so we
+ // should be restarted if terminated by an update or restart.
+ ::RegisterApplicationRestart(restartCommandLine, RESTART_NO_CRASH |
+ RESTART_NO_HANG);
+ free(restartCommandLine);
+ }
+ } else {
+ ::UnregisterApplicationRestart();
+ }
+}
+#endif // XP_WIN
// If aBlankCommandLine is true, then the application will be launched with a
// blank command line instead of being launched with the same command line that
// it was initially started with.
static nsresult LaunchChild(nsINativeAppSupport* aNative,
bool aBlankCommandLine = false)
{
aNative->Quit(); // release DDE mutex, if we're holding it
@@ -4730,16 +4764,21 @@ XREMain::XRE_mainRun()
SaveToEnv("NO_EM_RESTART=");
SaveToEnv("XUL_APP_FILE=");
SaveToEnv("XRE_BINARY_PATH=");
if (!mShuttingDown) {
rv = appStartup->CreateHiddenWindow();
NS_ENSURE_SUCCESS(rv, NS_ERROR_FAILURE);
+#ifdef XP_WIN32
+ Preferences::RegisterCallbackAndCall(RegisterApplicationRestartChanged,
+ PREF_WIN_REGISTER_APPLICATION_RESTART);
+#endif
+
#if defined(HAVE_DESKTOP_STARTUP_ID) && defined(MOZ_WIDGET_GTK)
nsGTKToolkit* toolkit = nsGTKToolkit::GetToolkit();
if (toolkit && !mDesktopStartupID.IsEmpty()) {
toolkit->SetDesktopStartupID(mDesktopStartupID);
}
// Clear the environment variable so it won't be inherited by
// child processes and confuse things.
g_unsetenv ("DESKTOP_STARTUP_ID");
--- a/toolkit/xre/nsWindowsRestart.cpp
+++ b/toolkit/xre/nsWindowsRestart.cpp
@@ -108,18 +108,16 @@ static wchar_t* ArgToString(wchar_t *d,
}
return d;
}
/**
* Creates a command line from a list of arguments. The returned
* string is allocated with "malloc" and should be "free"d.
- *
- * argv is UTF8
*/
wchar_t*
MakeCommandLine(int argc, wchar_t **argv)
{
int i;
int len = 0;
// The + 1 of the last argument handles the allocation for null termination
@@ -173,16 +171,32 @@ FreeAllocStrings(int argc, wchar_t **arg
while (argc) {
--argc;
delete [] argv[argc];
}
delete [] argv;
}
+static wchar_t**
+AllocConvertUTF8toUTF16Strings(int argc, char **argv)
+{
+ wchar_t **argvConverted = new wchar_t*[argc];
+ if (!argvConverted)
+ return nullptr;
+
+ for (int i = 0; i < argc; ++i) {
+ argvConverted[i] = reinterpret_cast<wchar_t*>(AllocConvertUTF8toUTF16(argv[i]));
+ if (!argvConverted[i]) {
+ FreeAllocStrings(i, argvConverted);
+ return nullptr;
+ }
+ }
+ return argvConverted;
+}
/**
* Launch a child process with the specified arguments.
* @note argv[0] is ignored
* @note The form of this function that takes char **argv expects UTF-8
*/
@@ -193,28 +207,20 @@ WinLaunchChild(const wchar_t *exePath,
HANDLE *hProcess = nullptr);
BOOL
WinLaunchChild(const wchar_t *exePath,
int argc, char **argv,
HANDLE userToken,
HANDLE *hProcess)
{
- wchar_t** argvConverted = new wchar_t*[argc];
+ wchar_t **argvConverted = AllocConvertUTF8toUTF16Strings(argc, argv);
if (!argvConverted)
return FALSE;
- for (int i = 0; i < argc; ++i) {
- argvConverted[i] = reinterpret_cast<wchar_t*>(AllocConvertUTF8toUTF16(argv[i]));
- if (!argvConverted[i]) {
- FreeAllocStrings(i, argvConverted);
- return FALSE;
- }
- }
-
BOOL ok = WinLaunchChild(exePath, argc, argvConverted, userToken, hProcess);
FreeAllocStrings(argc, argvConverted);
return ok;
}
BOOL
WinLaunchChild(const wchar_t *exePath,
int argc,
--- a/widget/windows/nsWindow.cpp
+++ b/widget/windows/nsWindow.cpp
@@ -5176,31 +5176,39 @@ nsWindow::ProcessMessage(UINT msg, WPARA
/* We don't do this for win10 glass with a custom titlebar,
* in order to avoid the caption buttons breaking. */
!(IsWin10OrLater() && HasGlass()) &&
DwmDefWindowProc(mWnd, msg, wParam, lParam, &dwmHitResult)) {
*aRetValue = dwmHitResult;
return true;
}
+ static const char kRestartPrefName[] = "toolkit.winRegisterApplicationRestart";
+
// (Large blocks of code should be broken out into OnEvent handlers.)
switch (msg) {
// WM_QUERYENDSESSION must be handled by all windows.
// Otherwise Windows thinks the window can just be killed at will.
case WM_QUERYENDSESSION:
if (sCanQuit == TRI_UNKNOWN)
{
// Ask if it's ok to quit, and store the answer until we
// get WM_ENDSESSION signaling the round is complete.
nsCOMPtr<nsIObserverService> obsServ =
mozilla::services::GetObserverService();
nsCOMPtr<nsISupportsPRBool> cancelQuit =
do_CreateInstance(NS_SUPPORTS_PRBOOL_CONTRACTID);
cancelQuit->SetData(false);
- obsServ->NotifyObservers(cancelQuit, "quit-application-requested", nullptr);
+
+ const char16_t* quitType = nullptr;
+ if (Preferences::GetBool(kRestartPrefName, true)) {
+ quitType = u"restart";
+ }
+
+ obsServ->NotifyObservers(cancelQuit, "quit-application-requested", quitType);
bool abortQuit;
cancelQuit->GetData(&abortQuit);
sCanQuit = abortQuit ? TRI_FALSE : TRI_TRUE;
}
*aRetValue = sCanQuit ? TRUE : FALSE;
result = true;
break;
@@ -5221,19 +5229,24 @@ nsWindow::ProcessMessage(UINT msg, WPARA
// Let's fake a shutdown sequence without actually closing windows etc.
// to avoid Windows killing us in the middle. A proper shutdown would
// require having a chance to pump some messages. Unfortunately
// Windows won't let us do that. Bug 212316.
nsCOMPtr<nsIObserverService> obsServ =
mozilla::services::GetObserverService();
const char16_t* context = u"shutdown-persist";
const char16_t* syncShutdown = u"syncShutdown";
+ const char16_t* quitType = nullptr;
+ if (Preferences::GetBool(kRestartPrefName, true)) {
+ quitType = u"restart";
+ }
+
obsServ->NotifyObservers(nullptr, "quit-application-granted", syncShutdown);
obsServ->NotifyObservers(nullptr, "quit-application-forced", nullptr);
- obsServ->NotifyObservers(nullptr, "quit-application", nullptr);
+ obsServ->NotifyObservers(nullptr, "quit-application", quitType);
obsServ->NotifyObservers(nullptr, "profile-change-net-teardown", context);
obsServ->NotifyObservers(nullptr, "profile-change-teardown", context);
obsServ->NotifyObservers(nullptr, "profile-before-change", context);
obsServ->NotifyObservers(nullptr, "profile-before-change-qm", context);
obsServ->NotifyObservers(nullptr, "profile-before-change-telemetry", context);
ExitThisProcessSafely();
}
sCanQuit = TRI_UNKNOWN;