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

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: s/base::ResetAndReturn/std::move/ Created 3 years, 9 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/callback_helpers.h"
10 #include "base/lazy_instance.h" 11 #include "base/lazy_instance.h"
11 #include "base/macros.h" 12 #include "base/macros.h"
12 #include "base/memory/ptr_util.h" 13 #include "base/memory/ptr_util.h"
13 #include "base/metrics/histogram_macros.h" 14 #include "base/metrics/histogram_macros.h"
14 #include "base/process/process_info.h" 15 #include "base/process/process_info.h"
15 #include "base/rand_util.h" 16 #include "base/rand_util.h"
16 #include "base/synchronization/atomic_flag.h" 17 #include "base/synchronization/atomic_flag.h"
17 #include "base/task_runner.h" 18 #include "base/task_runner.h"
18 #include "base/tracked_objects.h" 19 #include "base/tracked_objects.h"
19 #include "build/build_config.h" 20 #include "build/build_config.h"
20 #include "chrome/browser/ui/browser.h" 21 #include "chrome/browser/ui/browser.h"
21 #include "chrome/browser/ui/browser_list.h" 22 #include "chrome/browser/ui/browser_list.h"
22 #include "chrome/browser/ui/tabs/tab_strip_model.h" 23 #include "chrome/browser/ui/tabs/tab_strip_model.h"
23 #include "content/public/browser/browser_thread.h" 24 #include "content/public/browser/browser_thread.h"
24 #include "content/public/browser/render_frame_host.h" 25 #include "content/public/browser/render_frame_host.h"
25 #include "content/public/browser/web_contents.h" 26 #include "content/public/browser/web_contents.h"
26 #include "content/public/browser/web_contents_observer.h" 27 #include "content/public/browser/web_contents_observer.h"
27 28
28 using content::BrowserThread; 29 using content::BrowserThread;
29 using content::WebContents; 30 using content::WebContents;
30 using content::WebContentsObserver; 31 using content::WebContentsObserver;
31 32
32 namespace { 33 namespace {
33 34
34 struct AfterStartupTask { 35 struct AfterStartupTask {
35 AfterStartupTask(const tracked_objects::Location& from_here, 36 AfterStartupTask(const tracked_objects::Location& from_here,
36 const scoped_refptr<base::TaskRunner>& task_runner, 37 const scoped_refptr<base::TaskRunner>& task_runner,
37 const base::Closure& task) 38 base::Closure task)
38 : from_here(from_here), task_runner(task_runner), task(task) {} 39 : from_here(from_here), task_runner(task_runner), task(std::move(task)) {}
39 ~AfterStartupTask() {} 40 ~AfterStartupTask() {}
40 41
41 const tracked_objects::Location from_here; 42 const tracked_objects::Location from_here;
42 const scoped_refptr<base::TaskRunner> task_runner; 43 const scoped_refptr<base::TaskRunner> task_runner;
43 const base::Closure task; 44 base::Closure task;
44 }; 45 };
45 46
46 // The flag may be read on any thread, but must only be set on the UI thread. 47 // 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; 48 base::LazyInstance<base::AtomicFlag>::Leaky g_startup_complete_flag;
48 49
49 // The queue may only be accessed on the UI thread. 50 // The queue may only be accessed on the UI thread.
50 base::LazyInstance<std::deque<AfterStartupTask*>>::Leaky g_after_startup_tasks; 51 base::LazyInstance<std::deque<AfterStartupTask*>>::Leaky g_after_startup_tasks;
51 52
52 bool IsBrowserStartupComplete() { 53 bool IsBrowserStartupComplete() {
53 // Be sure to initialize the LazyInstance on the main thread since the flag 54 // Be sure to initialize the LazyInstance on the main thread since the flag
54 // may only be set on it's initializing thread. 55 // may only be set on it's initializing thread.
55 if (g_startup_complete_flag == nullptr) 56 if (g_startup_complete_flag == nullptr)
56 return false; 57 return false;
57 return g_startup_complete_flag.Get().IsSet(); 58 return g_startup_complete_flag.Get().IsSet();
58 } 59 }
59 60
60 void RunTask(std::unique_ptr<AfterStartupTask> queued_task) { 61 void RunTask(std::unique_ptr<AfterStartupTask> queued_task) {
61 // We're careful to delete the caller's |task| on the target runner's thread. 62 // We're careful to delete the caller's |task| on the target runner's thread.
62 DCHECK(queued_task->task_runner->RunsTasksOnCurrentThread()); 63 DCHECK(queued_task->task_runner->RunsTasksOnCurrentThread());
63 queued_task->task.Run(); 64 std::move(queued_task->task).Run();
sky 2017/03/22 17:34:11 Similar comment about needing std::move here.
64 } 65 }
65 66
66 void ScheduleTask(std::unique_ptr<AfterStartupTask> queued_task) { 67 void ScheduleTask(std::unique_ptr<AfterStartupTask> queued_task) {
67 // Spread their execution over a brief time. 68 // Spread their execution over a brief time.
68 const int kMinDelaySec = 0; 69 const int kMinDelaySec = 0;
69 const int kMaxDelaySec = 10; 70 const int kMaxDelaySec = 10;
70 scoped_refptr<base::TaskRunner> target_runner = queued_task->task_runner; 71 scoped_refptr<base::TaskRunner> target_runner = queued_task->task_runner;
71 tracked_objects::Location from_here = queued_task->from_here; 72 tracked_objects::Location from_here = queued_task->from_here;
72 target_runner->PostDelayedTask( 73 target_runner->PostDelayedTask(
73 from_here, base::Bind(&RunTask, base::Passed(std::move(queued_task))), 74 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( 196 AfterStartupTaskUtils::Runner::Runner(
196 scoped_refptr<base::TaskRunner> destination_runner) 197 scoped_refptr<base::TaskRunner> destination_runner)
197 : destination_runner_(std::move(destination_runner)) { 198 : destination_runner_(std::move(destination_runner)) {
198 DCHECK(destination_runner_); 199 DCHECK(destination_runner_);
199 } 200 }
200 201
201 AfterStartupTaskUtils::Runner::~Runner() = default; 202 AfterStartupTaskUtils::Runner::~Runner() = default;
202 203
203 bool AfterStartupTaskUtils::Runner::PostDelayedTask( 204 bool AfterStartupTaskUtils::Runner::PostDelayedTask(
204 const tracked_objects::Location& from_here, 205 const tracked_objects::Location& from_here,
205 const base::Closure& task, 206 base::Closure task,
206 base::TimeDelta delay) { 207 base::TimeDelta delay) {
207 DCHECK(delay.is_zero()); 208 DCHECK(delay.is_zero());
208 AfterStartupTaskUtils::PostTask(from_here, destination_runner_, task); 209 AfterStartupTaskUtils::PostTask(from_here, destination_runner_,
210 std::move(task));
209 return true; 211 return true;
210 } 212 }
211 213
212 bool AfterStartupTaskUtils::Runner::RunsTasksOnCurrentThread() const { 214 bool AfterStartupTaskUtils::Runner::RunsTasksOnCurrentThread() const {
213 return destination_runner_->RunsTasksOnCurrentThread(); 215 return destination_runner_->RunsTasksOnCurrentThread();
214 } 216 }
215 217
216 void AfterStartupTaskUtils::StartMonitoringStartup() { 218 void AfterStartupTaskUtils::StartMonitoringStartup() {
217 // The observer is self-deleting. 219 // The observer is self-deleting.
218 (new StartupObserver)->Start(); 220 (new StartupObserver)->Start();
219 } 221 }
220 222
221 void AfterStartupTaskUtils::PostTask( 223 void AfterStartupTaskUtils::PostTask(
222 const tracked_objects::Location& from_here, 224 const tracked_objects::Location& from_here,
223 const scoped_refptr<base::TaskRunner>& destination_runner, 225 const scoped_refptr<base::TaskRunner>& destination_runner,
224 const base::Closure& task) { 226 base::Closure task) {
225 if (IsBrowserStartupComplete()) { 227 if (IsBrowserStartupComplete()) {
226 destination_runner->PostTask(from_here, task); 228 destination_runner->PostTask(from_here, std::move(task));
227 return; 229 return;
228 } 230 }
229 231
230 std::unique_ptr<AfterStartupTask> queued_task( 232 std::unique_ptr<AfterStartupTask> queued_task(
231 new AfterStartupTask(from_here, destination_runner, task)); 233 new AfterStartupTask(from_here, destination_runner, std::move(task)));
232 QueueTask(std::move(queued_task)); 234 QueueTask(std::move(queued_task));
233 } 235 }
234 236
235 void AfterStartupTaskUtils::SetBrowserStartupIsCompleteForTesting() { 237 void AfterStartupTaskUtils::SetBrowserStartupIsCompleteForTesting() {
236 ::SetBrowserStartupIsComplete(); 238 ::SetBrowserStartupIsComplete();
237 } 239 }
238 240
239 void AfterStartupTaskUtils::SetBrowserStartupIsComplete() { 241 void AfterStartupTaskUtils::SetBrowserStartupIsComplete() {
240 ::SetBrowserStartupIsComplete(); 242 ::SetBrowserStartupIsComplete();
241 } 243 }
242 244
243 bool AfterStartupTaskUtils::IsBrowserStartupComplete() { 245 bool AfterStartupTaskUtils::IsBrowserStartupComplete() {
244 return ::IsBrowserStartupComplete(); 246 return ::IsBrowserStartupComplete();
245 } 247 }
246 248
247 void AfterStartupTaskUtils::UnsafeResetForTesting() { 249 void AfterStartupTaskUtils::UnsafeResetForTesting() {
248 DCHECK(g_after_startup_tasks.Get().empty()); 250 DCHECK(g_after_startup_tasks.Get().empty());
249 if (!IsBrowserStartupComplete()) 251 if (!IsBrowserStartupComplete())
250 return; 252 return;
251 g_startup_complete_flag.Get().UnsafeResetForTesting(); 253 g_startup_complete_flag.Get().UnsafeResetForTesting();
252 DCHECK(!IsBrowserStartupComplete()); 254 DCHECK(!IsBrowserStartupComplete());
253 } 255 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698