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