Update V8 to version 4.7.21.
[chromium-blink-merge.git] / win8 / metro_driver / metro_driver.cc
blob650b23837612ff9539939730c9b113da5a24c525
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.
5 #include "stdafx.h"
6 #include "win8/metro_driver/metro_driver.h"
8 #include <roerrorapi.h>
9 #include <shobjidl.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"
19 namespace {
21 #if !defined(NDEBUG)
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;
38 #endif
40 void SetMetroReportingFlags() {
41 #if !defined(NDEBUG)
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);
48 #endif
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 } };
58 } // namespace
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;
66 #endif
68 mswr::ComPtr<winapp::Core::ICoreApplication> InitWindows8() {
69 SetMetroReportingFlags();
70 HRESULT hr = ::Windows::Foundation::Initialize(RO_INIT_MULTITHREADED);
71 if (FAILED(hr))
72 CHECK(false);
73 mswr::ComPtr<winapp::Core::ICoreApplication> core_app;
74 hr = winrt_utils::CreateActivationFactory(
75 RuntimeClass_Windows_ApplicationModel_Core_CoreApplication,
76 core_app.GetAddressOf());
77 if (FAILED(hr))
78 CHECK(false);
79 return core_app;
82 mswr::ComPtr<winapp::Core::ICoreApplication> InitWindows7();
84 extern "C" __declspec(dllexport)
85 int InitMetro() {
86 // Metro mode or its emulation is not supported in Vista or XP.
87 if (base::win::GetVersion() < base::win::VERSION_WIN7)
88 return 1;
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);
95 #if defined(NDEBUG)
96 logging::SetMinLogLevel(logging::LOG_ERROR);
97 #else
98 logging::SetMinLogLevel(logging::LOG_VERBOSE);
99 HANDLE registration =
100 ::AddVectoredExceptionHandler(TRUE, ErrorReportingHandler);
101 #endif
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();
110 else
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;
117 #if !defined(NDEBUG)
118 ::RemoveVectoredExceptionHandler(registration);
119 #endif
120 return hr;
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);
130 if (SUCCEEDED(hr)) {
131 DWORD pid = 0;
132 hr = activator->ActivateApplication(app_id, L"", AO_NONE, &pid);
134 return hr;