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.
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 #ifndef nsWindowsHelpers_h
6 #define nsWindowsHelpers_h
8 #include <windows.h>
9 #include "nsAutoRef.h"
10 #include "nscore.h"
12 // ----------------------------------------------------------------------------
13 // Critical Section helper class
14 // ----------------------------------------------------------------------------
16 class AutoCriticalSection
17 {
18 public:
19 AutoCriticalSection(LPCRITICAL_SECTION section)
20 : mSection(section)
21 {
22 ::EnterCriticalSection(mSection);
23 }
24 ~AutoCriticalSection()
25 {
26 ::LeaveCriticalSection(mSection);
27 }
28 private:
29 LPCRITICAL_SECTION mSection;
30 };
32 template<>
33 class nsAutoRefTraits<HKEY>
34 {
35 public:
36 typedef HKEY RawRef;
37 static HKEY Void()
38 {
39 return nullptr;
40 }
42 static void Release(RawRef aFD)
43 {
44 if (aFD != Void()) {
45 RegCloseKey(aFD);
46 }
47 }
48 };
50 template<>
51 class nsAutoRefTraits<SC_HANDLE>
52 {
53 public:
54 typedef SC_HANDLE RawRef;
55 static SC_HANDLE Void()
56 {
57 return nullptr;
58 }
60 static void Release(RawRef aFD)
61 {
62 if (aFD != Void()) {
63 CloseServiceHandle(aFD);
64 }
65 }
66 };
68 template<>
69 class nsSimpleRef<HANDLE>
70 {
71 protected:
72 typedef HANDLE RawRef;
74 nsSimpleRef() : mRawRef(nullptr)
75 {
76 }
78 nsSimpleRef(RawRef aRawRef) : mRawRef(aRawRef)
79 {
80 }
82 bool HaveResource() const
83 {
84 return mRawRef != nullptr && mRawRef != INVALID_HANDLE_VALUE;
85 }
87 public:
88 RawRef get() const
89 {
90 return mRawRef;
91 }
93 static void Release(RawRef aRawRef)
94 {
95 if (aRawRef != nullptr && aRawRef != INVALID_HANDLE_VALUE) {
96 CloseHandle(aRawRef);
97 }
98 }
99 RawRef mRawRef;
100 };
103 template<>
104 class nsAutoRefTraits<HMODULE>
105 {
106 public:
107 typedef HMODULE RawRef;
108 static RawRef Void()
109 {
110 return nullptr;
111 }
113 static void Release(RawRef aFD)
114 {
115 if (aFD != Void()) {
116 FreeLibrary(aFD);
117 }
118 }
119 };
121 typedef nsAutoRef<HKEY> nsAutoRegKey;
122 typedef nsAutoRef<SC_HANDLE> nsAutoServiceHandle;
123 typedef nsAutoRef<HANDLE> nsAutoHandle;
124 typedef nsAutoRef<HMODULE> nsModuleHandle;
126 namespace
127 {
128 bool
129 IsRunningInWindowsMetro()
130 {
131 static bool alreadyChecked = false;
132 static bool isMetro = false;
133 if (alreadyChecked) {
134 return isMetro;
135 }
137 HMODULE user32DLL = LoadLibraryW(L"user32.dll");
138 if (!user32DLL) {
139 return false;
140 }
142 typedef BOOL (WINAPI* IsImmersiveProcessFunc)(HANDLE process);
143 IsImmersiveProcessFunc IsImmersiveProcessPtr =
144 (IsImmersiveProcessFunc)GetProcAddress(user32DLL,
145 "IsImmersiveProcess");
146 FreeLibrary(user32DLL);
147 if (!IsImmersiveProcessPtr) {
148 // isMetro is already set to false.
149 alreadyChecked = true;
150 return false;
151 }
153 isMetro = IsImmersiveProcessPtr(GetCurrentProcess());
154 alreadyChecked = true;
155 return isMetro;
156 }
158 HMODULE
159 LoadLibrarySystem32(LPCWSTR module)
160 {
161 WCHAR systemPath[MAX_PATH + 1] = { L'\0' };
163 // If GetSystemPath fails we accept that we'll load the DLLs from the
164 // normal search path.
165 GetSystemDirectoryW(systemPath, MAX_PATH + 1);
166 size_t systemDirLen = wcslen(systemPath);
168 // Make the system directory path terminate with a slash
169 if (systemDirLen && systemPath[systemDirLen - 1] != L'\\') {
170 systemPath[systemDirLen] = L'\\';
171 ++systemDirLen;
172 // No need to re-nullptr terminate
173 }
175 size_t fileLen = wcslen(module);
176 wcsncpy(systemPath + systemDirLen, module,
177 MAX_PATH - systemDirLen);
178 if (systemDirLen + fileLen <= MAX_PATH) {
179 systemPath[systemDirLen + fileLen] = L'\0';
180 } else {
181 systemPath[MAX_PATH] = L'\0';
182 }
183 return LoadLibraryW(systemPath);
184 }
185 }
187 #endif