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

Side by Side Diff: base/win/scoped_handle.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: Respond to comments. 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
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef BASE_WIN_SCOPED_HANDLE_H_ 5 #ifndef BASE_WIN_SCOPED_HANDLE_H_
6 #define BASE_WIN_SCOPED_HANDLE_H_ 6 #define BASE_WIN_SCOPED_HANDLE_H_
7 #pragma once 7 #pragma once
8 8
9 #include <windows.h> 9 #include <windows.h>
10 10
(...skipping 10 matching lines...) Expand all
21 // and INVALID_HANDLE_VALUE (-1) for Win32 handles. 21 // and INVALID_HANDLE_VALUE (-1) for Win32 handles.
22 // - Receive() method allows to receive a handle value from a function that 22 // - Receive() method allows to receive a handle value from a function that
23 // takes a raw handle pointer only. 23 // takes a raw handle pointer only.
24 template <class Traits> 24 template <class Traits>
25 class GenericScopedHandle { 25 class GenericScopedHandle {
26 public: 26 public:
27 typedef typename Traits::Handle Handle; 27 typedef typename Traits::Handle Handle;
28 28
29 GenericScopedHandle() : handle_(Traits::NullHandle()) {} 29 GenericScopedHandle() : handle_(Traits::NullHandle()) {}
30 30
31 explicit GenericScopedHandle(Handle handle) : handle_(Traits::NullHandle()) { 31 explicit GenericScopedHandle(Handle handle) : handle_(Traits::NullHandle()) {
alexeypa (please no reviews) 2012/03/15 16:38:27 const Handle& handle
erikwright (departed) 2012/03/15 18:02:44 Done.
32 Set(handle); 32 Set(handle);
33 } 33 }
34 34
35 ~GenericScopedHandle() { 35 ~GenericScopedHandle() {
36 Close(); 36 Close();
37 } 37 }
38 38
39 bool IsValid() const { 39 bool IsValid() const {
40 return Traits::IsHandleValid(handle_); 40 return Traits::IsHandleValid(handle_);
41 } 41 }
42 42
43 void Set(Handle handle) { 43 void Set(Handle handle) {
alexeypa (please no reviews) 2012/03/15 16:38:27 const Handle& handle
erikwright (departed) 2012/03/15 18:02:44 Done.
44 if (handle_ != handle) { 44 if (!Traits::IsSame(handle_, handle)) {
45 Close(); 45 Close();
46 46
47 if (Traits::IsHandleValid(handle)) { 47 if (Traits::IsHandleValid(handle)) {
48 handle_ = handle; 48 handle_ = handle;
49 } 49 }
50 } 50 }
51 } 51 }
52 52
53 Handle Get() const { 53 Handle Get() const {
alexeypa (please no reviews) 2012/03/15 16:38:27 Now that Handle is not a simple value this should
erikwright (departed) 2012/03/15 18:02:44 Done.
54 return handle_; 54 return handle_;
55 } 55 }
56 56
57 operator Handle() const { 57 operator Handle() const {
58 return handle_; 58 return handle_;
59 } 59 }
60 60
61 Handle* Receive() { 61 Handle* Receive() {
62 DCHECK(!Traits::IsHandleValid(handle_)) << "Handle must be NULL"; 62 DCHECK(!Traits::IsHandleValid(handle_)) << "Handle must be NULL";
63 return &handle_; 63 return &handle_;
(...skipping 30 matching lines...) Expand all
94 // Closes the handle. 94 // Closes the handle.
95 static bool CloseHandle(HANDLE handle) { 95 static bool CloseHandle(HANDLE handle) {
96 return ::CloseHandle(handle) != FALSE; 96 return ::CloseHandle(handle) != FALSE;
97 } 97 }
98 98
99 // Returns true if the handle value is valid. 99 // Returns true if the handle value is valid.
100 static bool IsHandleValid(HANDLE handle) { 100 static bool IsHandleValid(HANDLE handle) {
101 return handle != NULL && handle != INVALID_HANDLE_VALUE; 101 return handle != NULL && handle != INVALID_HANDLE_VALUE;
102 } 102 }
103 103
104 // Returns true if the handle values are the same.
105 static bool IsSame(HANDLE lhs, HANDLE rhs) {
106 return lhs == rhs;
107 }
108
104 // Returns NULL handle value. 109 // Returns NULL handle value.
105 static HANDLE NullHandle() { 110 static HANDLE NullHandle() {
106 return NULL; 111 return NULL;
107 } 112 }
108 113
109 private: 114 private:
110 DISALLOW_IMPLICIT_CONSTRUCTORS(HandleTraits); 115 DISALLOW_IMPLICIT_CONSTRUCTORS(HandleTraits);
111 }; 116 };
112 117
113 typedef GenericScopedHandle<HandleTraits> ScopedHandle; 118 typedef GenericScopedHandle<HandleTraits> ScopedHandle;
114 119
115 } // namespace win 120 } // namespace win
116 } // namespace base 121 } // namespace base
117 122
118 #endif // BASE_SCOPED_HANDLE_WIN_H_ 123 #endif // BASE_SCOPED_HANDLE_WIN_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698