Avi Drissman | 4e1b7bc3 | 2022-09-15 14:03:50 | [diff] [blame] | 1 | // Copyright 2012 The Chromium Authors |
[email protected] | f573ed6b | 2012-02-10 15:58:52 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
3 | // found in the LICENSE file. | ||||
4 | |||||
Xi Han | 6740d62 | 2018-05-22 19:21:07 | [diff] [blame] | 5 | #include "content/browser/browser_main_runner_impl.h" |
[email protected] | f573ed6b | 2012-02-10 15:58:52 | [diff] [blame] | 6 | |
Peter Boström | dd7e40ec | 2021-04-05 20:40:10 | [diff] [blame] | 7 | #include <memory> |
8 | |||||
Kalvin Lee | 8529bdde | 2024-10-25 14:10:42 | [diff] [blame] | 9 | #include "base/allocator/partition_alloc_support.h" |
[email protected] | f573ed6b | 2012-02-10 15:58:52 | [diff] [blame] | 10 | #include "base/base_switches.h" |
Hans Wennborg | 0917de89 | 2020-04-28 20:21:15 | [diff] [blame] | 11 | #include "base/check.h" |
[email protected] | f573ed6b | 2012-02-10 15:58:52 | [diff] [blame] | 12 | #include "base/command_line.h" |
ananta | f265187 | 2016-06-16 22:21:02 | [diff] [blame] | 13 | #include "base/debug/debugger.h" |
[email protected] | 1cefd4b | 2013-11-20 21:55:40 | [diff] [blame] | 14 | #include "base/debug/leak_annotations.h" |
asvitkine | 8d51e9d | 2016-09-02 23:55:43 | [diff] [blame] | 15 | #include "base/metrics/histogram_macros.h" |
Lei Zhang | 81f8569 | 2022-10-06 21:03:22 | [diff] [blame] | 16 | #include "base/no_destructor.h" |
gab | 7af9dc0 | 2017-05-05 13:38:54 | [diff] [blame] | 17 | #include "base/run_loop.h" |
Gabriel Charette | 8eb4dff | 2018-03-27 14:22:54 | [diff] [blame] | 18 | #include "base/synchronization/atomic_flag.h" |
robliao | 7fac1d8 | 2015-09-23 23:07:40 | [diff] [blame] | 19 | #include "base/time/time.h" |
ssid | b3fabc66 | 2016-04-01 04:39:25 | [diff] [blame] | 20 | #include "base/trace_event/heap_profiler_allocation_context_tracker.h" |
ssid | 3e76561 | 2015-01-28 04:03:42 | [diff] [blame] | 21 | #include "base/trace_event/trace_event.h" |
avi | b734894 | 2015-12-25 20:57:10 | [diff] [blame] | 22 | #include "build/build_config.h" |
primiano | ccb26c6 | 2016-06-01 21:50:02 | [diff] [blame] | 23 | #include "components/tracing/common/tracing_switches.h" |
[email protected] | f573ed6b | 2012-02-10 15:58:52 | [diff] [blame] | 24 | #include "content/browser/browser_main_loop.h" |
Alexander Timin | fc975678 | 2020-12-23 22:05:52 | [diff] [blame] | 25 | #include "content/browser/tracing/startup_tracing_controller.h" |
John Abd-El-Malek | 884291c | 2017-08-09 06:43:48 | [diff] [blame] | 26 | #include "content/common/content_switches_internal.h" |
[email protected] | f573ed6b | 2012-02-10 15:58:52 | [diff] [blame] | 27 | #include "content/public/common/content_switches.h" |
28 | #include "content/public/common/main_function_params.h" | ||||
Etienne Pierre-doray | f3fd9e8 | 2024-06-12 16:40:35 | [diff] [blame] | 29 | #include "services/tracing/public/cpp/trace_startup_config.h" |
boliu | 9847d57 | 2015-09-10 20:29:18 | [diff] [blame] | 30 | #include "third_party/skia/include/core/SkGraphics.h" |
Michael Spang | 539c6f3 | 2019-04-03 19:08:52 | [diff] [blame] | 31 | #include "ui/base/ime/init/input_method_initializer.h" |
Etienne Bergeron | 0acd0fcc | 2019-09-25 22:45:26 | [diff] [blame] | 32 | #include "ui/gfx/font_util.h" |
[email protected] | f573ed6b | 2012-02-10 15:58:52 | [diff] [blame] | 33 | |
Xiaohan Wang | 1ecfd00 | 2022-01-19 22:33:10 | [diff] [blame] | 34 | #if BUILDFLAG(IS_ANDROID) |
zhenw | c074d28f | 2015-08-31 17:28:17 | [diff] [blame] | 35 | #include "content/browser/android/tracing_controller_android.h" |
36 | #endif | ||||
37 | |||||
Xiaohan Wang | 1ecfd00 | 2022-01-19 22:33:10 | [diff] [blame] | 38 | #if BUILDFLAG(IS_WIN) |
Bill Carr | ba4cbb97 | 2022-10-10 18:24:11 | [diff] [blame] | 39 | #include "base/win/win_util.h" |
[email protected] | b68b51e | 2012-11-15 01:10:45 | [diff] [blame] | 40 | #include "base/win/windows_version.h" |
[email protected] | 10208ea | 2013-06-06 20:08:03 | [diff] [blame] | 41 | #include "ui/base/win/scoped_ole_initializer.h" |
[email protected] | f573ed6b | 2012-02-10 15:58:52 | [diff] [blame] | 42 | #endif |
43 | |||||
[email protected] | 4648832 | 2012-10-30 03:22:20 | [diff] [blame] | 44 | namespace content { |
rsleevi | 25e2bc0a | 2014-09-24 03:12:55 | [diff] [blame] | 45 | namespace { |
46 | |||||
Lei Zhang | 81f8569 | 2022-10-06 21:03:22 | [diff] [blame] | 47 | base::AtomicFlag& GetExitedMainMessageLoopFlag() { |
48 | static base::NoDestructor<base::AtomicFlag> flag; | ||||
49 | return *flag; | ||||
50 | } | ||||
thestig | 5a551c4c | 2015-08-29 02:45:35 | [diff] [blame] | 51 | |
rsleevi | 25e2bc0a | 2014-09-24 03:12:55 | [diff] [blame] | 52 | } // namespace |
53 | |||||
Xi Han | 6740d62 | 2018-05-22 19:21:07 | [diff] [blame] | 54 | // static |
Ayu Ishii | 4e19b5f | 2019-01-16 23:22:00 | [diff] [blame] | 55 | std::unique_ptr<BrowserMainRunnerImpl> BrowserMainRunnerImpl::Create() { |
56 | return std::make_unique<BrowserMainRunnerImpl>(); | ||||
Xi Han | 6740d62 | 2018-05-22 19:21:07 | [diff] [blame] | 57 | } |
[email protected] | f573ed6b | 2012-02-10 15:58:52 | [diff] [blame] | 58 | |
Xi Han | 6740d62 | 2018-05-22 19:21:07 | [diff] [blame] | 59 | BrowserMainRunnerImpl::BrowserMainRunnerImpl() |
Xi Han | 8012e46 | 2018-10-05 19:52:30 | [diff] [blame] | 60 | : initialization_started_(false), |
61 | is_shutdown_(false), | ||||
62 | scoped_execution_fence_( | ||||
Gabriel Charette | 43fd370 | 2019-05-29 16:36:51 | [diff] [blame] | 63 | std::make_unique<base::ThreadPoolInstance::ScopedExecutionFence>()) {} |
[email protected] | f573ed6b | 2012-02-10 15:58:52 | [diff] [blame] | 64 | |
Xi Han | 6740d62 | 2018-05-22 19:21:07 | [diff] [blame] | 65 | BrowserMainRunnerImpl::~BrowserMainRunnerImpl() { |
Sina Firoozabadi | 9ff575ac | 2023-02-22 22:42:46 | [diff] [blame] | 66 | if (initialization_started_ && !is_shutdown_) { |
Xi Han | 6740d62 | 2018-05-22 19:21:07 | [diff] [blame] | 67 | Shutdown(); |
Sina Firoozabadi | 9ff575ac | 2023-02-22 22:42:46 | [diff] [blame] | 68 | } |
Xi Han | 6740d62 | 2018-05-22 19:21:07 | [diff] [blame] | 69 | } |
yiyaoliu | 9e6a5ab3 | 2015-03-18 18:04:37 | [diff] [blame] | 70 | |
Gabriel Charette | fbeeb1c | 2021-11-10 20:50:06 | [diff] [blame] | 71 | int BrowserMainRunnerImpl::Initialize(MainFunctionParams parameters) { |
Xi Han | 6740d62 | 2018-05-22 19:21:07 | [diff] [blame] | 72 | SCOPED_UMA_HISTOGRAM_LONG_TIMER( |
73 | "Startup.BrowserMainRunnerImplInitializeLongTime"); | ||||
74 | TRACE_EVENT0("startup", "BrowserMainRunnerImpl::Initialize"); | ||||
robliao | 7fac1d8 | 2015-09-23 23:07:40 | [diff] [blame] | 75 | |
Xi Han | 6740d62 | 2018-05-22 19:21:07 | [diff] [blame] | 76 | // On Android we normally initialize the browser in a series of UI thread |
77 | // tasks. While this is happening a second request can come from the OS or | ||||
78 | // another application to start the browser. If this happens then we must | ||||
79 | // not run these parts of initialization twice. | ||||
80 | if (!initialization_started_) { | ||||
81 | initialization_started_ = true; | ||||
Alexei Filippov | 6cd45c9 | 2018-02-10 03:29:15 | [diff] [blame] | 82 | |
Xi Han | 6740d62 | 2018-05-22 19:21:07 | [diff] [blame] | 83 | SkGraphics::Init(); |
John Abd-El-Malek | 884291c | 2017-08-09 06:43:48 | [diff] [blame] | 84 | |
Sina Firoozabadi | 9ff575ac | 2023-02-22 22:42:46 | [diff] [blame] | 85 | if (parameters.command_line->HasSwitch(switches::kWaitForDebugger)) { |
Xi Han | 6740d62 | 2018-05-22 19:21:07 | [diff] [blame] | 86 | base::debug::WaitForDebugger(60, true); |
Sina Firoozabadi | 9ff575ac | 2023-02-22 22:42:46 | [diff] [blame] | 87 | } |
Xi Han | 6740d62 | 2018-05-22 19:21:07 | [diff] [blame] | 88 | |
Sina Firoozabadi | 9ff575ac | 2023-02-22 22:42:46 | [diff] [blame] | 89 | if (parameters.command_line->HasSwitch(switches::kBrowserStartupDialog)) { |
Xi Han | 6740d62 | 2018-05-22 19:21:07 | [diff] [blame] | 90 | WaitForDebugger("Browser"); |
Sina Firoozabadi | 9ff575ac | 2023-02-22 22:42:46 | [diff] [blame] | 91 | } |
Xi Han | 6740d62 | 2018-05-22 19:21:07 | [diff] [blame] | 92 | |
Xiaohan Wang | 1ecfd00 | 2022-01-19 22:33:10 | [diff] [blame] | 93 | #if BUILDFLAG(IS_WIN) |
Bill Carr | ba4cbb97 | 2022-10-10 18:24:11 | [diff] [blame] | 94 | base::win::EnableHighDPISupport(); |
Xi Han | 6740d62 | 2018-05-22 19:21:07 | [diff] [blame] | 95 | // Ole must be initialized before starting message pump, so that TSF |
96 | // (Text Services Framework) module can interact with the message pump | ||||
97 | // on Windows 8 Metro mode. | ||||
Peter Boström | 560859d | 2021-05-01 01:31:25 | [diff] [blame] | 98 | ole_initializer_ = std::make_unique<ui::ScopedOleInitializer>(); |
Xiaohan Wang | 1ecfd00 | 2022-01-19 22:33:10 | [diff] [blame] | 99 | #endif // BUILDFLAG(IS_WIN) |
[email protected] | 962a848 | 2013-01-15 03:42:56 | [diff] [blame] | 100 | |
Etienne Bergeron | 0acd0fcc | 2019-09-25 22:45:26 | [diff] [blame] | 101 | gfx::InitializeFonts(); |
102 | |||||
Gabriel Charette | fbeeb1c | 2021-11-10 20:50:06 | [diff] [blame] | 103 | auto created_main_parts_closure = |
104 | std::move(parameters.created_main_parts_closure); | ||||
105 | |||||
Peter Boström | dd7e40ec | 2021-04-05 20:40:10 | [diff] [blame] | 106 | main_loop_ = std::make_unique<BrowserMainLoop>( |
Gabriel Charette | fbeeb1c | 2021-11-10 20:50:06 | [diff] [blame] | 107 | std::move(parameters), std::move(scoped_execution_fence_)); |
[email protected] | f573ed6b | 2012-02-10 15:58:52 | [diff] [blame] | 108 | |
Xi Han | 4090dcc | 2018-07-11 03:15:20 | [diff] [blame] | 109 | main_loop_->Init(); |
[email protected] | f573ed6b | 2012-02-10 15:58:52 | [diff] [blame] | 110 | |
Gabriel Charette | fbeeb1c | 2021-11-10 20:50:06 | [diff] [blame] | 111 | if (created_main_parts_closure) { |
112 | std::move(created_main_parts_closure).Run(main_loop_->parts()); | ||||
[email protected] | 232e09d | 2013-08-27 15:29:56 | [diff] [blame] | 113 | } |
[email protected] | f573ed6b | 2012-02-10 15:58:52 | [diff] [blame] | 114 | |
Xi Han | 6740d62 | 2018-05-22 19:21:07 | [diff] [blame] | 115 | const int early_init_error_code = main_loop_->EarlyInitialization(); |
Will Harris | ba4e1a5 | 2022-04-20 23:05:33 | [diff] [blame] | 116 | if (early_init_error_code > 0) { |
117 | main_loop_->CreateMessageLoopForEarlyShutdown(); | ||||
Xi Han | 6740d62 | 2018-05-22 19:21:07 | [diff] [blame] | 118 | return early_init_error_code; |
Will Harris | ba4e1a5 | 2022-04-20 23:05:33 | [diff] [blame] | 119 | } |
robliao | 7fac1d8 | 2015-09-23 23:07:40 | [diff] [blame] | 120 | |
Xi Han | 6740d62 | 2018-05-22 19:21:07 | [diff] [blame] | 121 | // Must happen before we try to use a message loop or display any UI. |
Alexander Dunaev | 0521017 | 2022-01-19 19:43:09 | [diff] [blame] | 122 | if (!main_loop_->InitializeToolkit()) { |
123 | main_loop_->CreateMessageLoopForEarlyShutdown(); | ||||
Xi Han | 6740d62 | 2018-05-22 19:21:07 | [diff] [blame] | 124 | return 1; |
Alexander Dunaev | 0521017 | 2022-01-19 19:43:09 | [diff] [blame] | 125 | } |
Xi Han | 6740d62 | 2018-05-22 19:21:07 | [diff] [blame] | 126 | |
Gabriel Charette | 09c6a96e | 2021-05-17 14:52:59 | [diff] [blame] | 127 | main_loop_->PreCreateMainMessageLoop(); |
128 | main_loop_->CreateMainMessageLoop(); | ||||
129 | main_loop_->PostCreateMainMessageLoop(); | ||||
Xi Han | 6740d62 | 2018-05-22 19:21:07 | [diff] [blame] | 130 | |
131 | // WARNING: If we get a WM_ENDSESSION, objects created on the stack here | ||||
132 | // are NOT deleted. If you need something to run during WM_ENDSESSION add it | ||||
133 | // to browser_shutdown::Shutdown or BrowserProcess::EndSession. | ||||
134 | |||||
135 | ui::InitializeInputMethod(); | ||||
[email protected] | f573ed6b | 2012-02-10 15:58:52 | [diff] [blame] | 136 | } |
Xi Han | 6740d62 | 2018-05-22 19:21:07 | [diff] [blame] | 137 | main_loop_->CreateStartupTasks(); |
138 | int result_code = main_loop_->GetResultCode(); | ||||
Sina Firoozabadi | 9ff575ac | 2023-02-22 22:42:46 | [diff] [blame] | 139 | if (result_code > 0) { |
Xi Han | 6740d62 | 2018-05-22 19:21:07 | [diff] [blame] | 140 | return result_code; |
Sina Firoozabadi | 9ff575ac | 2023-02-22 22:42:46 | [diff] [blame] | 141 | } |
Xi Han | 6740d62 | 2018-05-22 19:21:07 | [diff] [blame] | 142 | |
143 | // Return -1 to indicate no early termination. | ||||
144 | return -1; | ||||
145 | } | ||||
[email protected] | f573ed6b | 2012-02-10 15:58:52 | [diff] [blame] | 146 | |
Xiaohan Wang | 1ecfd00 | 2022-01-19 22:33:10 | [diff] [blame] | 147 | #if BUILDFLAG(IS_ANDROID) |
Xi Han | 6740d62 | 2018-05-22 19:21:07 | [diff] [blame] | 148 | void BrowserMainRunnerImpl::SynchronouslyFlushStartupTasks() { |
149 | main_loop_->SynchronouslyFlushStartupTasks(); | ||||
150 | } | ||||
John Budorick | a797150 | 2017-08-14 20:03:03 | [diff] [blame] | 151 | #endif |
152 | |||||
Xi Han | 6740d62 | 2018-05-22 19:21:07 | [diff] [blame] | 153 | int BrowserMainRunnerImpl::Run() { |
154 | DCHECK(initialization_started_); | ||||
155 | DCHECK(!is_shutdown_); | ||||
Gabriel Charette | c716bcf3 | 2021-03-16 16:25:35 | [diff] [blame] | 156 | main_loop_->RunMainMessageLoop(); |
Xi Han | 6740d62 | 2018-05-22 19:21:07 | [diff] [blame] | 157 | return main_loop_->GetResultCode(); |
158 | } | ||||
[email protected] | f573ed6b | 2012-02-10 15:58:52 | [diff] [blame] | 159 | |
Xi Han | 6740d62 | 2018-05-22 19:21:07 | [diff] [blame] | 160 | void BrowserMainRunnerImpl::Shutdown() { |
161 | DCHECK(initialization_started_); | ||||
162 | DCHECK(!is_shutdown_); | ||||
bcwhite | da097d3 | 2017-01-12 23:55:03 | [diff] [blame] | 163 | |
Kalvin Lee | 8529bdde | 2024-10-25 14:10:42 | [diff] [blame] | 164 | #if BUILDFLAG(IS_CHROMEOS) |
165 | // Reduces shutdown hangs on CrOS. | ||||
166 | // Googlers: see go/cros-no-op-free-2024 for the experiment write-up. | ||||
167 | base::allocator::MakeFreeNoOp(); | ||||
168 | #endif | ||||
Kalvin Lee | afefbfe | 2024-05-23 18:37:16 | [diff] [blame] | 169 | |
Xi Han | 6740d62 | 2018-05-22 19:21:07 | [diff] [blame] | 170 | main_loop_->PreShutdown(); |
bcwhite | da097d3 | 2017-01-12 23:55:03 | [diff] [blame] | 171 | |
Eric Seckler | 7d928e2d | 2019-04-05 19:05:35 | [diff] [blame] | 172 | // Finalize the startup tracing session if it is still active. |
ssid | 6bedaa9 | 2021-06-16 10:25:24 | [diff] [blame] | 173 | StartupTracingController::GetInstance().ShutdownAndWaitForStopIfNeeded(); |
Chris Pickel | 5d76e33 | 2018-05-15 12:18:57 | [diff] [blame] | 174 | |
Xi Han | 6740d62 | 2018-05-22 19:21:07 | [diff] [blame] | 175 | { |
176 | // The trace event has to stay between profiler creation and destruction. | ||||
177 | TRACE_EVENT0("shutdown", "BrowserMainRunner"); | ||||
Lei Zhang | 81f8569 | 2022-10-06 21:03:22 | [diff] [blame] | 178 | GetExitedMainMessageLoopFlag().Set(); |
Chris Pickel | 5d76e33 | 2018-05-15 12:18:57 | [diff] [blame] | 179 | |
Xi Han | 6740d62 | 2018-05-22 19:21:07 | [diff] [blame] | 180 | main_loop_->ShutdownThreadsAndCleanUp(); |
181 | |||||
182 | ui::ShutdownInputMethod(); | ||||
Xiaohan Wang | 1ecfd00 | 2022-01-19 22:33:10 | [diff] [blame] | 183 | #if BUILDFLAG(IS_WIN) |
Xi Han | 6740d62 | 2018-05-22 19:21:07 | [diff] [blame] | 184 | ole_initializer_.reset(NULL); |
Chris Pickel | 5d76e33 | 2018-05-15 12:18:57 | [diff] [blame] | 185 | #endif |
Xi Han | 6740d62 | 2018-05-22 19:21:07 | [diff] [blame] | 186 | main_loop_.reset(nullptr); |
Chris Pickel | 5d76e33 | 2018-05-15 12:18:57 | [diff] [blame] | 187 | |
Xi Han | 6740d62 | 2018-05-22 19:21:07 | [diff] [blame] | 188 | is_shutdown_ = true; |
189 | } | ||||
190 | } | ||||
[email protected] | f573ed6b | 2012-02-10 15:58:52 | [diff] [blame] | 191 | |
[email protected] | f573ed6b | 2012-02-10 15:58:52 | [diff] [blame] | 192 | // static |
Ayu Ishii | 4e19b5f | 2019-01-16 23:22:00 | [diff] [blame] | 193 | std::unique_ptr<BrowserMainRunner> BrowserMainRunner::Create() { |
Xi Han | 6740d62 | 2018-05-22 19:21:07 | [diff] [blame] | 194 | return BrowserMainRunnerImpl::Create(); |
[email protected] | f573ed6b | 2012-02-10 15:58:52 | [diff] [blame] | 195 | } |
196 | |||||
thestig | 5a551c4c | 2015-08-29 02:45:35 | [diff] [blame] | 197 | // static |
198 | bool BrowserMainRunner::ExitedMainMessageLoop() { | ||||
Lei Zhang | 81f8569 | 2022-10-06 21:03:22 | [diff] [blame] | 199 | return GetExitedMainMessageLoopFlag().IsSet(); |
thestig | 5a551c4c | 2015-08-29 02:45:35 | [diff] [blame] | 200 | } |
201 | |||||
[email protected] | f573ed6b | 2012-02-10 15:58:52 | [diff] [blame] | 202 | } // namespace content |