Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. |
michael@0 | 2 | // Use of this source code is governed by a BSD-style license that can be |
michael@0 | 3 | // found in the LICENSE file. |
michael@0 | 4 | |
michael@0 | 5 | #ifndef BASE_SCOPED_HANDLE_WIN_H_ |
michael@0 | 6 | #define BASE_SCOPED_HANDLE_WIN_H_ |
michael@0 | 7 | |
michael@0 | 8 | #include <windows.h> |
michael@0 | 9 | |
michael@0 | 10 | #include "base/basictypes.h" |
michael@0 | 11 | #include "base/logging.h" |
michael@0 | 12 | |
michael@0 | 13 | // Used so we always remember to close the handle. |
michael@0 | 14 | // The class interface matches that of ScopedStdioHandle in addition to an |
michael@0 | 15 | // IsValid() method since invalid handles on windows can be either NULL or |
michael@0 | 16 | // INVALID_HANDLE_VALUE (-1). |
michael@0 | 17 | // |
michael@0 | 18 | // Example: |
michael@0 | 19 | // ScopedHandle hfile(CreateFile(...)); |
michael@0 | 20 | // if (!hfile.Get()) |
michael@0 | 21 | // ...process error |
michael@0 | 22 | // ReadFile(hfile.Get(), ...); |
michael@0 | 23 | // |
michael@0 | 24 | // To sqirrel the handle away somewhere else: |
michael@0 | 25 | // secret_handle_ = hfile.Take(); |
michael@0 | 26 | // |
michael@0 | 27 | // To explicitly close the handle: |
michael@0 | 28 | // hfile.Close(); |
michael@0 | 29 | class ScopedHandle { |
michael@0 | 30 | public: |
michael@0 | 31 | ScopedHandle() : handle_(NULL) { |
michael@0 | 32 | } |
michael@0 | 33 | |
michael@0 | 34 | explicit ScopedHandle(HANDLE h) : handle_(NULL) { |
michael@0 | 35 | Set(h); |
michael@0 | 36 | } |
michael@0 | 37 | |
michael@0 | 38 | ~ScopedHandle() { |
michael@0 | 39 | Close(); |
michael@0 | 40 | } |
michael@0 | 41 | |
michael@0 | 42 | // Use this instead of comparing to INVALID_HANDLE_VALUE to pick up our NULL |
michael@0 | 43 | // usage for errors. |
michael@0 | 44 | bool IsValid() const { |
michael@0 | 45 | return handle_ != NULL; |
michael@0 | 46 | } |
michael@0 | 47 | |
michael@0 | 48 | void Set(HANDLE new_handle) { |
michael@0 | 49 | Close(); |
michael@0 | 50 | |
michael@0 | 51 | // Windows is inconsistent about invalid handles, so we always use NULL |
michael@0 | 52 | if (new_handle != INVALID_HANDLE_VALUE) |
michael@0 | 53 | handle_ = new_handle; |
michael@0 | 54 | } |
michael@0 | 55 | |
michael@0 | 56 | HANDLE Get() { |
michael@0 | 57 | return handle_; |
michael@0 | 58 | } |
michael@0 | 59 | |
michael@0 | 60 | operator HANDLE() { return handle_; } |
michael@0 | 61 | |
michael@0 | 62 | HANDLE Take() { |
michael@0 | 63 | // transfers ownership away from this object |
michael@0 | 64 | HANDLE h = handle_; |
michael@0 | 65 | handle_ = NULL; |
michael@0 | 66 | return h; |
michael@0 | 67 | } |
michael@0 | 68 | |
michael@0 | 69 | void Close() { |
michael@0 | 70 | if (handle_) { |
michael@0 | 71 | if (!::CloseHandle(handle_)) { |
michael@0 | 72 | NOTREACHED(); |
michael@0 | 73 | } |
michael@0 | 74 | handle_ = NULL; |
michael@0 | 75 | } |
michael@0 | 76 | } |
michael@0 | 77 | |
michael@0 | 78 | private: |
michael@0 | 79 | HANDLE handle_; |
michael@0 | 80 | DISALLOW_EVIL_CONSTRUCTORS(ScopedHandle); |
michael@0 | 81 | }; |
michael@0 | 82 | |
michael@0 | 83 | // Like ScopedHandle, but for HANDLEs returned from FindFile(). |
michael@0 | 84 | class ScopedFindFileHandle { |
michael@0 | 85 | public: |
michael@0 | 86 | explicit ScopedFindFileHandle(HANDLE handle) : handle_(handle) { |
michael@0 | 87 | // Windows is inconsistent about invalid handles, so we always use NULL |
michael@0 | 88 | if (handle_ == INVALID_HANDLE_VALUE) |
michael@0 | 89 | handle_ = NULL; |
michael@0 | 90 | } |
michael@0 | 91 | |
michael@0 | 92 | ~ScopedFindFileHandle() { |
michael@0 | 93 | if (handle_) |
michael@0 | 94 | FindClose(handle_); |
michael@0 | 95 | } |
michael@0 | 96 | |
michael@0 | 97 | // Use this instead of comparing to INVALID_HANDLE_VALUE to pick up our NULL |
michael@0 | 98 | // usage for errors. |
michael@0 | 99 | bool IsValid() const { return handle_ != NULL; } |
michael@0 | 100 | |
michael@0 | 101 | operator HANDLE() { return handle_; } |
michael@0 | 102 | |
michael@0 | 103 | private: |
michael@0 | 104 | HANDLE handle_; |
michael@0 | 105 | |
michael@0 | 106 | DISALLOW_EVIL_CONSTRUCTORS(ScopedFindFileHandle); |
michael@0 | 107 | }; |
michael@0 | 108 | |
michael@0 | 109 | // Like ScopedHandle but for HDC. Only use this on HDCs returned from |
michael@0 | 110 | // CreateCompatibleDC. For an HDC returned by GetDC, use ReleaseDC instead. |
michael@0 | 111 | class ScopedHDC { |
michael@0 | 112 | public: |
michael@0 | 113 | ScopedHDC() : hdc_(NULL) { } |
michael@0 | 114 | explicit ScopedHDC(HDC h) : hdc_(h) { } |
michael@0 | 115 | |
michael@0 | 116 | ~ScopedHDC() { |
michael@0 | 117 | Close(); |
michael@0 | 118 | } |
michael@0 | 119 | |
michael@0 | 120 | HDC Get() { |
michael@0 | 121 | return hdc_; |
michael@0 | 122 | } |
michael@0 | 123 | |
michael@0 | 124 | void Set(HDC h) { |
michael@0 | 125 | Close(); |
michael@0 | 126 | hdc_ = h; |
michael@0 | 127 | } |
michael@0 | 128 | |
michael@0 | 129 | operator HDC() { return hdc_; } |
michael@0 | 130 | |
michael@0 | 131 | private: |
michael@0 | 132 | void Close() { |
michael@0 | 133 | #ifdef NOGDI |
michael@0 | 134 | assert(false); |
michael@0 | 135 | #else |
michael@0 | 136 | if (hdc_) |
michael@0 | 137 | DeleteDC(hdc_); |
michael@0 | 138 | #endif // NOGDI |
michael@0 | 139 | } |
michael@0 | 140 | |
michael@0 | 141 | HDC hdc_; |
michael@0 | 142 | DISALLOW_EVIL_CONSTRUCTORS(ScopedHDC); |
michael@0 | 143 | }; |
michael@0 | 144 | |
michael@0 | 145 | // Like ScopedHandle but for GDI objects. |
michael@0 | 146 | template<class T> |
michael@0 | 147 | class ScopedGDIObject { |
michael@0 | 148 | public: |
michael@0 | 149 | ScopedGDIObject() : object_(NULL) {} |
michael@0 | 150 | explicit ScopedGDIObject(T object) : object_(object) {} |
michael@0 | 151 | |
michael@0 | 152 | ~ScopedGDIObject() { |
michael@0 | 153 | Close(); |
michael@0 | 154 | } |
michael@0 | 155 | |
michael@0 | 156 | T Get() { |
michael@0 | 157 | return object_; |
michael@0 | 158 | } |
michael@0 | 159 | |
michael@0 | 160 | void Set(T object) { |
michael@0 | 161 | if (object_ && object != object_) |
michael@0 | 162 | Close(); |
michael@0 | 163 | object_ = object; |
michael@0 | 164 | } |
michael@0 | 165 | |
michael@0 | 166 | ScopedGDIObject& operator=(T object) { |
michael@0 | 167 | Set(object); |
michael@0 | 168 | return *this; |
michael@0 | 169 | } |
michael@0 | 170 | |
michael@0 | 171 | operator T() { return object_; } |
michael@0 | 172 | |
michael@0 | 173 | private: |
michael@0 | 174 | void Close() { |
michael@0 | 175 | if (object_) |
michael@0 | 176 | DeleteObject(object_); |
michael@0 | 177 | } |
michael@0 | 178 | |
michael@0 | 179 | T object_; |
michael@0 | 180 | DISALLOW_COPY_AND_ASSIGN(ScopedGDIObject); |
michael@0 | 181 | }; |
michael@0 | 182 | |
michael@0 | 183 | // Typedefs for some common use cases. |
michael@0 | 184 | typedef ScopedGDIObject<HBITMAP> ScopedBitmap; |
michael@0 | 185 | typedef ScopedGDIObject<HRGN> ScopedHRGN; |
michael@0 | 186 | typedef ScopedGDIObject<HFONT> ScopedHFONT; |
michael@0 | 187 | |
michael@0 | 188 | |
michael@0 | 189 | // Like ScopedHandle except for HGLOBAL. |
michael@0 | 190 | template<class T> |
michael@0 | 191 | class ScopedHGlobal { |
michael@0 | 192 | public: |
michael@0 | 193 | explicit ScopedHGlobal(HGLOBAL glob) : glob_(glob) { |
michael@0 | 194 | data_ = static_cast<T*>(GlobalLock(glob_)); |
michael@0 | 195 | } |
michael@0 | 196 | ~ScopedHGlobal() { |
michael@0 | 197 | GlobalUnlock(glob_); |
michael@0 | 198 | } |
michael@0 | 199 | |
michael@0 | 200 | T* get() { return data_; } |
michael@0 | 201 | |
michael@0 | 202 | size_t Size() const { return GlobalSize(glob_); } |
michael@0 | 203 | |
michael@0 | 204 | T* operator->() const { |
michael@0 | 205 | assert(data_ != 0); |
michael@0 | 206 | return data_; |
michael@0 | 207 | } |
michael@0 | 208 | |
michael@0 | 209 | private: |
michael@0 | 210 | HGLOBAL glob_; |
michael@0 | 211 | |
michael@0 | 212 | T* data_; |
michael@0 | 213 | |
michael@0 | 214 | DISALLOW_EVIL_CONSTRUCTORS(ScopedHGlobal); |
michael@0 | 215 | }; |
michael@0 | 216 | |
michael@0 | 217 | #endif // BASE_SCOPED_HANDLE_WIN_H_ |