OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 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 "content/browser/startup_task_runner.h" | 5 #include "content/browser/startup_task_runner.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/location.h" | 8 #include "base/location.h" |
9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
10 | 10 |
11 namespace content { | 11 namespace content { |
12 | 12 |
13 StartupTaskRunner::StartupTaskRunner( | 13 StartupTaskRunner::StartupTaskRunner( |
14 bool browser_may_start_asynchronously, | |
15 base::Callback<void(int)> const startup_complete_callback, | 14 base::Callback<void(int)> const startup_complete_callback, |
16 scoped_refptr<base::SingleThreadTaskRunner> proxy) | 15 scoped_refptr<base::SingleThreadTaskRunner> proxy) |
17 : asynchronous_startup_(browser_may_start_asynchronously), | 16 : startup_complete_callback_(startup_complete_callback), proxy_(proxy) {} |
18 startup_complete_callback_(startup_complete_callback), | 17 |
19 proxy_(proxy) {} | 18 StartupTaskRunner::~StartupTaskRunner() {} |
20 | 19 |
21 void StartupTaskRunner::AddTask(StartupTask& callback) { | 20 void StartupTaskRunner::AddTask(StartupTask& callback) { |
22 task_list_.push_back(callback); | 21 task_list_.push_back(callback); |
23 } | 22 } |
24 | 23 |
25 void StartupTaskRunner::StartRunningTasks() { | 24 void StartupTaskRunner::StartRunningTasksAsync() { |
26 DCHECK(proxy_); | 25 DCHECK(proxy_); |
27 int result = 0; | 26 int result = 0; |
28 if (asynchronous_startup_ && !task_list_.empty()) { | 27 if (task_list_.empty()) { |
29 const base::Closure next_task = | |
30 base::Bind(&StartupTaskRunner::WrappedTask, this); | |
31 proxy_->PostNonNestableTask(FROM_HERE, next_task); | |
32 } else { | |
33 for (std::list<StartupTask>::iterator it = task_list_.begin(); | |
34 it != task_list_.end(); | |
35 it++) { | |
36 result = it->Run(); | |
37 if (result > 0) { | |
38 break; | |
39 } | |
40 } | |
41 if (!startup_complete_callback_.is_null()) { | 28 if (!startup_complete_callback_.is_null()) { |
42 startup_complete_callback_.Run(result); | 29 startup_complete_callback_.Run(result); |
43 } | 30 } |
| 31 } else { |
| 32 const base::Closure next_task = |
| 33 base::Bind(&StartupTaskRunner::WrappedTask, base::Unretained(this)); |
| 34 proxy_->PostNonNestableTask(FROM_HERE, next_task); |
| 35 } |
| 36 } |
| 37 |
| 38 void StartupTaskRunner::RunAllTasksNow() { |
| 39 int result = 0; |
| 40 for (std::list<StartupTask>::iterator it = task_list_.begin(); |
| 41 it != task_list_.end(); |
| 42 it++) { |
| 43 result = it->Run(); |
| 44 if (result > 0) break; |
| 45 } |
| 46 if (!startup_complete_callback_.is_null()) { |
| 47 startup_complete_callback_.Run(result); |
44 } | 48 } |
45 } | 49 } |
46 | 50 |
47 void StartupTaskRunner::WrappedTask() { | 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 |
| 56 return; |
| 57 } |
48 int result = task_list_.front().Run(); | 58 int result = task_list_.front().Run(); |
49 task_list_.pop_front(); | 59 task_list_.pop_front(); |
50 if (result > 0 || task_list_.empty()) { | 60 if (result > 0 || task_list_.empty()) { |
51 if (!startup_complete_callback_.is_null()) { | 61 if (!startup_complete_callback_.is_null()) { |
52 startup_complete_callback_.Run(result); | 62 startup_complete_callback_.Run(result); |
53 } | 63 } |
54 } else { | 64 } else { |
55 const base::Closure next_task = | 65 const base::Closure next_task = |
56 base::Bind(&StartupTaskRunner::WrappedTask, this); | 66 base::Bind(&StartupTaskRunner::WrappedTask, base::Unretained(this)); |
57 proxy_->PostNonNestableTask(FROM_HERE, next_task); | 67 proxy_->PostNonNestableTask(FROM_HERE, next_task); |
58 } | 68 } |
59 } | 69 } |
60 | 70 |
61 StartupTaskRunner::~StartupTaskRunner() {} | |
62 | |
63 } // namespace content | 71 } // namespace content |
OLD | NEW |