| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/after_startup_task_utils.h" | 5 #include "chrome/browser/after_startup_task_utils.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/lazy_instance.h" | 10 #include "base/lazy_instance.h" |
| (...skipping 16 matching lines...) Expand all Loading... |
| 27 | 27 |
| 28 using content::BrowserThread; | 28 using content::BrowserThread; |
| 29 using content::WebContents; | 29 using content::WebContents; |
| 30 using content::WebContentsObserver; | 30 using content::WebContentsObserver; |
| 31 | 31 |
| 32 namespace { | 32 namespace { |
| 33 | 33 |
| 34 struct AfterStartupTask { | 34 struct AfterStartupTask { |
| 35 AfterStartupTask(const tracked_objects::Location& from_here, | 35 AfterStartupTask(const tracked_objects::Location& from_here, |
| 36 const scoped_refptr<base::TaskRunner>& task_runner, | 36 const scoped_refptr<base::TaskRunner>& task_runner, |
| 37 base::Closure task) | 37 base::OnceClosure task) |
| 38 : from_here(from_here), task_runner(task_runner), task(std::move(task)) {} | 38 : from_here(from_here), task_runner(task_runner), task(std::move(task)) {} |
| 39 ~AfterStartupTask() {} | 39 ~AfterStartupTask() {} |
| 40 | 40 |
| 41 const tracked_objects::Location from_here; | 41 const tracked_objects::Location from_here; |
| 42 const scoped_refptr<base::TaskRunner> task_runner; | 42 const scoped_refptr<base::TaskRunner> task_runner; |
| 43 base::Closure task; | 43 base::OnceClosure task; |
| 44 }; | 44 }; |
| 45 | 45 |
| 46 // The flag may be read on any thread, but must only be set on the UI thread. | 46 // The flag may be read on any thread, but must only be set on the UI thread. |
| 47 base::LazyInstance<base::AtomicFlag>::Leaky g_startup_complete_flag; | 47 base::LazyInstance<base::AtomicFlag>::Leaky g_startup_complete_flag; |
| 48 | 48 |
| 49 // The queue may only be accessed on the UI thread. | 49 // The queue may only be accessed on the UI thread. |
| 50 base::LazyInstance<std::deque<AfterStartupTask*>>::Leaky g_after_startup_tasks; | 50 base::LazyInstance<std::deque<AfterStartupTask*>>::Leaky g_after_startup_tasks; |
| 51 | 51 |
| 52 bool IsBrowserStartupComplete() { | 52 bool IsBrowserStartupComplete() { |
| 53 // Be sure to initialize the LazyInstance on the main thread since the flag | 53 // Be sure to initialize the LazyInstance on the main thread since the flag |
| (...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 195 AfterStartupTaskUtils::Runner::Runner( | 195 AfterStartupTaskUtils::Runner::Runner( |
| 196 scoped_refptr<base::TaskRunner> destination_runner) | 196 scoped_refptr<base::TaskRunner> destination_runner) |
| 197 : destination_runner_(std::move(destination_runner)) { | 197 : destination_runner_(std::move(destination_runner)) { |
| 198 DCHECK(destination_runner_); | 198 DCHECK(destination_runner_); |
| 199 } | 199 } |
| 200 | 200 |
| 201 AfterStartupTaskUtils::Runner::~Runner() = default; | 201 AfterStartupTaskUtils::Runner::~Runner() = default; |
| 202 | 202 |
| 203 bool AfterStartupTaskUtils::Runner::PostDelayedTask( | 203 bool AfterStartupTaskUtils::Runner::PostDelayedTask( |
| 204 const tracked_objects::Location& from_here, | 204 const tracked_objects::Location& from_here, |
| 205 base::Closure task, | 205 base::OnceClosure task, |
| 206 base::TimeDelta delay) { | 206 base::TimeDelta delay) { |
| 207 DCHECK(delay.is_zero()); | 207 DCHECK(delay.is_zero()); |
| 208 AfterStartupTaskUtils::PostTask(from_here, destination_runner_, | 208 AfterStartupTaskUtils::PostTask(from_here, destination_runner_, |
| 209 std::move(task)); | 209 std::move(task)); |
| 210 return true; | 210 return true; |
| 211 } | 211 } |
| 212 | 212 |
| 213 bool AfterStartupTaskUtils::Runner::RunsTasksOnCurrentThread() const { | 213 bool AfterStartupTaskUtils::Runner::RunsTasksOnCurrentThread() const { |
| 214 return destination_runner_->RunsTasksOnCurrentThread(); | 214 return destination_runner_->RunsTasksOnCurrentThread(); |
| 215 } | 215 } |
| 216 | 216 |
| 217 void AfterStartupTaskUtils::StartMonitoringStartup() { | 217 void AfterStartupTaskUtils::StartMonitoringStartup() { |
| 218 // The observer is self-deleting. | 218 // The observer is self-deleting. |
| 219 (new StartupObserver)->Start(); | 219 (new StartupObserver)->Start(); |
| 220 } | 220 } |
| 221 | 221 |
| 222 void AfterStartupTaskUtils::PostTask( | 222 void AfterStartupTaskUtils::PostTask( |
| 223 const tracked_objects::Location& from_here, | 223 const tracked_objects::Location& from_here, |
| 224 const scoped_refptr<base::TaskRunner>& destination_runner, | 224 const scoped_refptr<base::TaskRunner>& destination_runner, |
| 225 base::Closure task) { | 225 base::OnceClosure task) { |
| 226 if (IsBrowserStartupComplete()) { | 226 if (IsBrowserStartupComplete()) { |
| 227 destination_runner->PostTask(from_here, std::move(task)); | 227 destination_runner->PostTask(from_here, std::move(task)); |
| 228 return; | 228 return; |
| 229 } | 229 } |
| 230 | 230 |
| 231 std::unique_ptr<AfterStartupTask> queued_task( | 231 std::unique_ptr<AfterStartupTask> queued_task( |
| 232 new AfterStartupTask(from_here, destination_runner, std::move(task))); | 232 new AfterStartupTask(from_here, destination_runner, std::move(task))); |
| 233 QueueTask(std::move(queued_task)); | 233 QueueTask(std::move(queued_task)); |
| 234 } | 234 } |
| 235 | 235 |
| 236 void AfterStartupTaskUtils::SetBrowserStartupIsCompleteForTesting() { | 236 void AfterStartupTaskUtils::SetBrowserStartupIsCompleteForTesting() { |
| 237 ::SetBrowserStartupIsComplete(); | 237 ::SetBrowserStartupIsComplete(); |
| 238 } | 238 } |
| 239 | 239 |
| 240 void AfterStartupTaskUtils::SetBrowserStartupIsComplete() { | 240 void AfterStartupTaskUtils::SetBrowserStartupIsComplete() { |
| 241 ::SetBrowserStartupIsComplete(); | 241 ::SetBrowserStartupIsComplete(); |
| 242 } | 242 } |
| 243 | 243 |
| 244 bool AfterStartupTaskUtils::IsBrowserStartupComplete() { | 244 bool AfterStartupTaskUtils::IsBrowserStartupComplete() { |
| 245 return ::IsBrowserStartupComplete(); | 245 return ::IsBrowserStartupComplete(); |
| 246 } | 246 } |
| 247 | 247 |
| 248 void AfterStartupTaskUtils::UnsafeResetForTesting() { | 248 void AfterStartupTaskUtils::UnsafeResetForTesting() { |
| 249 DCHECK(g_after_startup_tasks.Get().empty()); | 249 DCHECK(g_after_startup_tasks.Get().empty()); |
| 250 if (!IsBrowserStartupComplete()) | 250 if (!IsBrowserStartupComplete()) |
| 251 return; | 251 return; |
| 252 g_startup_complete_flag.Get().UnsafeResetForTesting(); | 252 g_startup_complete_flag.Get().UnsafeResetForTesting(); |
| 253 DCHECK(!IsBrowserStartupComplete()); | 253 DCHECK(!IsBrowserStartupComplete()); |
| 254 } | 254 } |
| OLD | NEW |