michael@0: // Copyright (c) 2012 The Chromium Authors. All rights reserved. michael@0: // Use of this source code is governed by a BSD-style license that can be michael@0: // found in the LICENSE file. michael@0: michael@0: #ifndef BASE_WIN_SCOPED_PROCESS_INFORMATION_H_ michael@0: #define BASE_WIN_SCOPED_PROCESS_INFORMATION_H_ michael@0: michael@0: #include michael@0: michael@0: #include "base/basictypes.h" michael@0: #include "base/base_export.h" michael@0: #include "base/win/scoped_handle.h" michael@0: michael@0: namespace base { michael@0: namespace win { michael@0: michael@0: // Manages the closing of process and thread handles from PROCESS_INFORMATION michael@0: // structures. Allows clients to take ownership of either handle independently. michael@0: class BASE_EXPORT ScopedProcessInformation { michael@0: public: michael@0: // Helper object to contain the effect of Receive() to the funtion that needs michael@0: // a pointer. michael@0: class Receiver { michael@0: public: michael@0: explicit Receiver(ScopedProcessInformation* owner) michael@0: : info_(), michael@0: owner_(owner) {} michael@0: ~Receiver() { owner_->Set(info_); } michael@0: michael@0: operator PROCESS_INFORMATION*() { return &info_; } michael@0: michael@0: private: michael@0: PROCESS_INFORMATION info_; michael@0: ScopedProcessInformation* owner_; michael@0: }; michael@0: michael@0: ScopedProcessInformation(); michael@0: ~ScopedProcessInformation(); michael@0: michael@0: // Returns an object that may be passed to API calls such as CreateProcess. michael@0: // DCHECKs that the object is not currently holding any handles. michael@0: // HANDLEs stored in the returned PROCESS_INFORMATION will be owned by this michael@0: // instance. michael@0: // The intended use case is something like this: michael@0: // if (::CreateProcess(..., startup_info, scoped_proces_info.Receive())) michael@0: Receiver Receive(); michael@0: michael@0: // Returns true iff this instance is holding a thread and/or process handle. michael@0: bool IsValid() const; michael@0: michael@0: // Closes the held thread and process handles, if any. michael@0: void Close(); michael@0: michael@0: // Populates this instance with the provided |process_info|. michael@0: void Set(const PROCESS_INFORMATION& process_info); michael@0: michael@0: // Populates this instance with duplicate handles and the thread/process IDs michael@0: // from |other|. Returns false in case of failure, in which case this instance michael@0: // will be completely unpopulated. michael@0: bool DuplicateFrom(const ScopedProcessInformation& other); michael@0: michael@0: // Transfers ownership of the held PROCESS_INFORMATION, if any, away from this michael@0: // instance. michael@0: PROCESS_INFORMATION Take(); michael@0: michael@0: // Transfers ownership of the held process handle, if any, away from this michael@0: // instance. Note that the related process_id will also be cleared. michael@0: HANDLE TakeProcessHandle(); michael@0: michael@0: // Transfers ownership of the held thread handle, if any, away from this michael@0: // instance. Note that the related thread_id will also be cleared. michael@0: HANDLE TakeThreadHandle(); michael@0: michael@0: // Returns the held process handle, if any, while retaining ownership. michael@0: HANDLE process_handle() const { michael@0: return process_handle_.Get(); michael@0: } michael@0: michael@0: // Returns the held thread handle, if any, while retaining ownership. michael@0: HANDLE thread_handle() const { michael@0: return thread_handle_.Get(); michael@0: } michael@0: michael@0: // Returns the held process id, if any. michael@0: DWORD process_id() const { michael@0: return process_id_; michael@0: } michael@0: michael@0: // Returns the held thread id, if any. michael@0: DWORD thread_id() const { michael@0: return thread_id_; michael@0: } michael@0: michael@0: private: michael@0: ScopedHandle process_handle_; michael@0: ScopedHandle thread_handle_; michael@0: DWORD process_id_; michael@0: DWORD thread_id_; michael@0: michael@0: DISALLOW_COPY_AND_ASSIGN(ScopedProcessInformation); michael@0: }; michael@0: michael@0: } // namespace win michael@0: } // namespace base michael@0: michael@0: #endif // BASE_WIN_SCOPED_PROCESS_INFORMATION_H_