blob: bbf47b058441425cb735a49138ebd09d0d8821a6 [file] [log] [blame]
[email protected]f573ed6b2012-02-10 15:58:521// 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 "content/public/browser/browser_main_runner.h"
6
[email protected]f573ed6b2012-02-10 15:58:527#include "base/base_switches.h"
8#include "base/command_line.h"
[email protected]1cefd4b2013-11-20 21:55:409#include "base/debug/leak_annotations.h"
[email protected]f573ed6b2012-02-10 15:58:5210#include "base/logging.h"
avib7348942015-12-25 20:57:1011#include "base/macros.h"
[email protected]f573ed6b2012-02-10 15:58:5212#include "base/metrics/histogram.h"
robliao7fac1d82015-09-23 23:07:4013#include "base/metrics/histogram_macros.h"
[email protected]567d30e2012-07-13 21:48:2914#include "base/metrics/statistics_recorder.h"
yiyaoliu252f915e2015-04-23 21:56:3615#include "base/profiler/scoped_profile.h"
yiyaoliu9e6a5ab32015-03-18 18:04:3716#include "base/profiler/scoped_tracker.h"
robliao7fac1d82015-09-23 23:07:4017#include "base/time/time.h"
ssid3e765612015-01-28 04:03:4218#include "base/trace_event/trace_event.h"
yiyaoliu9e6a5ab32015-03-18 18:04:3719#include "base/tracked_objects.h"
avib7348942015-12-25 20:57:1020#include "build/build_config.h"
zhenwc074d282015-08-31 17:28:1721#include "components/tracing/trace_config_file.h"
zhenwecedcf22015-08-18 23:37:4622#include "components/tracing/tracing_switches.h"
[email protected]f573ed6b2012-02-10 15:58:5223#include "content/browser/browser_main_loop.h"
[email protected]89af4002013-09-06 07:47:0724#include "content/browser/browser_shutdown_profile_dumper.h"
[email protected]f573ed6b2012-02-10 15:58:5225#include "content/browser/notification_service_impl.h"
zhenwc074d282015-08-31 17:28:1726#include "content/public/browser/tracing_controller.h"
[email protected]f573ed6b2012-02-10 15:58:5227#include "content/public/common/content_switches.h"
28#include "content/public/common/main_function_params.h"
boliu9847d572015-09-10 20:29:1829#include "third_party/skia/include/core/SkGraphics.h"
[email protected]749bf6432013-06-12 16:00:0830#include "ui/base/ime/input_method_initializer.h"
[email protected]f573ed6b2012-02-10 15:58:5231
zhenwc074d282015-08-31 17:28:1732#if defined(OS_ANDROID)
33#include "content/browser/android/tracing_controller_android.h"
34#endif
35
[email protected]f573ed6b2012-02-10 15:58:5236#if defined(OS_WIN)
[email protected]b68b51e2012-11-15 01:10:4537#include "base/win/windows_version.h"
[email protected]10208ea2013-06-06 20:08:0338#include "ui/base/win/scoped_ole_initializer.h"
anantaacfcfce2014-10-15 01:14:1139#include "ui/gfx/win/direct_write.h"
[email protected]f573ed6b2012-02-10 15:58:5240#endif
41
[email protected]46488322012-10-30 03:22:2042namespace content {
[email protected]eb398192012-10-22 20:16:1943
rsleevi25e2bc0a2014-09-24 03:12:5544namespace {
45
thestig5a551c4c2015-08-29 02:45:3546bool g_exited_main_message_loop = false;
47
rsleevi25e2bc0a2014-09-24 03:12:5548} // namespace
49
[email protected]46488322012-10-30 03:22:2050class BrowserMainRunnerImpl : public BrowserMainRunner {
[email protected]f573ed6b2012-02-10 15:58:5251 public:
[email protected]232e09d2013-08-27 15:29:5652 BrowserMainRunnerImpl()
53 : initialization_started_(false), is_shutdown_(false) {}
[email protected]f573ed6b2012-02-10 15:58:5254
dchengc2282aa2014-10-21 12:07:5855 ~BrowserMainRunnerImpl() override {
[email protected]232e09d2013-08-27 15:29:5656 if (initialization_started_ && !is_shutdown_)
[email protected]f573ed6b2012-02-10 15:58:5257 Shutdown();
58 }
59
dchengc2282aa2014-10-21 12:07:5860 int Initialize(const MainFunctionParams& parameters) override {
robliao7fac1d82015-09-23 23:07:4061 SCOPED_UMA_HISTOGRAM_LONG_TIMER(
62 "Startup.BrowserMainRunnerImplInitializeLongTime");
63
yiyaoliu9e6a5ab32015-03-18 18:04:3764 // TODO(vadimt, yiyaoliu): Remove all tracked_objects references below once
65 // crbug.com/453640 is fixed.
66 tracked_objects::ThreadData::InitializeThreadContext("CrBrowserMain");
yiyaoliu252f915e2015-04-23 21:56:3667 TRACK_SCOPED_REGION(
68 "Startup", "BrowserMainRunnerImpl::Initialize");
[email protected]232e09d2013-08-27 15:29:5669 TRACE_EVENT0("startup", "BrowserMainRunnerImpl::Initialize");
yiyaoliu9e6a5ab32015-03-18 18:04:3770
[email protected]232e09d2013-08-27 15:29:5671 // On Android we normally initialize the browser in a series of UI thread
72 // tasks. While this is happening a second request can come from the OS or
73 // another application to start the browser. If this happens then we must
74 // not run these parts of initialization twice.
75 if (!initialization_started_) {
76 initialization_started_ = true;
[email protected]f573ed6b2012-02-10 15:58:5277
robliao7fac1d82015-09-23 23:07:4078 const base::TimeTicks start_time_step1 = base::TimeTicks::Now();
79
boliu9847d572015-09-10 20:29:1880 SkGraphics::Init();
81
[email protected]7951bca42013-06-17 20:01:3082#if !defined(OS_IOS)
[email protected]232e09d2013-08-27 15:29:5683 if (parameters.command_line.HasSwitch(switches::kWaitForDebugger))
84 base::debug::WaitForDebugger(60, true);
[email protected]7951bca42013-06-17 20:01:3085#endif
86
[email protected]091e96262012-09-12 15:35:1087#if defined(OS_WIN)
[email protected]b4b591bc2014-02-03 11:45:0588 if (base::win::GetVersion() < base::win::VERSION_VISTA) {
[email protected]232e09d2013-08-27 15:29:5689 // When "Extend support of advanced text services to all programs"
90 // (a.k.a. Cicero Unaware Application Support; CUAS) is enabled on
91 // Windows XP and handwriting modules shipped with Office 2003 are
92 // installed, "penjpn.dll" and "skchui.dll" will be loaded and then
[email protected]b4b591bc2014-02-03 11:45:0593 // crash unless a user installs Office 2003 SP3. To prevent these
94 // modules from being loaded, disable TSF entirely. crbug.com/160914.
[email protected]232e09d2013-08-27 15:29:5695 // TODO(yukawa): Add a high-level wrapper for this instead of calling
96 // Win32 API here directly.
97 ImmDisableTextFrameService(static_cast<DWORD>(-1));
98 }
[email protected]091e96262012-09-12 15:35:1099#endif // OS_WIN
100
[email protected]232e09d2013-08-27 15:29:56101 base::StatisticsRecorder::Initialize();
[email protected]c4805102012-03-02 23:34:32102
[email protected]232e09d2013-08-27 15:29:56103 notification_service_.reset(new NotificationServiceImpl);
[email protected]f573ed6b2012-02-10 15:58:52104
[email protected]962a8482013-01-15 03:42:56105#if defined(OS_WIN)
[email protected]232e09d2013-08-27 15:29:56106 // Ole must be initialized before starting message pump, so that TSF
107 // (Text Services Framework) module can interact with the message pump
108 // on Windows 8 Metro mode.
109 ole_initializer_.reset(new ui::ScopedOleInitializer);
anantaacfcfce2014-10-15 01:14:11110 // Enable DirectWrite font rendering if needed.
ckocagil9a4438e2014-12-02 04:48:30111 gfx::win::MaybeInitializeDirectWrite();
[email protected]962a8482013-01-15 03:42:56112#endif // OS_WIN
113
[email protected]232e09d2013-08-27 15:29:56114 main_loop_.reset(new BrowserMainLoop(parameters));
[email protected]f573ed6b2012-02-10 15:58:52115
[email protected]232e09d2013-08-27 15:29:56116 main_loop_->Init();
[email protected]f573ed6b2012-02-10 15:58:52117
[email protected]232e09d2013-08-27 15:29:56118 main_loop_->EarlyInitialization();
[email protected]f573ed6b2012-02-10 15:58:52119
[email protected]232e09d2013-08-27 15:29:56120 // Must happen before we try to use a message loop or display any UI.
[email protected]d957b102014-04-25 20:17:19121 if (!main_loop_->InitializeToolkit())
122 return 1;
[email protected]f573ed6b2012-02-10 15:58:52123
thestig7e202d1a2015-05-27 21:54:55124 main_loop_->PreMainMessageLoopStart();
[email protected]232e09d2013-08-27 15:29:56125 main_loop_->MainMessageLoopStart();
thestig7e202d1a2015-05-27 21:54:55126 main_loop_->PostMainMessageLoopStart();
[email protected]f573ed6b2012-02-10 15:58:52127
[email protected]232e09d2013-08-27 15:29:56128// WARNING: If we get a WM_ENDSESSION, objects created on the stack here
129// are NOT deleted. If you need something to run during WM_ENDSESSION add it
130// to browser_shutdown::Shutdown or BrowserProcess::EndSession.
[email protected]f573ed6b2012-02-10 15:58:52131
[email protected]232e09d2013-08-27 15:29:56132 ui::InitializeInputMethod();
robliao7fac1d82015-09-23 23:07:40133 UMA_HISTOGRAM_TIMES("Startup.BrowserMainRunnerImplInitializeStep1Time",
134 base::TimeTicks::Now() - start_time_step1);
[email protected]232e09d2013-08-27 15:29:56135 }
robliao7fac1d82015-09-23 23:07:40136 const base::TimeTicks start_time_step2 = base::TimeTicks::Now();
[email protected]57624ab2013-08-01 16:01:51137 main_loop_->CreateStartupTasks();
[email protected]f573ed6b2012-02-10 15:58:52138 int result_code = main_loop_->GetResultCode();
139 if (result_code > 0)
140 return result_code;
[email protected]f573ed6b2012-02-10 15:58:52141
robliao7fac1d82015-09-23 23:07:40142 UMA_HISTOGRAM_TIMES("Startup.BrowserMainRunnerImplInitializeStep2Time",
143 base::TimeTicks::Now() - start_time_step2);
144
[email protected]f573ed6b2012-02-10 15:58:52145 // Return -1 to indicate no early termination.
146 return -1;
147 }
148
dchengc2282aa2014-10-21 12:07:58149 int Run() override {
[email protected]232e09d2013-08-27 15:29:56150 DCHECK(initialization_started_);
[email protected]f573ed6b2012-02-10 15:58:52151 DCHECK(!is_shutdown_);
152 main_loop_->RunMainMessageLoopParts();
153 return main_loop_->GetResultCode();
154 }
155
dchengc2282aa2014-10-21 12:07:58156 void Shutdown() override {
[email protected]232e09d2013-08-27 15:29:56157 DCHECK(initialization_started_);
[email protected]f573ed6b2012-02-10 15:58:52158 DCHECK(!is_shutdown_);
[email protected]1cefd4b2013-11-20 21:55:40159#ifdef LEAK_SANITIZER
160 // Invoke leak detection now, to avoid dealing with shutdown-only leaks.
161 // Normally this will have already happened in
162 // BroserProcessImpl::ReleaseModule(), so this call has no effect. This is
163 // only for processes which do not instantiate a BrowserProcess.
164 // If leaks are found, the process will exit here.
165 __lsan_do_leak_check();
166#endif
[email protected]cec95632014-07-02 18:01:50167 // If startup tracing has not been finished yet, replace it's dumper
168 // with special version, which would save trace file on exit (i.e.
169 // startup tracing becomes a version of shutdown tracing).
zhenwc074d282015-08-31 17:28:17170 // There are two cases:
171 // 1. Startup duration is not reached.
172 // 2. Or startup duration is not specified for --trace-config-file flag.
[email protected]cec95632014-07-02 18:01:50173 scoped_ptr<BrowserShutdownProfileDumper> startup_profiler;
zhenwc074d282015-08-31 17:28:17174 if (main_loop_->is_tracing_startup_for_duration()) {
[email protected]cec95632014-07-02 18:01:50175 main_loop_->StopStartupTracingTimer();
176 if (main_loop_->startup_trace_file() !=
177 base::FilePath().AppendASCII("none")) {
178 startup_profiler.reset(
179 new BrowserShutdownProfileDumper(main_loop_->startup_trace_file()));
180 }
zhenwc074d282015-08-31 17:28:17181 } else if (tracing::TraceConfigFile::GetInstance()->IsEnabled() &&
zhenw873bdff2015-11-11 22:16:55182 TracingController::GetInstance()->IsTracing()) {
zhenwc074d282015-08-31 17:28:17183 base::FilePath result_file;
mfomitchev2b8b066a2016-01-28 19:23:15184#if defined(OS_ANDROID)
zhenwc074d282015-08-31 17:28:17185 TracingControllerAndroid::GenerateTracingFilePath(&result_file);
186#else
187 result_file = tracing::TraceConfigFile::GetInstance()->GetResultFile();
188#endif
189 startup_profiler.reset(new BrowserShutdownProfileDumper(result_file));
[email protected]cec95632014-07-02 18:01:50190 }
191
[email protected]89af4002013-09-06 07:47:07192 // The shutdown tracing got enabled in AttemptUserExit earlier, but someone
193 // needs to write the result to disc. For that a dumper needs to get created
194 // which will dump the traces to disc when it gets destroyed.
[email protected]479278702014-08-11 20:32:09195 const base::CommandLine& command_line =
196 *base::CommandLine::ForCurrentProcess();
[email protected]cec95632014-07-02 18:01:50197 scoped_ptr<BrowserShutdownProfileDumper> shutdown_profiler;
198 if (command_line.HasSwitch(switches::kTraceShutdown)) {
199 shutdown_profiler.reset(new BrowserShutdownProfileDumper(
200 BrowserShutdownProfileDumper::GetShutdownProfileFileName()));
201 }
[email protected]f573ed6b2012-02-10 15:58:52202
[email protected]89af4002013-09-06 07:47:07203 {
204 // The trace event has to stay between profiler creation and destruction.
205 TRACE_EVENT0("shutdown", "BrowserMainRunner");
206 g_exited_main_message_loop = true;
[email protected]f573ed6b2012-02-10 15:58:52207
[email protected]89af4002013-09-06 07:47:07208 main_loop_->ShutdownThreadsAndCleanUp();
[email protected]f573ed6b2012-02-10 15:58:52209
[email protected]89af4002013-09-06 07:47:07210 ui::ShutdownInputMethod();
211 #if defined(OS_WIN)
212 ole_initializer_.reset(NULL);
213 #endif
[email protected]d2cdd962014-06-18 09:04:32214 #if defined(OS_ANDROID)
215 // Forcefully terminates the RunLoop inside MessagePumpForUI, ensuring
216 // proper shutdown for content_browsertests. Shutdown() is not used by
217 // the actual browser.
jaekyun617edc52014-11-19 00:11:06218 if (base::MessageLoop::current()->is_running())
219 base::MessageLoop::current()->QuitNow();
[email protected]d2cdd962014-06-18 09:04:32220 #endif
[email protected]89af4002013-09-06 07:47:07221 main_loop_.reset(NULL);
[email protected]f573ed6b2012-02-10 15:58:52222
[email protected]89af4002013-09-06 07:47:07223 notification_service_.reset(NULL);
224
225 is_shutdown_ = true;
226 }
[email protected]f573ed6b2012-02-10 15:58:52227 }
228
229 protected:
[email protected]232e09d2013-08-27 15:29:56230 // True if we have started to initialize the runner.
231 bool initialization_started_;
[email protected]f573ed6b2012-02-10 15:58:52232
233 // True if the runner has been shut down.
234 bool is_shutdown_;
235
[email protected]f573ed6b2012-02-10 15:58:52236 scoped_ptr<NotificationServiceImpl> notification_service_;
[email protected]46488322012-10-30 03:22:20237 scoped_ptr<BrowserMainLoop> main_loop_;
[email protected]f573ed6b2012-02-10 15:58:52238#if defined(OS_WIN)
[email protected]e000ac92012-10-01 18:20:48239 scoped_ptr<ui::ScopedOleInitializer> ole_initializer_;
[email protected]f573ed6b2012-02-10 15:58:52240#endif
[email protected]f573ed6b2012-02-10 15:58:52241
thestig5a551c4c2015-08-29 02:45:35242 private:
[email protected]f573ed6b2012-02-10 15:58:52243 DISALLOW_COPY_AND_ASSIGN(BrowserMainRunnerImpl);
244};
245
[email protected]f573ed6b2012-02-10 15:58:52246// static
247BrowserMainRunner* BrowserMainRunner::Create() {
248 return new BrowserMainRunnerImpl();
249}
250
thestig5a551c4c2015-08-29 02:45:35251// static
252bool BrowserMainRunner::ExitedMainMessageLoop() {
253 return g_exited_main_message_loop;
254}
255
[email protected]f573ed6b2012-02-10 15:58:52256} // namespace content