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