Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
michael@0 | 4 | * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | #include "CEHHelper.h" |
michael@0 | 7 | #include <tlhelp32.h> |
michael@0 | 8 | #include "mozilla/widget/MetroD3DCheckHelper.h" |
michael@0 | 9 | |
michael@0 | 10 | #ifdef SHOW_CONSOLE |
michael@0 | 11 | #include <io.h> // _open_osfhandle |
michael@0 | 12 | #endif |
michael@0 | 13 | |
michael@0 | 14 | HANDLE sCon; |
michael@0 | 15 | LPCWSTR metroDX10Available = L"MetroD3DAvailable"; |
michael@0 | 16 | LPCWSTR metroLastAHE = L"MetroLastAHE"; |
michael@0 | 17 | LPCWSTR cehDumpDebugStrings = L"CEHDump"; |
michael@0 | 18 | extern const WCHAR* kFirefoxExe; |
michael@0 | 19 | |
michael@0 | 20 | void |
michael@0 | 21 | Log(const wchar_t *fmt, ...) |
michael@0 | 22 | { |
michael@0 | 23 | #if !defined(SHOW_CONSOLE) |
michael@0 | 24 | DWORD dwRes = 0; |
michael@0 | 25 | if (!GetDWORDRegKey(cehDumpDebugStrings, dwRes) || !dwRes) { |
michael@0 | 26 | return; |
michael@0 | 27 | } |
michael@0 | 28 | #endif |
michael@0 | 29 | va_list a = nullptr; |
michael@0 | 30 | wchar_t szDebugString[1024]; |
michael@0 | 31 | if(!lstrlenW(fmt)) |
michael@0 | 32 | return; |
michael@0 | 33 | va_start(a,fmt); |
michael@0 | 34 | vswprintf(szDebugString, 1024, fmt, a); |
michael@0 | 35 | va_end(a); |
michael@0 | 36 | if(!lstrlenW(szDebugString)) |
michael@0 | 37 | return; |
michael@0 | 38 | |
michael@0 | 39 | DWORD len; |
michael@0 | 40 | WriteConsoleW(sCon, szDebugString, lstrlenW(szDebugString), &len, nullptr); |
michael@0 | 41 | WriteConsoleW(sCon, L"\n", 1, &len, nullptr); |
michael@0 | 42 | |
michael@0 | 43 | OutputDebugStringW(szDebugString); |
michael@0 | 44 | OutputDebugStringW(L"\n"); |
michael@0 | 45 | } |
michael@0 | 46 | |
michael@0 | 47 | #if defined(SHOW_CONSOLE) |
michael@0 | 48 | void |
michael@0 | 49 | SetupConsole() |
michael@0 | 50 | { |
michael@0 | 51 | FILE *fp; |
michael@0 | 52 | AllocConsole(); |
michael@0 | 53 | sCon = GetStdHandle(STD_OUTPUT_HANDLE); |
michael@0 | 54 | int fd = _open_osfhandle(reinterpret_cast<intptr_t>(sCon), 0); |
michael@0 | 55 | fp = _fdopen(fd, "w"); |
michael@0 | 56 | *stdout = *fp; |
michael@0 | 57 | setvbuf(stdout, nullptr, _IONBF, 0); |
michael@0 | 58 | } |
michael@0 | 59 | #endif |
michael@0 | 60 | |
michael@0 | 61 | bool |
michael@0 | 62 | IsImmersiveProcessDynamic(HANDLE process) |
michael@0 | 63 | { |
michael@0 | 64 | HMODULE user32DLL = LoadLibraryW(L"user32.dll"); |
michael@0 | 65 | if (!user32DLL) { |
michael@0 | 66 | return false; |
michael@0 | 67 | } |
michael@0 | 68 | |
michael@0 | 69 | decltype(IsImmersiveProcess)* IsImmersiveProcessPtr = |
michael@0 | 70 | (decltype(IsImmersiveProcess)*) GetProcAddress(user32DLL, |
michael@0 | 71 | "IsImmersiveProcess"); |
michael@0 | 72 | if (!IsImmersiveProcessPtr) { |
michael@0 | 73 | FreeLibrary(user32DLL); |
michael@0 | 74 | return false; |
michael@0 | 75 | } |
michael@0 | 76 | |
michael@0 | 77 | BOOL bImmersiveProcess = IsImmersiveProcessPtr(process); |
michael@0 | 78 | FreeLibrary(user32DLL); |
michael@0 | 79 | return bImmersiveProcess; |
michael@0 | 80 | } |
michael@0 | 81 | |
michael@0 | 82 | bool |
michael@0 | 83 | IsProcessRunning(const wchar_t *processName, bool bCheckIfMetro) |
michael@0 | 84 | { |
michael@0 | 85 | bool exists = false; |
michael@0 | 86 | PROCESSENTRY32W entry; |
michael@0 | 87 | entry.dwSize = sizeof(PROCESSENTRY32W); |
michael@0 | 88 | |
michael@0 | 89 | HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); |
michael@0 | 90 | |
michael@0 | 91 | if (Process32First(snapshot, &entry)) { |
michael@0 | 92 | while (!exists && Process32Next(snapshot, &entry)) { |
michael@0 | 93 | if (!_wcsicmp(entry.szExeFile, processName)) { |
michael@0 | 94 | HANDLE process = OpenProcess(GENERIC_READ, FALSE, entry.th32ProcessID); |
michael@0 | 95 | bool isImmersiveProcess = IsImmersiveProcessDynamic(process); |
michael@0 | 96 | if ((bCheckIfMetro && isImmersiveProcess) || |
michael@0 | 97 | (!bCheckIfMetro && !isImmersiveProcess)) { |
michael@0 | 98 | exists = true; |
michael@0 | 99 | } |
michael@0 | 100 | CloseHandle(process); |
michael@0 | 101 | } |
michael@0 | 102 | } |
michael@0 | 103 | } |
michael@0 | 104 | |
michael@0 | 105 | CloseHandle(snapshot); |
michael@0 | 106 | return exists; |
michael@0 | 107 | } |
michael@0 | 108 | |
michael@0 | 109 | bool |
michael@0 | 110 | IsMetroProcessRunning() |
michael@0 | 111 | { |
michael@0 | 112 | return IsProcessRunning(kFirefoxExe, true); |
michael@0 | 113 | } |
michael@0 | 114 | |
michael@0 | 115 | bool |
michael@0 | 116 | IsDesktopProcessRunning() |
michael@0 | 117 | { |
michael@0 | 118 | return IsProcessRunning(kFirefoxExe, false); |
michael@0 | 119 | } |
michael@0 | 120 | |
michael@0 | 121 | /* |
michael@0 | 122 | * Retrieve the last front end ui we launched so we can target it |
michael@0 | 123 | * again. This value is updated down in nsAppRunner when the browser |
michael@0 | 124 | * starts up. |
michael@0 | 125 | */ |
michael@0 | 126 | AHE_TYPE |
michael@0 | 127 | GetLastAHE() |
michael@0 | 128 | { |
michael@0 | 129 | DWORD ahe; |
michael@0 | 130 | if (GetDWORDRegKey(metroLastAHE, ahe)) { |
michael@0 | 131 | return (AHE_TYPE) ahe; |
michael@0 | 132 | } |
michael@0 | 133 | return AHE_DESKTOP; |
michael@0 | 134 | } |
michael@0 | 135 | |
michael@0 | 136 | bool |
michael@0 | 137 | IsDX10Available() |
michael@0 | 138 | { |
michael@0 | 139 | DWORD isDX10Available; |
michael@0 | 140 | if (GetDWORDRegKey(metroDX10Available, isDX10Available)) { |
michael@0 | 141 | return isDX10Available; |
michael@0 | 142 | } |
michael@0 | 143 | bool check = D3DFeatureLevelCheck(); |
michael@0 | 144 | SetDWORDRegKey(metroDX10Available, check); |
michael@0 | 145 | return check; |
michael@0 | 146 | } |
michael@0 | 147 | |
michael@0 | 148 | bool |
michael@0 | 149 | GetDWORDRegKey(LPCWSTR name, DWORD &value) |
michael@0 | 150 | { |
michael@0 | 151 | HKEY key; |
michael@0 | 152 | LONG result = RegOpenKeyExW(HKEY_CURRENT_USER, L"SOFTWARE\\Mozilla\\Firefox", |
michael@0 | 153 | 0, KEY_READ, &key); |
michael@0 | 154 | if (result != ERROR_SUCCESS) { |
michael@0 | 155 | return false; |
michael@0 | 156 | } |
michael@0 | 157 | |
michael@0 | 158 | DWORD bufferSize = sizeof(DWORD); |
michael@0 | 159 | DWORD type; |
michael@0 | 160 | result = RegQueryValueExW(key, name, nullptr, &type, |
michael@0 | 161 | reinterpret_cast<LPBYTE>(&value), |
michael@0 | 162 | &bufferSize); |
michael@0 | 163 | RegCloseKey(key); |
michael@0 | 164 | return result == ERROR_SUCCESS; |
michael@0 | 165 | } |
michael@0 | 166 | |
michael@0 | 167 | bool |
michael@0 | 168 | SetDWORDRegKey(LPCWSTR name, DWORD value) |
michael@0 | 169 | { |
michael@0 | 170 | HKEY key; |
michael@0 | 171 | LONG result = RegOpenKeyExW(HKEY_CURRENT_USER, L"SOFTWARE\\Mozilla\\Firefox", |
michael@0 | 172 | 0, KEY_WRITE, &key); |
michael@0 | 173 | if (result != ERROR_SUCCESS) { |
michael@0 | 174 | return false; |
michael@0 | 175 | } |
michael@0 | 176 | |
michael@0 | 177 | result = RegSetValueEx(key, name, 0, REG_DWORD, |
michael@0 | 178 | reinterpret_cast<LPBYTE>(&value), |
michael@0 | 179 | sizeof(DWORD)); |
michael@0 | 180 | RegCloseKey(key); |
michael@0 | 181 | return result == ERROR_SUCCESS; |
michael@0 | 182 | } |