Add ICU message format support
[chromium-blink-merge.git] / content / browser / browser_main_runner.cc
blobc061658dfb49fd57ba3e1c07e5fac60d2816acb1
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 "content/public/browser/browser_main_runner.h"
7 #include "base/base_switches.h"
8 #include "base/command_line.h"
9 #include "base/debug/leak_annotations.h"
10 #include "base/logging.h"
11 #include "base/metrics/histogram.h"
12 #include "base/metrics/statistics_recorder.h"
13 #include "base/profiler/scoped_profile.h"
14 #include "base/profiler/scoped_tracker.h"
15 #include "base/trace_event/trace_event.h"
16 #include "base/tracked_objects.h"
17 #include "content/browser/browser_main_loop.h"
18 #include "content/browser/browser_shutdown_profile_dumper.h"
19 #include "content/browser/notification_service_impl.h"
20 #include "content/public/common/content_switches.h"
21 #include "content/public/common/main_function_params.h"
22 #include "ui/base/ime/input_method_initializer.h"
24 #if defined(OS_WIN)
25 #include "base/win/win_util.h"
26 #include "base/win/windows_version.h"
27 #include "net/cert/sha256_legacy_support_win.h"
28 #include "sandbox/win/src/sidestep/preamble_patcher.h"
29 #include "ui/base/win/scoped_ole_initializer.h"
30 #include "ui/gfx/switches.h"
31 #include "ui/gfx/win/direct_write.h"
32 #endif
34 bool g_exited_main_message_loop = false;
36 namespace content {
38 #if defined(OS_WIN)
39 namespace {
41 // Pointer to the original CryptVerifyCertificateSignatureEx function.
42 net::sha256_interception::CryptVerifyCertificateSignatureExFunc
43 g_real_crypt_verify_signature_stub = NULL;
45 // Stub function that is called whenever the Crypt32 function
46 // CryptVerifyCertificateSignatureEx is called. It just defers to net to perform
47 // the actual verification.
48 BOOL WINAPI CryptVerifyCertificateSignatureExStub(
49 HCRYPTPROV_LEGACY provider,
50 DWORD encoding_type,
51 DWORD subject_type,
52 void* subject_data,
53 DWORD issuer_type,
54 void* issuer_data,
55 DWORD flags,
56 void* extra) {
57 return net::sha256_interception::CryptVerifyCertificateSignatureExHook(
58 g_real_crypt_verify_signature_stub, provider, encoding_type, subject_type,
59 subject_data, issuer_type, issuer_data, flags, extra);
62 // If necessary, install an interception
63 void InstallSha256LegacyHooks() {
64 #if defined(_WIN64)
65 // Interception on x64 is not supported.
66 return;
67 #else
68 if (base::win::MaybeHasSHA256Support())
69 return;
71 net::sha256_interception::CryptVerifyCertificateSignatureExFunc
72 cert_verify_signature_ptr = reinterpret_cast<
73 net::sha256_interception::CryptVerifyCertificateSignatureExFunc>(
74 ::GetProcAddress(::GetModuleHandle(L"crypt32.dll"),
75 "CryptVerifyCertificateSignatureEx"));
76 CHECK(cert_verify_signature_ptr);
78 DWORD old_protect = 0;
79 if (!::VirtualProtect(cert_verify_signature_ptr, 5, PAGE_EXECUTE_READWRITE,
80 &old_protect)) {
81 return;
84 g_real_crypt_verify_signature_stub =
85 reinterpret_cast<
86 net::sha256_interception::CryptVerifyCertificateSignatureExFunc>(
87 VirtualAllocEx(::GetCurrentProcess(), NULL,
88 sidestep::kMaxPreambleStubSize, MEM_COMMIT,
89 PAGE_EXECUTE_READWRITE));
90 if (g_real_crypt_verify_signature_stub == NULL) {
91 CHECK(::VirtualProtect(cert_verify_signature_ptr, 5, old_protect,
92 &old_protect));
93 return;
96 sidestep::SideStepError patch_result =
97 sidestep::PreamblePatcher::Patch(
98 cert_verify_signature_ptr, CryptVerifyCertificateSignatureExStub,
99 g_real_crypt_verify_signature_stub, sidestep::kMaxPreambleStubSize);
100 if (patch_result != sidestep::SIDESTEP_SUCCESS) {
101 CHECK(::VirtualFreeEx(::GetCurrentProcess(),
102 g_real_crypt_verify_signature_stub, 0,
103 MEM_RELEASE));
104 CHECK(::VirtualProtect(cert_verify_signature_ptr, 5, old_protect,
105 &old_protect));
106 return;
109 DWORD dummy = 0;
110 CHECK(::VirtualProtect(cert_verify_signature_ptr, 5, old_protect, &dummy));
111 CHECK(::VirtualProtect(g_real_crypt_verify_signature_stub,
112 sidestep::kMaxPreambleStubSize, old_protect,
113 &old_protect));
114 #endif // _WIN64
117 } // namespace
119 #endif // OS_WIN
121 class BrowserMainRunnerImpl : public BrowserMainRunner {
122 public:
123 BrowserMainRunnerImpl()
124 : initialization_started_(false), is_shutdown_(false) {}
126 ~BrowserMainRunnerImpl() override {
127 if (initialization_started_ && !is_shutdown_)
128 Shutdown();
131 int Initialize(const MainFunctionParams& parameters) override {
132 // TODO(vadimt, yiyaoliu): Remove all tracked_objects references below once
133 // crbug.com/453640 is fixed.
134 tracked_objects::ThreadData::InitializeThreadContext("CrBrowserMain");
135 TRACK_SCOPED_REGION(
136 "Startup", "BrowserMainRunnerImpl::Initialize");
137 TRACE_EVENT0("startup", "BrowserMainRunnerImpl::Initialize");
139 // On Android we normally initialize the browser in a series of UI thread
140 // tasks. While this is happening a second request can come from the OS or
141 // another application to start the browser. If this happens then we must
142 // not run these parts of initialization twice.
143 if (!initialization_started_) {
144 initialization_started_ = true;
146 #if !defined(OS_IOS)
147 if (parameters.command_line.HasSwitch(switches::kWaitForDebugger))
148 base::debug::WaitForDebugger(60, true);
149 #endif
151 #if defined(OS_WIN)
152 if (base::win::GetVersion() < base::win::VERSION_VISTA) {
153 // When "Extend support of advanced text services to all programs"
154 // (a.k.a. Cicero Unaware Application Support; CUAS) is enabled on
155 // Windows XP and handwriting modules shipped with Office 2003 are
156 // installed, "penjpn.dll" and "skchui.dll" will be loaded and then
157 // crash unless a user installs Office 2003 SP3. To prevent these
158 // modules from being loaded, disable TSF entirely. crbug.com/160914.
159 // TODO(yukawa): Add a high-level wrapper for this instead of calling
160 // Win32 API here directly.
161 ImmDisableTextFrameService(static_cast<DWORD>(-1));
163 InstallSha256LegacyHooks();
164 #endif // OS_WIN
166 base::StatisticsRecorder::Initialize();
168 notification_service_.reset(new NotificationServiceImpl);
170 #if defined(OS_WIN)
171 // Ole must be initialized before starting message pump, so that TSF
172 // (Text Services Framework) module can interact with the message pump
173 // on Windows 8 Metro mode.
174 ole_initializer_.reset(new ui::ScopedOleInitializer);
175 // Enable DirectWrite font rendering if needed.
176 gfx::win::MaybeInitializeDirectWrite();
177 #endif // OS_WIN
179 main_loop_.reset(new BrowserMainLoop(parameters));
181 main_loop_->Init();
183 main_loop_->EarlyInitialization();
185 // Must happen before we try to use a message loop or display any UI.
186 if (!main_loop_->InitializeToolkit())
187 return 1;
189 main_loop_->PreMainMessageLoopStart();
190 main_loop_->MainMessageLoopStart();
191 main_loop_->PostMainMessageLoopStart();
193 // WARNING: If we get a WM_ENDSESSION, objects created on the stack here
194 // are NOT deleted. If you need something to run during WM_ENDSESSION add it
195 // to browser_shutdown::Shutdown or BrowserProcess::EndSession.
197 ui::InitializeInputMethod();
199 main_loop_->CreateStartupTasks();
200 int result_code = main_loop_->GetResultCode();
201 if (result_code > 0)
202 return result_code;
204 // Return -1 to indicate no early termination.
205 return -1;
208 int Run() override {
209 DCHECK(initialization_started_);
210 DCHECK(!is_shutdown_);
211 main_loop_->RunMainMessageLoopParts();
212 return main_loop_->GetResultCode();
215 void Shutdown() override {
216 DCHECK(initialization_started_);
217 DCHECK(!is_shutdown_);
218 #ifdef LEAK_SANITIZER
219 // Invoke leak detection now, to avoid dealing with shutdown-only leaks.
220 // Normally this will have already happened in
221 // BroserProcessImpl::ReleaseModule(), so this call has no effect. This is
222 // only for processes which do not instantiate a BrowserProcess.
223 // If leaks are found, the process will exit here.
224 __lsan_do_leak_check();
225 #endif
226 // If startup tracing has not been finished yet, replace it's dumper
227 // with special version, which would save trace file on exit (i.e.
228 // startup tracing becomes a version of shutdown tracing).
229 scoped_ptr<BrowserShutdownProfileDumper> startup_profiler;
230 if (main_loop_->is_tracing_startup()) {
231 main_loop_->StopStartupTracingTimer();
232 if (main_loop_->startup_trace_file() !=
233 base::FilePath().AppendASCII("none")) {
234 startup_profiler.reset(
235 new BrowserShutdownProfileDumper(main_loop_->startup_trace_file()));
239 // The shutdown tracing got enabled in AttemptUserExit earlier, but someone
240 // needs to write the result to disc. For that a dumper needs to get created
241 // which will dump the traces to disc when it gets destroyed.
242 const base::CommandLine& command_line =
243 *base::CommandLine::ForCurrentProcess();
244 scoped_ptr<BrowserShutdownProfileDumper> shutdown_profiler;
245 if (command_line.HasSwitch(switches::kTraceShutdown)) {
246 shutdown_profiler.reset(new BrowserShutdownProfileDumper(
247 BrowserShutdownProfileDumper::GetShutdownProfileFileName()));
251 // The trace event has to stay between profiler creation and destruction.
252 TRACE_EVENT0("shutdown", "BrowserMainRunner");
253 g_exited_main_message_loop = true;
255 main_loop_->ShutdownThreadsAndCleanUp();
257 ui::ShutdownInputMethod();
258 #if defined(OS_WIN)
259 ole_initializer_.reset(NULL);
260 #endif
261 #if defined(OS_ANDROID)
262 // Forcefully terminates the RunLoop inside MessagePumpForUI, ensuring
263 // proper shutdown for content_browsertests. Shutdown() is not used by
264 // the actual browser.
265 if (base::MessageLoop::current()->is_running())
266 base::MessageLoop::current()->QuitNow();
267 #endif
268 main_loop_.reset(NULL);
270 notification_service_.reset(NULL);
272 is_shutdown_ = true;
276 protected:
277 // True if we have started to initialize the runner.
278 bool initialization_started_;
280 // True if the runner has been shut down.
281 bool is_shutdown_;
283 scoped_ptr<NotificationServiceImpl> notification_service_;
284 scoped_ptr<BrowserMainLoop> main_loop_;
285 #if defined(OS_WIN)
286 scoped_ptr<ui::ScopedOleInitializer> ole_initializer_;
287 #endif
289 DISALLOW_COPY_AND_ASSIGN(BrowserMainRunnerImpl);
292 // static
293 BrowserMainRunner* BrowserMainRunner::Create() {
294 return new BrowserMainRunnerImpl();
297 } // namespace content