1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/ipc/chromium/src/chrome/common/process_watcher_win.cc Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,101 @@ 1.4 +// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. 1.5 +// Use of this source code is governed by a BSD-style license that can be 1.6 +// found in the LICENSE file. 1.7 + 1.8 +#include "chrome/common/process_watcher.h" 1.9 + 1.10 +#include "base/message_loop.h" 1.11 +#include "base/object_watcher.h" 1.12 +#include "base/sys_info.h" 1.13 +#include "chrome/common/env_vars.h" 1.14 +#include "chrome/common/result_codes.h" 1.15 + 1.16 +// Maximum amount of time (in milliseconds) to wait for the process to exit. 1.17 +static const int kWaitInterval = 2000; 1.18 + 1.19 +namespace { 1.20 + 1.21 +class TimerExpiredTask : public Task, public base::ObjectWatcher::Delegate { 1.22 + public: 1.23 + explicit TimerExpiredTask(base::ProcessHandle process) : process_(process) { 1.24 + watcher_.StartWatching(process_, this); 1.25 + } 1.26 + 1.27 + virtual ~TimerExpiredTask() { 1.28 + if (process_) { 1.29 + KillProcess(); 1.30 + DCHECK(!process_) << "Make sure to close the handle."; 1.31 + } 1.32 + } 1.33 + 1.34 + // Task --------------------------------------------------------------------- 1.35 + 1.36 + virtual void Run() { 1.37 + if (process_) 1.38 + KillProcess(); 1.39 + } 1.40 + 1.41 + // MessageLoop::Watcher ----------------------------------------------------- 1.42 + 1.43 + virtual void OnObjectSignaled(HANDLE object) { 1.44 + // When we're called from KillProcess, the ObjectWatcher may still be 1.45 + // watching. the process handle, so make sure it has stopped. 1.46 + watcher_.StopWatching(); 1.47 + 1.48 + base::CloseProcessHandle(process_); 1.49 + process_ = NULL; 1.50 + } 1.51 + 1.52 + private: 1.53 + void KillProcess() { 1.54 + if (base::SysInfo::HasEnvVar(env_vars::kHeadless)) { 1.55 + // If running the distributed tests, give the renderer a little time 1.56 + // to figure out that the channel is shutdown and unwind. 1.57 + if (WaitForSingleObject(process_, kWaitInterval) == WAIT_OBJECT_0) { 1.58 + OnObjectSignaled(process_); 1.59 + return; 1.60 + } 1.61 + } 1.62 + 1.63 + // OK, time to get frisky. We don't actually care when the process 1.64 + // terminates. We just care that it eventually terminates, and that's what 1.65 + // TerminateProcess should do for us. Don't check for the result code since 1.66 + // it fails quite often. This should be investigated eventually. 1.67 + TerminateProcess(process_, ResultCodes::HUNG); 1.68 + 1.69 + // Now, just cleanup as if the process exited normally. 1.70 + OnObjectSignaled(process_); 1.71 + } 1.72 + 1.73 + // The process that we are watching. 1.74 + base::ProcessHandle process_; 1.75 + 1.76 + base::ObjectWatcher watcher_; 1.77 + 1.78 + DISALLOW_EVIL_CONSTRUCTORS(TimerExpiredTask); 1.79 +}; 1.80 + 1.81 +} // namespace 1.82 + 1.83 +// static 1.84 +void ProcessWatcher::EnsureProcessTerminated(base::ProcessHandle process 1.85 + , bool force 1.86 +) { 1.87 + DCHECK(process != GetCurrentProcess()); 1.88 + 1.89 + if (!force) { 1.90 + WaitForSingleObject(process, INFINITE); 1.91 + base::CloseProcessHandle(process); 1.92 + return; 1.93 + } 1.94 + 1.95 + // If already signaled, then we are done! 1.96 + if (WaitForSingleObject(process, 0) == WAIT_OBJECT_0) { 1.97 + base::CloseProcessHandle(process); 1.98 + return; 1.99 + } 1.100 + 1.101 + MessageLoop::current()->PostDelayedTask(FROM_HERE, 1.102 + new TimerExpiredTask(process), 1.103 + kWaitInterval); 1.104 +}