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