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

Side by Side Diff: base/process/kill_win.cc

Issue 759903002: Upgrade the windows specific version of LaunchProcess to avoid raw handles. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix chrome build Created 6 years 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 unified diff | Download patch
« no previous file with comments | « base/process/kill_posix.cc ('k') | base/process/launch.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2013 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 "base/process/kill.h" 5 #include "base/process/kill.h"
6 6
7 #include <io.h> 7 #include <io.h>
8 #include <windows.h> 8 #include <windows.h>
9 9
10 #include "base/bind.h" 10 #include "base/bind.h"
(...skipping 20 matching lines...) Expand all
31 // seems to be common practice on Windows to test for it as an 31 // seems to be common practice on Windows to test for it as an
32 // indication that the task manager has killed something if the 32 // indication that the task manager has killed something if the
33 // process goes away. 33 // process goes away.
34 const DWORD kProcessKilledExitCode = 1; 34 const DWORD kProcessKilledExitCode = 1;
35 35
36 // Maximum amount of time (in milliseconds) to wait for the process to exit. 36 // Maximum amount of time (in milliseconds) to wait for the process to exit.
37 static const int kWaitInterval = 2000; 37 static const int kWaitInterval = 2000;
38 38
39 class TimerExpiredTask : public win::ObjectWatcher::Delegate { 39 class TimerExpiredTask : public win::ObjectWatcher::Delegate {
40 public: 40 public:
41 explicit TimerExpiredTask(ProcessHandle process); 41 explicit TimerExpiredTask(Process process);
42 ~TimerExpiredTask(); 42 ~TimerExpiredTask();
43 43
44 void TimedOut(); 44 void TimedOut();
45 45
46 // MessageLoop::Watcher ----------------------------------------------------- 46 // MessageLoop::Watcher -----------------------------------------------------
47 virtual void OnObjectSignaled(HANDLE object); 47 virtual void OnObjectSignaled(HANDLE object);
48 48
49 private: 49 private:
50 void KillProcess(); 50 void KillProcess();
51 51
52 // The process that we are watching. 52 // The process that we are watching.
53 ProcessHandle process_; 53 Process process_;
54 54
55 win::ObjectWatcher watcher_; 55 win::ObjectWatcher watcher_;
56 56
57 DISALLOW_COPY_AND_ASSIGN(TimerExpiredTask); 57 DISALLOW_COPY_AND_ASSIGN(TimerExpiredTask);
58 }; 58 };
59 59
60 TimerExpiredTask::TimerExpiredTask(ProcessHandle process) : process_(process) { 60 TimerExpiredTask::TimerExpiredTask(Process process) : process_(process.Pass()) {
61 watcher_.StartWatching(process_, this); 61 watcher_.StartWatching(process_.Handle(), this);
62 } 62 }
63 63
64 TimerExpiredTask::~TimerExpiredTask() { 64 TimerExpiredTask::~TimerExpiredTask() {
65 TimedOut(); 65 TimedOut();
66 DCHECK(!process_) << "Make sure to close the handle.";
67 } 66 }
68 67
69 void TimerExpiredTask::TimedOut() { 68 void TimerExpiredTask::TimedOut() {
70 if (process_) 69 if (process_.IsValid())
71 KillProcess(); 70 KillProcess();
72 } 71 }
73 72
74 void TimerExpiredTask::OnObjectSignaled(HANDLE object) { 73 void TimerExpiredTask::OnObjectSignaled(HANDLE object) {
75 // TODO(vadimt): Remove ScopedTracker below once crbug.com/418183 is fixed. 74 // TODO(vadimt): Remove ScopedTracker below once crbug.com/418183 is fixed.
76 tracked_objects::ScopedTracker tracking_profile( 75 tracked_objects::ScopedTracker tracking_profile(
77 FROM_HERE_WITH_EXPLICIT_FUNCTION("TimerExpiredTask_OnObjectSignaled")); 76 FROM_HERE_WITH_EXPLICIT_FUNCTION("TimerExpiredTask_OnObjectSignaled"));
78 77
79 CloseHandle(process_); 78 process_.Close();
80 process_ = NULL;
81 } 79 }
82 80
83 void TimerExpiredTask::KillProcess() { 81 void TimerExpiredTask::KillProcess() {
84 // Stop watching the process handle since we're killing it. 82 // Stop watching the process handle since we're killing it.
85 watcher_.StopWatching(); 83 watcher_.StopWatching();
86 84
87 // OK, time to get frisky. We don't actually care when the process 85 // OK, time to get frisky. We don't actually care when the process
88 // terminates. We just care that it eventually terminates, and that's what 86 // terminates. We just care that it eventually terminates, and that's what
89 // TerminateProcess should do for us. Don't check for the result code since 87 // TerminateProcess should do for us. Don't check for the result code since
90 // it fails quite often. This should be investigated eventually. 88 // it fails quite often. This should be investigated eventually.
91 base::KillProcess(process_, kProcessKilledExitCode, false); 89 base::KillProcess(process_.Handle(), kProcessKilledExitCode, false);
92 90
93 // Now, just cleanup as if the process exited normally. 91 // Now, just cleanup as if the process exited normally.
94 OnObjectSignaled(process_); 92 OnObjectSignaled(process_.Handle());
95 } 93 }
96 94
97 } // namespace 95 } // namespace
98 96
99 bool KillProcess(ProcessHandle process, int exit_code, bool wait) { 97 bool KillProcess(ProcessHandle process, int exit_code, bool wait) {
100 bool result = (TerminateProcess(process, exit_code) != FALSE); 98 bool result = (TerminateProcess(process, exit_code) != FALSE);
101 if (result && wait) { 99 if (result && wait) {
102 // The process may not end immediately due to pending I/O 100 // The process may not end immediately due to pending I/O
103 if (WAIT_OBJECT_0 != WaitForSingleObject(process, 60 * 1000)) 101 if (WAIT_OBJECT_0 != WaitForSingleObject(process, 60 * 1000))
104 DPLOG(ERROR) << "Error waiting for process exit"; 102 DPLOG(ERROR) << "Error waiting for process exit";
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after
234 bool CleanupProcesses(const FilePath::StringType& executable_name, 232 bool CleanupProcesses(const FilePath::StringType& executable_name,
235 base::TimeDelta wait, 233 base::TimeDelta wait,
236 int exit_code, 234 int exit_code,
237 const ProcessFilter* filter) { 235 const ProcessFilter* filter) {
238 if (WaitForProcessesToExit(executable_name, wait, filter)) 236 if (WaitForProcessesToExit(executable_name, wait, filter))
239 return true; 237 return true;
240 KillProcesses(executable_name, exit_code, filter); 238 KillProcesses(executable_name, exit_code, filter);
241 return false; 239 return false;
242 } 240 }
243 241
244 void EnsureProcessTerminated(ProcessHandle process) { 242 void EnsureProcessTerminated(Process process) {
245 DCHECK(process != GetCurrentProcess()); 243 DCHECK(!process.is_current());
246 244
247 // If already signaled, then we are done! 245 // If already signaled, then we are done!
248 if (WaitForSingleObject(process, 0) == WAIT_OBJECT_0) { 246 if (WaitForSingleObject(process.Handle(), 0) == WAIT_OBJECT_0) {
249 CloseHandle(process);
250 return; 247 return;
251 } 248 }
252 249
253 MessageLoop::current()->PostDelayedTask( 250 MessageLoop::current()->PostDelayedTask(
254 FROM_HERE, 251 FROM_HERE,
255 base::Bind(&TimerExpiredTask::TimedOut, 252 base::Bind(&TimerExpiredTask::TimedOut,
256 base::Owned(new TimerExpiredTask(process))), 253 base::Owned(new TimerExpiredTask(process.Pass()))),
257 base::TimeDelta::FromMilliseconds(kWaitInterval)); 254 base::TimeDelta::FromMilliseconds(kWaitInterval));
258 } 255 }
259 256
260 } // namespace base 257 } // namespace base
OLDNEW
« no previous file with comments | « base/process/kill_posix.cc ('k') | base/process/launch.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698