1 // Copyright 2012 The Chromium Authors 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef BASE_WIN_SCOPED_PROCESS_INFORMATION_H_ 6 #define BASE_WIN_SCOPED_PROCESS_INFORMATION_H_ 7 8 #include <windows.h> 9 10 #include "base/base_export.h" 11 #include "base/win/scoped_handle.h" 12 13 namespace base { 14 namespace win { 15 16 // Manages the closing of process and thread handles from PROCESS_INFORMATION 17 // structures. Allows clients to take ownership of either handle independently. 18 class BASE_EXPORT ScopedProcessInformation { 19 public: 20 ScopedProcessInformation(); 21 explicit ScopedProcessInformation(const PROCESS_INFORMATION& process_info); 22 23 ScopedProcessInformation(ScopedProcessInformation&&); 24 ScopedProcessInformation& operator=(ScopedProcessInformation&&); 25 26 ScopedProcessInformation(const ScopedProcessInformation&) = delete; 27 ScopedProcessInformation& operator=(const ScopedProcessInformation&) = delete; 28 29 ~ScopedProcessInformation(); 30 31 // Returns true iff this instance is holding a thread and/or process handle. 32 bool IsValid() const; 33 34 // Closes the held thread and process handles, if any. 35 void Close(); 36 37 // Populates this instance with the provided |process_info|. 38 void Set(const PROCESS_INFORMATION& process_info); 39 40 // Populates this instance with duplicate handles and the thread/process IDs 41 // from |other|. Returns false in case of failure, in which case this instance 42 // will be completely unpopulated. 43 bool DuplicateFrom(const ScopedProcessInformation& other); 44 45 // Transfers ownership of the held PROCESS_INFORMATION, if any, away from this 46 // instance. 47 PROCESS_INFORMATION Take(); 48 49 // Transfers ownership of the held process handle, if any, away from this 50 // instance. Note that the related process_id will also be cleared. 51 HANDLE TakeProcessHandle(); 52 53 // Transfers ownership of the held thread handle, if any, away from this 54 // instance. Note that the related thread_id will also be cleared. 55 HANDLE TakeThreadHandle(); 56 57 // Returns the held process handle, if any, while retaining ownership. process_handle()58 HANDLE process_handle() const { return process_handle_.get(); } 59 60 // Returns the held thread handle, if any, while retaining ownership. thread_handle()61 HANDLE thread_handle() const { return thread_handle_.get(); } 62 63 // Returns the held process id, if any. process_id()64 DWORD process_id() const { return process_id_; } 65 66 // Returns the held thread id, if any. thread_id()67 DWORD thread_id() const { return thread_id_; } 68 69 private: 70 ScopedHandle process_handle_; 71 ScopedHandle thread_handle_; 72 DWORD process_id_ = 0; 73 DWORD thread_id_ = 0; 74 }; 75 76 } // namespace win 77 } // namespace base 78 79 #endif // BASE_WIN_SCOPED_PROCESS_INFORMATION_H_ 80