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 | |
dcheng | 4af4858 | 2016-04-19 00:29:35 | [diff] [blame] | 7 | #include <memory> |
dcheng | e73d8520c | 2015-12-27 01:19:09 | [diff] [blame] | 8 | #include <utility> |
| 9 | |
Brett Wilson | 275a137 | 2017-09-01 20:27:54 | [diff] [blame] | 10 | #include "base/containers/circular_deque.h" |
michaeln | 96f887e2 | 2015-04-13 23:58:31 | [diff] [blame] | 11 | #include "base/lazy_instance.h" |
avi | e4d7b6f | 2015-12-26 00:59:18 | [diff] [blame] | 12 | #include "base/macros.h" |
dcheng | 4af4858 | 2016-04-19 00:29:35 | [diff] [blame] | 13 | #include "base/memory/ptr_util.h" |
michaeln | 96f887e2 | 2015-04-13 23:58:31 | [diff] [blame] | 14 | #include "base/metrics/histogram_macros.h" |
Francois Doray | 1456375 | 2018-10-23 14:15:57 | [diff] [blame] | 15 | #include "base/process/process.h" |
michaeln | 96f887e2 | 2015-04-13 23:58:31 | [diff] [blame] | 16 | #include "base/rand_util.h" |
gab | 25894fe | 2017-05-30 03:40:36 | [diff] [blame] | 17 | #include "base/sequence_checker.h" |
Gabriel Charette | e926fc1 | 2019-12-16 19:00:02 | [diff] [blame] | 18 | #include "base/sequenced_task_runner.h" |
fdoray | ef19112 | 2016-07-25 14:43:17 | [diff] [blame] | 19 | #include "base/synchronization/atomic_flag.h" |
avi | e4d7b6f | 2015-12-26 00:59:18 | [diff] [blame] | 20 | #include "build/build_config.h" |
Yuta Hijikata | 235fc62b | 2020-12-08 03:48:32 | [diff] [blame] | 21 | #include "build/chromeos_buildflags.h" |
Eric Seckler | 8652dcd5 | 2018-09-20 10:42:28 | [diff] [blame] | 22 | #include "content/public/browser/browser_task_traits.h" |
michaeln | 96f887e2 | 2015-04-13 23:58:31 | [diff] [blame] | 23 | #include "content/public/browser/browser_thread.h" |
Eric Lawrence | 45eee3a | 2019-10-14 22:46:36 | [diff] [blame] | 24 | #include "content/public/browser/navigation_handle.h" |
michaeln | 96f887e2 | 2015-04-13 23:58:31 | [diff] [blame] | 25 | #include "content/public/browser/render_frame_host.h" |
| 26 | #include "content/public/browser/web_contents.h" |
| 27 | #include "content/public/browser/web_contents_observer.h" |
Collin Baker | 98457b5 | 2019-11-06 21:34:29 | [diff] [blame] | 28 | #include "content/public/common/page_visibility_state.h" |
michaeln | 96f887e2 | 2015-04-13 23:58:31 | [diff] [blame] | 29 | |
Collin Baker | 8a21755 | 2019-05-29 19:47:51 | [diff] [blame] | 30 | #if !defined(OS_ANDROID) |
| 31 | #include "chrome/browser/ui/browser.h" |
| 32 | #include "chrome/browser/ui/browser_list.h" |
| 33 | #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 34 | #endif |
| 35 | |
Yuta Hijikata | 235fc62b | 2020-12-08 03:48:32 | [diff] [blame] | 36 | // TODO(crbug.com/1052397): Revisit the macro expression once build flag switch |
| 37 | // of lacros-chrome is complete. |
| 38 | #if defined(OS_LINUX) || BUILDFLAG(IS_CHROMEOS_LACROS) |
Mario Sanchez Prada | 0a85ddf | 2018-06-07 20:32:59 | [diff] [blame] | 39 | #include "ui/views/linux_ui/linux_ui.h" |
| 40 | #endif |
| 41 | |
michaeln | 96f887e2 | 2015-04-13 23:58:31 | [diff] [blame] | 42 | using content::BrowserThread; |
| 43 | using content::WebContents; |
| 44 | using content::WebContentsObserver; |
michaeln | 96f887e2 | 2015-04-13 23:58:31 | [diff] [blame] | 45 | |
| 46 | namespace { |
| 47 | |
| 48 | struct AfterStartupTask { |
Brett Wilson | e1a7042 | 2017-09-12 05:10:09 | [diff] [blame] | 49 | AfterStartupTask(const base::Location& from_here, |
Gabriel Charette | e926fc1 | 2019-12-16 19:00:02 | [diff] [blame] | 50 | const scoped_refptr<base::SequencedTaskRunner>& task_runner, |
tzik | 6e42784 | 2017-04-05 10:13:21 | [diff] [blame] | 51 | base::OnceClosure task) |
tzik | 070c8ffb | 2017-03-29 05:28:12 | [diff] [blame] | 52 | : from_here(from_here), task_runner(task_runner), task(std::move(task)) {} |
michaeln | 96f887e2 | 2015-04-13 23:58:31 | [diff] [blame] | 53 | ~AfterStartupTask() {} |
| 54 | |
Brett Wilson | e1a7042 | 2017-09-12 05:10:09 | [diff] [blame] | 55 | const base::Location from_here; |
Gabriel Charette | e926fc1 | 2019-12-16 19:00:02 | [diff] [blame] | 56 | const scoped_refptr<base::SequencedTaskRunner> task_runner; |
tzik | 6e42784 | 2017-04-05 10:13:21 | [diff] [blame] | 57 | base::OnceClosure task; |
michaeln | 96f887e2 | 2015-04-13 23:58:31 | [diff] [blame] | 58 | }; |
| 59 | |
| 60 | // 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] | 61 | base::LazyInstance<base::AtomicFlag>::Leaky g_startup_complete_flag; |
michaeln | 96f887e2 | 2015-04-13 23:58:31 | [diff] [blame] | 62 | |
| 63 | // The queue may only be accessed on the UI thread. |
Brett Wilson | 275a137 | 2017-09-01 20:27:54 | [diff] [blame] | 64 | base::LazyInstance<base::circular_deque<AfterStartupTask*>>::Leaky |
| 65 | g_after_startup_tasks; |
michaeln | 96f887e2 | 2015-04-13 23:58:31 | [diff] [blame] | 66 | |
| 67 | bool IsBrowserStartupComplete() { |
| 68 | // Be sure to initialize the LazyInstance on the main thread since the flag |
| 69 | // may only be set on it's initializing thread. |
Lukasz Anforowicz | d3e1913 | 2017-12-06 19:44:27 | [diff] [blame] | 70 | if (!g_startup_complete_flag.IsCreated()) |
michaeln | 96f887e2 | 2015-04-13 23:58:31 | [diff] [blame] | 71 | return false; |
| 72 | return g_startup_complete_flag.Get().IsSet(); |
| 73 | } |
| 74 | |
dcheng | 4af4858 | 2016-04-19 00:29:35 | [diff] [blame] | 75 | void RunTask(std::unique_ptr<AfterStartupTask> queued_task) { |
michaeln | 96f887e2 | 2015-04-13 23:58:31 | [diff] [blame] | 76 | // 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] | 77 | DCHECK(queued_task->task_runner->RunsTasksInCurrentSequence()); |
tzik | 070c8ffb | 2017-03-29 05:28:12 | [diff] [blame] | 78 | std::move(queued_task->task).Run(); |
michaeln | 96f887e2 | 2015-04-13 23:58:31 | [diff] [blame] | 79 | } |
| 80 | |
dcheng | 4af4858 | 2016-04-19 00:29:35 | [diff] [blame] | 81 | void ScheduleTask(std::unique_ptr<AfterStartupTask> queued_task) { |
Gabriel Charette | e926fc1 | 2019-12-16 19:00:02 | [diff] [blame] | 82 | scoped_refptr<base::SequencedTaskRunner> target_runner = |
| 83 | queued_task->task_runner; |
Brett Wilson | e1a7042 | 2017-09-12 05:10:09 | [diff] [blame] | 84 | base::Location from_here = queued_task->from_here; |
Chris Davis (EDGE) | 75ff9260 | 2021-03-29 17:10:14 | [diff] [blame^] | 85 | target_runner->PostTask(from_here, |
| 86 | base::BindOnce(&RunTask, std::move(queued_task))); |
michaeln | 96f887e2 | 2015-04-13 23:58:31 | [diff] [blame] | 87 | } |
| 88 | |
dcheng | 4af4858 | 2016-04-19 00:29:35 | [diff] [blame] | 89 | void QueueTask(std::unique_ptr<AfterStartupTask> queued_task) { |
tzik | c697696 | 2017-04-04 17:27:34 | [diff] [blame] | 90 | DCHECK(queued_task); |
tzik | 498d42b | 2017-04-13 07:42:48 | [diff] [blame] | 91 | |
| 92 | // Use CHECK instead of DCHECK to crash earlier. See http://crbug.com/711167 |
| 93 | // for details. |
| 94 | CHECK(queued_task->task); |
| 95 | |
michaeln | 96f887e2 | 2015-04-13 23:58:31 | [diff] [blame] | 96 | if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { |
Eric Seckler | 0618f40 | 2018-10-29 12:08:52 | [diff] [blame] | 97 | // Posted with USER_VISIBLE priority to avoid this becoming an after startup |
| 98 | // task itself. |
Gabriel Charette | e7cdc5cd | 2020-05-27 23:35:05 | [diff] [blame] | 99 | content::GetUIThreadTaskRunner({base::TaskPriority::USER_VISIBLE}) |
| 100 | ->PostTask(FROM_HERE, |
Sami Kyostila | 7d640eb | 2019-07-31 18:50:26 | [diff] [blame] | 101 | base::BindOnce(QueueTask, std::move(queued_task))); |
michaeln | 96f887e2 | 2015-04-13 23:58:31 | [diff] [blame] | 102 | return; |
| 103 | } |
| 104 | |
| 105 | // The flag may have been set while the task to invoke this method |
| 106 | // on the UI thread was inflight. |
| 107 | if (IsBrowserStartupComplete()) { |
dcheng | e73d8520c | 2015-12-27 01:19:09 | [diff] [blame] | 108 | ScheduleTask(std::move(queued_task)); |
michaeln | 96f887e2 | 2015-04-13 23:58:31 | [diff] [blame] | 109 | return; |
| 110 | } |
| 111 | g_after_startup_tasks.Get().push_back(queued_task.release()); |
| 112 | } |
| 113 | |
| 114 | void SetBrowserStartupIsComplete() { |
| 115 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
Sean McAllister | df80771 | 2020-08-13 23:19:09 | [diff] [blame] | 116 | #if defined(OS_MAC) || defined(OS_WIN) || defined(OS_LINUX) || \ |
| 117 | defined(OS_CHROMEOS) |
Francois Doray | 1456375 | 2018-10-23 14:15:57 | [diff] [blame] | 118 | // Process::Current().CreationTime() is not available on all platforms. |
michaeln | 96f887e2 | 2015-04-13 23:58:31 | [diff] [blame] | 119 | const base::Time process_creation_time = |
Francois Doray | 1456375 | 2018-10-23 14:15:57 | [diff] [blame] | 120 | base::Process::Current().CreationTime(); |
michaeln | 96f887e2 | 2015-04-13 23:58:31 | [diff] [blame] | 121 | if (!process_creation_time.is_null()) { |
| 122 | UMA_HISTOGRAM_LONG_TIMES("Startup.AfterStartupTaskDelayedUntilTime", |
| 123 | base::Time::Now() - process_creation_time); |
| 124 | } |
Sean McAllister | df80771 | 2020-08-13 23:19:09 | [diff] [blame] | 125 | #endif // defined(OS_MAC) || defined(OS_WIN) || defined(OS_LINUX) || |
| 126 | // defined(OS_CHROMEOS) |
michaeln | 96f887e2 | 2015-04-13 23:58:31 | [diff] [blame] | 127 | UMA_HISTOGRAM_COUNTS_10000("Startup.AfterStartupTaskCount", |
| 128 | g_after_startup_tasks.Get().size()); |
| 129 | g_startup_complete_flag.Get().Set(); |
| 130 | for (AfterStartupTask* queued_task : g_after_startup_tasks.Get()) |
dcheng | 4af4858 | 2016-04-19 00:29:35 | [diff] [blame] | 131 | ScheduleTask(base::WrapUnique(queued_task)); |
michaeln | 96f887e2 | 2015-04-13 23:58:31 | [diff] [blame] | 132 | g_after_startup_tasks.Get().clear(); |
Lei Zhang | 3b9950b | 2018-12-21 22:24:51 | [diff] [blame] | 133 | g_after_startup_tasks.Get().shrink_to_fit(); |
Mario Sanchez Prada | 0a85ddf | 2018-06-07 20:32:59 | [diff] [blame] | 134 | |
Yuta Hijikata | 235fc62b | 2020-12-08 03:48:32 | [diff] [blame] | 135 | // TODO(crbug.com/1052397): Revisit the macro expression once build flag switch |
| 136 | // of lacros-chrome is complete. |
| 137 | #if defined(OS_LINUX) || BUILDFLAG(IS_CHROMEOS_LACROS) |
Mario Sanchez Prada | 0a85ddf | 2018-06-07 20:32:59 | [diff] [blame] | 138 | // Make sure we complete the startup notification sequence, or launchers will |
| 139 | // get confused by not receiving the expected message from the main process. |
| 140 | views::LinuxUI* linux_ui = views::LinuxUI::instance(); |
| 141 | if (linux_ui) |
| 142 | linux_ui->NotifyWindowManagerStartupComplete(); |
| 143 | #endif |
michaeln | 96f887e2 | 2015-04-13 23:58:31 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | // Observes the first visible page load and sets the startup complete |
| 147 | // flag accordingly. |
gab | 25894fe | 2017-05-30 03:40:36 | [diff] [blame] | 148 | class StartupObserver : public WebContentsObserver { |
michaeln | 96f887e2 | 2015-04-13 23:58:31 | [diff] [blame] | 149 | public: |
Jeremy Roman | 495db68 | 2019-07-12 16:03:24 | [diff] [blame] | 150 | StartupObserver() {} |
gab | 25894fe | 2017-05-30 03:40:36 | [diff] [blame] | 151 | ~StartupObserver() override { |
| 152 | DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 153 | DCHECK(IsBrowserStartupComplete()); |
| 154 | } |
michaeln | 96f887e2 | 2015-04-13 23:58:31 | [diff] [blame] | 155 | |
| 156 | void Start(); |
| 157 | |
| 158 | private: |
| 159 | void OnStartupComplete() { |
gab | 25894fe | 2017-05-30 03:40:36 | [diff] [blame] | 160 | DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
michaeln | 96f887e2 | 2015-04-13 23:58:31 | [diff] [blame] | 161 | SetBrowserStartupIsComplete(); |
| 162 | delete this; |
| 163 | } |
| 164 | |
| 165 | void OnFailsafeTimeout() { OnStartupComplete(); } |
| 166 | |
| 167 | // WebContentsObserver overrides |
| 168 | void DidFinishLoad(content::RenderFrameHost* render_frame_host, |
| 169 | const GURL& validated_url) override { |
| 170 | if (!render_frame_host->GetParent()) |
| 171 | OnStartupComplete(); |
| 172 | } |
| 173 | |
| 174 | void DidFailLoad(content::RenderFrameHost* render_frame_host, |
| 175 | const GURL& validated_url, |
Dave Tapuska | 924ef3c | 2020-01-22 18:20:59 | [diff] [blame] | 176 | int error_code) override { |
michaeln | 96f887e2 | 2015-04-13 23:58:31 | [diff] [blame] | 177 | if (!render_frame_host->GetParent()) |
| 178 | OnStartupComplete(); |
| 179 | } |
| 180 | |
Eric Lawrence | 45eee3a | 2019-10-14 22:46:36 | [diff] [blame] | 181 | // Starting the browser with a file download url will not result in |
| 182 | // DidFinishLoad firing, so watch for this case too. crbug.com/1006954 |
| 183 | void DidFinishNavigation( |
| 184 | content::NavigationHandle* navigation_handle) override { |
| 185 | if (navigation_handle->IsInMainFrame() && navigation_handle->IsDownload()) |
| 186 | OnStartupComplete(); |
| 187 | } |
| 188 | |
michaeln | 96f887e2 | 2015-04-13 23:58:31 | [diff] [blame] | 189 | void WebContentsDestroyed() override { OnStartupComplete(); } |
| 190 | |
gab | 25894fe | 2017-05-30 03:40:36 | [diff] [blame] | 191 | SEQUENCE_CHECKER(sequence_checker_); |
| 192 | |
Jeremy Roman | 495db68 | 2019-07-12 16:03:24 | [diff] [blame] | 193 | base::WeakPtrFactory<StartupObserver> weak_factory_{this}; |
michaeln | 96f887e2 | 2015-04-13 23:58:31 | [diff] [blame] | 194 | |
| 195 | DISALLOW_COPY_AND_ASSIGN(StartupObserver); |
| 196 | }; |
| 197 | |
| 198 | void StartupObserver::Start() { |
| 199 | // Signal completion quickly when there is no first page to load. |
| 200 | const int kShortDelaySecs = 3; |
| 201 | base::TimeDelta delay = base::TimeDelta::FromSeconds(kShortDelaySecs); |
| 202 | |
| 203 | #if !defined(OS_ANDROID) |
| 204 | WebContents* contents = nullptr; |
scottmg | 8abbff83 | 2016-01-28 22:57:37 | [diff] [blame] | 205 | for (auto* browser : *BrowserList::GetInstance()) { |
| 206 | contents = browser->tab_strip_model()->GetActiveWebContents(); |
michaeln | 96f887e2 | 2015-04-13 23:58:31 | [diff] [blame] | 207 | if (contents && contents->GetMainFrame() && |
| 208 | contents->GetMainFrame()->GetVisibilityState() == |
danakj | 0018a29a | 2018-12-01 01:03:43 | [diff] [blame] | 209 | content::PageVisibilityState::kVisible) { |
michaeln | 96f887e2 | 2015-04-13 23:58:31 | [diff] [blame] | 210 | break; |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | if (contents) { |
| 215 | // Give the page time to finish loading. |
| 216 | const int kLongerDelayMins = 3; |
| 217 | Observe(contents); |
| 218 | delay = base::TimeDelta::FromMinutes(kLongerDelayMins); |
| 219 | } |
| 220 | #else |
michaeln | 68bf4a8e | 2015-08-11 01:37:31 | [diff] [blame] | 221 | // Startup completion is signaled via AfterStartupTaskUtils.java, |
| 222 | // this is just a failsafe timeout. |
| 223 | const int kLongerDelayMins = 3; |
| 224 | delay = base::TimeDelta::FromMinutes(kLongerDelayMins); |
michaeln | 96f887e2 | 2015-04-13 23:58:31 | [diff] [blame] | 225 | #endif // !defined(OS_ANDROID) |
| 226 | |
Gabriel Charette | e7cdc5cd | 2020-05-27 23:35:05 | [diff] [blame] | 227 | content::GetUIThreadTaskRunner({})->PostDelayedTask( |
| 228 | FROM_HERE, |
| 229 | base::BindOnce(&StartupObserver::OnFailsafeTimeout, |
| 230 | weak_factory_.GetWeakPtr()), |
| 231 | delay); |
michaeln | 96f887e2 | 2015-04-13 23:58:31 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | } // namespace |
| 235 | |
| 236 | void AfterStartupTaskUtils::StartMonitoringStartup() { |
| 237 | // The observer is self-deleting. |
| 238 | (new StartupObserver)->Start(); |
| 239 | } |
| 240 | |
| 241 | void AfterStartupTaskUtils::PostTask( |
Brett Wilson | e1a7042 | 2017-09-12 05:10:09 | [diff] [blame] | 242 | const base::Location& from_here, |
Gabriel Charette | e926fc1 | 2019-12-16 19:00:02 | [diff] [blame] | 243 | const scoped_refptr<base::SequencedTaskRunner>& destination_runner, |
tzik | 6e42784 | 2017-04-05 10:13:21 | [diff] [blame] | 244 | base::OnceClosure task) { |
michaeln | 96f887e2 | 2015-04-13 23:58:31 | [diff] [blame] | 245 | if (IsBrowserStartupComplete()) { |
tzik | 070c8ffb | 2017-03-29 05:28:12 | [diff] [blame] | 246 | destination_runner->PostTask(from_here, std::move(task)); |
michaeln | 96f887e2 | 2015-04-13 23:58:31 | [diff] [blame] | 247 | return; |
| 248 | } |
| 249 | |
dcheng | 4af4858 | 2016-04-19 00:29:35 | [diff] [blame] | 250 | std::unique_ptr<AfterStartupTask> queued_task( |
tzik | 070c8ffb | 2017-03-29 05:28:12 | [diff] [blame] | 251 | new AfterStartupTask(from_here, destination_runner, std::move(task))); |
dcheng | e73d8520c | 2015-12-27 01:19:09 | [diff] [blame] | 252 | QueueTask(std::move(queued_task)); |
michaeln | 96f887e2 | 2015-04-13 23:58:31 | [diff] [blame] | 253 | } |
| 254 | |
wkorman | 8a21c4f | 2015-11-18 19:06:11 | [diff] [blame] | 255 | void AfterStartupTaskUtils::SetBrowserStartupIsCompleteForTesting() { |
| 256 | ::SetBrowserStartupIsComplete(); |
| 257 | } |
| 258 | |
michaeln | 96f887e2 | 2015-04-13 23:58:31 | [diff] [blame] | 259 | void AfterStartupTaskUtils::SetBrowserStartupIsComplete() { |
| 260 | ::SetBrowserStartupIsComplete(); |
| 261 | } |
| 262 | |
| 263 | bool AfterStartupTaskUtils::IsBrowserStartupComplete() { |
| 264 | return ::IsBrowserStartupComplete(); |
| 265 | } |
| 266 | |
| 267 | void AfterStartupTaskUtils::UnsafeResetForTesting() { |
| 268 | DCHECK(g_after_startup_tasks.Get().empty()); |
| 269 | if (!IsBrowserStartupComplete()) |
| 270 | return; |
| 271 | g_startup_complete_flag.Get().UnsafeResetForTesting(); |
| 272 | DCHECK(!IsBrowserStartupComplete()); |
| 273 | } |