Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(613)

Side by Side Diff: chrome/browser/after_startup_task_utils.cc

Issue 2726523002: Pass Callback to TaskRunner by value and consume it on invocation (1) (Closed)
Patch Set: erase Closure* Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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 const base::Closure& task) 37 base::Closure task)
38 : from_here(from_here), task_runner(task_runner), task(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 const base::Closure task; 43 base::Closure 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
54 // may only be set on it's initializing thread. 54 // may only be set on it's initializing thread.
55 if (g_startup_complete_flag == nullptr) 55 if (g_startup_complete_flag == nullptr)
56 return false; 56 return false;
57 return g_startup_complete_flag.Get().IsSet(); 57 return g_startup_complete_flag.Get().IsSet();
58 } 58 }
59 59
60 void RunTask(std::unique_ptr<AfterStartupTask> queued_task) { 60 void RunTask(std::unique_ptr<AfterStartupTask> queued_task) {
61 // We're careful to delete the caller's |task| on the target runner's thread. 61 // We're careful to delete the caller's |task| on the target runner's thread.
62 DCHECK(queued_task->task_runner->RunsTasksOnCurrentThread()); 62 DCHECK(queued_task->task_runner->RunsTasksOnCurrentThread());
63 queued_task->task.Run(); 63 std::move(queued_task->task).Run();
64 } 64 }
65 65
66 void ScheduleTask(std::unique_ptr<AfterStartupTask> queued_task) { 66 void ScheduleTask(std::unique_ptr<AfterStartupTask> queued_task) {
67 // Spread their execution over a brief time. 67 // Spread their execution over a brief time.
68 const int kMinDelaySec = 0; 68 const int kMinDelaySec = 0;
69 const int kMaxDelaySec = 10; 69 const int kMaxDelaySec = 10;
70 scoped_refptr<base::TaskRunner> target_runner = queued_task->task_runner; 70 scoped_refptr<base::TaskRunner> target_runner = queued_task->task_runner;
71 tracked_objects::Location from_here = queued_task->from_here; 71 tracked_objects::Location from_here = queued_task->from_here;
72 target_runner->PostDelayedTask( 72 target_runner->PostDelayedTask(
73 from_here, base::Bind(&RunTask, base::Passed(std::move(queued_task))), 73 from_here, base::Bind(&RunTask, base::Passed(std::move(queued_task))),
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after
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 const base::Closure& task, 205 base::Closure 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_, task); 208 AfterStartupTaskUtils::PostTask(from_here, destination_runner_,
209 std::move(task));
209 return true; 210 return true;
210 } 211 }
211 212
212 bool AfterStartupTaskUtils::Runner::RunsTasksOnCurrentThread() const { 213 bool AfterStartupTaskUtils::Runner::RunsTasksOnCurrentThread() const {
213 return destination_runner_->RunsTasksOnCurrentThread(); 214 return destination_runner_->RunsTasksOnCurrentThread();
214 } 215 }
215 216
216 void AfterStartupTaskUtils::StartMonitoringStartup() { 217 void AfterStartupTaskUtils::StartMonitoringStartup() {
217 // The observer is self-deleting. 218 // The observer is self-deleting.
218 (new StartupObserver)->Start(); 219 (new StartupObserver)->Start();
219 } 220 }
220 221
221 void AfterStartupTaskUtils::PostTask( 222 void AfterStartupTaskUtils::PostTask(
222 const tracked_objects::Location& from_here, 223 const tracked_objects::Location& from_here,
223 const scoped_refptr<base::TaskRunner>& destination_runner, 224 const scoped_refptr<base::TaskRunner>& destination_runner,
224 const base::Closure& task) { 225 base::Closure task) {
225 if (IsBrowserStartupComplete()) { 226 if (IsBrowserStartupComplete()) {
226 destination_runner->PostTask(from_here, task); 227 destination_runner->PostTask(from_here, std::move(task));
227 return; 228 return;
228 } 229 }
229 230
230 std::unique_ptr<AfterStartupTask> queued_task( 231 std::unique_ptr<AfterStartupTask> queued_task(
231 new AfterStartupTask(from_here, destination_runner, task)); 232 new AfterStartupTask(from_here, destination_runner, std::move(task)));
232 QueueTask(std::move(queued_task)); 233 QueueTask(std::move(queued_task));
233 } 234 }
234 235
235 void AfterStartupTaskUtils::SetBrowserStartupIsCompleteForTesting() { 236 void AfterStartupTaskUtils::SetBrowserStartupIsCompleteForTesting() {
236 ::SetBrowserStartupIsComplete(); 237 ::SetBrowserStartupIsComplete();
237 } 238 }
238 239
239 void AfterStartupTaskUtils::SetBrowserStartupIsComplete() { 240 void AfterStartupTaskUtils::SetBrowserStartupIsComplete() {
240 ::SetBrowserStartupIsComplete(); 241 ::SetBrowserStartupIsComplete();
241 } 242 }
242 243
243 bool AfterStartupTaskUtils::IsBrowserStartupComplete() { 244 bool AfterStartupTaskUtils::IsBrowserStartupComplete() {
244 return ::IsBrowserStartupComplete(); 245 return ::IsBrowserStartupComplete();
245 } 246 }
246 247
247 void AfterStartupTaskUtils::UnsafeResetForTesting() { 248 void AfterStartupTaskUtils::UnsafeResetForTesting() {
248 DCHECK(g_after_startup_tasks.Get().empty()); 249 DCHECK(g_after_startup_tasks.Get().empty());
249 if (!IsBrowserStartupComplete()) 250 if (!IsBrowserStartupComplete())
250 return; 251 return;
251 g_startup_complete_flag.Get().UnsafeResetForTesting(); 252 g_startup_complete_flag.Get().UnsafeResetForTesting();
252 DCHECK(!IsBrowserStartupComplete()); 253 DCHECK(!IsBrowserStartupComplete());
253 } 254 }
OLDNEW
« no previous file with comments | « chrome/browser/after_startup_task_utils.h ('k') | chrome/browser/after_startup_task_utils_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698