summaryrefslogtreecommitdiff
path: root/chromium/components/offline_pages/task/task_queue.cc
diff options
context:
space:
mode:
Diffstat (limited to 'chromium/components/offline_pages/task/task_queue.cc')
-rw-r--r--chromium/components/offline_pages/task/task_queue.cc60
1 files changed, 47 insertions, 13 deletions
diff --git a/chromium/components/offline_pages/task/task_queue.cc b/chromium/components/offline_pages/task/task_queue.cc
index 952a9a7815f..4cb63f99a1d 100644
--- a/chromium/components/offline_pages/task/task_queue.cc
+++ b/chromium/components/offline_pages/task/task_queue.cc
@@ -19,11 +19,14 @@ namespace offline_pages {
struct TaskQueue::Entry {
Entry() = default;
explicit Entry(std::unique_ptr<Task> task) : task(std::move(task)) {}
+ Entry(const base::Location& location, std::unique_ptr<Task> task)
+ : task(std::move(task)), from_here(location) {}
Entry(std::unique_ptr<Task> task, base::OnceClosure resume_callback)
: task(std::move(task)), resume_callback(std::move(resume_callback)) {}
std::unique_ptr<Task> task;
base::OnceClosure resume_callback;
+ base::Location from_here;
};
TaskQueue::TaskQueue(Delegate* delegate)
@@ -40,6 +43,15 @@ void TaskQueue::AddTask(std::unique_ptr<Task> task) {
StartTaskIfAvailable();
}
+void TaskQueue::AddTask(const base::Location& from_here,
+ std::unique_ptr<Task> task) {
+ DVLOG(2) << "Adding task " << from_here.ToString();
+ DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
+ task->task_queue_ = this;
+ tasks_.emplace_back(from_here, std::move(task));
+ StartTaskIfAvailable();
+}
+
bool TaskQueue::HasPendingTasks() const {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
return !tasks_.empty() || HasRunningTask();
@@ -65,6 +77,8 @@ void TaskQueue::StartTaskIfAvailable() {
}
current_task_ = std::move(tasks_.front().task);
+ current_task_location_ = tasks_.front().from_here;
+
base::OnceClosure resume_callback = std::move(tasks_.front().resume_callback);
tasks_.pop_front();
if (resume_callback) {
@@ -80,13 +94,15 @@ void TaskQueue::StartTaskIfAvailable() {
}
void TaskQueue::RunCurrentTask() {
+ DVLOG(2) << "Running task " << current_task_location_.ToString();
current_task_->Execute(base::BindOnce(&TaskCompletedCallback, task_runner_,
weak_ptr_factory_.GetWeakPtr(),
current_task_.get()));
}
void TaskQueue::ResumeCurrentTask(base::OnceClosure on_resume) {
- DCHECK_EQ(Task::TaskState::kSuspended, current_task_->state_);
+ DVLOG(2) << "Resuming task " << current_task_location_.ToString();
+ DCHECK_EQ(Task::TaskState::kPendingResume, current_task_->state_);
current_task_->state_ = Task::TaskState::kRunning;
std::move(on_resume).Run();
}
@@ -105,6 +121,7 @@ void TaskQueue::TaskCompleted(Task* task) {
// Normally, the completed task is the current task.
if (task == current_task_.get()) {
current_task_.reset(nullptr);
+ DVLOG(2) << "Current task completed " << current_task_location_.ToString();
StartTaskIfAvailable();
return;
}
@@ -112,16 +129,9 @@ void TaskQueue::TaskCompleted(Task* task) {
// If the task is in the suspended_tasks_ list, remove it.
for (auto iter = suspended_tasks_.begin(); iter != suspended_tasks_.end();
++iter) {
- if (iter->get() == task) {
- suspended_tasks_.erase(iter);
- return;
- }
- }
-
- // Otherwise, this is an enqueued task. Find and remove it.
- for (auto iter = tasks_.begin(); iter != tasks_.end(); ++iter) {
if (iter->task.get() == task) {
- tasks_.erase(iter);
+ DVLOG(2) << "Suspended task completed " << iter->from_here.ToString();
+ suspended_tasks_.erase(iter);
return;
}
}
@@ -134,7 +144,8 @@ void TaskQueue::SuspendTask(Task* task) {
// Task::Suspend() sets state to kSuspended.
DCHECK_EQ(Task::TaskState::kSuspended, task->state_);
DCHECK_EQ(task, current_task_.get());
- suspended_tasks_.push_back(std::move(current_task_));
+ suspended_tasks_.emplace_back(current_task_location_,
+ std::move(current_task_));
StartTaskIfAvailable();
}
@@ -143,9 +154,11 @@ void TaskQueue::ResumeTask(Task* task, base::OnceClosure on_resume) {
DCHECK_EQ(Task::TaskState::kSuspended, task->state_);
for (auto iter = suspended_tasks_.begin(); iter != suspended_tasks_.end();
++iter) {
- if (iter->get() == task) {
- tasks_.emplace_back(std::move(*iter), std::move(on_resume));
+ if (iter->task.get() == task) {
+ iter->resume_callback = std::move(on_resume);
+ tasks_.push_back(std::move(*iter));
suspended_tasks_.erase(iter);
+ task->state_ = Task::TaskState::kPendingResume;
StartTaskIfAvailable();
return;
}
@@ -159,4 +172,25 @@ void TaskQueue::InformTaskQueueIsIdle() {
delegate_->OnTaskQueueIsIdle();
}
+// Returns a human-readable string describing the contents of the task queue.
+std::string TaskQueue::GetStateForTesting() const {
+ std::stringstream ss;
+ if (current_task_) {
+ ss << "Current task: " << current_task_location_.ToString() << '\n';
+ } else {
+ ss << "No current task\n";
+ }
+ int number = 1;
+ for (const auto& entry : tasks_) {
+ ss << "Pending task " << number++ << ": " << entry.from_here.ToString()
+ << '\n';
+ }
+ number = 1;
+ for (const auto& entry : suspended_tasks_) {
+ ss << "Suspended task " << number++ << ": " << entry.from_here.ToString()
+ << '\n';
+ }
+ return ss.str();
+}
+
} // namespace offline_pages