OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #ifndef WEBKIT_FILEAPI_SYNCABLE_SYNCABLE_FILE_OPERATION_RUNNER_H_ | |
6 #define WEBKIT_FILEAPI_SYNCABLE_SYNCABLE_FILE_OPERATION_RUNNER_H_ | |
7 | |
8 #include <list> | |
9 #include <vector> | |
10 | |
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/browser/fileapi/file_system_url.h" | |
17 #include "webkit/fileapi/syncable/local_file_sync_status.h" | |
18 #include "webkit/storage/webkit_storage_export.h" | |
19 | |
20 namespace fileapi { | |
21 class FileSystemURL; | |
22 } | |
23 | |
24 namespace sync_file_system { | |
25 | |
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() {} | |
38 | |
39 // Only one of Run() or Cancel() is called. | |
40 virtual void Run() = 0; | |
41 virtual void Cancel() = 0; | |
42 | |
43 protected: | |
44 // This is never called after Run() or Cancel() is called. | |
45 virtual const std::vector<fileapi::FileSystemURL>& target_paths() const = 0; | |
46 | |
47 private: | |
48 friend class SyncableFileOperationRunner; | |
49 bool IsRunnable(LocalFileSyncStatus* status) const; | |
50 void Start(LocalFileSyncStatus* status); | |
51 static void CancelAndDelete(Task* task); | |
52 | |
53 DISALLOW_COPY_AND_ASSIGN(Task); | |
54 }; | |
55 | |
56 SyncableFileOperationRunner(int64 max_inflight_tasks, | |
57 LocalFileSyncStatus* sync_status); | |
58 virtual ~SyncableFileOperationRunner(); | |
59 | |
60 // LocalFileSyncStatus::Observer overrides. | |
61 virtual void OnSyncEnabled(const fileapi::FileSystemURL& url) OVERRIDE; | |
62 virtual void OnWriteEnabled(const fileapi::FileSystemURL& url) OVERRIDE; | |
63 | |
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); | |
71 | |
72 // Runs a next runnable task (if there's any). | |
73 void RunNextRunnableTask(); | |
74 | |
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); | |
79 | |
80 LocalFileSyncStatus* sync_status() const { return sync_status_; } | |
81 | |
82 int64 num_pending_tasks() const { | |
83 return static_cast<int64>(pending_tasks_.size()); | |
84 } | |
85 | |
86 int64 num_inflight_tasks() const { return num_inflight_tasks_; } | |
87 | |
88 private: | |
89 // Returns true if we should start more tasks. | |
90 bool ShouldStartMoreTasks() const; | |
91 | |
92 // Keeps track of the writing/syncing status. Not owned. | |
93 LocalFileSyncStatus* sync_status_; | |
94 | |
95 std::list<Task*> pending_tasks_; | |
96 | |
97 const int64 max_inflight_tasks_; | |
98 int64 num_inflight_tasks_; | |
99 | |
100 DISALLOW_COPY_AND_ASSIGN(SyncableFileOperationRunner); | |
101 }; | |
102 | |
103 } // namespace sync_file_system | |
104 | |
105 #endif // WEBKIT_FILEAPI_SYNCABLE_SYNCABLE_FILE_OPERATION_RUNNER_H_ | |
OLD | NEW |