1 // Copyright 2013 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.
5 #include "content/browser/startup_task_runner.h"
8 #include "base/location.h"
9 #include "base/message_loop/message_loop.h"
13 StartupTaskRunner::StartupTaskRunner(
14 base::Callback
<void(int)> const startup_complete_callback
,
15 scoped_refptr
<base::SingleThreadTaskRunner
> proxy
)
16 : startup_complete_callback_(startup_complete_callback
), proxy_(proxy
) {}
18 StartupTaskRunner::~StartupTaskRunner() {}
20 void StartupTaskRunner::AddTask(StartupTask
& callback
) {
21 task_list_
.push_back(callback
);
24 void StartupTaskRunner::StartRunningTasksAsync() {
27 if (task_list_
.empty()) {
28 if (!startup_complete_callback_
.is_null()) {
29 startup_complete_callback_
.Run(result
);
30 // Clear the callback to prevent it being called a second time
31 startup_complete_callback_
.Reset();
34 const base::Closure next_task
=
35 base::Bind(&StartupTaskRunner::WrappedTask
, base::Unretained(this));
36 proxy_
->PostNonNestableTask(FROM_HERE
, next_task
);
40 void StartupTaskRunner::RunAllTasksNow() {
42 for (std::list
<StartupTask
>::iterator it
= task_list_
.begin();
43 it
!= task_list_
.end();
46 if (result
> 0) break;
49 if (!startup_complete_callback_
.is_null()) {
50 startup_complete_callback_
.Run(result
);
51 // Clear the callback to prevent it being called a second time
52 startup_complete_callback_
.Reset();
56 void StartupTaskRunner::WrappedTask() {
57 if (task_list_
.empty()) {
58 // This will happen if the remaining tasks have been run synchronously since
59 // the WrappedTask was created. Any callback will already have been called,
60 // so there is nothing to do
63 int result
= task_list_
.front().Run();
64 task_list_
.pop_front();
66 // Stop now and throw away the remaining tasks
69 if (task_list_
.empty()) {
70 if (!startup_complete_callback_
.is_null()) {
71 startup_complete_callback_
.Run(result
);
72 // Clear the callback to prevent it being called a second time
73 startup_complete_callback_
.Reset();
76 const base::Closure next_task
=
77 base::Bind(&StartupTaskRunner::WrappedTask
, base::Unretained(this));
78 proxy_
->PostNonNestableTask(FROM_HERE
, next_task
);
82 } // namespace content