[Storage] Blob Storage Refactoring pt 1:
[chromium-blink-merge.git] / content / browser / startup_task_runner.cc
blob8e680bd6d086011c56673f4c2cd71751939014e1
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"
7 #include "base/bind.h"
8 #include "base/location.h"
9 #include "base/message_loop/message_loop.h"
11 namespace content {
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() {
25 DCHECK(proxy_.get());
26 int result = 0;
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();
33 } else {
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() {
41 int result = 0;
42 for (std::list<StartupTask>::iterator it = task_list_.begin();
43 it != task_list_.end();
44 it++) {
45 result = it->Run();
46 if (result > 0) break;
48 task_list_.clear();
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
61 return;
63 int result = task_list_.front().Run();
64 task_list_.pop_front();
65 if (result > 0) {
66 // Stop now and throw away the remaining tasks
67 task_list_.clear();
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();
75 } else {
76 const base::Closure next_task =
77 base::Bind(&StartupTaskRunner::WrappedTask, base::Unretained(this));
78 proxy_->PostNonNestableTask(FROM_HERE, next_task);
82 } // namespace content