OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/common/process_watcher.h" | 5 #include "chrome/common/process_watcher.h" |
6 | 6 |
7 #include "base/basictypes.h" | |
8 #include "base/message_loop.h" | 7 #include "base/message_loop.h" |
9 #include "base/process.h" | 8 #include "base/object_watcher.h" |
10 #include "base/process_util.h" | |
11 #include "base/sys_info.h" | 9 #include "base/sys_info.h" |
12 #include "base/timer.h" | |
13 #include "base/worker_pool.h" | |
14 #include "chrome/app/result_codes.h" | 10 #include "chrome/app/result_codes.h" |
15 #include "chrome/common/env_vars.h" | 11 #include "chrome/common/env_vars.h" |
16 | 12 |
17 // Maximum amount of time (in milliseconds) to wait for the process to exit. | 13 // Maximum amount of time (in milliseconds) to wait for the process to exit. |
18 static const int kWaitInterval = 2000; | 14 static const int kWaitInterval = 2000; |
19 | 15 |
20 namespace { | 16 namespace { |
21 | 17 |
22 class TerminatorTask : public Task { | 18 class TimerExpiredTask : public Task, public base::ObjectWatcher::Delegate { |
23 public: | 19 public: |
24 explicit TerminatorTask(base::ProcessHandle process) : process_(process) { | 20 explicit TimerExpiredTask(base::ProcessHandle process) : process_(process) { |
25 timer_.Start(base::TimeDelta::FromMilliseconds(kWaitInterval), | 21 watcher_.StartWatching(process_, this); |
26 this, &TerminatorTask::KillProcess); | |
27 } | 22 } |
28 | 23 |
29 virtual ~TerminatorTask() { | 24 virtual ~TimerExpiredTask() { |
30 if (process_) { | 25 if (process_) { |
31 KillProcess(); | 26 KillProcess(); |
32 DCHECK(!process_); | 27 DCHECK(!process_) << "Make sure to close the handle."; |
33 } | 28 } |
34 } | 29 } |
35 | 30 |
| 31 // Task --------------------------------------------------------------------- |
| 32 |
36 virtual void Run() { | 33 virtual void Run() { |
37 base::WaitForSingleProcess(process_, kWaitInterval); | |
38 timer_.Stop(); | |
39 if (process_) | 34 if (process_) |
40 KillProcess(); | 35 KillProcess(); |
41 } | 36 } |
42 | 37 |
| 38 // MessageLoop::Watcher ----------------------------------------------------- |
| 39 |
| 40 virtual void OnObjectSignaled(HANDLE object) { |
| 41 // When we're called from KillProcess, the ObjectWatcher may still be |
| 42 // watching. the process handle, so make sure it has stopped. |
| 43 watcher_.StopWatching(); |
| 44 |
| 45 CloseHandle(process_); |
| 46 process_ = NULL; |
| 47 } |
| 48 |
43 private: | 49 private: |
44 void KillProcess() { | 50 void KillProcess() { |
45 if (base::SysInfo::HasEnvVar(env_vars::kHeadless)) { | 51 if (base::SysInfo::HasEnvVar(env_vars::kHeadless)) { |
46 // If running the distributed tests, give the renderer a little time | 52 // If running the distributed tests, give the renderer a little time |
47 // to figure out that the channel is shutdown and unwind. | 53 // to figure out that the channel is shutdown and unwind. |
48 if (base::WaitForSingleProcess(process_, kWaitInterval)) { | 54 if (WaitForSingleObject(process_, kWaitInterval) == WAIT_OBJECT_0) { |
49 Cleanup(); | 55 OnObjectSignaled(process_); |
50 return; | 56 return; |
51 } | 57 } |
52 } | 58 } |
53 | 59 |
54 // OK, time to get frisky. We don't actually care when the process | 60 // OK, time to get frisky. We don't actually care when the process |
55 // terminates. We just care that it eventually terminates. | 61 // terminates. We just care that it eventually terminates, and that's what |
56 base::KillProcess(base::Process(process_).pid(), | 62 // TerminateProcess should do for us. Don't check for the result code since |
57 ResultCodes::HUNG, | 63 // it fails quite often. This should be investigated eventually. |
58 false /* don't wait */); | 64 TerminateProcess(process_, ResultCodes::HUNG); |
59 | 65 |
60 Cleanup(); | 66 // Now, just cleanup as if the process exited normally. |
61 } | 67 OnObjectSignaled(process_); |
62 | |
63 void Cleanup() { | |
64 timer_.Stop(); | |
65 base::CloseProcessHandle(process_); | |
66 process_ = NULL; | |
67 } | 68 } |
68 | 69 |
69 // The process that we are watching. | 70 // The process that we are watching. |
70 base::ProcessHandle process_; | 71 base::ProcessHandle process_; |
71 | 72 |
72 base::OneShotTimer<TerminatorTask> timer_; | 73 base::ObjectWatcher watcher_; |
73 | 74 |
74 DISALLOW_COPY_AND_ASSIGN(TerminatorTask); | 75 DISALLOW_EVIL_CONSTRUCTORS(TimerExpiredTask); |
75 }; | 76 }; |
76 | 77 |
77 } // namespace | 78 } // namespace |
78 | 79 |
79 // static | 80 // static |
80 void ProcessWatcher::EnsureProcessTerminated(base::ProcessHandle process) { | 81 void ProcessWatcher::EnsureProcessTerminated(base::ProcessHandle process) { |
81 DCHECK(base::GetProcId(process) != base::GetCurrentProcId()); | 82 DCHECK(process != GetCurrentProcess()); |
82 | 83 |
83 // Check if the process has already exited. | 84 // If already signaled, then we are done! |
84 if (base::WaitForSingleProcess(process, 0)) { | 85 if (WaitForSingleObject(process, 0) == WAIT_OBJECT_0) { |
85 base::CloseProcessHandle(process); | 86 CloseHandle(process); |
86 return; | 87 return; |
87 } | 88 } |
88 | 89 |
89 WorkerPool::PostTask(FROM_HERE, new TerminatorTask(process), true); | 90 MessageLoop::current()->PostDelayedTask(FROM_HERE, |
| 91 new TimerExpiredTask(process), |
| 92 kWaitInterval); |
90 } | 93 } |
| 94 |
OLD | NEW |