summaryrefslogtreecommitdiffstats
path: root/xpcom/base/nsWindowsHelpers.h
blob: 7c9127580371b1c9b765a8dc9c724f70ece01765 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

// NB: This code may be used from non-XPCOM code, in particular, the
// Windows Default Browser Agent.

#ifndef nsWindowsHelpers_h
#define nsWindowsHelpers_h

#include <windows.h>
#include <msi.h>
#include "nsAutoRef.h"
#include "mozilla/Assertions.h"
#include "mozilla/UniquePtr.h"

// ----------------------------------------------------------------------------
// Critical Section helper class
// ----------------------------------------------------------------------------

class AutoCriticalSection {
 public:
  explicit AutoCriticalSection(LPCRITICAL_SECTION aSection)
      : mSection(aSection) {
    ::EnterCriticalSection(mSection);
  }
  ~AutoCriticalSection() { ::LeaveCriticalSection(mSection); }

 private:
  LPCRITICAL_SECTION mSection;
};

template <>
class nsAutoRefTraits<HKEY> {
 public:
  typedef HKEY RawRef;
  static HKEY Void() { return nullptr; }

  static void Release(RawRef aFD) {
    if (aFD != Void()) {
      RegCloseKey(aFD);
    }
  }
};

template <>
class nsAutoRefTraits<HDC> {
 public:
  typedef HDC RawRef;
  static HDC Void() { return nullptr; }

  static void Release(RawRef aFD) {
    if (aFD != Void()) {
      ::DeleteDC(aFD);
    }
  }
};

template <>
class nsAutoRefTraits<HFONT> {
 public:
  typedef HFONT RawRef;
  static HFONT Void() { return nullptr; }

  static void Release(RawRef aFD) {
    if (aFD != Void()) {
      ::DeleteObject(aFD);
    }
  }
};

template <>
class nsAutoRefTraits<HBRUSH> {
 public:
  typedef HBRUSH RawRef;
  static HBRUSH Void() { return nullptr; }

  static void Release(RawRef aFD) {
    if (aFD != Void()) {
      ::DeleteObject(aFD);
    }
  }
};

template <>
class nsAutoRefTraits<HRGN> {
 public:
  typedef HRGN RawRef;
  static HRGN Void() { return nullptr; }

  static void Release(RawRef aFD) {
    if (aFD != Void()) {
      ::DeleteObject(aFD);
    }
  }
};

template <>
class nsAutoRefTraits<HBITMAP> {
 public:
  typedef HBITMAP RawRef;
  static HBITMAP Void() { return nullptr; }

  static void Release(RawRef aFD) {
    if (aFD != Void()) {
      ::DeleteObject(aFD);
    }
  }
};

template <>
class nsAutoRefTraits<SC_HANDLE> {
 public:
  typedef SC_HANDLE RawRef;
  static SC_HANDLE Void() { return nullptr; }

  static void Release(RawRef aFD) {
    if (aFD != Void()) {
      CloseServiceHandle(aFD);
    }
  }
};

template <>
class nsSimpleRef<HANDLE> {
 protected:
  typedef HANDLE RawRef;

  nsSimpleRef() : mRawRef(nullptr) {}

  explicit nsSimpleRef(RawRef aRawRef) : mRawRef(aRawRef) {}

  bool HaveResource() const {
    return mRawRef && mRawRef != INVALID_HANDLE_VALUE;
  }

 public:
  RawRef get() const { return mRawRef; }

  static void Release(RawRef aRawRef) {
    if (aRawRef && aRawRef != INVALID_HANDLE_VALUE) {
      CloseHandle(aRawRef);
    }
  }
  RawRef mRawRef;
};

template <>
class nsAutoRefTraits<HMODULE> {
 public:
  typedef HMODULE RawRef;
  static RawRef Void() { return nullptr; }

  static void Release(RawRef aFD) {
    if (aFD != Void()) {
      FreeLibrary(aFD);
    }
  }
};

template <>
class nsAutoRefTraits<DEVMODEW*> {
 public:
  typedef DEVMODEW* RawRef;
  static RawRef Void() { return nullptr; }

  static void Release(RawRef aDevMode) {
    if (aDevMode != Void()) {
      ::HeapFree(::GetProcessHeap(), 0, aDevMode);
    }
  }
};

template <>
class nsAutoRefTraits<MSIHANDLE> {
 public:
  typedef MSIHANDLE RawRef;
  static RawRef Void() { return 0; }

  static void Release(RawRef aHandle) {
    if (aHandle != Void()) {
      ::MsiCloseHandle(aHandle);
    }
  }
};

// HGLOBAL is just a typedef of HANDLE which nsSimpleRef has a specialization
// of, that means having a nsAutoRefTraits specialization for HGLOBAL is
// useless. Therefore we create a wrapper class for HGLOBAL to make
// nsAutoRefTraits and nsAutoRef work as intention.
class nsHGLOBAL {
 public:
  MOZ_IMPLICIT nsHGLOBAL(HGLOBAL hGlobal) : m_hGlobal(hGlobal) {}

  operator HGLOBAL() const { return m_hGlobal; }

 private:
  HGLOBAL m_hGlobal;
};

template <>
class nsAutoRefTraits<nsHGLOBAL> {
 public:
  typedef nsHGLOBAL RawRef;
  static RawRef Void() { return nullptr; }

  static void Release(RawRef hGlobal) { ::GlobalFree(hGlobal); }
};

// Because Printer's HANDLE uses ClosePrinter and we already have
// nsAutoRef<HANDLE> which uses CloseHandle so we need to create a wrapper class
// for HANDLE to have another specialization for nsAutoRefTraits.
class nsHPRINTER {
 public:
  MOZ_IMPLICIT nsHPRINTER(HANDLE hPrinter) : m_hPrinter(hPrinter) {}

  operator HANDLE() const { return m_hPrinter; }

  HANDLE* operator&() { return &m_hPrinter; }

 private:
  HANDLE m_hPrinter;
};

// winspool.h header has AddMonitor macro, it conflicts with AddMonitor member
// function in TaskbarPreview.cpp and TaskbarTabPreview.cpp. Beside, we only
// need ClosePrinter here for Release function, so having its prototype is
// enough.
extern "C" BOOL WINAPI ClosePrinter(HANDLE hPrinter);

template <>
class nsAutoRefTraits<nsHPRINTER> {
 public:
  typedef nsHPRINTER RawRef;
  static RawRef Void() { return nullptr; }

  static void Release(RawRef hPrinter) { ::ClosePrinter(hPrinter); }
};

typedef nsAutoRef<HKEY> nsAutoRegKey;
typedef nsAutoRef<HDC> nsAutoHDC;
typedef nsAutoRef<HFONT> nsAutoFont;
typedef nsAutoRef<HBRUSH> nsAutoBrush;
typedef nsAutoRef<HRGN> nsAutoRegion;
typedef nsAutoRef<HBITMAP> nsAutoBitmap;
typedef nsAutoRef<SC_HANDLE> nsAutoServiceHandle;
typedef nsAutoRef<HANDLE> nsAutoHandle;
typedef nsAutoRef<HMODULE> nsModuleHandle;
typedef nsAutoRef<DEVMODEW*> nsAutoDevMode;
typedef nsAutoRef<nsHGLOBAL> nsAutoGlobalMem;
typedef nsAutoRef<nsHPRINTER> nsAutoPrinter;
typedef nsAutoRef<MSIHANDLE> nsAutoMsiHandle;

// Construct a path "<system32>\<aModule>". return false if the output buffer
// is too small.
// Note: If the system path cannot be found, or doesn't fit in the output buffer
// with the module name, we will just ignore the system path and output the
// module name alone;
// this may mean using a normal search path wherever the output is used.
bool inline ConstructSystem32Path(LPCWSTR aModule, WCHAR* aSystemPath,
                                  UINT aSize) {
  MOZ_ASSERT(aSystemPath);

  size_t fileLen = wcslen(aModule);
  if (fileLen >= aSize) {
    // The module name alone cannot even fit!
    return false;
  }

  size_t systemDirLen = GetSystemDirectoryW(aSystemPath, aSize);

  if (systemDirLen) {
    if (systemDirLen < aSize - fileLen) {
      // Make the system directory path terminate with a slash.
      if (aSystemPath[systemDirLen - 1] != L'\\') {
        if (systemDirLen + 1 < aSize - fileLen) {
          aSystemPath[systemDirLen] = L'\\';
          ++systemDirLen;
          // No need to re-nullptr terminate.
        } else {
          // Couldn't fit the system path with added slash.
          systemDirLen = 0;
        }
      }
    } else {
      // Couldn't fit the system path.
      systemDirLen = 0;
    }
  }

  MOZ_ASSERT(systemDirLen + fileLen < aSize);

  wcsncpy(aSystemPath + systemDirLen, aModule, fileLen);
  aSystemPath[systemDirLen + fileLen] = L'\0';
  return true;
}

HMODULE inline LoadLibrarySystem32(LPCWSTR aModule) {
  return LoadLibraryExW(aModule, nullptr, LOAD_LIBRARY_SEARCH_SYSTEM32);
}

// for UniquePtr
struct LocalFreeDeleter {
  void operator()(void* aPtr) { ::LocalFree(aPtr); }
};

struct VirtualFreeDeleter {
  void operator()(void* aPtr) { ::VirtualFree(aPtr, 0, MEM_RELEASE); }
};

// for UniquePtr to store a PSID
struct FreeSidDeleter {
  void operator()(void* aPtr) { ::FreeSid(aPtr); }
};
// Unfortunately, although SID is a struct, PSID is a void*
// This typedef will work for storing a PSID in a UniquePtr and should make
// things a bit more readable.
typedef mozilla::UniquePtr<void, FreeSidDeleter> UniqueSidPtr;

struct CloseHandleDeleter {
  typedef HANDLE pointer;
  void operator()(pointer aHandle) {
    if (aHandle != INVALID_HANDLE_VALUE) {
      ::CloseHandle(aHandle);
    }
  }
};

// One caller of this function is early in startup and several others are not,
// so they have different ways of determining the two parameters. This function
// exists just so any future code that needs to determine whether the dynamic
// blocklist is disabled remembers to check whether safe mode is active.
inline bool IsDynamicBlocklistDisabled(bool isSafeMode,
                                       bool hasCommandLineDisableArgument) {
  return isSafeMode || hasCommandLineDisableArgument;
}
#endif