Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(842)

Side by Side Diff: base/win/scoped_process_information.h

Issue 9700038: ScopedProcessInformation protects against process/thread handle leaks from CreateProcess calls. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Touch a previously missed use of PROCESS_INFORMATION Created 8 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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 #pragma once
8
9 #include <windows.h>
10
11 #include "base/basictypes.h"
12 #include "base/base_export.h"
13
14 namespace base {
15 namespace win {
16
17 // Manages the closing of process and thread handles from PROCESS_INFORMATION
18 // structures. Allows clients to take ownership of either handle independently.
19 class BASE_EXPORT ScopedProcessInformation {
20 public:
21 // Creates an instance holding a null PROCESS_INFORMATION.
22 ScopedProcessInformation();
23
24 // Closes the held thread and process handles, if any.
25 ~ScopedProcessInformation();
26
27 // Returns a pointer that may be passed to API calls such as CreateProcess.
28 // DCHECKs that the object is not currently holding any handles.
29 // HANDLEs stored in the returned PROCESS_INFORMATION will be owned by this
30 // instance.
31 PROCESS_INFORMATION* Receive();
32
33 // Returns true iff this instance is holding a thread and/or process handle.
34 bool IsValid() const;
35
36 // Closes the held thread and process handles, if any, and resets the held
37 // PROCESS_INFORMATION to null.
38 void Close();
39
40 // Transfers ownership of the held PROCESS_INFORMATION, if any, away from this
41 // instance. Resets the held PROCESS_INFORMATION to null.
42 PROCESS_INFORMATION Take();
43
44 // Transfers ownership of the held process handle, if any, away from this
45 // instance. The hProcess and dwProcessId members of the held
46 // PROCESS_INFORMATION will be reset.
47 HANDLE TakeProcessHandle();
48
49 // Transfers ownership of the held thread handle, if any, away from this
50 // instance. The hThread and dwThreadId members of the held
51 // PROCESS_INFORMATION will be reset.
52 HANDLE TakeThreadHandle();
53
54 // Returns the held process handle, if any, while retaining ownership.
55 HANDLE process_handle() const {
56 return process_information_.hProcess;
57 }
58
59 // Returns the held thread handle, if any, while retaining ownership.
60 HANDLE thread_handle() const {
61 return process_information_.hThread;
62 }
63
64 // Returns the held process id, if any.
65 DWORD process_id() const {
66 return process_information_.dwProcessId;
67 }
68
69 // Returns the held thread id, if any.
70 DWORD thread_id() const {
71 return process_information_.dwThreadId;
72 }
73
74 private:
75 // Resets the held PROCESS_INFORMATION to null.
76 void Reset();
77
78 PROCESS_INFORMATION process_information_;
79
80 DISALLOW_COPY_AND_ASSIGN(ScopedProcessInformation);
81 };
82
83 } // namespace win
84 } // namespace base
85
86 #endif // BASE_WIN_SCOPED_PROCESS_INFORMATION_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698