| 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 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 197 AfterStartupTaskUtils::Runner::Runner( | 197 AfterStartupTaskUtils::Runner::Runner( |
| 198 scoped_refptr<base::TaskRunner> destination_runner) | 198 scoped_refptr<base::TaskRunner> destination_runner) |
| 199 : destination_runner_(std::move(destination_runner)) { | 199 : destination_runner_(std::move(destination_runner)) { |
| 200 DCHECK(destination_runner_); | 200 DCHECK(destination_runner_); |
| 201 } | 201 } |
| 202 | 202 |
| 203 AfterStartupTaskUtils::Runner::~Runner() = default; | 203 AfterStartupTaskUtils::Runner::~Runner() = default; |
| 204 | 204 |
| 205 bool AfterStartupTaskUtils::Runner::PostDelayedTask( | 205 bool AfterStartupTaskUtils::Runner::PostDelayedTask( |
| 206 const tracked_objects::Location& from_here, | 206 const tracked_objects::Location& from_here, |
| 207 base::Closure task, | 207 base::OnceClosure task, |
| 208 base::TimeDelta delay) { | 208 base::TimeDelta delay) { |
| 209 DCHECK(delay.is_zero()); | 209 DCHECK(delay.is_zero()); |
| 210 AfterStartupTaskUtils::PostTask(from_here, destination_runner_, | 210 AfterStartupTaskUtils::PostTask(from_here, destination_runner_, |
| 211 std::move(task)); | 211 std::move(task)); |
| 212 return true; | 212 return true; |
| 213 } | 213 } |
| 214 | 214 |
| 215 bool AfterStartupTaskUtils::Runner::RunsTasksOnCurrentThread() const { | 215 bool AfterStartupTaskUtils::Runner::RunsTasksOnCurrentThread() const { |
| 216 return destination_runner_->RunsTasksOnCurrentThread(); | 216 return destination_runner_->RunsTasksOnCurrentThread(); |
| 217 } | 217 } |
| 218 | 218 |
| 219 void AfterStartupTaskUtils::StartMonitoringStartup() { | 219 void AfterStartupTaskUtils::StartMonitoringStartup() { |
| 220 // The observer is self-deleting. | 220 // The observer is self-deleting. |
| 221 (new StartupObserver)->Start(); | 221 (new StartupObserver)->Start(); |
| 222 } | 222 } |
| 223 | 223 |
| 224 void AfterStartupTaskUtils::PostTask( | 224 void AfterStartupTaskUtils::PostTask( |
| 225 const tracked_objects::Location& from_here, | 225 const tracked_objects::Location& from_here, |
| 226 const scoped_refptr<base::TaskRunner>& destination_runner, | 226 const scoped_refptr<base::TaskRunner>& destination_runner, |
| 227 base::Closure task) { | 227 base::OnceClosure task) { |
| 228 if (IsBrowserStartupComplete()) { | 228 if (IsBrowserStartupComplete()) { |
| 229 destination_runner->PostTask(from_here, std::move(task)); | 229 destination_runner->PostTask(from_here, std::move(task)); |
| 230 return; | 230 return; |
| 231 } | 231 } |
| 232 | 232 |
| 233 std::unique_ptr<AfterStartupTask> queued_task( | 233 std::unique_ptr<AfterStartupTask> queued_task( |
| 234 new AfterStartupTask(from_here, destination_runner, std::move(task))); | 234 new AfterStartupTask(from_here, destination_runner, std::move(task))); |
| 235 QueueTask(std::move(queued_task)); | 235 QueueTask(std::move(queued_task)); |
| 236 } | 236 } |
| 237 | 237 |
| 238 void AfterStartupTaskUtils::SetBrowserStartupIsCompleteForTesting() { | 238 void AfterStartupTaskUtils::SetBrowserStartupIsCompleteForTesting() { |
| 239 ::SetBrowserStartupIsComplete(); | 239 ::SetBrowserStartupIsComplete(); |
| 240 } | 240 } |
| 241 | 241 |
| 242 void AfterStartupTaskUtils::SetBrowserStartupIsComplete() { | 242 void AfterStartupTaskUtils::SetBrowserStartupIsComplete() { |
| 243 ::SetBrowserStartupIsComplete(); | 243 ::SetBrowserStartupIsComplete(); |
| 244 } | 244 } |
| 245 | 245 |
| 246 bool AfterStartupTaskUtils::IsBrowserStartupComplete() { | 246 bool AfterStartupTaskUtils::IsBrowserStartupComplete() { |
| 247 return ::IsBrowserStartupComplete(); | 247 return ::IsBrowserStartupComplete(); |
| 248 } | 248 } |
| 249 | 249 |
| 250 void AfterStartupTaskUtils::UnsafeResetForTesting() { | 250 void AfterStartupTaskUtils::UnsafeResetForTesting() { |
| 251 DCHECK(g_after_startup_tasks.Get().empty()); | 251 DCHECK(g_after_startup_tasks.Get().empty()); |
| 252 if (!IsBrowserStartupComplete()) | 252 if (!IsBrowserStartupComplete()) |
| 253 return; | 253 return; |
| 254 g_startup_complete_flag.Get().UnsafeResetForTesting(); | 254 g_startup_complete_flag.Get().UnsafeResetForTesting(); |
| 255 DCHECK(!IsBrowserStartupComplete()); | 255 DCHECK(!IsBrowserStartupComplete()); |
| 256 } | 256 } |
| OLD | NEW |