OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2009 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 "chrome/test/chrome_process_util.h" |
| 6 |
| 7 #include <vector> |
| 8 |
| 9 #include "base/process_util.h" |
| 10 #include "base/time.h" |
| 11 #include "chrome/common/result_codes.h" |
| 12 |
| 13 using base::Time; |
| 14 using base::TimeDelta; |
| 15 |
| 16 void TerminateAllChromeProcesses(const FilePath& data_dir) { |
| 17 // Total time the function will wait for chrome processes |
| 18 // to terminate after it told them to do so. |
| 19 const TimeDelta kExitTimeout = TimeDelta::FromMilliseconds(5000); |
| 20 |
| 21 ChromeProcessList process_pids(GetRunningChromeProcesses(data_dir)); |
| 22 |
| 23 std::vector<base::ProcessHandle> handles; |
| 24 { |
| 25 ChromeProcessList::const_iterator it; |
| 26 for (it = process_pids.begin(); it != process_pids.end(); ++it) { |
| 27 base::ProcessHandle handle; |
| 28 // Ignore processes for which we can't open the handle. We don't guarantee |
| 29 // that all processes will terminate, only try to do so. |
| 30 if (base::OpenProcessHandle(*it, &handle)) |
| 31 handles.push_back(handle); |
| 32 } |
| 33 } |
| 34 |
| 35 std::vector<base::ProcessHandle>::const_iterator it; |
| 36 for (it = handles.begin(); it != handles.end(); ++it) |
| 37 base::KillProcess(*it, ResultCodes::TASKMAN_KILL, false); |
| 38 |
| 39 const Time start = Time::Now(); |
| 40 for (it = handles.begin(); |
| 41 it != handles.end() && Time::Now() - start < kExitTimeout; |
| 42 ++it) { |
| 43 // TODO(phajdan.jr): Fix int/int64 problems with TimeDelta::InMilliseconds. |
| 44 int wait_time_ms = static_cast<int>((Time::Now() - start).InMilliseconds()); |
| 45 base::WaitForSingleProcess(*it, wait_time_ms); |
| 46 } |
| 47 |
| 48 for (it = handles.begin(); it != handles.end(); ++it) |
| 49 base::CloseProcessHandle(*it); |
| 50 } |
OLD | NEW |