Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(3118)

Unified Diff: chrome/common/process_watcher.cc

Issue 18446: Porting in chrome/ (Closed)
Patch Set: last fixes Created 11 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « chrome/common/common.scons ('k') | chrome/common/security_filter_peer.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: chrome/common/process_watcher.cc
diff --git a/chrome/common/process_watcher.cc b/chrome/common/process_watcher.cc
index b43e2db601e7fb6799e0ec780cbb433aa8033925..ceabaea2704a9e1676aaca7ab716e53b86eca7a8 100644
--- a/chrome/common/process_watcher.cc
+++ b/chrome/common/process_watcher.cc
@@ -4,9 +4,13 @@
#include "chrome/common/process_watcher.h"
+#include "base/basictypes.h"
#include "base/message_loop.h"
-#include "base/object_watcher.h"
+#include "base/process.h"
+#include "base/process_util.h"
#include "base/sys_info.h"
+#include "base/timer.h"
+#include "base/worker_pool.h"
#include "chrome/app/result_codes.h"
#include "chrome/common/env_vars.h"
@@ -15,80 +19,72 @@ static const int kWaitInterval = 2000;
namespace {
-class TimerExpiredTask : public Task, public base::ObjectWatcher::Delegate {
+class TerminatorTask : public Task {
public:
- explicit TimerExpiredTask(base::ProcessHandle process) : process_(process) {
- watcher_.StartWatching(process_, this);
+ explicit TerminatorTask(base::ProcessHandle process) : process_(process) {
+ timer_.Start(base::TimeDelta::FromMilliseconds(kWaitInterval),
+ this, &TerminatorTask::KillProcess);
}
- virtual ~TimerExpiredTask() {
+ virtual ~TerminatorTask() {
if (process_) {
KillProcess();
- DCHECK(!process_) << "Make sure to close the handle.";
+ DCHECK(!process_);
}
}
- // Task ---------------------------------------------------------------------
-
virtual void Run() {
+ base::WaitForSingleProcess(process_, kWaitInterval);
+ timer_.Stop();
if (process_)
KillProcess();
}
- // MessageLoop::Watcher -----------------------------------------------------
-
- virtual void OnObjectSignaled(HANDLE object) {
- // When we're called from KillProcess, the ObjectWatcher may still be
- // watching. the process handle, so make sure it has stopped.
- watcher_.StopWatching();
-
- CloseHandle(process_);
- process_ = NULL;
- }
-
private:
void KillProcess() {
if (base::SysInfo::HasEnvVar(env_vars::kHeadless)) {
- // If running the distributed tests, give the renderer a little time
- // to figure out that the channel is shutdown and unwind.
- if (WaitForSingleObject(process_, kWaitInterval) == WAIT_OBJECT_0) {
- OnObjectSignaled(process_);
- return;
- }
+ // If running the distributed tests, give the renderer a little time
+ // to figure out that the channel is shutdown and unwind.
+ if (base::WaitForSingleProcess(process_, kWaitInterval)) {
+ Cleanup();
+ return;
+ }
}
// OK, time to get frisky. We don't actually care when the process
- // terminates. We just care that it eventually terminates, and that's what
- // TerminateProcess should do for us. Don't check for the result code since
- // it fails quite often. This should be investigated eventually.
- TerminateProcess(process_, ResultCodes::HUNG);
+ // terminates. We just care that it eventually terminates.
+ base::KillProcess(base::Process(process_).pid(),
+ ResultCodes::HUNG,
+ false /* don't wait */);
- // Now, just cleanup as if the process exited normally.
- OnObjectSignaled(process_);
+ Cleanup();
+ }
+
+ void Cleanup() {
+ timer_.Stop();
+ base::CloseProcessHandle(process_);
+ process_ = NULL;
}
// The process that we are watching.
base::ProcessHandle process_;
- base::ObjectWatcher watcher_;
+ base::OneShotTimer<TerminatorTask> timer_;
- DISALLOW_EVIL_CONSTRUCTORS(TimerExpiredTask);
+ DISALLOW_COPY_AND_ASSIGN(TerminatorTask);
};
} // namespace
// static
void ProcessWatcher::EnsureProcessTerminated(base::ProcessHandle process) {
- DCHECK(process != GetCurrentProcess());
+ DCHECK(base::GetProcId(process) != base::GetCurrentProcId());
- // If already signaled, then we are done!
- if (WaitForSingleObject(process, 0) == WAIT_OBJECT_0) {
- CloseHandle(process);
+ // Check if the process has already exited.
+ if (base::WaitForSingleProcess(process, 0)) {
+ base::CloseProcessHandle(process);
return;
}
- MessageLoop::current()->PostDelayedTask(FROM_HERE,
- new TimerExpiredTask(process),
- kWaitInterval);
+ WorkerPool::PostTask(FROM_HERE, new TerminatorTask(process), true);
}
-
« no previous file with comments | « chrome/common/common.scons ('k') | chrome/common/security_filter_peer.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698