diff --git a/browser/app/winlauncher/LauncherProcessWin.cpp b/browser/app/winlauncher/LauncherProcessWin.cpp index 35f2cb53506b..e4afa97d8594 100644 --- a/browser/app/winlauncher/LauncherProcessWin.cpp +++ b/browser/app/winlauncher/LauncherProcessWin.cpp @@ -17,6 +17,7 @@ #include "mozilla/Maybe.h" #include "mozilla/SafeMode.h" #include "mozilla/UniquePtr.h" +#include "mozilla/WindowsConsole.h" #include "mozilla/WindowsVersion.h" #include "mozilla/WinHeaderOnlyUtils.h" #include "nsWindowsHelpers.h" @@ -219,6 +220,8 @@ Maybe LauncherMain(int& argc, wchar_t* argv[], } } + mozilla::UseParentConsole(); + if (!SetArgv0ToFullBinaryPath(argv)) { HandleLauncherError(LAUNCHER_ERROR_GENERIC()); return Nothing(); diff --git a/browser/app/winlauncher/moz.build b/browser/app/winlauncher/moz.build index 1b74cca4d003..ec616ff1c35a 100644 --- a/browser/app/winlauncher/moz.build +++ b/browser/app/winlauncher/moz.build @@ -31,6 +31,7 @@ TEST_DIRS += [ if CONFIG['MOZ_LAUNCHER_PROCESS']: UNIFIED_SOURCES += [ '/toolkit/xre/LauncherRegistryInfo.cpp', + '/widget/windows/WindowsConsole.cpp', ] for var in ('MOZ_APP_BASENAME', 'MOZ_APP_VENDOR'): DEFINES[var] = '"%s"' % CONFIG[var] diff --git a/toolkit/xre/nsAppRunner.h b/toolkit/xre/nsAppRunner.h index 916580f7cf24..310e30d8a692 100644 --- a/toolkit/xre/nsAppRunner.h +++ b/toolkit/xre/nsAppRunner.h @@ -8,6 +8,7 @@ #ifdef XP_WIN # include +# include "mozilla/WindowsConsole.h" #else # include #endif @@ -100,7 +101,6 @@ void OverrideDefaultLocaleIfNeeded(); void MozExpectedExit(); #ifdef XP_WIN -void UseParentConsole(); BOOL WinLaunchChild(const wchar_t* exePath, int argc, char** argv, HANDLE userToken = nullptr, HANDLE* hProcess = nullptr); diff --git a/toolkit/xre/nsEmbedFunctions.cpp b/toolkit/xre/nsEmbedFunctions.cpp index 5976b4bfc02f..9ff6077435fc 100644 --- a/toolkit/xre/nsEmbedFunctions.cpp +++ b/toolkit/xre/nsEmbedFunctions.cpp @@ -80,6 +80,7 @@ #include "mozilla/ipc/TestShellParent.h" #include "mozilla/ipc/XPCShellEnvironment.h" +#include "mozilla/WindowsConsole.h" #include "mozilla/WindowsDllBlocklist.h" #include "GMPProcessChild.h" @@ -381,19 +382,7 @@ nsresult XRE_InitChildProcess(int aArgc, char* aArgv[], // Try to attach console to the parent process. // It will succeed when the parent process is a command line, // so that stdio will be displayed in it. - if (AttachConsole(ATTACH_PARENT_PROCESS)) { - // Change std handles to refer to new console handles. - // Before doing so, ensure that stdout/stderr haven't been - // redirected to a valid file - if (_fileno(stdout) == -1 || _get_osfhandle(fileno(stdout)) == -1) - freopen("CONOUT$", "w", stdout); - // Merge stderr into CONOUT$ since there isn't any `CONERR$`. - // http://msdn.microsoft.com/en-us/library/windows/desktop/ms683231%28v=vs.85%29.aspx - if (_fileno(stderr) == -1 || _get_osfhandle(fileno(stderr)) == -1) - freopen("CONOUT$", "w", stderr); - if (_fileno(stdin) == -1 || _get_osfhandle(fileno(stdin)) == -1) - freopen("CONIN$", "r", stdin); - } + UseParentConsole(); # if defined(MOZ_SANDBOX) if (aChildData->sandboxTargetServices) { diff --git a/toolkit/xre/nsNativeAppSupportWin.cpp b/toolkit/xre/nsNativeAppSupportWin.cpp index 7735257d7e0a..881ba02ccd64 100644 --- a/toolkit/xre/nsNativeAppSupportWin.cpp +++ b/toolkit/xre/nsNativeAppSupportWin.cpp @@ -6,8 +6,7 @@ #include "nsNativeAppSupportBase.h" #include "nsNativeAppSupportWin.h" -#include -#include +#include "mozilla/WindowsConsole.h" using namespace mozilla; @@ -26,25 +25,6 @@ class nsNativeAppSupportWin : public nsNativeAppSupportBase { ~nsNativeAppSupportWin() {} }; // nsNativeAppSupportWin -void UseParentConsole() { - if (AttachConsole(ATTACH_PARENT_PROCESS)) { - // Redirect the standard streams to the existing console, but - // only if they haven't been redirected to a valid file. - // Visual Studio's _fileno() returns -2 for the standard - // streams if they aren't associated with an output stream. - if (_fileno(stdout) == -2) { - freopen("CONOUT$", "w", stdout); - } - // There is no CONERR$, so use CONOUT$ for stderr as well. - if (_fileno(stderr) == -2) { - freopen("CONOUT$", "w", stderr); - } - if (_fileno(stdin) == -2) { - freopen("CONIN$", "r", stdin); - } - } -} - void nsNativeAppSupportWin::CheckConsole() { for (int i = 1; i < gArgc; ++i) { if (strcmp("-console", gArgv[i]) == 0 || diff --git a/widget/windows/WindowsConsole.cpp b/widget/windows/WindowsConsole.cpp new file mode 100644 index 000000000000..eafb454109ec --- /dev/null +++ b/widget/windows/WindowsConsole.cpp @@ -0,0 +1,33 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#include "WindowsConsole.h" + +#include +#include + +namespace mozilla { + +// This code attaches the process to the appropriate console. +void UseParentConsole() { + if (AttachConsole(ATTACH_PARENT_PROCESS)) { + // Redirect the standard streams to the existing console, but + // only if they haven't been redirected to a valid file. + // Visual Studio's _fileno() returns -2 for the standard + // streams if they aren't associated with an output stream. + if (_fileno(stdout) == -2) { + freopen("CONOUT$", "w", stdout); + } + // There is no CONERR$, so use CONOUT$ for stderr as well. + if (_fileno(stderr) == -2) { + freopen("CONOUT$", "w", stderr); + } + if (_fileno(stdin) == -2) { + freopen("CONIN$", "r", stdin); + } + } +} + +} // namespace mozilla diff --git a/widget/windows/WindowsConsole.h b/widget/windows/WindowsConsole.h new file mode 100644 index 000000000000..4b8ecf182310 --- /dev/null +++ b/widget/windows/WindowsConsole.h @@ -0,0 +1,16 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#ifndef mozilla_WindowsConsole_h +#define mozilla_WindowsConsole_h + +namespace mozilla { + +// This code attaches the process to the appropriate console. +void UseParentConsole(); + +} // namespace mozilla + +#endif // mozilla_WindowsConsole_h diff --git a/widget/windows/moz.build b/widget/windows/moz.build index edb683ebd85d..9a924b934d7c 100644 --- a/widget/windows/moz.build +++ b/widget/windows/moz.build @@ -19,6 +19,7 @@ EXPORTS += [ ] EXPORTS.mozilla += [ + 'WindowsConsole.h', 'WinHeaderOnlyUtils.h', ] @@ -75,6 +76,7 @@ UNIFIED_SOURCES += [ 'WidgetTraceEvent.cpp', 'WinCompositorWindowThread.cpp', 'WindowHook.cpp', + 'WindowsConsole.cpp', 'WinIMEHandler.cpp', 'WinPointerEvents.cpp', 'WinTaskbar.cpp',