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