Fix Win8 metro startup crash from window switcher button
[chromium-blink-merge.git] / webkit / fileapi / syncable / syncable_file_operation_runner.cc
blob91fca3ebe74a3def61ba6854206992a6843a6b07
1 // Copyright (c) 2012 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 "webkit/fileapi/syncable/syncable_file_operation_runner.h"
7 #include <algorithm>
8 #include <functional>
10 #include "base/callback.h"
11 #include "base/stl_util.h"
12 #include "webkit/fileapi/syncable/local_file_sync_status.h"
14 using fileapi::FileSystemURL;
16 namespace sync_file_system {
18 // SyncableFileOperationRunner::Task -------------------------------------------
20 // static
21 void SyncableFileOperationRunner::Task::CancelAndDelete(
22 SyncableFileOperationRunner::Task* task) {
23 task->Cancel();
24 delete task;
27 bool SyncableFileOperationRunner::Task::IsRunnable(
28 LocalFileSyncStatus* status) const {
29 for (size_t i = 0; i < target_paths().size(); ++i) {
30 if (!status->IsWritable(target_paths()[i]))
31 return false;
33 return true;
36 void SyncableFileOperationRunner::Task::Start(LocalFileSyncStatus* status) {
37 for (size_t i = 0; i < target_paths().size(); ++i) {
38 DCHECK(status->IsWritable(target_paths()[i]));
39 status->StartWriting(target_paths()[i]);
41 Run();
44 // SyncableFileOperationRunner -------------------------------------------------
46 SyncableFileOperationRunner::SyncableFileOperationRunner(
47 int64 max_inflight_tasks,
48 LocalFileSyncStatus* sync_status)
49 : sync_status_(sync_status),
50 max_inflight_tasks_(max_inflight_tasks),
51 num_inflight_tasks_(0) {
52 DCHECK(CalledOnValidThread());
53 sync_status_->AddObserver(this);
56 SyncableFileOperationRunner::~SyncableFileOperationRunner() {
57 DCHECK(CalledOnValidThread());
58 for_each(pending_tasks_.begin(), pending_tasks_.end(),
59 SyncableFileOperationRunner::Task::CancelAndDelete);
62 void SyncableFileOperationRunner::OnSyncEnabled(const FileSystemURL& url) {
65 void SyncableFileOperationRunner::OnWriteEnabled(const FileSystemURL& url) {
66 DCHECK(CalledOnValidThread());
67 RunNextRunnableTask();
70 void SyncableFileOperationRunner::PostOperationTask(scoped_ptr<Task> task) {
71 DCHECK(CalledOnValidThread());
72 pending_tasks_.push_back(task.release());
73 RunNextRunnableTask();
76 void SyncableFileOperationRunner::RunNextRunnableTask() {
77 DCHECK(CalledOnValidThread());
78 for (std::list<Task*>::iterator iter = pending_tasks_.begin();
79 iter != pending_tasks_.end() && ShouldStartMoreTasks();) {
80 if ((*iter)->IsRunnable(sync_status())) {
81 ++num_inflight_tasks_;
82 DCHECK_GE(num_inflight_tasks_, 1);
83 scoped_ptr<Task> task(*iter);
84 pending_tasks_.erase(iter++);
85 task->Start(sync_status());
86 continue;
88 ++iter;
92 void SyncableFileOperationRunner::OnOperationCompleted(
93 const std::vector<FileSystemURL>& target_paths) {
94 --num_inflight_tasks_;
95 DCHECK_GE(num_inflight_tasks_, 0);
96 for (size_t i = 0; i < target_paths.size(); ++i) {
97 DCHECK(sync_status()->IsWriting(target_paths[i]));
98 sync_status()->EndWriting(target_paths[i]);
100 RunNextRunnableTask();
103 bool SyncableFileOperationRunner::ShouldStartMoreTasks() const {
104 return num_inflight_tasks_ < max_inflight_tasks_;
107 } // namespace sync_file_system