• 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/macros.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 ScopedProcessInformation {
19  public:
20   ScopedProcessInformation();
21   explicit ScopedProcessInformation(const PROCESS_INFORMATION& process_info);
22   ~ScopedProcessInformation();
23 
24   // Returns true iff this instance is holding a thread and/or process handle.
25   bool IsValid() const;
26 
27   // Closes the held thread and process handles, if any.
28   void Close();
29 
30   // Populates this instance with the provided |process_info|.
31   void Set(const PROCESS_INFORMATION& process_info);
32 
33   // Populates this instance with duplicate handles and the thread/process IDs
34   // from |other|. Returns false in case of failure, in which case this instance
35   // will be completely unpopulated.
36   bool DuplicateFrom(const ScopedProcessInformation& other);
37 
38   // Transfers ownership of the held PROCESS_INFORMATION, if any, away from this
39   // instance.
40   PROCESS_INFORMATION Take();
41 
42   // Transfers ownership of the held process handle, if any, away from this
43   // instance. Note that the related process_id will also be cleared.
44   HANDLE TakeProcessHandle();
45 
46   // Transfers ownership of the held thread handle, if any, away from this
47   // instance. Note that the related thread_id will also be cleared.
48   HANDLE TakeThreadHandle();
49 
50   // Returns the held process handle, if any, while retaining ownership.
process_handle()51   HANDLE process_handle() const { return process_handle_.Get(); }
52 
53   // Returns the held thread handle, if any, while retaining ownership.
thread_handle()54   HANDLE thread_handle() const { return thread_handle_.Get(); }
55 
56   // Returns the held process id, if any.
process_id()57   DWORD process_id() const { return process_id_; }
58 
59   // Returns the held thread id, if any.
thread_id()60   DWORD thread_id() const { return thread_id_; }
61 
62  private:
63   ScopedHandle process_handle_;
64   ScopedHandle thread_handle_;
65   DWORD process_id_;
66   DWORD thread_id_;
67 
68   DISALLOW_COPY_AND_ASSIGN(ScopedProcessInformation);
69 };
70 
71 }  // namespace win
72 }  // namespace base
73 
74 #endif  // BASE_WIN_SCOPED_PROCESS_INFORMATION_H_
75