OLD | NEW |
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 | 7 |
8 #include <windows.h> | 8 #include <windows.h> |
9 | 9 |
10 #include "base/base_export.h" | 10 #include "base/base_export.h" |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
94 } | 94 } |
95 return temp; | 95 return temp; |
96 } | 96 } |
97 | 97 |
98 // Explicitly closes the owned handle. | 98 // Explicitly closes the owned handle. |
99 void Close() { | 99 void Close() { |
100 if (Traits::IsHandleValid(handle_)) { | 100 if (Traits::IsHandleValid(handle_)) { |
101 Verifier::StopTracking(handle_, this, BASE_WIN_GET_CALLER, | 101 Verifier::StopTracking(handle_, this, BASE_WIN_GET_CALLER, |
102 tracked_objects::GetProgramCounter()); | 102 tracked_objects::GetProgramCounter()); |
103 | 103 |
104 Traits::CloseHandle(handle_); | 104 if (!Traits::CloseHandle(handle_)) |
| 105 CHECK(false); |
| 106 |
105 handle_ = Traits::NullHandle(); | 107 handle_ = Traits::NullHandle(); |
106 } | 108 } |
107 } | 109 } |
108 | 110 |
109 private: | 111 private: |
110 Handle handle_; | 112 Handle handle_; |
111 }; | 113 }; |
112 | 114 |
113 #undef BASE_WIN_GET_CALLER | 115 #undef BASE_WIN_GET_CALLER |
114 | 116 |
115 // The traits class for Win32 handles that can be closed via CloseHandle() API. | 117 // The traits class for Win32 handles that can be closed via CloseHandle() API. |
116 class HandleTraits { | 118 class HandleTraits { |
117 public: | 119 public: |
118 typedef HANDLE Handle; | 120 typedef HANDLE Handle; |
119 | 121 |
120 // Closes the handle. | 122 // Closes the handle. |
121 static bool BASE_EXPORT CloseHandle(HANDLE handle); | 123 static bool CloseHandle(HANDLE handle) { |
| 124 return ::CloseHandle(handle) != FALSE; |
| 125 } |
122 | 126 |
123 // Returns true if the handle value is valid. | 127 // Returns true if the handle value is valid. |
124 static bool IsHandleValid(HANDLE handle) { | 128 static bool IsHandleValid(HANDLE handle) { |
125 return handle != NULL && handle != INVALID_HANDLE_VALUE; | 129 return handle != NULL && handle != INVALID_HANDLE_VALUE; |
126 } | 130 } |
127 | 131 |
128 // Returns NULL handle value. | 132 // Returns NULL handle value. |
129 static HANDLE NullHandle() { | 133 static HANDLE NullHandle() { |
130 return NULL; | 134 return NULL; |
131 } | 135 } |
(...skipping 25 matching lines...) Expand all Loading... |
157 const void* pc1, const void* pc2); | 161 const void* pc1, const void* pc2); |
158 static void StopTracking(HANDLE handle, const void* owner, | 162 static void StopTracking(HANDLE handle, const void* owner, |
159 const void* pc1, const void* pc2); | 163 const void* pc1, const void* pc2); |
160 | 164 |
161 private: | 165 private: |
162 DISALLOW_IMPLICIT_CONSTRUCTORS(VerifierTraits); | 166 DISALLOW_IMPLICIT_CONSTRUCTORS(VerifierTraits); |
163 }; | 167 }; |
164 | 168 |
165 typedef GenericScopedHandle<HandleTraits, VerifierTraits> ScopedHandle; | 169 typedef GenericScopedHandle<HandleTraits, VerifierTraits> ScopedHandle; |
166 | 170 |
167 // This function should be called by the embedder to enable the use of | |
168 // VerifierTraits at runtime. It has no effect if DummyVerifierTraits is used | |
169 // for ScopedHandle. | |
170 void BASE_EXPORT EnableHandleVerifier(); | |
171 | |
172 // This should be called whenever the OS is closing a handle, if extended | |
173 // verification of improper handle closing is desired. If |handle| is being | |
174 // tracked by the handle verifier and ScopedHandle is not the one closing it, | |
175 // a CHECK is generated. | |
176 void BASE_EXPORT OnHandleBeingClosed(HANDLE handle); | |
177 | |
178 } // namespace win | 171 } // namespace win |
179 } // namespace base | 172 } // namespace base |
180 | 173 |
181 #endif // BASE_SCOPED_HANDLE_WIN_H_ | 174 #endif // BASE_SCOPED_HANDLE_WIN_H_ |
OLD | NEW |