Commit e9d42a32 authored by Zijie He's avatar Zijie He Committed by Commit Bot

Add GetAppOutputWithExitCode for Windows

GetAppOutputWithExitCode() is implemented for Windows, but not exposed.

Bug: 741926
Change-Id: Ib0518a8c6b28bc0946e6e820756e264d13028312
Reviewed-on: https://chromium-review.googlesource.com/571239Reviewed-by: default avatarDaniel Cheng <dcheng@chromium.org>
Commit-Queue: Zijie He <zijiehe@chromium.org>
Cr-Commit-Position: refs/heads/master@{#487230}
parent b28ad49b
...@@ -2123,6 +2123,7 @@ test("base_unittests") { ...@@ -2123,6 +2123,7 @@ test("base_unittests") {
"posix/file_descriptor_shuffle_unittest.cc", "posix/file_descriptor_shuffle_unittest.cc",
"posix/unix_domain_socket_linux_unittest.cc", "posix/unix_domain_socket_linux_unittest.cc",
"power_monitor/power_monitor_unittest.cc", "power_monitor/power_monitor_unittest.cc",
"process/launch_unittest_win.cc",
"process/memory_unittest.cc", "process/memory_unittest.cc",
"process/memory_unittest_mac.h", "process/memory_unittest_mac.h",
"process/memory_unittest_mac.mm", "process/memory_unittest_mac.mm",
......
...@@ -259,6 +259,13 @@ BASE_EXPORT bool GetAppOutput(const CommandLine& cl, std::string* output); ...@@ -259,6 +259,13 @@ BASE_EXPORT bool GetAppOutput(const CommandLine& cl, std::string* output);
BASE_EXPORT bool GetAppOutputAndError(const CommandLine& cl, BASE_EXPORT bool GetAppOutputAndError(const CommandLine& cl,
std::string* output); std::string* output);
// A version of |GetAppOutput()| which also returns the exit code of the
// executed command. Returns true if the application runs and exits cleanly. If
// this is the case the exit code of the application is available in
// |*exit_code|.
BASE_EXPORT bool GetAppOutputWithExitCode(const CommandLine& cl,
std::string* output, int* exit_code);
#if defined(OS_WIN) #if defined(OS_WIN)
// A Windows-specific version of GetAppOutput that takes a command line string // A Windows-specific version of GetAppOutput that takes a command line string
// instead of a CommandLine object. Useful for situations where you need to // instead of a CommandLine object. Useful for situations where you need to
...@@ -277,13 +284,6 @@ BASE_EXPORT bool GetAppOutput(const std::vector<std::string>& argv, ...@@ -277,13 +284,6 @@ BASE_EXPORT bool GetAppOutput(const std::vector<std::string>& argv,
// stderr. // stderr.
BASE_EXPORT bool GetAppOutputAndError(const std::vector<std::string>& argv, BASE_EXPORT bool GetAppOutputAndError(const std::vector<std::string>& argv,
std::string* output); std::string* output);
// A version of |GetAppOutput()| which also returns the exit code of the
// executed command. Returns true if the application runs and exits cleanly. If
// this is the case the exit code of the application is available in
// |*exit_code|.
BASE_EXPORT bool GetAppOutputWithExitCode(const CommandLine& cl,
std::string* output, int* exit_code);
#endif // defined(OS_POSIX) #endif // defined(OS_POSIX)
// If supported on the platform, and the user has sufficent rights, increase // If supported on the platform, and the user has sufficent rights, increase
......
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/process/launch.h"
#include "base/command_line.h"
#include "base/files/file_path.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace base {
TEST(LaunchWinTest, GetAppOutputWithExitCodeShouldReturnExitCode) {
CommandLine cl(FilePath(FILE_PATH_LITERAL("cmd")));
cl.AppendArg("/c");
cl.AppendArg("this-is-not-an-application");
std::string output;
int exit_code;
ASSERT_TRUE(GetAppOutputWithExitCode(cl, &output, &exit_code));
ASSERT_TRUE(output.empty());
ASSERT_EQ(1, exit_code);
}
} // namespace
...@@ -44,7 +44,8 @@ const DWORD kProcessKilledExitCode = 1; ...@@ -44,7 +44,8 @@ const DWORD kProcessKilledExitCode = 1;
bool GetAppOutputInternal(const StringPiece16& cl, bool GetAppOutputInternal(const StringPiece16& cl,
bool include_stderr, bool include_stderr,
std::string* output) { std::string* output,
int* exit_code) {
HANDLE out_read = nullptr; HANDLE out_read = nullptr;
HANDLE out_write = nullptr; HANDLE out_write = nullptr;
...@@ -122,11 +123,10 @@ bool GetAppOutputInternal(const StringPiece16& cl, ...@@ -122,11 +123,10 @@ bool GetAppOutputInternal(const StringPiece16& cl,
// Let's wait for the process to finish. // Let's wait for the process to finish.
WaitForSingleObject(proc_info.process_handle(), INFINITE); WaitForSingleObject(proc_info.process_handle(), INFINITE);
int exit_code;
base::TerminationStatus status = GetTerminationStatus( base::TerminationStatus status = GetTerminationStatus(
proc_info.process_handle(), &exit_code); proc_info.process_handle(), exit_code);
base::debug::GlobalActivityTracker::RecordProcessExitIfEnabled( base::debug::GlobalActivityTracker::RecordProcessExitIfEnabled(
proc_info.process_id(), exit_code); proc_info.process_id(), *exit_code);
return status != base::TERMINATION_STATUS_PROCESS_CRASHED && return status != base::TERMINATION_STATUS_PROCESS_CRASHED &&
status != base::TERMINATION_STATUS_ABNORMAL_TERMINATION; status != base::TERMINATION_STATUS_ABNORMAL_TERMINATION;
} }
...@@ -381,11 +381,21 @@ bool GetAppOutput(const CommandLine& cl, std::string* output) { ...@@ -381,11 +381,21 @@ bool GetAppOutput(const CommandLine& cl, std::string* output) {
} }
bool GetAppOutputAndError(const CommandLine& cl, std::string* output) { bool GetAppOutputAndError(const CommandLine& cl, std::string* output) {
return GetAppOutputInternal(cl.GetCommandLineString(), true, output); int exit_code;
return GetAppOutputInternal(
cl.GetCommandLineString(), true, output, &exit_code);
}
bool GetAppOutputWithExitCode(const CommandLine& cl,
std::string* output,
int* exit_code) {
return GetAppOutputInternal(
cl.GetCommandLineString(), false, output, exit_code);
} }
bool GetAppOutput(const StringPiece16& cl, std::string* output) { bool GetAppOutput(const StringPiece16& cl, std::string* output) {
return GetAppOutputInternal(cl, false, output); int exit_code;
return GetAppOutputInternal(cl, false, output, &exit_code);
} }
void RaiseProcessToHighPriority() { void RaiseProcessToHighPriority() {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment