OLD | NEW |
---|---|
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "base/bind.h" | 5 #include "base/bind.h" |
6 #include "base/location.h" | 6 #include "base/location.h" |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/single_thread_task_runner.h" | 8 #include "base/single_thread_task_runner.h" |
9 #include "base/thread_task_runner_handle.h" | 9 #include "base/thread_task_runner_handle.h" |
10 #include "chrome/browser/chromeos/file_system_provider/queue.h" | 10 #include "chrome/browser/chromeos/file_system_provider/queue.h" |
(...skipping 18 matching lines...) Expand all Loading... | |
29 DCHECK_LT(0u, max_in_parallel); | 29 DCHECK_LT(0u, max_in_parallel); |
30 } | 30 } |
31 | 31 |
32 Queue::~Queue() { | 32 Queue::~Queue() { |
33 } | 33 } |
34 | 34 |
35 size_t Queue::NewToken() { | 35 size_t Queue::NewToken() { |
36 return next_token_++; | 36 return next_token_++; |
37 } | 37 } |
38 | 38 |
39 AbortCallback Queue::Enqueue(size_t token, const AbortableCallback& callback) { | 39 void Queue::Enqueue(size_t token, const AbortableCallback& callback) { |
40 #if !NDEBUG | 40 #if !NDEBUG |
41 const auto it = executed_.find(token); | 41 const auto it = executed_.find(token); |
42 DCHECK(it == executed_.end()); | 42 DCHECK(it == executed_.end()); |
43 for (auto& task : pending_) { | 43 for (auto& task : pending_) { |
44 DCHECK(token != task.token); | 44 DCHECK(token != task.token); |
45 } | 45 } |
46 #endif | 46 #endif |
47 pending_.push_back(Task(token, callback)); | 47 pending_.push_back(Task(token, callback)); |
48 base::ThreadTaskRunnerHandle::Get()->PostTask( | 48 base::ThreadTaskRunnerHandle::Get()->PostTask( |
49 FROM_HERE, base::Bind(&Queue::MaybeRun, weak_ptr_factory_.GetWeakPtr())); | 49 FROM_HERE, base::Bind(&Queue::MaybeRun, weak_ptr_factory_.GetWeakPtr())); |
50 return base::Bind(&Queue::Abort, weak_ptr_factory_.GetWeakPtr(), token); | |
51 } | 50 } |
52 | 51 |
53 void Queue::Complete(size_t token) { | 52 void Queue::Complete(size_t token) { |
54 const auto it = executed_.find(token); | 53 const auto it = executed_.find(token); |
55 DCHECK(it != executed_.end() && !it->second.completed); | 54 DCHECK(it != executed_.end() && !it->second.completed); |
56 it->second.completed = true; | 55 it->second.completed = true; |
57 } | 56 } |
58 | 57 |
59 void Queue::Remove(size_t token) { | 58 void Queue::Remove(size_t token) { |
60 const auto it = executed_.find(token); | 59 const auto it = executed_.find(token); |
(...skipping 10 matching lines...) Expand all Loading... | |
71 } | 70 } |
72 | 71 |
73 DCHECK_GT(max_in_parallel_, executed_.size()); | 72 DCHECK_GT(max_in_parallel_, executed_.size()); |
74 Task task = pending_.front(); | 73 Task task = pending_.front(); |
75 pending_.pop_front(); | 74 pending_.pop_front(); |
76 | 75 |
77 executed_[task.token] = task; | 76 executed_[task.token] = task; |
78 executed_[task.token].abort_callback = task.callback.Run(); | 77 executed_[task.token].abort_callback = task.callback.Run(); |
79 } | 78 } |
80 | 79 |
81 void Queue::Abort(size_t token, | 80 void Queue::Abort(size_t token) { |
82 const storage::AsyncFileUtil::StatusCallback& callback) { | |
83 // Check if it's running. | 81 // Check if it's running. |
84 const auto it = executed_.find(token); | 82 const auto it = executed_.find(token); |
85 if (it != executed_.end()) { | 83 if (it != executed_.end()) { |
86 const Task& task = it->second; | 84 const Task& task = it->second; |
87 // If the task is marked as completed, then it's impossible to abort it. | 85 // If the task is marked as completed, then it's impossible to abort it. |
88 if (task.completed) { | 86 DCHECK(!task.completed); |
89 callback.Run(base::File::FILE_ERROR_INVALID_OPERATION); | |
90 return; | |
91 } | |
92 DCHECK(!task.abort_callback.is_null()); | 87 DCHECK(!task.abort_callback.is_null()); |
93 it->second.abort_callback.Run(callback); | 88 it->second.abort_callback.Run(); |
94 executed_.erase(it); | 89 executed_.erase(it); |
hirono
2015/01/14 05:36:06
Aborted task should not be removed from the queue.
mtomasz
2015/01/14 08:55:10
Good catch. Fixed + upgraded tests. Done.
| |
95 base::ThreadTaskRunnerHandle::Get()->PostTask( | 90 base::ThreadTaskRunnerHandle::Get()->PostTask( |
96 FROM_HERE, | 91 FROM_HERE, |
97 base::Bind(&Queue::MaybeRun, weak_ptr_factory_.GetWeakPtr())); | 92 base::Bind(&Queue::MaybeRun, weak_ptr_factory_.GetWeakPtr())); |
98 return; | 93 return; |
99 } | 94 } |
100 | 95 |
101 // Aborting not running tasks is linear. TODO(mtomasz): Optimize if feasible. | 96 // Aborting not running tasks is linear. TODO(mtomasz): Optimize if feasible. |
102 for (auto it = pending_.begin(); it != pending_.end(); ++it) { | 97 for (auto it = pending_.begin(); it != pending_.end(); ++it) { |
103 if (token == it->token) { | 98 if (token == it->token) { |
104 pending_.erase(it); | 99 pending_.erase(it); |
hirono
2015/01/14 05:36:06
ditto
mtomasz
2015/01/14 08:55:09
Done.
| |
105 callback.Run(base::File::FILE_OK); | |
106 base::ThreadTaskRunnerHandle::Get()->PostTask( | 100 base::ThreadTaskRunnerHandle::Get()->PostTask( |
107 FROM_HERE, | 101 FROM_HERE, |
108 base::Bind(&Queue::MaybeRun, weak_ptr_factory_.GetWeakPtr())); | 102 base::Bind(&Queue::MaybeRun, weak_ptr_factory_.GetWeakPtr())); |
109 return; | 103 return; |
110 } | 104 } |
111 } | 105 } |
112 | 106 |
113 // The task is already removed. | 107 // The task is already removed. |
114 callback.Run(base::File::FILE_ERROR_INVALID_OPERATION); | 108 NOTREACHED(); |
115 } | 109 } |
116 | 110 |
117 } // namespace file_system_provider | 111 } // namespace file_system_provider |
118 } // namespace chromeos | 112 } // namespace chromeos |
OLD | NEW |