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