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" |
11 #include "base/gtest_prod_util.h" | 11 #include "base/gtest_prod_util.h" |
12 #include "base/location.h" | 12 #include "base/location.h" |
13 #include "base/logging.h" | 13 #include "base/logging.h" |
14 #include "base/macros.h" | 14 #include "base/macros.h" |
15 #include "base/move.h" | |
16 | 15 |
17 // TODO(rvargas): remove this with the rest of the verifier. | 16 // TODO(rvargas): remove this with the rest of the verifier. |
18 #if defined(COMPILER_MSVC) | 17 #if defined(COMPILER_MSVC) |
19 #include <intrin.h> | 18 #include <intrin.h> |
20 #define BASE_WIN_GET_CALLER _ReturnAddress() | 19 #define BASE_WIN_GET_CALLER _ReturnAddress() |
21 #elif defined(COMPILER_GCC) | 20 #elif defined(COMPILER_GCC) |
22 #define BASE_WIN_GET_CALLER __builtin_extract_return_addr(\\ | 21 #define BASE_WIN_GET_CALLER __builtin_extract_return_addr(\\ |
23 __builtin_return_address(0)) | 22 __builtin_return_address(0)) |
24 #endif | 23 #endif |
25 | 24 |
26 namespace base { | 25 namespace base { |
27 namespace win { | 26 namespace win { |
28 | 27 |
29 // Generic wrapper for raw handles that takes care of closing handles | 28 // Generic wrapper for raw handles that takes care of closing handles |
30 // automatically. The class interface follows the style of | 29 // automatically. The class interface follows the style of |
31 // the ScopedFILE class with two additions: | 30 // the ScopedFILE class with two additions: |
32 // - IsValid() method can tolerate multiple invalid handle values such as NULL | 31 // - IsValid() method can tolerate multiple invalid handle values such as NULL |
33 // and INVALID_HANDLE_VALUE (-1) for Win32 handles. | 32 // and INVALID_HANDLE_VALUE (-1) for Win32 handles. |
34 // - Set() (and the constructors and assignment operators that call it) | 33 // - Set() (and the constructors and assignment operators that call it) |
35 // preserve the Windows LastError code. This ensures that GetLastError() can | 34 // preserve the Windows LastError code. This ensures that GetLastError() can |
36 // be called after stashing a handle in a GenericScopedHandle object. Doing | 35 // be called after stashing a handle in a GenericScopedHandle object. Doing |
37 // this explicitly is necessary because of bug 528394 and VC++ 2015. | 36 // this explicitly is necessary because of bug 528394 and VC++ 2015. |
38 template <class Traits, class Verifier> | 37 template <class Traits, class Verifier> |
39 class GenericScopedHandle { | 38 class GenericScopedHandle { |
40 MOVE_ONLY_TYPE_FOR_CPP_03(GenericScopedHandle) | |
41 | |
42 public: | 39 public: |
43 typedef typename Traits::Handle Handle; | 40 typedef typename Traits::Handle Handle; |
44 | 41 |
45 GenericScopedHandle() : handle_(Traits::NullHandle()) {} | 42 GenericScopedHandle() : handle_(Traits::NullHandle()) {} |
46 | 43 |
47 explicit GenericScopedHandle(Handle handle) : handle_(Traits::NullHandle()) { | 44 explicit GenericScopedHandle(Handle handle) : handle_(Traits::NullHandle()) { |
48 Set(handle); | 45 Set(handle); |
49 } | 46 } |
50 | 47 |
51 GenericScopedHandle(GenericScopedHandle&& other) | 48 GenericScopedHandle(GenericScopedHandle&& other) |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
105 | 102 |
106 Traits::CloseHandle(handle_); | 103 Traits::CloseHandle(handle_); |
107 handle_ = Traits::NullHandle(); | 104 handle_ = Traits::NullHandle(); |
108 } | 105 } |
109 } | 106 } |
110 | 107 |
111 private: | 108 private: |
112 FRIEND_TEST_ALL_PREFIXES(ScopedHandleTest, ActiveVerifierWrongOwner); | 109 FRIEND_TEST_ALL_PREFIXES(ScopedHandleTest, ActiveVerifierWrongOwner); |
113 FRIEND_TEST_ALL_PREFIXES(ScopedHandleTest, ActiveVerifierUntrackedHandle); | 110 FRIEND_TEST_ALL_PREFIXES(ScopedHandleTest, ActiveVerifierUntrackedHandle); |
114 Handle handle_; | 111 Handle handle_; |
| 112 |
| 113 DISALLOW_COPY_AND_ASSIGN(GenericScopedHandle); |
115 }; | 114 }; |
116 | 115 |
117 #undef BASE_WIN_GET_CALLER | 116 #undef BASE_WIN_GET_CALLER |
118 | 117 |
119 // The traits class for Win32 handles that can be closed via CloseHandle() API. | 118 // The traits class for Win32 handles that can be closed via CloseHandle() API. |
120 class HandleTraits { | 119 class HandleTraits { |
121 public: | 120 public: |
122 typedef HANDLE Handle; | 121 typedef HANDLE Handle; |
123 | 122 |
124 // Closes the handle. | 123 // Closes the handle. |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
180 BASE_EXPORT void OnHandleBeingClosed(HANDLE handle); | 179 BASE_EXPORT void OnHandleBeingClosed(HANDLE handle); |
181 | 180 |
182 // This testing function returns the module that the ActiveVerifier concrete | 181 // This testing function returns the module that the ActiveVerifier concrete |
183 // implementation was instantiated in. | 182 // implementation was instantiated in. |
184 BASE_EXPORT HMODULE GetHandleVerifierModuleForTesting(); | 183 BASE_EXPORT HMODULE GetHandleVerifierModuleForTesting(); |
185 | 184 |
186 } // namespace win | 185 } // namespace win |
187 } // namespace base | 186 } // namespace base |
188 | 187 |
189 #endif // BASE_WIN_SCOPED_HANDLE_H_ | 188 #endif // BASE_WIN_SCOPED_HANDLE_H_ |
OLD | NEW |