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" |
| 15 | #include "base/process/process_info.h" |
| 16 | #include "base/rand_util.h" |
gab | 25894fe | 2017-05-30 03:40:36 | [diff] [blame] | 17 | #include "base/sequence_checker.h" |
fdoray | ef19112 | 2016-07-25 14:43:17 | [diff] [blame] | 18 | #include "base/synchronization/atomic_flag.h" |
michaeln | 96f887e2 | 2015-04-13 23:58:31 | [diff] [blame] | 19 | #include "base/task_runner.h" |
| 20 | #include "base/tracked_objects.h" |
avi | e4d7b6f | 2015-12-26 00:59:18 | [diff] [blame] | 21 | #include "build/build_config.h" |
michaeln | 96f887e2 | 2015-04-13 23:58:31 | [diff] [blame] | 22 | #include "chrome/browser/ui/browser.h" |
scottmg | 8abbff83 | 2016-01-28 22:57:37 | [diff] [blame] | 23 | #include "chrome/browser/ui/browser_list.h" |
michaeln | 96f887e2 | 2015-04-13 23:58:31 | [diff] [blame] | 24 | #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 25 | #include "content/public/browser/browser_thread.h" |
| 26 | #include "content/public/browser/render_frame_host.h" |
| 27 | #include "content/public/browser/web_contents.h" |
| 28 | #include "content/public/browser/web_contents_observer.h" |
| 29 | |
| 30 | using content::BrowserThread; |
| 31 | using content::WebContents; |
| 32 | using content::WebContentsObserver; |
michaeln | 96f887e2 | 2015-04-13 23:58:31 | [diff] [blame] | 33 | |
| 34 | namespace { |
| 35 | |
| 36 | struct AfterStartupTask { |
Brett Wilson | e1a7042 | 2017-09-12 05:10:09 | [diff] [blame^] | 37 | AfterStartupTask(const base::Location& from_here, |
michaeln | 96f887e2 | 2015-04-13 23:58:31 | [diff] [blame] | 38 | const scoped_refptr<base::TaskRunner>& task_runner, |
tzik | 6e42784 | 2017-04-05 10:13:21 | [diff] [blame] | 39 | base::OnceClosure task) |
tzik | 070c8ffb | 2017-03-29 05:28:12 | [diff] [blame] | 40 | : from_here(from_here), task_runner(task_runner), task(std::move(task)) {} |
michaeln | 96f887e2 | 2015-04-13 23:58:31 | [diff] [blame] | 41 | ~AfterStartupTask() {} |
| 42 | |
Brett Wilson | e1a7042 | 2017-09-12 05:10:09 | [diff] [blame^] | 43 | const base::Location from_here; |
michaeln | 96f887e2 | 2015-04-13 23:58:31 | [diff] [blame] | 44 | const scoped_refptr<base::TaskRunner> task_runner; |
tzik | 6e42784 | 2017-04-05 10:13:21 | [diff] [blame] | 45 | base::OnceClosure task; |
michaeln | 96f887e2 | 2015-04-13 23:58:31 | [diff] [blame] | 46 | }; |
| 47 | |
| 48 | // 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] | 49 | base::LazyInstance<base::AtomicFlag>::Leaky g_startup_complete_flag; |
michaeln | 96f887e2 | 2015-04-13 23:58:31 | [diff] [blame] | 50 | |
| 51 | // The queue may only be accessed on the UI thread. |
Brett Wilson | 275a137 | 2017-09-01 20:27:54 | [diff] [blame] | 52 | base::LazyInstance<base::circular_deque<AfterStartupTask*>>::Leaky |
| 53 | g_after_startup_tasks; |
michaeln | 96f887e2 | 2015-04-13 23:58:31 | [diff] [blame] | 54 | |
| 55 | bool IsBrowserStartupComplete() { |
| 56 | // Be sure to initialize the LazyInstance on the main thread since the flag |
| 57 | // may only be set on it's initializing thread. |
| 58 | if (g_startup_complete_flag == nullptr) |
| 59 | return false; |
| 60 | return g_startup_complete_flag.Get().IsSet(); |
| 61 | } |
| 62 | |
dcheng | 4af4858 | 2016-04-19 00:29:35 | [diff] [blame] | 63 | void RunTask(std::unique_ptr<AfterStartupTask> queued_task) { |
michaeln | 96f887e2 | 2015-04-13 23:58:31 | [diff] [blame] | 64 | // 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] | 65 | DCHECK(queued_task->task_runner->RunsTasksInCurrentSequence()); |
tzik | 070c8ffb | 2017-03-29 05:28:12 | [diff] [blame] | 66 | std::move(queued_task->task).Run(); |
michaeln | 96f887e2 | 2015-04-13 23:58:31 | [diff] [blame] | 67 | } |
| 68 | |
dcheng | 4af4858 | 2016-04-19 00:29:35 | [diff] [blame] | 69 | void ScheduleTask(std::unique_ptr<AfterStartupTask> queued_task) { |
michaeln | 96f887e2 | 2015-04-13 23:58:31 | [diff] [blame] | 70 | // Spread their execution over a brief time. |
| 71 | const int kMinDelaySec = 0; |
| 72 | const int kMaxDelaySec = 10; |
| 73 | scoped_refptr<base::TaskRunner> target_runner = queued_task->task_runner; |
Brett Wilson | e1a7042 | 2017-09-12 05:10:09 | [diff] [blame^] | 74 | base::Location from_here = queued_task->from_here; |
michaeln | 96f887e2 | 2015-04-13 23:58:31 | [diff] [blame] | 75 | target_runner->PostDelayedTask( |
tzik | 29ea5c7 | 2017-04-20 02:16:51 | [diff] [blame] | 76 | from_here, base::BindOnce(&RunTask, base::Passed(std::move(queued_task))), |
michaeln | 96f887e2 | 2015-04-13 23:58:31 | [diff] [blame] | 77 | base::TimeDelta::FromSeconds(base::RandInt(kMinDelaySec, kMaxDelaySec))); |
| 78 | } |
| 79 | |
dcheng | 4af4858 | 2016-04-19 00:29:35 | [diff] [blame] | 80 | void QueueTask(std::unique_ptr<AfterStartupTask> queued_task) { |
tzik | c697696 | 2017-04-04 17:27:34 | [diff] [blame] | 81 | DCHECK(queued_task); |
tzik | 498d42b | 2017-04-13 07:42:48 | [diff] [blame] | 82 | |
| 83 | // Use CHECK instead of DCHECK to crash earlier. See http://crbug.com/711167 |
| 84 | // for details. |
| 85 | CHECK(queued_task->task); |
| 86 | |
michaeln | 96f887e2 | 2015-04-13 23:58:31 | [diff] [blame] | 87 | if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { |
| 88 | BrowserThread::PostTask( |
| 89 | BrowserThread::UI, FROM_HERE, |
tzik | 29ea5c7 | 2017-04-20 02:16:51 | [diff] [blame] | 90 | base::BindOnce(QueueTask, base::Passed(std::move(queued_task)))); |
michaeln | 96f887e2 | 2015-04-13 23:58:31 | [diff] [blame] | 91 | return; |
| 92 | } |
| 93 | |
| 94 | // The flag may have been set while the task to invoke this method |
| 95 | // on the UI thread was inflight. |
| 96 | if (IsBrowserStartupComplete()) { |
dcheng | e73d8520c | 2015-12-27 01:19:09 | [diff] [blame] | 97 | ScheduleTask(std::move(queued_task)); |
michaeln | 96f887e2 | 2015-04-13 23:58:31 | [diff] [blame] | 98 | return; |
| 99 | } |
| 100 | g_after_startup_tasks.Get().push_back(queued_task.release()); |
| 101 | } |
| 102 | |
| 103 | void SetBrowserStartupIsComplete() { |
| 104 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 105 | #if defined(OS_MACOSX) || defined(OS_WIN) || defined(OS_LINUX) |
| 106 | // CurrentProcessInfo::CreationTime() is not available on all platforms. |
| 107 | const base::Time process_creation_time = |
| 108 | base::CurrentProcessInfo::CreationTime(); |
| 109 | if (!process_creation_time.is_null()) { |
| 110 | UMA_HISTOGRAM_LONG_TIMES("Startup.AfterStartupTaskDelayedUntilTime", |
| 111 | base::Time::Now() - process_creation_time); |
| 112 | } |
| 113 | #endif // defined(OS_MACOSX) || defined(OS_WIN) || defined(OS_LINUX) |
| 114 | UMA_HISTOGRAM_COUNTS_10000("Startup.AfterStartupTaskCount", |
| 115 | g_after_startup_tasks.Get().size()); |
| 116 | g_startup_complete_flag.Get().Set(); |
| 117 | for (AfterStartupTask* queued_task : g_after_startup_tasks.Get()) |
dcheng | 4af4858 | 2016-04-19 00:29:35 | [diff] [blame] | 118 | ScheduleTask(base::WrapUnique(queued_task)); |
michaeln | 96f887e2 | 2015-04-13 23:58:31 | [diff] [blame] | 119 | g_after_startup_tasks.Get().clear(); |
| 120 | |
| 121 | // The shrink_to_fit() method is not available for all of our build targets. |
Brett Wilson | 275a137 | 2017-09-01 20:27:54 | [diff] [blame] | 122 | base::circular_deque<AfterStartupTask*>(g_after_startup_tasks.Get()) |
michaeln | 96f887e2 | 2015-04-13 23:58:31 | [diff] [blame] | 123 | .swap(g_after_startup_tasks.Get()); |
| 124 | } |
| 125 | |
| 126 | // Observes the first visible page load and sets the startup complete |
| 127 | // flag accordingly. |
gab | 25894fe | 2017-05-30 03:40:36 | [diff] [blame] | 128 | class StartupObserver : public WebContentsObserver { |
michaeln | 96f887e2 | 2015-04-13 23:58:31 | [diff] [blame] | 129 | public: |
| 130 | StartupObserver() : weak_factory_(this) {} |
gab | 25894fe | 2017-05-30 03:40:36 | [diff] [blame] | 131 | ~StartupObserver() override { |
| 132 | DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 133 | DCHECK(IsBrowserStartupComplete()); |
| 134 | } |
michaeln | 96f887e2 | 2015-04-13 23:58:31 | [diff] [blame] | 135 | |
| 136 | void Start(); |
| 137 | |
| 138 | private: |
| 139 | void OnStartupComplete() { |
gab | 25894fe | 2017-05-30 03:40:36 | [diff] [blame] | 140 | DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
michaeln | 96f887e2 | 2015-04-13 23:58:31 | [diff] [blame] | 141 | SetBrowserStartupIsComplete(); |
| 142 | delete this; |
| 143 | } |
| 144 | |
| 145 | void OnFailsafeTimeout() { OnStartupComplete(); } |
| 146 | |
| 147 | // WebContentsObserver overrides |
| 148 | void DidFinishLoad(content::RenderFrameHost* render_frame_host, |
| 149 | const GURL& validated_url) override { |
| 150 | if (!render_frame_host->GetParent()) |
| 151 | OnStartupComplete(); |
| 152 | } |
| 153 | |
| 154 | void DidFailLoad(content::RenderFrameHost* render_frame_host, |
| 155 | const GURL& validated_url, |
| 156 | int error_code, |
Yutaka Hirano | 16d3c5d | 2017-07-31 06:17:30 | [diff] [blame] | 157 | const base::string16& error_description) override { |
michaeln | 96f887e2 | 2015-04-13 23:58:31 | [diff] [blame] | 158 | if (!render_frame_host->GetParent()) |
| 159 | OnStartupComplete(); |
| 160 | } |
| 161 | |
| 162 | void WebContentsDestroyed() override { OnStartupComplete(); } |
| 163 | |
gab | 25894fe | 2017-05-30 03:40:36 | [diff] [blame] | 164 | SEQUENCE_CHECKER(sequence_checker_); |
| 165 | |
michaeln | 96f887e2 | 2015-04-13 23:58:31 | [diff] [blame] | 166 | base::WeakPtrFactory<StartupObserver> weak_factory_; |
| 167 | |
| 168 | DISALLOW_COPY_AND_ASSIGN(StartupObserver); |
| 169 | }; |
| 170 | |
| 171 | void StartupObserver::Start() { |
| 172 | // Signal completion quickly when there is no first page to load. |
| 173 | const int kShortDelaySecs = 3; |
| 174 | base::TimeDelta delay = base::TimeDelta::FromSeconds(kShortDelaySecs); |
| 175 | |
| 176 | #if !defined(OS_ANDROID) |
| 177 | WebContents* contents = nullptr; |
scottmg | 8abbff83 | 2016-01-28 22:57:37 | [diff] [blame] | 178 | for (auto* browser : *BrowserList::GetInstance()) { |
| 179 | contents = browser->tab_strip_model()->GetActiveWebContents(); |
michaeln | 96f887e2 | 2015-04-13 23:58:31 | [diff] [blame] | 180 | if (contents && contents->GetMainFrame() && |
| 181 | contents->GetMainFrame()->GetVisibilityState() == |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 182 | blink::kWebPageVisibilityStateVisible) { |
michaeln | 96f887e2 | 2015-04-13 23:58:31 | [diff] [blame] | 183 | break; |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | if (contents) { |
| 188 | // Give the page time to finish loading. |
| 189 | const int kLongerDelayMins = 3; |
| 190 | Observe(contents); |
| 191 | delay = base::TimeDelta::FromMinutes(kLongerDelayMins); |
| 192 | } |
| 193 | #else |
michaeln | 68bf4a8e | 2015-08-11 01:37:31 | [diff] [blame] | 194 | // Startup completion is signaled via AfterStartupTaskUtils.java, |
| 195 | // this is just a failsafe timeout. |
| 196 | const int kLongerDelayMins = 3; |
| 197 | delay = base::TimeDelta::FromMinutes(kLongerDelayMins); |
michaeln | 96f887e2 | 2015-04-13 23:58:31 | [diff] [blame] | 198 | #endif // !defined(OS_ANDROID) |
| 199 | |
tzik | 29ea5c7 | 2017-04-20 02:16:51 | [diff] [blame] | 200 | BrowserThread::PostDelayedTask( |
| 201 | BrowserThread::UI, FROM_HERE, |
| 202 | base::BindOnce(&StartupObserver::OnFailsafeTimeout, |
| 203 | weak_factory_.GetWeakPtr()), |
| 204 | delay); |
michaeln | 96f887e2 | 2015-04-13 23:58:31 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | } // namespace |
| 208 | |
gab | 27e6d33f | 2016-08-11 13:15:33 | [diff] [blame] | 209 | AfterStartupTaskUtils::Runner::Runner( |
| 210 | scoped_refptr<base::TaskRunner> destination_runner) |
| 211 | : destination_runner_(std::move(destination_runner)) { |
| 212 | DCHECK(destination_runner_); |
| 213 | } |
| 214 | |
| 215 | AfterStartupTaskUtils::Runner::~Runner() = default; |
| 216 | |
| 217 | bool AfterStartupTaskUtils::Runner::PostDelayedTask( |
Brett Wilson | e1a7042 | 2017-09-12 05:10:09 | [diff] [blame^] | 218 | const base::Location& from_here, |
tzik | 6e42784 | 2017-04-05 10:13:21 | [diff] [blame] | 219 | base::OnceClosure task, |
gab | 27e6d33f | 2016-08-11 13:15:33 | [diff] [blame] | 220 | base::TimeDelta delay) { |
| 221 | DCHECK(delay.is_zero()); |
tzik | 070c8ffb | 2017-03-29 05:28:12 | [diff] [blame] | 222 | AfterStartupTaskUtils::PostTask(from_here, destination_runner_, |
| 223 | std::move(task)); |
gab | 27e6d33f | 2016-08-11 13:15:33 | [diff] [blame] | 224 | return true; |
| 225 | } |
| 226 | |
peary2 | 3322df6 | 2017-05-09 03:55:48 | [diff] [blame] | 227 | bool AfterStartupTaskUtils::Runner::RunsTasksInCurrentSequence() const { |
| 228 | return destination_runner_->RunsTasksInCurrentSequence(); |
gab | 27e6d33f | 2016-08-11 13:15:33 | [diff] [blame] | 229 | } |
| 230 | |
michaeln | 96f887e2 | 2015-04-13 23:58:31 | [diff] [blame] | 231 | void AfterStartupTaskUtils::StartMonitoringStartup() { |
| 232 | // The observer is self-deleting. |
| 233 | (new StartupObserver)->Start(); |
| 234 | } |
| 235 | |
| 236 | void AfterStartupTaskUtils::PostTask( |
Brett Wilson | e1a7042 | 2017-09-12 05:10:09 | [diff] [blame^] | 237 | const base::Location& from_here, |
gab | 27e6d33f | 2016-08-11 13:15:33 | [diff] [blame] | 238 | const scoped_refptr<base::TaskRunner>& destination_runner, |
tzik | 6e42784 | 2017-04-05 10:13:21 | [diff] [blame] | 239 | base::OnceClosure task) { |
michaeln | 96f887e2 | 2015-04-13 23:58:31 | [diff] [blame] | 240 | if (IsBrowserStartupComplete()) { |
tzik | 070c8ffb | 2017-03-29 05:28:12 | [diff] [blame] | 241 | destination_runner->PostTask(from_here, std::move(task)); |
michaeln | 96f887e2 | 2015-04-13 23:58:31 | [diff] [blame] | 242 | return; |
| 243 | } |
| 244 | |
dcheng | 4af4858 | 2016-04-19 00:29:35 | [diff] [blame] | 245 | std::unique_ptr<AfterStartupTask> queued_task( |
tzik | 070c8ffb | 2017-03-29 05:28:12 | [diff] [blame] | 246 | new AfterStartupTask(from_here, destination_runner, std::move(task))); |
dcheng | e73d8520c | 2015-12-27 01:19:09 | [diff] [blame] | 247 | QueueTask(std::move(queued_task)); |
michaeln | 96f887e2 | 2015-04-13 23:58:31 | [diff] [blame] | 248 | } |
| 249 | |
wkorman | 8a21c4f | 2015-11-18 19:06:11 | [diff] [blame] | 250 | void AfterStartupTaskUtils::SetBrowserStartupIsCompleteForTesting() { |
| 251 | ::SetBrowserStartupIsComplete(); |
| 252 | } |
| 253 | |
michaeln | 96f887e2 | 2015-04-13 23:58:31 | [diff] [blame] | 254 | void AfterStartupTaskUtils::SetBrowserStartupIsComplete() { |
| 255 | ::SetBrowserStartupIsComplete(); |
| 256 | } |
| 257 | |
| 258 | bool AfterStartupTaskUtils::IsBrowserStartupComplete() { |
| 259 | return ::IsBrowserStartupComplete(); |
| 260 | } |
| 261 | |
| 262 | void AfterStartupTaskUtils::UnsafeResetForTesting() { |
| 263 | DCHECK(g_after_startup_tasks.Get().empty()); |
| 264 | if (!IsBrowserStartupComplete()) |
| 265 | return; |
| 266 | g_startup_complete_flag.Get().UnsafeResetForTesting(); |
| 267 | DCHECK(!IsBrowserStartupComplete()); |
| 268 | } |