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

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: Add tests for new functions. Created 8 years, 8 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 // Swaps contents with the other ScopedProcessInformation.
41 void Swap(ScopedProcessInformation* other);
42
43 // Populates this instance with duplicate handles and the thread/process IDs
44 // from |other|. Returns false in case of failure, in which case this instance
45 // will be completely unpopulated.
46 bool DuplicateFrom(const ScopedProcessInformation& other);
47
48 // Transfers ownership of the held PROCESS_INFORMATION, if any, away from this
49 // instance. Resets the held PROCESS_INFORMATION to null.
50 PROCESS_INFORMATION Take();
51
52 // Transfers ownership of the held process handle, if any, away from this
53 // instance. The hProcess and dwProcessId members of the held
54 // PROCESS_INFORMATION will be reset.
55 HANDLE TakeProcessHandle();
56
57 // Transfers ownership of the held thread handle, if any, away from this
58 // instance. The hThread and dwThreadId members of the held
59 // PROCESS_INFORMATION will be reset.
60 HANDLE TakeThreadHandle();
61
62 // Returns the held process handle, if any, while retaining ownership.
63 HANDLE process_handle() const {
64 return process_information_.hProcess;
65 }
66
67 // Returns the held thread handle, if any, while retaining ownership.
68 HANDLE thread_handle() const {
69 return process_information_.hThread;
70 }
71
72 // Returns the held process id, if any.
73 DWORD process_id() const {
74 return process_information_.dwProcessId;
75 }
76
77 // Returns the held thread id, if any.
78 DWORD thread_id() const {
79 return process_information_.dwThreadId;
80 }
81
82 private:
83 // Resets the held PROCESS_INFORMATION to null.
84 void Reset();
85
86 PROCESS_INFORMATION process_information_;
87
88 DISALLOW_COPY_AND_ASSIGN(ScopedProcessInformation);
89 };
90
91 } // namespace win
92 } // namespace base
93
94 #endif // BASE_WIN_SCOPED_PROCESS_INFORMATION_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698