Commit 126fd582 authored by rvargas's avatar rvargas Committed by Commit bot

Base: Add Process::WaitForExit and WaitForExitWithTimeout.

The new methods are meant to replace similar functions from kill.h

BUG=417532

Review URL: https://codereview.chromium.org/791503002

Cr-Commit-Position: refs/heads/master@{#308009}
parent c2c29d78
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
#include "base/basictypes.h" #include "base/basictypes.h"
#include "base/move.h" #include "base/move.h"
#include "base/process/process_handle.h" #include "base/process/process_handle.h"
#include "base/time/time.h"
#include "build/build_config.h" #include "build/build_config.h"
#if defined(OS_WIN) #if defined(OS_WIN)
...@@ -81,6 +82,13 @@ class BASE_EXPORT Process { ...@@ -81,6 +82,13 @@ class BASE_EXPORT Process {
// NOTE: On POSIX |result_code| is ignored. // NOTE: On POSIX |result_code| is ignored.
void Terminate(int result_code); void Terminate(int result_code);
// Waits for the process to exit. Returns true on success.
// On POSIX, if the process has been signaled then |exit_code| is set to -1.
bool WaitForExit(int* exit_code);
// Same as WaitForExit() but only waits for up to |timeout|.
bool WaitForExitWithTimeout(TimeDelta timeout, int* exit_code);
// A process is backgrounded when it's priority is lower than normal. // A process is backgrounded when it's priority is lower than normal.
// Return true if this process is backgrounded, false otherwise. // Return true if this process is backgrounded, false otherwise.
bool IsProcessBackgrounded() const; bool IsProcessBackgrounded() const;
......
...@@ -89,6 +89,18 @@ void Process::Terminate(int result_code) { ...@@ -89,6 +89,18 @@ void Process::Terminate(int result_code) {
KillProcess(process_, result_code, false); KillProcess(process_, result_code, false);
} }
bool Process::WaitForExit(int* exit_code) {
// TODO(rvargas) crbug.com/417532: Remove this constant.
const int kNoTimeout = -1;
return WaitForExitWithTimeout(TimeDelta::FromMilliseconds(kNoTimeout),
exit_code);
}
bool Process::WaitForExitWithTimeout(TimeDelta timeout, int* exit_code) {
// TODO(rvargas) crbug.com/417532: Move the implementation here.
return base::WaitForExitCodeWithTimeout(Handle(), exit_code, timeout);
}
#if !defined(OS_LINUX) #if !defined(OS_LINUX)
bool Process::IsProcessBackgrounded() const { bool Process::IsProcessBackgrounded() const {
// See SetProcessBackgrounded(). // See SetProcessBackgrounded().
......
...@@ -135,6 +135,34 @@ TEST_F(ProcessTest, Terminate) { ...@@ -135,6 +135,34 @@ TEST_F(ProcessTest, Terminate) {
#endif #endif
} }
MULTIPROCESS_TEST_MAIN(FastSleepyChildProcess) {
PlatformThread::Sleep(TestTimeouts::tiny_timeout() * 10);
return 0;
}
TEST_F(ProcessTest, WaitForExit) {
Process process(SpawnChild("FastSleepyChildProcess"));
ASSERT_TRUE(process.IsValid());
const int kDummyExitCode = 42;
int exit_code = kDummyExitCode;
EXPECT_TRUE(process.WaitForExit(&exit_code));
EXPECT_EQ(0, exit_code);
}
TEST_F(ProcessTest, WaitForExitWithTimeout) {
Process process(SpawnChild("SleepyChildProcess"));
ASSERT_TRUE(process.IsValid());
const int kDummyExitCode = 42;
int exit_code = kDummyExitCode;
TimeDelta timeout = TestTimeouts::tiny_timeout();
EXPECT_FALSE(process.WaitForExitWithTimeout(timeout, &exit_code));
EXPECT_EQ(kDummyExitCode, exit_code);
process.Terminate(kDummyExitCode);
}
// Ensure that the priority of a process is restored correctly after // Ensure that the priority of a process is restored correctly after
// backgrounding and restoring. // backgrounding and restoring.
// Note: a platform may not be willing or able to lower the priority of // Note: a platform may not be willing or able to lower the priority of
......
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
#include "base/logging.h" #include "base/logging.h"
#include "base/memory/scoped_ptr.h" #include "base/memory/scoped_ptr.h"
#include "base/process/kill.h"
#include "base/win/windows_version.h" #include "base/win/windows_version.h"
namespace base { namespace base {
...@@ -110,6 +111,18 @@ void Process::Terminate(int result_code) { ...@@ -110,6 +111,18 @@ void Process::Terminate(int result_code) {
terminate_process(Handle(), result_code); terminate_process(Handle(), result_code);
} }
bool Process::WaitForExit(int* exit_code) {
return WaitForExitWithTimeout(TimeDelta::FromMilliseconds(INFINITE),
exit_code);
}
bool Process::WaitForExitWithTimeout(TimeDelta timeout, int* exit_code) {
// TODO(rvargas) crbug.com/417532: Move the implementation here.
if (timeout > TimeDelta::FromMilliseconds(INFINITE))
timeout = TimeDelta::FromMilliseconds(INFINITE);
return base::WaitForExitCodeWithTimeout(Handle(), exit_code, timeout);
}
bool Process::IsProcessBackgrounded() const { bool Process::IsProcessBackgrounded() const {
DCHECK(IsValid()); DCHECK(IsValid());
DWORD priority = GetPriority(); DWORD priority = GetPriority();
......
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