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