diff options
author | jam@chromium.org <jam@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-03-17 06:06:12 +0000 |
---|---|---|
committer | jam@chromium.org <jam@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-03-17 06:06:12 +0000 |
commit | 4dd5793c22cc4e9991055adcb4e7163fbba8c246 (patch) | |
tree | fb2307f59725643e4c4ce8d4f215188fd8920bdd /content/common/process_watcher_win.cc | |
parent | 7e06cb9f6a1bfd890eebd08195a903a58912cdc9 (diff) | |
download | chromium_src-4dd5793c22cc4e9991055adcb4e7163fbba8c246.zip chromium_src-4dd5793c22cc4e9991055adcb4e7163fbba8c246.tar.gz chromium_src-4dd5793c22cc4e9991055adcb4e7163fbba8c246.tar.bz2 |
Move the remaining files in chrome\common to content\common.
TBR=avi
Review URL: http://codereview.chromium.org/6672070
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@78516 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'content/common/process_watcher_win.cc')
-rw-r--r-- | content/common/process_watcher_win.cc | 84 |
1 files changed, 84 insertions, 0 deletions
diff --git a/content/common/process_watcher_win.cc b/content/common/process_watcher_win.cc new file mode 100644 index 0000000..c1ccbf8 --- /dev/null +++ b/content/common/process_watcher_win.cc @@ -0,0 +1,84 @@ +// Copyright (c) 2010 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "content/common/process_watcher.h" + +#include "base/scoped_ptr.h" +#include "base/message_loop.h" +#include "base/win/object_watcher.h" +#include "content/common/result_codes.h" + +// Maximum amount of time (in milliseconds) to wait for the process to exit. +static const int kWaitInterval = 2000; + +namespace { + +class TimerExpiredTask : public Task, + public base::win::ObjectWatcher::Delegate { + public: + explicit TimerExpiredTask(base::ProcessHandle process) : process_(process) { + watcher_.StartWatching(process_, this); + } + + virtual ~TimerExpiredTask() { + if (process_) { + KillProcess(); + DCHECK(!process_) << "Make sure to close the handle."; + } + } + + // Task --------------------------------------------------------------------- + + virtual void Run() { + if (process_) + KillProcess(); + } + + // MessageLoop::Watcher ----------------------------------------------------- + + virtual void OnObjectSignaled(HANDLE object) { + // When we're called from KillProcess, the ObjectWatcher may still be + // watching. the process handle, so make sure it has stopped. + watcher_.StopWatching(); + + CloseHandle(process_); + process_ = NULL; + } + + private: + void KillProcess() { + // OK, time to get frisky. We don't actually care when the process + // terminates. We just care that it eventually terminates, and that's what + // TerminateProcess should do for us. Don't check for the result code since + // it fails quite often. This should be investigated eventually. + base::KillProcess(process_, ResultCodes::HUNG, false); + + // Now, just cleanup as if the process exited normally. + OnObjectSignaled(process_); + } + + // The process that we are watching. + base::ProcessHandle process_; + + base::win::ObjectWatcher watcher_; + + DISALLOW_COPY_AND_ASSIGN(TimerExpiredTask); +}; + +} // namespace + +// static +void ProcessWatcher::EnsureProcessTerminated(base::ProcessHandle process) { + DCHECK(process != GetCurrentProcess()); + + // If already signaled, then we are done! + if (WaitForSingleObject(process, 0) == WAIT_OBJECT_0) { + CloseHandle(process); + return; + } + + MessageLoop::current()->PostDelayedTask(FROM_HERE, + new TimerExpiredTask(process), + kWaitInterval); +} |