Wed, 31 Dec 2014 06:55:46 +0100
Added tag TORBROWSER_REPLICA for changeset 6474c204b198
michael@0 | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
michael@0 | 2 | // Use of this source code is governed by a BSD-style license that can be |
michael@0 | 3 | // found in the LICENSE file. |
michael@0 | 4 | |
michael@0 | 5 | #ifndef BASE_WIN_SCOPED_PROCESS_INFORMATION_H_ |
michael@0 | 6 | #define BASE_WIN_SCOPED_PROCESS_INFORMATION_H_ |
michael@0 | 7 | |
michael@0 | 8 | #include <windows.h> |
michael@0 | 9 | |
michael@0 | 10 | #include "base/basictypes.h" |
michael@0 | 11 | #include "base/base_export.h" |
michael@0 | 12 | #include "base/win/scoped_handle.h" |
michael@0 | 13 | |
michael@0 | 14 | namespace base { |
michael@0 | 15 | namespace win { |
michael@0 | 16 | |
michael@0 | 17 | // Manages the closing of process and thread handles from PROCESS_INFORMATION |
michael@0 | 18 | // structures. Allows clients to take ownership of either handle independently. |
michael@0 | 19 | class BASE_EXPORT ScopedProcessInformation { |
michael@0 | 20 | public: |
michael@0 | 21 | // Helper object to contain the effect of Receive() to the funtion that needs |
michael@0 | 22 | // a pointer. |
michael@0 | 23 | class Receiver { |
michael@0 | 24 | public: |
michael@0 | 25 | explicit Receiver(ScopedProcessInformation* owner) |
michael@0 | 26 | : info_(), |
michael@0 | 27 | owner_(owner) {} |
michael@0 | 28 | ~Receiver() { owner_->Set(info_); } |
michael@0 | 29 | |
michael@0 | 30 | operator PROCESS_INFORMATION*() { return &info_; } |
michael@0 | 31 | |
michael@0 | 32 | private: |
michael@0 | 33 | PROCESS_INFORMATION info_; |
michael@0 | 34 | ScopedProcessInformation* owner_; |
michael@0 | 35 | }; |
michael@0 | 36 | |
michael@0 | 37 | ScopedProcessInformation(); |
michael@0 | 38 | ~ScopedProcessInformation(); |
michael@0 | 39 | |
michael@0 | 40 | // Returns an object that may be passed to API calls such as CreateProcess. |
michael@0 | 41 | // DCHECKs that the object is not currently holding any handles. |
michael@0 | 42 | // HANDLEs stored in the returned PROCESS_INFORMATION will be owned by this |
michael@0 | 43 | // instance. |
michael@0 | 44 | // The intended use case is something like this: |
michael@0 | 45 | // if (::CreateProcess(..., startup_info, scoped_proces_info.Receive())) |
michael@0 | 46 | Receiver Receive(); |
michael@0 | 47 | |
michael@0 | 48 | // Returns true iff this instance is holding a thread and/or process handle. |
michael@0 | 49 | bool IsValid() const; |
michael@0 | 50 | |
michael@0 | 51 | // Closes the held thread and process handles, if any. |
michael@0 | 52 | void Close(); |
michael@0 | 53 | |
michael@0 | 54 | // Populates this instance with the provided |process_info|. |
michael@0 | 55 | void Set(const PROCESS_INFORMATION& process_info); |
michael@0 | 56 | |
michael@0 | 57 | // Populates this instance with duplicate handles and the thread/process IDs |
michael@0 | 58 | // from |other|. Returns false in case of failure, in which case this instance |
michael@0 | 59 | // will be completely unpopulated. |
michael@0 | 60 | bool DuplicateFrom(const ScopedProcessInformation& other); |
michael@0 | 61 | |
michael@0 | 62 | // Transfers ownership of the held PROCESS_INFORMATION, if any, away from this |
michael@0 | 63 | // instance. |
michael@0 | 64 | PROCESS_INFORMATION Take(); |
michael@0 | 65 | |
michael@0 | 66 | // Transfers ownership of the held process handle, if any, away from this |
michael@0 | 67 | // instance. Note that the related process_id will also be cleared. |
michael@0 | 68 | HANDLE TakeProcessHandle(); |
michael@0 | 69 | |
michael@0 | 70 | // Transfers ownership of the held thread handle, if any, away from this |
michael@0 | 71 | // instance. Note that the related thread_id will also be cleared. |
michael@0 | 72 | HANDLE TakeThreadHandle(); |
michael@0 | 73 | |
michael@0 | 74 | // Returns the held process handle, if any, while retaining ownership. |
michael@0 | 75 | HANDLE process_handle() const { |
michael@0 | 76 | return process_handle_.Get(); |
michael@0 | 77 | } |
michael@0 | 78 | |
michael@0 | 79 | // Returns the held thread handle, if any, while retaining ownership. |
michael@0 | 80 | HANDLE thread_handle() const { |
michael@0 | 81 | return thread_handle_.Get(); |
michael@0 | 82 | } |
michael@0 | 83 | |
michael@0 | 84 | // Returns the held process id, if any. |
michael@0 | 85 | DWORD process_id() const { |
michael@0 | 86 | return process_id_; |
michael@0 | 87 | } |
michael@0 | 88 | |
michael@0 | 89 | // Returns the held thread id, if any. |
michael@0 | 90 | DWORD thread_id() const { |
michael@0 | 91 | return thread_id_; |
michael@0 | 92 | } |
michael@0 | 93 | |
michael@0 | 94 | private: |
michael@0 | 95 | ScopedHandle process_handle_; |
michael@0 | 96 | ScopedHandle thread_handle_; |
michael@0 | 97 | DWORD process_id_; |
michael@0 | 98 | DWORD thread_id_; |
michael@0 | 99 | |
michael@0 | 100 | DISALLOW_COPY_AND_ASSIGN(ScopedProcessInformation); |
michael@0 | 101 | }; |
michael@0 | 102 | |
michael@0 | 103 | } // namespace win |
michael@0 | 104 | } // namespace base |
michael@0 | 105 | |
michael@0 | 106 | #endif // BASE_WIN_SCOPED_PROCESS_INFORMATION_H_ |