| 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 "base/win/process_monitor.h" |
| 6 |
| 7 #include "base/location.h" |
| 8 #include "base/logging.h" |
| 9 #include "base/message_loop_proxy.h" |
| 10 |
| 11 namespace base { |
| 12 namespace win { |
| 13 |
| 14 ProcessMonitor::ProcessMonitor() : monitoring_job_completion_port_(NULL), |
| 15 stop_job_monitoring_(false), |
| 16 monitoring_job_(NULL) { |
| 17 } |
| 18 |
| 19 ProcessMonitor::~ProcessMonitor() { |
| 20 Stop(); |
| 21 if (monitoring_job_) { |
| 22 ::CloseHandle(monitoring_job_); |
| 23 monitoring_job_ = NULL; |
| 24 } |
| 25 } |
| 26 |
| 27 void ProcessMonitor::Initialize(HANDLE process_handle, |
| 28 const Closure& callback) { |
| 29 DCHECK(monitoring_job_ == NULL); |
| 30 |
| 31 monitoring_job_ = ::CreateJobObject(NULL, NULL); |
| 32 if (monitoring_job_ == NULL) { |
| 33 DPLOG(FATAL) << "Failed to create process monitoring job: "; |
| 34 return; |
| 35 } |
| 36 |
| 37 if (!PlatformThread::Create(0, this, &thread_)) { |
| 38 NOTREACHED(); |
| 39 return; |
| 40 } |
| 41 |
| 42 callback_ = callback; |
| 43 callback_message_loop_ = MessageLoopProxy::current(); |
| 44 if (!AssignProcessToJobObject(monitoring_job_, process_handle)) { |
| 45 DPLOG(FATAL) << "Couldn't assign process to job: "; |
| 46 } |
| 47 } |
| 48 |
| 49 void ProcessMonitor::ThreadMain() { |
| 50 PlatformThread::SetName("Process monitor thread"); |
| 51 |
| 52 if (!monitoring_job_) { |
| 53 DLOG(FATAL) << "Invalid job information"; |
| 54 return; |
| 55 } |
| 56 |
| 57 DCHECK(monitoring_job_completion_port_ == NULL); |
| 58 |
| 59 monitoring_job_completion_port_ = |
| 60 ::CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, |
| 61 reinterpret_cast<ULONG_PTR>(monitoring_job_), 0); |
| 62 DCHECK(monitoring_job_completion_port_ != NULL); |
| 63 |
| 64 JOBOBJECT_ASSOCIATE_COMPLETION_PORT job_completion_port = {0}; |
| 65 job_completion_port.CompletionKey = monitoring_job_; |
| 66 job_completion_port.CompletionPort = monitoring_job_completion_port_; |
| 67 |
| 68 if (!SetInformationJobObject(monitoring_job_, |
| 69 JobObjectAssociateCompletionPortInformation, |
| 70 &job_completion_port, |
| 71 sizeof(job_completion_port))) { |
| 72 DPLOG(FATAL) << "Failed to associate completion port with job object: "; |
| 73 return; |
| 74 } |
| 75 |
| 76 while (!stop_job_monitoring_) { |
| 77 unsigned long job_event = 0; |
| 78 ULONG_PTR completion_key = 0; |
| 79 LPOVERLAPPED overlapped = NULL; |
| 80 |
| 81 if (::GetQueuedCompletionStatus( |
| 82 monitoring_job_completion_port_, &job_event, |
| 83 &completion_key, &overlapped, INFINITE)) { |
| 84 if (job_event == JOB_OBJECT_MSG_ACTIVE_PROCESS_ZERO) { |
| 85 DVLOG(1) << "All processes in the monitoring job have exited."; |
| 86 callback_message_loop_->PostTask(FROM_HERE, callback_); |
| 87 } |
| 88 } |
| 89 } |
| 90 } |
| 91 |
| 92 void ProcessMonitor::Stop() { |
| 93 stop_job_monitoring_ = true; |
| 94 ::PostQueuedCompletionStatus( |
| 95 monitoring_job_completion_port_, JOB_OBJECT_MSG_END_OF_JOB_TIME, |
| 96 reinterpret_cast<ULONG_PTR>(monitoring_job_), NULL); |
| 97 PlatformThread::Join(thread_); |
| 98 ::CloseHandle(monitoring_job_completion_port_); |
| 99 monitoring_job_completion_port_ = NULL; |
| 100 } |
| 101 |
| 102 } // namespace win |
| 103 } // namespace base |
| OLD | NEW |