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 base::Callback<void(int)> const startup_complete_callback, | 14 base::Callback<void(int)> const startup_complete_callback, |
15 scoped_refptr<base::SingleThreadTaskRunner> proxy) | 15 scoped_refptr<base::SingleThreadTaskRunner> proxy) |
16 : startup_complete_callback_(startup_complete_callback), proxy_(proxy) {} | 16 : startup_complete_callback_(startup_complete_callback), proxy_(proxy) {} |
17 | 17 |
18 StartupTaskRunner::~StartupTaskRunner() {} | 18 StartupTaskRunner::~StartupTaskRunner() {} |
19 | 19 |
20 void StartupTaskRunner::AddTask(StartupTask& callback) { | 20 void StartupTaskRunner::AddTask(StartupTask& callback) { |
21 task_list_.push_back(callback); | 21 task_list_.push_back(callback); |
22 } | 22 } |
23 | 23 |
24 void StartupTaskRunner::StartRunningTasksAsync() { | 24 void StartupTaskRunner::StartRunningTasksAsync() { |
25 DCHECK(proxy_); | 25 DCHECK(proxy_); |
26 int result = 0; | 26 int result = 0; |
27 if (task_list_.empty()) { | 27 if (task_list_.empty()) { |
28 if (!startup_complete_callback_.is_null()) { | 28 if (!startup_complete_callback_.is_null()) { |
29 startup_complete_callback_.Run(result); | 29 startup_complete_callback_.Run(result); |
| 30 // Clear the callback to prevent it being called a second time |
| 31 startup_complete_callback_.Reset(); |
30 } | 32 } |
31 } else { | 33 } else { |
32 const base::Closure next_task = | 34 const base::Closure next_task = |
33 base::Bind(&StartupTaskRunner::WrappedTask, base::Unretained(this)); | 35 base::Bind(&StartupTaskRunner::WrappedTask, base::Unretained(this)); |
34 proxy_->PostNonNestableTask(FROM_HERE, next_task); | 36 proxy_->PostNonNestableTask(FROM_HERE, next_task); |
35 } | 37 } |
36 } | 38 } |
37 | 39 |
38 void StartupTaskRunner::RunAllTasksNow() { | 40 void StartupTaskRunner::RunAllTasksNow() { |
39 int result = 0; | 41 int result = 0; |
40 for (std::list<StartupTask>::iterator it = task_list_.begin(); | 42 for (std::list<StartupTask>::iterator it = task_list_.begin(); |
41 it != task_list_.end(); | 43 it != task_list_.end(); |
42 it++) { | 44 it++) { |
43 result = it->Run(); | 45 result = it->Run(); |
44 if (result > 0) break; | 46 if (result > 0) break; |
45 } | 47 } |
| 48 task_list_.clear(); |
46 if (!startup_complete_callback_.is_null()) { | 49 if (!startup_complete_callback_.is_null()) { |
47 startup_complete_callback_.Run(result); | 50 startup_complete_callback_.Run(result); |
| 51 // Clear the callback to prevent it being called a second time |
| 52 startup_complete_callback_.Reset(); |
48 } | 53 } |
49 } | 54 } |
50 | 55 |
51 void StartupTaskRunner::WrappedTask() { | 56 void StartupTaskRunner::WrappedTask() { |
52 if (task_list_.empty()) { | 57 if (task_list_.empty()) { |
53 // This will happen if the remaining tasks have been run synchronously since | 58 // This will happen if the remaining tasks have been run synchronously since |
54 // the WrappedTask was created. Any callback will already have been called, | 59 // the WrappedTask was created. Any callback will already have been called, |
55 // so there is nothing to do | 60 // so there is nothing to do |
56 return; | 61 return; |
57 } | 62 } |
58 int result = task_list_.front().Run(); | 63 int result = task_list_.front().Run(); |
59 task_list_.pop_front(); | 64 task_list_.pop_front(); |
60 if (result > 0 || task_list_.empty()) { | 65 if (result > 0) { |
| 66 // Stop now and throw away the remaining tasks |
| 67 task_list_.clear(); |
| 68 } |
| 69 if (task_list_.empty()) { |
61 if (!startup_complete_callback_.is_null()) { | 70 if (!startup_complete_callback_.is_null()) { |
62 startup_complete_callback_.Run(result); | 71 startup_complete_callback_.Run(result); |
| 72 // Clear the callback to prevent it being called a second time |
| 73 startup_complete_callback_.Reset(); |
63 } | 74 } |
64 } else { | 75 } else { |
65 const base::Closure next_task = | 76 const base::Closure next_task = |
66 base::Bind(&StartupTaskRunner::WrappedTask, base::Unretained(this)); | 77 base::Bind(&StartupTaskRunner::WrappedTask, base::Unretained(this)); |
67 proxy_->PostNonNestableTask(FROM_HERE, next_task); | 78 proxy_->PostNonNestableTask(FROM_HERE, next_task); |
68 } | 79 } |
69 } | 80 } |
70 | 81 |
71 } // namespace content | 82 } // namespace content |
OLD | NEW |