OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "stdafx.h" |
| 6 |
| 7 #include <roerrorapi.h> |
| 8 #include <shobjidl.h> |
| 9 |
| 10 #include "base/at_exit.h" |
| 11 #include "base/command_line.h" |
| 12 #include "base/logging.h" |
| 13 #include "base/logging_win.h" |
| 14 #include "base/win/scoped_comptr.h" |
| 15 #include "win8/metro_driver/chrome_app_view.h" |
| 16 #include "win8/metro_driver/winrt_utils.h" |
| 17 #include "sandbox/win/src/sidestep/preamble_patcher.h" |
| 18 |
| 19 // TODO(siggi): Move this to GYP. |
| 20 #pragma comment(lib, "runtimeobject.lib") |
| 21 |
| 22 namespace { |
| 23 |
| 24 LONG WINAPI ErrorReportingHandler(EXCEPTION_POINTERS* ex_info) { |
| 25 // See roerrorapi.h for a description of the |
| 26 // exception codes and parameters. |
| 27 DWORD code = ex_info->ExceptionRecord->ExceptionCode; |
| 28 ULONG_PTR* info = ex_info->ExceptionRecord->ExceptionInformation; |
| 29 if (code == EXCEPTION_RO_ORIGINATEERROR) { |
| 30 string16 msg(reinterpret_cast<wchar_t*>(info[2]), info[1]); |
| 31 LOG(ERROR) << "VEH: Metro error 0x" << std::hex << info[0] << ": " << msg; |
| 32 } else if (code == EXCEPTION_RO_TRANSFORMERROR) { |
| 33 string16 msg(reinterpret_cast<wchar_t*>(info[3]), info[2]); |
| 34 LOG(ERROR) << "VEH: Metro old error 0x" << std::hex << info[0] |
| 35 << " new error 0x" << info[1] << ": " << msg; |
| 36 } |
| 37 |
| 38 return EXCEPTION_CONTINUE_SEARCH; |
| 39 } |
| 40 |
| 41 // TODO(robertshield): This GUID is hard-coded in a bunch of places that |
| 42 // don't allow explicit includes. Find a single place for it to live. |
| 43 // {7FE69228-633E-4f06-80C1-527FEA23E3A7} |
| 44 const GUID kChromeTraceProviderName = { |
| 45 0x7fe69228, 0x633e, 0x4f06, |
| 46 { 0x80, 0xc1, 0x52, 0x7f, 0xea, 0x23, 0xe3, 0xa7 } }; |
| 47 |
| 48 } |
| 49 // Required for base initialization. |
| 50 // TODO(siggi): This should be handled better, as this way our at exit |
| 51 // registrations will run under the loader's lock. However, |
| 52 // once metro_driver is merged into Chrome.dll, this will go away anyhow. |
| 53 base::AtExitManager at_exit; |
| 54 |
| 55 namespace Hacks { |
| 56 |
| 57 typedef BOOL (WINAPI* IsImmersiveFunctionPtr)(HANDLE process); |
| 58 char* g_real_is_immersive_proc_stub = NULL; |
| 59 |
| 60 HMODULE g_webrtc_quartz_dll_handle = NULL; |
| 61 bool g_fake_is_immersive_process_ret = false; |
| 62 |
| 63 BOOL WINAPI MetroChromeIsImmersiveIntercept(HANDLE process) { |
| 64 if (g_fake_is_immersive_process_ret && process == ::GetCurrentProcess()) |
| 65 return FALSE; |
| 66 |
| 67 IsImmersiveFunctionPtr real_proc = |
| 68 reinterpret_cast<IsImmersiveFunctionPtr>( |
| 69 static_cast<char*>(g_real_is_immersive_proc_stub)); |
| 70 return real_proc(process); |
| 71 } |
| 72 |
| 73 void MetroSpecificHacksInitialize() { |
| 74 // The quartz dll which is used by the webrtc code in Chrome fails in a metro |
| 75 // app. It checks this via the IsImmersiveProcess export in user32. We |
| 76 // intercept the same and spoof the value as false. This is ok as technically |
| 77 // a metro browser is not a real metro application. The webrtc functionality |
| 78 // works fine with this hack. |
| 79 // TODO(tommi) |
| 80 // BUG:- https://code.google.com/p/chromium/issues/detail?id=140545 |
| 81 // We should look into using media foundation on windows 8 in metro chrome. |
| 82 IsImmersiveFunctionPtr is_immersive_func_address = |
| 83 reinterpret_cast<IsImmersiveFunctionPtr>(::GetProcAddress( |
| 84 ::GetModuleHandle(L"user32.dll"), "IsImmersiveProcess")); |
| 85 DCHECK(is_immersive_func_address); |
| 86 |
| 87 // Allow the function to be patched by changing the protections on the page. |
| 88 DWORD old_protect = 0; |
| 89 ::VirtualProtect(is_immersive_func_address, 5, PAGE_EXECUTE_READWRITE, |
| 90 &old_protect); |
| 91 |
| 92 DCHECK(g_real_is_immersive_proc_stub == NULL); |
| 93 g_real_is_immersive_proc_stub = reinterpret_cast<char*>(VirtualAllocEx( |
| 94 ::GetCurrentProcess(), NULL, sidestep::kMaxPreambleStubSize, |
| 95 MEM_COMMIT, PAGE_EXECUTE_READWRITE)); |
| 96 DCHECK(g_real_is_immersive_proc_stub); |
| 97 |
| 98 sidestep::SideStepError patch_result = |
| 99 sidestep::PreamblePatcher::Patch( |
| 100 is_immersive_func_address, MetroChromeIsImmersiveIntercept, |
| 101 g_real_is_immersive_proc_stub, sidestep::kMaxPreambleStubSize); |
| 102 |
| 103 DCHECK(patch_result == sidestep::SIDESTEP_SUCCESS); |
| 104 |
| 105 // Restore the permissions on the page in user32 containing the |
| 106 // IsImmersiveProcess function code. |
| 107 DWORD dummy = 0; |
| 108 ::VirtualProtect(is_immersive_func_address, 5, old_protect, &dummy); |
| 109 |
| 110 // Mimic the original page permissions from the IsImmersiveProcess page |
| 111 // on our stub. |
| 112 ::VirtualProtect(g_real_is_immersive_proc_stub, |
| 113 sidestep::kMaxPreambleStubSize, |
| 114 old_protect, |
| 115 &old_protect); |
| 116 |
| 117 g_fake_is_immersive_process_ret = true; |
| 118 g_webrtc_quartz_dll_handle = LoadLibrary(L"quartz.dll"); |
| 119 g_fake_is_immersive_process_ret = false; |
| 120 |
| 121 DCHECK(g_webrtc_quartz_dll_handle); |
| 122 if (!g_webrtc_quartz_dll_handle) { |
| 123 DVLOG(1) << "Quartz dll load failed with error: " << GetLastError(); |
| 124 } else { |
| 125 // Pin the quartz module to protect against it being inadvarently unloaded. |
| 126 ::GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_PIN, L"quartz.dll", |
| 127 &g_webrtc_quartz_dll_handle); |
| 128 } |
| 129 } |
| 130 |
| 131 } // namespace Hacks |
| 132 |
| 133 extern "C" __declspec(dllexport) |
| 134 int InitMetro(LPTHREAD_START_ROUTINE thread_proc, void* context) { |
| 135 // Initialize the command line. |
| 136 CommandLine::Init(0, NULL); |
| 137 logging::InitLogging( |
| 138 NULL, |
| 139 logging::LOG_ONLY_TO_SYSTEM_DEBUG_LOG, |
| 140 logging::LOCK_LOG_FILE, |
| 141 logging::DELETE_OLD_LOG_FILE, |
| 142 logging::DISABLE_DCHECK_FOR_NON_OFFICIAL_RELEASE_BUILDS); |
| 143 |
| 144 #if defined(NDEBUG) |
| 145 logging::SetMinLogLevel(logging::LOG_ERROR); |
| 146 // Bind to the breakpad handling function. |
| 147 globals.breakpad_exception_handler = |
| 148 reinterpret_cast<BreakpadExceptionHandler>( |
| 149 ::GetProcAddress(::GetModuleHandle(NULL), |
| 150 "CrashForException")); |
| 151 if (!globals.breakpad_exception_handler) { |
| 152 DVLOG(0) << "CrashForException export not found"; |
| 153 } |
| 154 #else |
| 155 logging::SetMinLogLevel(logging::LOG_VERBOSE); |
| 156 // Set the error reporting flags to always raise an exception, |
| 157 // which is then processed by our vectored exception handling |
| 158 // above to log the error message. |
| 159 winfoundtn::Diagnostics::SetErrorReportingFlags( |
| 160 winfoundtn::Diagnostics::UseSetErrorInfo | |
| 161 winfoundtn::Diagnostics::ForceExceptions); |
| 162 |
| 163 HANDLE registration = |
| 164 ::AddVectoredExceptionHandler(TRUE, ErrorReportingHandler); |
| 165 #endif |
| 166 |
| 167 // Enable trace control and transport through event tracing for Windows. |
| 168 logging::LogEventProvider::Initialize(kChromeTraceProviderName); |
| 169 |
| 170 DVLOG(1) << "InitMetro"; |
| 171 |
| 172 mswrw::RoInitializeWrapper ro_initializer(RO_INIT_MULTITHREADED); |
| 173 CheckHR(ro_initializer, "RoInitialize failed"); |
| 174 |
| 175 mswr::ComPtr<winapp::Core::ICoreApplication> core_app; |
| 176 HRESULT hr = winrt_utils::CreateActivationFactory( |
| 177 RuntimeClass_Windows_ApplicationModel_Core_CoreApplication, |
| 178 core_app.GetAddressOf()); |
| 179 CheckHR(hr, "Failed to create app factory"); |
| 180 if (FAILED(hr)) |
| 181 return 1; |
| 182 |
| 183 // The metro specific hacks code assumes that there is only one thread active |
| 184 // at the moment. This better be the case or we may have race conditions. |
| 185 Hacks::MetroSpecificHacksInitialize(); |
| 186 |
| 187 auto view_factory = mswr::Make<ChromeAppViewFactory>( |
| 188 core_app.Get(), thread_proc, context); |
| 189 hr = core_app->Run(view_factory.Get()); |
| 190 DVLOG(1) << "exiting InitMetro, hr=" << hr; |
| 191 |
| 192 #if !defined(NDEBUG) |
| 193 ::RemoveVectoredExceptionHandler(registration); |
| 194 #endif |
| 195 |
| 196 return hr; |
| 197 } |
| 198 |
| 199 // Activates the application known by |app_id|. Returns, among other things, |
| 200 // E_APPLICATION_NOT_REGISTERED if |app_id| identifies Chrome and Chrome is not |
| 201 // the default browser. |
| 202 extern "C" __declspec(dllexport) |
| 203 HRESULT ActivateApplication(const wchar_t* app_id) { |
| 204 base::win::ScopedComPtr<IApplicationActivationManager> activator; |
| 205 HRESULT hr = activator.CreateInstance(CLSID_ApplicationActivationManager); |
| 206 if (SUCCEEDED(hr)) { |
| 207 DWORD pid = 0; |
| 208 hr = activator->ActivateApplication(app_id, L"", AO_NONE, &pid); |
| 209 } |
| 210 return hr; |
| 211 } |
OLD | NEW |