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"
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
;
35 return EXCEPTION_CONTINUE_SEARCH
;
38 void SetMetroReportingFlags() {
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
);
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 } };
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
;
66 mswr::ComPtr
<winapp::Core::ICoreApplication
> InitWindows8() {
67 SetMetroReportingFlags();
68 HRESULT hr
= ::Windows::Foundation::Initialize(RO_INIT_MULTITHREADED
);
71 mswr::ComPtr
<winapp::Core::ICoreApplication
> core_app
;
72 hr
= winrt_utils::CreateActivationFactory(
73 RuntimeClass_Windows_ApplicationModel_Core_CoreApplication
,
74 core_app
.GetAddressOf());
80 mswr::ComPtr
<winapp::Core::ICoreApplication
> InitWindows7();
82 extern "C" __declspec(dllexport
)
84 // Metro mode or its emulation is not supported in Vista or XP.
85 if (base::win::GetVersion() < base::win::VERSION_WIN7
)
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
);
94 logging::SetMinLogLevel(logging::LOG_ERROR
);
96 logging::SetMinLogLevel(logging::LOG_VERBOSE
);
98 ::AddVectoredExceptionHandler(TRUE
, ErrorReportingHandler
);
100 // Enable trace control and transport through event tracing for Windows.
101 logging::LogEventProvider::Initialize(kChromeTraceProviderName
);
102 DVLOG(1) << "InitMetro";
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();
109 core_app
= InitWindows8();
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
;
116 ::RemoveVectoredExceptionHandler(registration
);
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
);
130 hr
= activator
->ActivateApplication(app_id
, L
"", AO_NONE
, &pid
);