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 #include "win8/metro_driver/metro_driver.h" 7 8 #include <roerrorapi.h> 9 #include <shobjidl.h> 10 11 #include "base/at_exit.h" 12 #include "base/command_line.h" 13 #include "base/logging.h" 14 #include "base/logging_win.h" 15 #include "base/win/scoped_comptr.h" 16 #include "base/win/windows_version.h" 17 #include "win8/metro_driver/winrt_utils.h" 18 19 namespace { 20 21 LONG WINAPI ErrorReportingHandler(EXCEPTION_POINTERS* ex_info) { 22 // See roerrorapi.h for a description of the 23 // exception codes and parameters. 24 DWORD code = ex_info->ExceptionRecord->ExceptionCode; 25 ULONG_PTR* info = ex_info->ExceptionRecord->ExceptionInformation; 26 if (code == EXCEPTION_RO_ORIGINATEERROR) { 27 base::string16 msg(reinterpret_cast<wchar_t*>(info[2]), info[1]); 28 LOG(ERROR) << "VEH: Metro error 0x" << std::hex << info[0] << ": " << msg; 29 } else if (code == EXCEPTION_RO_TRANSFORMERROR) { 30 base::string16 msg(reinterpret_cast<wchar_t*>(info[3]), info[2]); 31 LOG(ERROR) << "VEH: Metro old error 0x" << std::hex << info[0] 32 << " new error 0x" << info[1] << ": " << msg; 33 } 34 35 return EXCEPTION_CONTINUE_SEARCH; 36 } 37 38 void SetMetroReportingFlags() { 39 #if !defined(NDEBUG) 40 // Set the error reporting flags to always raise an exception, 41 // which is then processed by our vectored exception handling 42 // above to log the error message. 43 winfoundtn::Diagnostics::SetErrorReportingFlags( 44 winfoundtn::Diagnostics::UseSetErrorInfo | 45 winfoundtn::Diagnostics::ForceExceptions); 46 #endif 47 } 48 49 // TODO(robertshield): This GUID is hard-coded in a bunch of places that 50 // don't allow explicit includes. Find a single place for it to live. 51 // {7FE69228-633E-4f06-80C1-527FEA23E3A7} 52 const GUID kChromeTraceProviderName = { 53 0x7fe69228, 0x633e, 0x4f06, 54 { 0x80, 0xc1, 0x52, 0x7f, 0xea, 0x23, 0xe3, 0xa7 } }; 55 56 } // namespace 57 58 #if !defined(COMPONENT_BUILD) 59 // Required for base initialization. 60 // TODO(siggi): This should be handled better, as this way our at exit 61 // registrations will run under the loader's lock. However, 62 // once metro_driver is merged into Chrome.dll, this will go away anyhow. 63 base::AtExitManager at_exit; 64 #endif 65 66 mswr::ComPtr<winapp::Core::ICoreApplication> InitWindows8() { 67 SetMetroReportingFlags(); 68 HRESULT hr = ::Windows::Foundation::Initialize(RO_INIT_MULTITHREADED); 69 if (FAILED(hr)) 70 CHECK(false); 71 mswr::ComPtr<winapp::Core::ICoreApplication> core_app; 72 hr = winrt_utils::CreateActivationFactory( 73 RuntimeClass_Windows_ApplicationModel_Core_CoreApplication, 74 core_app.GetAddressOf()); 75 if (FAILED(hr)) 76 CHECK(false); 77 return core_app; 78 } 79 80 mswr::ComPtr<winapp::Core::ICoreApplication> InitWindows7(); 81 82 extern "C" __declspec(dllexport) 83 int InitMetro() { 84 // Metro mode or its emulation is not supported in Vista or XP. 85 if (base::win::GetVersion() < base::win::VERSION_WIN7) 86 return 1; 87 // Initialize the command line. 88 CommandLine::Init(0, NULL); 89 // Initialize the logging system. 90 logging::LoggingSettings settings; 91 settings.logging_dest = logging::LOG_TO_SYSTEM_DEBUG_LOG; 92 logging::InitLogging(settings); 93 #if defined(NDEBUG) 94 logging::SetMinLogLevel(logging::LOG_ERROR); 95 #else 96 logging::SetMinLogLevel(logging::LOG_VERBOSE); 97 HANDLE registration = 98 ::AddVectoredExceptionHandler(TRUE, ErrorReportingHandler); 99 #endif 100 // Enable trace control and transport through event tracing for Windows. 101 logging::LogEventProvider::Initialize(kChromeTraceProviderName); 102 DVLOG(1) << "InitMetro"; 103 104 // OS specific initialization. 105 mswr::ComPtr<winapp::Core::ICoreApplication> core_app; 106 if (base::win::GetVersion() < base::win::VERSION_WIN8) 107 core_app = InitWindows7(); 108 else 109 core_app = InitWindows8(); 110 111 auto view_factory = mswr::Make<ChromeAppViewFactory>(core_app.Get()); 112 HRESULT hr = core_app->Run(view_factory.Get()); 113 DVLOG(1) << "exiting InitMetro, hr=" << hr; 114 115 #if !defined(NDEBUG) 116 ::RemoveVectoredExceptionHandler(registration); 117 #endif 118 return hr; 119 } 120 121 // Activates the application known by |app_id|. Returns, among other things, 122 // E_APPLICATION_NOT_REGISTERED if |app_id| identifies Chrome and Chrome is not 123 // the default browser. 124 extern "C" __declspec(dllexport) 125 HRESULT ActivateApplication(const wchar_t* app_id) { 126 base::win::ScopedComPtr<IApplicationActivationManager> activator; 127 HRESULT hr = activator.CreateInstance(CLSID_ApplicationActivationManager); 128 if (SUCCEEDED(hr)) { 129 DWORD pid = 0; 130 hr = activator->ActivateApplication(app_id, L"", AO_NONE, &pid); 131 } 132 return hr; 133 } 134