Only sync parent directory once after a leveldb file rename.
[chromium-blink-merge.git] / webkit / fileapi / syncable / syncable_file_operation_runner.h
blob9f15e211eb33a743cd851414a21e675d5e1250dc
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 #ifndef WEBKIT_FILEAPI_SYNCABLE_SYNCABLE_FILE_OPERATION_RUNNER_H_
6 #define WEBKIT_FILEAPI_SYNCABLE_SYNCABLE_FILE_OPERATION_RUNNER_H_
8 #include <list>
9 #include <vector>
11 #include "base/basictypes.h"
12 #include "base/callback.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/memory/weak_ptr.h"
15 #include "base/threading/non_thread_safe.h"
16 #include "webkit/fileapi/file_system_url.h"
17 #include "webkit/fileapi/syncable/local_file_sync_status.h"
18 #include "webkit/storage/webkit_storage_export.h"
20 namespace fileapi {
21 class FileSystemURL;
24 namespace sync_file_system {
26 // This class must run only on IO thread.
27 // Owned by LocalFileSyncContext.
28 class WEBKIT_STORAGE_EXPORT SyncableFileOperationRunner
29 : public base::NonThreadSafe,
30 public base::SupportsWeakPtr<SyncableFileOperationRunner>,
31 public LocalFileSyncStatus::Observer {
32 public:
33 // Represents an operation task (which usually wraps one FileSystemOperation).
34 class Task {
35 public:
36 Task() {}
37 virtual ~Task() {}
39 // Only one of Run() or Cancel() is called.
40 virtual void Run() = 0;
41 virtual void Cancel() = 0;
43 protected:
44 // This is never called after Run() or Cancel() is called.
45 virtual const std::vector<fileapi::FileSystemURL>& target_paths() const = 0;
47 private:
48 friend class SyncableFileOperationRunner;
49 bool IsRunnable(LocalFileSyncStatus* status) const;
50 void Start(LocalFileSyncStatus* status);
51 static void CancelAndDelete(Task* task);
53 DISALLOW_COPY_AND_ASSIGN(Task);
56 SyncableFileOperationRunner(int64 max_inflight_tasks,
57 LocalFileSyncStatus* sync_status);
58 virtual ~SyncableFileOperationRunner();
60 // LocalFileSyncStatus::Observer overrides.
61 virtual void OnSyncEnabled(const fileapi::FileSystemURL& url) OVERRIDE;
62 virtual void OnWriteEnabled(const fileapi::FileSystemURL& url) OVERRIDE;
64 // Runs the given |task| if no sync operation is running on any of
65 // its target_paths(). This also runs pending tasks that have become
66 // runnable (before running the given operation).
67 // If there're ongoing sync tasks on the target_paths this method
68 // just queues up the |task|.
69 // Pending tasks are cancelled when this class is destructed.
70 void PostOperationTask(scoped_ptr<Task> task);
72 // Runs a next runnable task (if there's any).
73 void RunNextRunnableTask();
75 // Called when an operation is completed. This will make |target_paths|
76 // writable and may start a next runnable task.
77 void OnOperationCompleted(
78 const std::vector<fileapi::FileSystemURL>& target_paths);
80 LocalFileSyncStatus* sync_status() const { return sync_status_; }
82 int64 num_pending_tasks() const {
83 return static_cast<int64>(pending_tasks_.size());
86 int64 num_inflight_tasks() const { return num_inflight_tasks_; }
88 private:
89 // Returns true if we should start more tasks.
90 bool ShouldStartMoreTasks() const;
92 // Keeps track of the writing/syncing status. Not owned.
93 LocalFileSyncStatus* sync_status_;
95 std::list<Task*> pending_tasks_;
97 const int64 max_inflight_tasks_;
98 int64 num_inflight_tasks_;
100 DISALLOW_COPY_AND_ASSIGN(SyncableFileOperationRunner);
103 } // namespace sync_file_system
105 #endif // WEBKIT_FILEAPI_SYNCABLE_SYNCABLE_FILE_OPERATION_RUNNER_H_