michaeln | 96f887e2 | 2015-04-13 23:58:31 | [diff] [blame] | 1 | // Copyright 2015 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 "chrome/browser/after_startup_task_utils.h" |
| 6 | |
Brett Wilson | 275a137 | 2017-09-01 20:27:54 | [diff] [blame] | 7 | #include "base/containers/circular_deque.h" |
michaeln | 96f887e2 | 2015-04-13 23:58:31 | [diff] [blame] | 8 | #include "base/lazy_instance.h" |
dcheng | 4af4858 | 2016-04-19 00:29:35 | [diff] [blame] | 9 | #include "base/memory/ptr_util.h" |
michaeln | 96f887e2 | 2015-04-13 23:58:31 | [diff] [blame] | 10 | #include "base/metrics/histogram_macros.h" |
Francois Doray | 1456375 | 2018-10-23 14:15:57 | [diff] [blame] | 11 | #include "base/process/process.h" |
fdoray | ef19112 | 2016-07-25 14:43:17 | [diff] [blame] | 12 | #include "base/synchronization/atomic_flag.h" |
Patrick Monette | 643cdf6 | 2021-10-15 19:13:42 | [diff] [blame] | 13 | #include "base/task/sequenced_task_runner.h" |
avi | e4d7b6f | 2015-12-26 00:59:18 | [diff] [blame] | 14 | #include "build/build_config.h" |
Yuta Hijikata | 235fc62b | 2020-12-08 03:48:32 | [diff] [blame] | 15 | #include "build/chromeos_buildflags.h" |
Chris Davis (EDGE) | 08c0877d | 2021-04-28 22:18:44 | [diff] [blame] | 16 | #include "components/performance_manager/performance_manager_impl.h" |
| 17 | #include "components/performance_manager/public/graph/graph.h" |
| 18 | #include "components/performance_manager/public/graph/page_node.h" |
Eric Seckler | 8652dcd5 | 2018-09-20 10:42:28 | [diff] [blame] | 19 | #include "content/public/browser/browser_task_traits.h" |
michaeln | 96f887e2 | 2015-04-13 23:58:31 | [diff] [blame] | 20 | #include "content/public/browser/browser_thread.h" |
Collin Baker | 8a21755 | 2019-05-29 19:47:51 | [diff] [blame] | 21 | |
Alexander Alekseev | 2942872 | 2021-11-24 02:08:52 | [diff] [blame^] | 22 | #if BUILDFLAG(IS_CHROMEOS_ASH) |
| 23 | #include "chrome/browser/ash/login/ui/login_display_host.h" |
| 24 | #endif |
| 25 | |
Yuta Hijikata | 235fc62b | 2020-12-08 03:48:32 | [diff] [blame] | 26 | // TODO(crbug.com/1052397): Revisit the macro expression once build flag switch |
| 27 | // of lacros-chrome is complete. |
| 28 | #if defined(OS_LINUX) || BUILDFLAG(IS_CHROMEOS_LACROS) |
Mario Sanchez Prada | 0a85ddf | 2018-06-07 20:32:59 | [diff] [blame] | 29 | #include "ui/views/linux_ui/linux_ui.h" |
| 30 | #endif |
| 31 | |
Erik Chen | 25887d7e | 2021-10-11 20:16:52 | [diff] [blame] | 32 | #if BUILDFLAG(IS_CHROMEOS_LACROS) |
| 33 | #include "chromeos/lacros/lacros_service.h" |
| 34 | #endif // BUILDFLAG(IS_CHROMEOS_LACROS) |
| 35 | |
michaeln | 96f887e2 | 2015-04-13 23:58:31 | [diff] [blame] | 36 | using content::BrowserThread; |
michaeln | 96f887e2 | 2015-04-13 23:58:31 | [diff] [blame] | 37 | |
| 38 | namespace { |
| 39 | |
| 40 | struct AfterStartupTask { |
Brett Wilson | e1a7042 | 2017-09-12 05:10:09 | [diff] [blame] | 41 | AfterStartupTask(const base::Location& from_here, |
Gabriel Charette | e926fc1 | 2019-12-16 19:00:02 | [diff] [blame] | 42 | const scoped_refptr<base::SequencedTaskRunner>& task_runner, |
tzik | 6e42784 | 2017-04-05 10:13:21 | [diff] [blame] | 43 | base::OnceClosure task) |
tzik | 070c8ffb | 2017-03-29 05:28:12 | [diff] [blame] | 44 | : from_here(from_here), task_runner(task_runner), task(std::move(task)) {} |
michaeln | 96f887e2 | 2015-04-13 23:58:31 | [diff] [blame] | 45 | ~AfterStartupTask() {} |
| 46 | |
Brett Wilson | e1a7042 | 2017-09-12 05:10:09 | [diff] [blame] | 47 | const base::Location from_here; |
Gabriel Charette | e926fc1 | 2019-12-16 19:00:02 | [diff] [blame] | 48 | const scoped_refptr<base::SequencedTaskRunner> task_runner; |
tzik | 6e42784 | 2017-04-05 10:13:21 | [diff] [blame] | 49 | base::OnceClosure task; |
michaeln | 96f887e2 | 2015-04-13 23:58:31 | [diff] [blame] | 50 | }; |
| 51 | |
| 52 | // The flag may be read on any thread, but must only be set on the UI thread. |
fdoray | ef19112 | 2016-07-25 14:43:17 | [diff] [blame] | 53 | base::LazyInstance<base::AtomicFlag>::Leaky g_startup_complete_flag; |
michaeln | 96f887e2 | 2015-04-13 23:58:31 | [diff] [blame] | 54 | |
| 55 | // The queue may only be accessed on the UI thread. |
Brett Wilson | 275a137 | 2017-09-01 20:27:54 | [diff] [blame] | 56 | base::LazyInstance<base::circular_deque<AfterStartupTask*>>::Leaky |
| 57 | g_after_startup_tasks; |
michaeln | 96f887e2 | 2015-04-13 23:58:31 | [diff] [blame] | 58 | |
| 59 | bool IsBrowserStartupComplete() { |
| 60 | // Be sure to initialize the LazyInstance on the main thread since the flag |
| 61 | // may only be set on it's initializing thread. |
Lukasz Anforowicz | d3e1913 | 2017-12-06 19:44:27 | [diff] [blame] | 62 | if (!g_startup_complete_flag.IsCreated()) |
michaeln | 96f887e2 | 2015-04-13 23:58:31 | [diff] [blame] | 63 | return false; |
| 64 | return g_startup_complete_flag.Get().IsSet(); |
| 65 | } |
| 66 | |
dcheng | 4af4858 | 2016-04-19 00:29:35 | [diff] [blame] | 67 | void RunTask(std::unique_ptr<AfterStartupTask> queued_task) { |
michaeln | 96f887e2 | 2015-04-13 23:58:31 | [diff] [blame] | 68 | // We're careful to delete the caller's |task| on the target runner's thread. |
peary2 | be58808 | 2017-05-17 01:59:49 | [diff] [blame] | 69 | DCHECK(queued_task->task_runner->RunsTasksInCurrentSequence()); |
tzik | 070c8ffb | 2017-03-29 05:28:12 | [diff] [blame] | 70 | std::move(queued_task->task).Run(); |
michaeln | 96f887e2 | 2015-04-13 23:58:31 | [diff] [blame] | 71 | } |
| 72 | |
dcheng | 4af4858 | 2016-04-19 00:29:35 | [diff] [blame] | 73 | void ScheduleTask(std::unique_ptr<AfterStartupTask> queued_task) { |
Gabriel Charette | e926fc1 | 2019-12-16 19:00:02 | [diff] [blame] | 74 | scoped_refptr<base::SequencedTaskRunner> target_runner = |
| 75 | queued_task->task_runner; |
Brett Wilson | e1a7042 | 2017-09-12 05:10:09 | [diff] [blame] | 76 | base::Location from_here = queued_task->from_here; |
Chris Davis (EDGE) | 75ff9260 | 2021-03-29 17:10:14 | [diff] [blame] | 77 | target_runner->PostTask(from_here, |
| 78 | base::BindOnce(&RunTask, std::move(queued_task))); |
michaeln | 96f887e2 | 2015-04-13 23:58:31 | [diff] [blame] | 79 | } |
| 80 | |
dcheng | 4af4858 | 2016-04-19 00:29:35 | [diff] [blame] | 81 | void QueueTask(std::unique_ptr<AfterStartupTask> queued_task) { |
tzik | c697696 | 2017-04-04 17:27:34 | [diff] [blame] | 82 | DCHECK(queued_task); |
tzik | 498d42b | 2017-04-13 07:42:48 | [diff] [blame] | 83 | |
| 84 | // Use CHECK instead of DCHECK to crash earlier. See http://crbug.com/711167 |
| 85 | // for details. |
| 86 | CHECK(queued_task->task); |
| 87 | |
michaeln | 96f887e2 | 2015-04-13 23:58:31 | [diff] [blame] | 88 | if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { |
Eric Seckler | 0618f40 | 2018-10-29 12:08:52 | [diff] [blame] | 89 | // Posted with USER_VISIBLE priority to avoid this becoming an after startup |
| 90 | // task itself. |
Gabriel Charette | e7cdc5cd | 2020-05-27 23:35:05 | [diff] [blame] | 91 | content::GetUIThreadTaskRunner({base::TaskPriority::USER_VISIBLE}) |
| 92 | ->PostTask(FROM_HERE, |
Sami Kyostila | 7d640eb | 2019-07-31 18:50:26 | [diff] [blame] | 93 | base::BindOnce(QueueTask, std::move(queued_task))); |
michaeln | 96f887e2 | 2015-04-13 23:58:31 | [diff] [blame] | 94 | return; |
| 95 | } |
| 96 | |
| 97 | // The flag may have been set while the task to invoke this method |
| 98 | // on the UI thread was inflight. |
| 99 | if (IsBrowserStartupComplete()) { |
dcheng | e73d8520c | 2015-12-27 01:19:09 | [diff] [blame] | 100 | ScheduleTask(std::move(queued_task)); |
michaeln | 96f887e2 | 2015-04-13 23:58:31 | [diff] [blame] | 101 | return; |
| 102 | } |
| 103 | g_after_startup_tasks.Get().push_back(queued_task.release()); |
| 104 | } |
| 105 | |
| 106 | void SetBrowserStartupIsComplete() { |
| 107 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
Chris Davis (EDGE) | 08c0877d | 2021-04-28 22:18:44 | [diff] [blame] | 108 | |
| 109 | if (IsBrowserStartupComplete()) |
| 110 | return; |
| 111 | |
| 112 | g_startup_complete_flag.Get().Set(); |
Sean McAllister | df80771 | 2020-08-13 23:19:09 | [diff] [blame] | 113 | #if defined(OS_MAC) || defined(OS_WIN) || defined(OS_LINUX) || \ |
| 114 | defined(OS_CHROMEOS) |
Francois Doray | 1456375 | 2018-10-23 14:15:57 | [diff] [blame] | 115 | // Process::Current().CreationTime() is not available on all platforms. |
michaeln | 96f887e2 | 2015-04-13 23:58:31 | [diff] [blame] | 116 | const base::Time process_creation_time = |
Francois Doray | 1456375 | 2018-10-23 14:15:57 | [diff] [blame] | 117 | base::Process::Current().CreationTime(); |
michaeln | 96f887e2 | 2015-04-13 23:58:31 | [diff] [blame] | 118 | if (!process_creation_time.is_null()) { |
| 119 | UMA_HISTOGRAM_LONG_TIMES("Startup.AfterStartupTaskDelayedUntilTime", |
| 120 | base::Time::Now() - process_creation_time); |
| 121 | } |
Sean McAllister | df80771 | 2020-08-13 23:19:09 | [diff] [blame] | 122 | #endif // defined(OS_MAC) || defined(OS_WIN) || defined(OS_LINUX) || |
| 123 | // defined(OS_CHROMEOS) |
michaeln | 96f887e2 | 2015-04-13 23:58:31 | [diff] [blame] | 124 | UMA_HISTOGRAM_COUNTS_10000("Startup.AfterStartupTaskCount", |
| 125 | g_after_startup_tasks.Get().size()); |
michaeln | 96f887e2 | 2015-04-13 23:58:31 | [diff] [blame] | 126 | for (AfterStartupTask* queued_task : g_after_startup_tasks.Get()) |
dcheng | 4af4858 | 2016-04-19 00:29:35 | [diff] [blame] | 127 | ScheduleTask(base::WrapUnique(queued_task)); |
michaeln | 96f887e2 | 2015-04-13 23:58:31 | [diff] [blame] | 128 | g_after_startup_tasks.Get().clear(); |
Lei Zhang | 3b9950b | 2018-12-21 22:24:51 | [diff] [blame] | 129 | g_after_startup_tasks.Get().shrink_to_fit(); |
Mario Sanchez Prada | 0a85ddf | 2018-06-07 20:32:59 | [diff] [blame] | 130 | |
Yuta Hijikata | 235fc62b | 2020-12-08 03:48:32 | [diff] [blame] | 131 | // TODO(crbug.com/1052397): Revisit the macro expression once build flag switch |
| 132 | // of lacros-chrome is complete. |
| 133 | #if defined(OS_LINUX) || BUILDFLAG(IS_CHROMEOS_LACROS) |
Mario Sanchez Prada | 0a85ddf | 2018-06-07 20:32:59 | [diff] [blame] | 134 | // Make sure we complete the startup notification sequence, or launchers will |
| 135 | // get confused by not receiving the expected message from the main process. |
| 136 | views::LinuxUI* linux_ui = views::LinuxUI::instance(); |
| 137 | if (linux_ui) |
| 138 | linux_ui->NotifyWindowManagerStartupComplete(); |
| 139 | #endif |
michaeln | 96f887e2 | 2015-04-13 23:58:31 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | // Observes the first visible page load and sets the startup complete |
Chris Davis (EDGE) | 08c0877d | 2021-04-28 22:18:44 | [diff] [blame] | 143 | // flag accordingly. Ownership is passed to the Performance Manager |
| 144 | // after creation. |
| 145 | class StartupObserver |
| 146 | : public performance_manager::GraphOwned, |
| 147 | public performance_manager::PageNode::ObserverDefaultImpl { |
michaeln | 96f887e2 | 2015-04-13 23:58:31 | [diff] [blame] | 148 | public: |
Peter Boström | 53c6c595 | 2021-09-17 09:41:26 | [diff] [blame] | 149 | StartupObserver(const StartupObserver&) = delete; |
| 150 | StartupObserver& operator=(const StartupObserver&) = delete; |
| 151 | |
Chris Davis (EDGE) | 08c0877d | 2021-04-28 22:18:44 | [diff] [blame] | 152 | ~StartupObserver() override = default; |
michaeln | 96f887e2 | 2015-04-13 23:58:31 | [diff] [blame] | 153 | |
Chris Davis (EDGE) | 08c0877d | 2021-04-28 22:18:44 | [diff] [blame] | 154 | static void Start(); |
michaeln | 96f887e2 | 2015-04-13 23:58:31 | [diff] [blame] | 155 | |
| 156 | private: |
Chris Davis (EDGE) | 08c0877d | 2021-04-28 22:18:44 | [diff] [blame] | 157 | StartupObserver() = default; |
| 158 | |
michaeln | 96f887e2 | 2015-04-13 23:58:31 | [diff] [blame] | 159 | void OnStartupComplete() { |
Chris Davis (EDGE) | 08c0877d | 2021-04-28 22:18:44 | [diff] [blame] | 160 | // This should only be called once. |
| 161 | if (!startup_complete_) { |
| 162 | startup_complete_ = true; |
| 163 | content::GetUIThreadTaskRunner({})->PostTask( |
| 164 | FROM_HERE, base::BindOnce(&SetBrowserStartupIsComplete)); |
| 165 | // This will result in delete getting called. |
| 166 | TakeFromGraph(); |
michaeln | 96f887e2 | 2015-04-13 23:58:31 | [diff] [blame] | 167 | } |
| 168 | } |
| 169 | |
Chris Davis (EDGE) | 08c0877d | 2021-04-28 22:18:44 | [diff] [blame] | 170 | // GraphOwned overrides |
| 171 | void OnPassedToGraph(performance_manager::Graph* graph) override { |
| 172 | graph->AddPageNodeObserver(this); |
michaeln | 96f887e2 | 2015-04-13 23:58:31 | [diff] [blame] | 173 | } |
michaeln | 96f887e2 | 2015-04-13 23:58:31 | [diff] [blame] | 174 | |
Chris Davis (EDGE) | 08c0877d | 2021-04-28 22:18:44 | [diff] [blame] | 175 | void OnTakenFromGraph(performance_manager::Graph* graph) override { |
| 176 | graph->RemovePageNodeObserver(this); |
| 177 | } |
| 178 | |
| 179 | // PageNodeObserver overrides |
| 180 | void OnLoadingStateChanged( |
François Doray | e90de75a | 2021-11-15 22:29:07 | [diff] [blame] | 181 | const performance_manager::PageNode* page_node, |
| 182 | performance_manager::PageNode::LoadingState previous_state) override { |
Chris Davis (EDGE) | 08c0877d | 2021-04-28 22:18:44 | [diff] [blame] | 183 | // Only interested in visible PageNodes |
| 184 | if (page_node->IsVisible()) { |
| 185 | if (page_node->GetLoadingState() == |
| 186 | performance_manager::PageNode::LoadingState::kLoadedIdle || |
| 187 | page_node->GetLoadingState() == |
| 188 | performance_manager::PageNode::LoadingState::kLoadingTimedOut) |
| 189 | OnStartupComplete(); |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | void PassToGraph() { |
| 194 | // Pass to the performance manager so we can get notified when |
| 195 | // loading completes. Ownership of this object is passed to the |
| 196 | // performance manager. |
| 197 | DCHECK(performance_manager::PerformanceManagerImpl::IsAvailable()); |
| 198 | performance_manager::PerformanceManagerImpl::PassToGraph( |
| 199 | FROM_HERE, base::WrapUnique(this)); |
| 200 | } |
| 201 | |
| 202 | void TakeFromGraph() { |
| 203 | // Remove this object from the performance manager. This will |
| 204 | // cause the object to be deleted. |
| 205 | DCHECK(performance_manager::PerformanceManagerImpl::IsAvailable()); |
| 206 | performance_manager::PerformanceManager::CallOnGraph( |
| 207 | FROM_HERE, base::BindOnce( |
| 208 | [](performance_manager::GraphOwned* observer, |
| 209 | performance_manager::Graph* graph) { |
| 210 | graph->TakeFromGraph(observer); |
| 211 | }, |
| 212 | base::Unretained(this))); |
| 213 | } |
| 214 | |
| 215 | bool startup_complete_ = false; |
Chris Davis (EDGE) | 08c0877d | 2021-04-28 22:18:44 | [diff] [blame] | 216 | }; |
| 217 | |
| 218 | // static |
| 219 | void StartupObserver::Start() { |
| 220 | // Create the StartupObserver and pass it to the Performance Manager which |
| 221 | // will own it going forward. |
| 222 | (new StartupObserver)->PassToGraph(); |
michaeln | 96f887e2 | 2015-04-13 23:58:31 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | } // namespace |
| 226 | |
| 227 | void AfterStartupTaskUtils::StartMonitoringStartup() { |
Chris Davis (EDGE) | 08c0877d | 2021-04-28 22:18:44 | [diff] [blame] | 228 | // For Android, startup completion is signaled via |
| 229 | // AfterStartupTaskUtils.java. We do not use the StartupObserver. |
| 230 | #if !defined(OS_ANDROID) |
Erik Chen | 25887d7e | 2021-10-11 20:16:52 | [diff] [blame] | 231 | #if BUILDFLAG(IS_CHROMEOS_LACROS) |
| 232 | // For Lacros, there may not be a Browser created at startup. |
| 233 | if (chromeos::LacrosService::Get()->init_params()->initial_browser_action == |
| 234 | crosapi::mojom::InitialBrowserAction::kDoNotOpenWindow) { |
| 235 | content::GetUIThreadTaskRunner({})->PostTask( |
| 236 | FROM_HERE, base::BindOnce(&SetBrowserStartupIsComplete)); |
| 237 | return; |
| 238 | } |
| 239 | #endif |
| 240 | |
Alexander Alekseev | 2942872 | 2021-11-24 02:08:52 | [diff] [blame^] | 241 | #if BUILDFLAG(IS_CHROMEOS_ASH) |
| 242 | // If we are on a login screen which does not expect WebUI to be loaded, |
| 243 | // Browser won't be created at startup. |
| 244 | if (ash::LoginDisplayHost::default_host() && |
| 245 | !ash::LoginDisplayHost::default_host()->IsWebUIStarted()) { |
| 246 | content::GetUIThreadTaskRunner({})->PostTask( |
| 247 | FROM_HERE, base::BindOnce(&SetBrowserStartupIsComplete)); |
| 248 | return; |
| 249 | } |
| 250 | #endif |
| 251 | |
Chris Davis (EDGE) | 08c0877d | 2021-04-28 22:18:44 | [diff] [blame] | 252 | StartupObserver::Start(); |
| 253 | #endif // !defined(OS_ANDROID) |
| 254 | |
| 255 | // Add failsafe timeout |
| 256 | content::GetUIThreadTaskRunner({})->PostDelayedTask( |
| 257 | FROM_HERE, base::BindOnce(&SetBrowserStartupIsComplete), |
Peter Kasting | e5a38ed | 2021-10-02 03:06:35 | [diff] [blame] | 258 | base::Minutes(3)); |
michaeln | 96f887e2 | 2015-04-13 23:58:31 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | void AfterStartupTaskUtils::PostTask( |
Brett Wilson | e1a7042 | 2017-09-12 05:10:09 | [diff] [blame] | 262 | const base::Location& from_here, |
Gabriel Charette | e926fc1 | 2019-12-16 19:00:02 | [diff] [blame] | 263 | const scoped_refptr<base::SequencedTaskRunner>& destination_runner, |
tzik | 6e42784 | 2017-04-05 10:13:21 | [diff] [blame] | 264 | base::OnceClosure task) { |
michaeln | 96f887e2 | 2015-04-13 23:58:31 | [diff] [blame] | 265 | if (IsBrowserStartupComplete()) { |
tzik | 070c8ffb | 2017-03-29 05:28:12 | [diff] [blame] | 266 | destination_runner->PostTask(from_here, std::move(task)); |
michaeln | 96f887e2 | 2015-04-13 23:58:31 | [diff] [blame] | 267 | return; |
| 268 | } |
| 269 | |
dcheng | 4af4858 | 2016-04-19 00:29:35 | [diff] [blame] | 270 | std::unique_ptr<AfterStartupTask> queued_task( |
tzik | 070c8ffb | 2017-03-29 05:28:12 | [diff] [blame] | 271 | new AfterStartupTask(from_here, destination_runner, std::move(task))); |
dcheng | e73d8520c | 2015-12-27 01:19:09 | [diff] [blame] | 272 | QueueTask(std::move(queued_task)); |
michaeln | 96f887e2 | 2015-04-13 23:58:31 | [diff] [blame] | 273 | } |
| 274 | |
wkorman | 8a21c4f | 2015-11-18 19:06:11 | [diff] [blame] | 275 | void AfterStartupTaskUtils::SetBrowserStartupIsCompleteForTesting() { |
| 276 | ::SetBrowserStartupIsComplete(); |
| 277 | } |
| 278 | |
michaeln | 96f887e2 | 2015-04-13 23:58:31 | [diff] [blame] | 279 | void AfterStartupTaskUtils::SetBrowserStartupIsComplete() { |
| 280 | ::SetBrowserStartupIsComplete(); |
| 281 | } |
| 282 | |
| 283 | bool AfterStartupTaskUtils::IsBrowserStartupComplete() { |
| 284 | return ::IsBrowserStartupComplete(); |
| 285 | } |
| 286 | |
| 287 | void AfterStartupTaskUtils::UnsafeResetForTesting() { |
| 288 | DCHECK(g_after_startup_tasks.Get().empty()); |
| 289 | if (!IsBrowserStartupComplete()) |
| 290 | return; |
| 291 | g_startup_complete_flag.Get().UnsafeResetForTesting(); |
| 292 | DCHECK(!IsBrowserStartupComplete()); |
| 293 | } |