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
);
32 const base::Closure next_task
=
33 base::Bind(&StartupTaskRunner::WrappedTask
, base::Unretained(this));
34 proxy_
->PostNonNestableTask(FROM_HERE
, next_task
);
38 void StartupTaskRunner::RunAllTasksNow() {
40 for (std::list
<StartupTask
>::iterator it
= task_list_
.begin();
41 it
!= task_list_
.end();
44 if (result
> 0) break;
46 if (!startup_complete_callback_
.is_null()) {
47 startup_complete_callback_
.Run(result
);
51 void StartupTaskRunner::WrappedTask() {
52 if (task_list_
.empty()) {
53 // This will happen if the remaining tasks have been run synchronously since
54 // the WrappedTask was created. Any callback will already have been called,
55 // so there is nothing to do
58 int result
= task_list_
.front().Run();
59 task_list_
.pop_front();
60 if (result
> 0 || task_list_
.empty()) {
61 if (!startup_complete_callback_
.is_null()) {
62 startup_complete_callback_
.Run(result
);
65 const base::Closure next_task
=
66 base::Bind(&StartupTaskRunner::WrappedTask
, base::Unretained(this));
67 proxy_
->PostNonNestableTask(FROM_HERE
, next_task
);
71 } // namespace content