browser/metro/shell/commandexecutehandler/CEHHelper.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/browser/metro/shell/commandexecutehandler/CEHHelper.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,182 @@
     1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this file,
     1.7 + * You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +#include "CEHHelper.h"
    1.10 +#include <tlhelp32.h>
    1.11 +#include "mozilla/widget/MetroD3DCheckHelper.h"
    1.12 +
    1.13 +#ifdef SHOW_CONSOLE
    1.14 +#include <io.h> // _open_osfhandle
    1.15 +#endif
    1.16 +
    1.17 +HANDLE sCon;
    1.18 +LPCWSTR metroDX10Available = L"MetroD3DAvailable";
    1.19 +LPCWSTR metroLastAHE = L"MetroLastAHE";
    1.20 +LPCWSTR cehDumpDebugStrings = L"CEHDump";
    1.21 +extern const WCHAR* kFirefoxExe;
    1.22 +
    1.23 +void
    1.24 +Log(const wchar_t *fmt, ...)
    1.25 +{
    1.26 +#if !defined(SHOW_CONSOLE)
    1.27 +  DWORD dwRes = 0;
    1.28 +  if (!GetDWORDRegKey(cehDumpDebugStrings, dwRes) || !dwRes) {
    1.29 +    return;
    1.30 +  }
    1.31 +#endif
    1.32 +  va_list a = nullptr;
    1.33 +  wchar_t szDebugString[1024];
    1.34 +  if(!lstrlenW(fmt))
    1.35 +    return;
    1.36 +  va_start(a,fmt);
    1.37 +  vswprintf(szDebugString, 1024, fmt, a);
    1.38 +  va_end(a);
    1.39 +  if(!lstrlenW(szDebugString))
    1.40 +    return;
    1.41 +
    1.42 +  DWORD len;
    1.43 +  WriteConsoleW(sCon, szDebugString, lstrlenW(szDebugString), &len, nullptr);
    1.44 +  WriteConsoleW(sCon, L"\n", 1, &len, nullptr);
    1.45 +
    1.46 +  OutputDebugStringW(szDebugString);
    1.47 +  OutputDebugStringW(L"\n");
    1.48 +}
    1.49 +
    1.50 +#if defined(SHOW_CONSOLE)
    1.51 +void
    1.52 +SetupConsole()
    1.53 +{
    1.54 +  FILE *fp;
    1.55 +  AllocConsole();
    1.56 +  sCon = GetStdHandle(STD_OUTPUT_HANDLE); 
    1.57 +  int fd = _open_osfhandle(reinterpret_cast<intptr_t>(sCon), 0);
    1.58 +  fp = _fdopen(fd, "w");
    1.59 +  *stdout = *fp;
    1.60 +  setvbuf(stdout, nullptr, _IONBF, 0);
    1.61 +}
    1.62 +#endif
    1.63 +
    1.64 +bool
    1.65 +IsImmersiveProcessDynamic(HANDLE process)
    1.66 +{
    1.67 +  HMODULE user32DLL = LoadLibraryW(L"user32.dll");
    1.68 +  if (!user32DLL) {
    1.69 +    return false;
    1.70 +  }
    1.71 +
    1.72 +  decltype(IsImmersiveProcess)* IsImmersiveProcessPtr =
    1.73 +    (decltype(IsImmersiveProcess)*) GetProcAddress(user32DLL,
    1.74 +                                                   "IsImmersiveProcess");
    1.75 +  if (!IsImmersiveProcessPtr) {
    1.76 +    FreeLibrary(user32DLL);
    1.77 +    return false;
    1.78 +  }
    1.79 +
    1.80 +  BOOL bImmersiveProcess = IsImmersiveProcessPtr(process);
    1.81 +  FreeLibrary(user32DLL);
    1.82 +  return bImmersiveProcess;
    1.83 +}
    1.84 +
    1.85 +bool
    1.86 +IsProcessRunning(const wchar_t *processName, bool bCheckIfMetro)
    1.87 +{
    1.88 +  bool exists = false;
    1.89 +  PROCESSENTRY32W entry;
    1.90 +  entry.dwSize = sizeof(PROCESSENTRY32W);
    1.91 +
    1.92 +  HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
    1.93 +
    1.94 +  if (Process32First(snapshot, &entry)) {
    1.95 +    while (!exists && Process32Next(snapshot, &entry)) {
    1.96 +      if (!_wcsicmp(entry.szExeFile, processName)) {
    1.97 +        HANDLE process = OpenProcess(GENERIC_READ, FALSE, entry.th32ProcessID);
    1.98 +        bool isImmersiveProcess = IsImmersiveProcessDynamic(process);
    1.99 +        if ((bCheckIfMetro && isImmersiveProcess) ||
   1.100 +            (!bCheckIfMetro && !isImmersiveProcess)) {
   1.101 +          exists = true;
   1.102 +        }
   1.103 +        CloseHandle(process);
   1.104 +      }
   1.105 +    }
   1.106 +  }
   1.107 +
   1.108 +  CloseHandle(snapshot);
   1.109 +  return exists;
   1.110 +}
   1.111 +
   1.112 +bool
   1.113 +IsMetroProcessRunning()
   1.114 +{
   1.115 +  return IsProcessRunning(kFirefoxExe, true);
   1.116 +}
   1.117 +
   1.118 +bool
   1.119 +IsDesktopProcessRunning()
   1.120 +{
   1.121 +  return IsProcessRunning(kFirefoxExe, false);
   1.122 +}
   1.123 +
   1.124 +/*
   1.125 + * Retrieve the last front end ui we launched so we can target it
   1.126 + * again. This value is updated down in nsAppRunner when the browser
   1.127 + * starts up.
   1.128 + */
   1.129 +AHE_TYPE
   1.130 +GetLastAHE()
   1.131 +{
   1.132 +  DWORD ahe;
   1.133 +  if (GetDWORDRegKey(metroLastAHE, ahe)) {
   1.134 +    return (AHE_TYPE) ahe;
   1.135 +  }
   1.136 +  return AHE_DESKTOP;
   1.137 +}
   1.138 +
   1.139 +bool
   1.140 +IsDX10Available()
   1.141 +{
   1.142 +  DWORD isDX10Available;
   1.143 +  if (GetDWORDRegKey(metroDX10Available, isDX10Available)) {
   1.144 +    return isDX10Available;
   1.145 +  }
   1.146 +  bool check = D3DFeatureLevelCheck();
   1.147 +  SetDWORDRegKey(metroDX10Available, check);
   1.148 +  return check;
   1.149 +}
   1.150 +
   1.151 +bool
   1.152 +GetDWORDRegKey(LPCWSTR name, DWORD &value)
   1.153 +{
   1.154 +  HKEY key;
   1.155 +  LONG result = RegOpenKeyExW(HKEY_CURRENT_USER, L"SOFTWARE\\Mozilla\\Firefox",
   1.156 +                              0, KEY_READ, &key);
   1.157 +  if (result != ERROR_SUCCESS) {
   1.158 +    return false;
   1.159 +  }
   1.160 +
   1.161 +  DWORD bufferSize = sizeof(DWORD);
   1.162 +  DWORD type;
   1.163 +  result = RegQueryValueExW(key, name, nullptr, &type,
   1.164 +                            reinterpret_cast<LPBYTE>(&value),
   1.165 +                            &bufferSize);
   1.166 +  RegCloseKey(key);
   1.167 +  return result == ERROR_SUCCESS;
   1.168 +}
   1.169 +
   1.170 +bool
   1.171 +SetDWORDRegKey(LPCWSTR name, DWORD value)
   1.172 +{
   1.173 +  HKEY key;
   1.174 +  LONG result = RegOpenKeyExW(HKEY_CURRENT_USER, L"SOFTWARE\\Mozilla\\Firefox",
   1.175 +                              0, KEY_WRITE, &key);
   1.176 +  if (result != ERROR_SUCCESS) {
   1.177 +    return false;
   1.178 +  }
   1.179 +
   1.180 +  result = RegSetValueEx(key, name, 0, REG_DWORD,
   1.181 +                         reinterpret_cast<LPBYTE>(&value),
   1.182 +                         sizeof(DWORD));
   1.183 +  RegCloseKey(key);
   1.184 +  return result == ERROR_SUCCESS;
   1.185 +}

mercurial