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