OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 #ifndef WEBKIT_FILEAPI_SYNCABLE_SYNCABLE_FILE_OPERATION_RUNNER_H_ | 5 #ifndef WEBKIT_FILEAPI_SYNCABLE_SYNCABLE_FILE_OPERATION_RUNNER_H_ |
6 #define WEBKIT_FILEAPI_SYNCABLE_SYNCABLE_FILE_OPERATION_RUNNER_H_ | 6 #define WEBKIT_FILEAPI_SYNCABLE_SYNCABLE_FILE_OPERATION_RUNNER_H_ |
7 | 7 |
8 #include <list> | 8 #include <list> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
12 #include "base/callback.h" | 12 #include "base/callback.h" |
13 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
14 #include "base/memory/weak_ptr.h" | 14 #include "base/memory/weak_ptr.h" |
15 #include "base/threading/non_thread_safe.h" | 15 #include "base/threading/non_thread_safe.h" |
16 #include "webkit/fileapi/file_system_url.h" | 16 #include "webkit/fileapi/file_system_url.h" |
17 #include "webkit/fileapi/syncable/local_file_sync_status.h" | |
17 #include "webkit/storage/webkit_storage_export.h" | 18 #include "webkit/storage/webkit_storage_export.h" |
18 | 19 |
19 namespace fileapi { | 20 namespace fileapi { |
20 | 21 |
21 class FileSystemURL; | 22 class FileSystemURL; |
22 class LocalFileSyncStatus; | |
23 | 23 |
24 // This class must run only on IO thread. | 24 // This class must run only on IO thread. |
25 // Owned by LocalFileSyncContext. | 25 // Owned by LocalFileSyncContext. |
26 class WEBKIT_STORAGE_EXPORT SyncableFileOperationRunner | 26 class WEBKIT_STORAGE_EXPORT SyncableFileOperationRunner |
27 : public base::NonThreadSafe, | 27 : public base::NonThreadSafe, |
28 public base::SupportsWeakPtr<SyncableFileOperationRunner> { | 28 public base::SupportsWeakPtr<SyncableFileOperationRunner>, |
29 public LocalFileSyncStatus::Observer { | |
29 public: | 30 public: |
30 // Represents an operation task (which usually wraps one FileSystemOperation). | 31 // Represents an operation task (which usually wraps one FileSystemOperation). |
31 class Task { | 32 class Task { |
32 public: | 33 public: |
33 Task() {} | 34 Task() {} |
34 virtual ~Task() {} | 35 virtual ~Task() {} |
35 | 36 |
36 // Only one of Run() or Cancel() is called. | 37 // Only one of Run() or Cancel() is called. |
37 virtual void Run() = 0; | 38 virtual void Run() = 0; |
38 virtual void Cancel() = 0; | 39 virtual void Cancel() = 0; |
39 | 40 |
40 protected: | 41 protected: |
41 // This is never called after Run() or Cancel() is called. | 42 // This is never called after Run() or Cancel() is called. |
42 virtual const std::vector<FileSystemURL>& target_paths() const = 0; | 43 virtual const std::vector<FileSystemURL>& target_paths() const = 0; |
43 | 44 |
44 private: | 45 private: |
45 friend class SyncableFileOperationRunner; | 46 friend class SyncableFileOperationRunner; |
46 bool IsRunnable(LocalFileSyncStatus* status) const; | 47 bool IsRunnable(LocalFileSyncStatus* status) const; |
47 void Start(LocalFileSyncStatus* status); | 48 void Start(LocalFileSyncStatus* status); |
48 static void CancelAndDelete(Task* task); | 49 static void CancelAndDelete(Task* task); |
49 | 50 |
50 DISALLOW_COPY_AND_ASSIGN(Task); | 51 DISALLOW_COPY_AND_ASSIGN(Task); |
51 }; | 52 }; |
52 | 53 |
53 SyncableFileOperationRunner(); | 54 SyncableFileOperationRunner(size_t max_inflight_tasks, |
54 ~SyncableFileOperationRunner(); | 55 LocalFileSyncStatus* sync_status); |
56 virtual ~SyncableFileOperationRunner(); | |
57 | |
58 // LocalFileSyncStatus::Observer overrides. | |
59 virtual void OnSyncEnabled(const FileSystemURL& url) OVERRIDE; | |
60 virtual void OnWriteEnabled(const FileSystemURL& url) OVERRIDE; | |
55 | 61 |
56 // Runs the given |task| if no sync operation is running on any of | 62 // Runs the given |task| if no sync operation is running on any of |
57 // its target_paths(). This also runs pending operations that have become | 63 // its target_paths(). This also runs pending tasks that have become |
58 // runnable (before running the given operation). | 64 // runnable (before running the given operation). |
59 // If there're ongoing sync operations on the target_paths this method | 65 // If there're ongoing sync tasks on the target_paths this method |
60 // just queues up the |task|. | 66 // just queues up the |task|. |
61 // Pending operations are cancelled when this class is destructed. | 67 // Pending tasks are cancelled when this class is destructed. |
62 void PostOperationTask(scoped_ptr<Task> task); | 68 void PostOperationTask(scoped_ptr<Task> task); |
63 | 69 |
64 // Runs a next runnable task (if there's any). | 70 // Runs a next runnable task (if there's any). |
65 void RunNextRunnableTask(); | 71 void RunNextRunnableTask(); |
66 | 72 |
67 // Called when an operation is completed. This will make |target_paths| | 73 // Called when an operation is completed. This will make |target_paths| |
68 // writable and may start a next runnable task. | 74 // writable and may start a next runnable task. |
69 void OnOperationCompleted(const std::vector<FileSystemURL>& target_paths); | 75 void OnOperationCompleted(const std::vector<FileSystemURL>& target_paths); |
70 | 76 |
71 // For syncable file systems. | 77 LocalFileSyncStatus* sync_status() const { return sync_status_; } |
72 LocalFileSyncStatus* sync_status() const { return sync_status_.get(); } | 78 |
79 size_t num_pending_tasks() const { return pending_tasks_.size(); } | |
tzik
2012/10/23 06:08:20
Could you s/size_t/int64/ and add static_cast?
kinuko
2012/10/23 07:54:03
Done.
| |
80 size_t num_inflight_tasks() const { return num_inflight_tasks_; } | |
73 | 81 |
74 private: | 82 private: |
75 // Keeps track of the writing/syncing status. | 83 // Returns true if we should start more tasks. |
76 scoped_ptr<LocalFileSyncStatus> sync_status_; | 84 bool ShouldStartMoreTasks() const; |
77 | 85 |
78 std::list<Task*> pending_operations_; | 86 // Keeps track of the writing/syncing status. Not owned. |
87 LocalFileSyncStatus* sync_status_; | |
88 | |
89 std::list<Task*> pending_tasks_; | |
90 | |
91 const size_t max_inflight_tasks_; | |
tzik
2012/10/23 06:08:20
ditto
kinuko
2012/10/23 07:54:03
Done.
| |
92 size_t num_inflight_tasks_; | |
79 | 93 |
80 DISALLOW_COPY_AND_ASSIGN(SyncableFileOperationRunner); | 94 DISALLOW_COPY_AND_ASSIGN(SyncableFileOperationRunner); |
81 }; | 95 }; |
82 | 96 |
83 } // namespace fileapi | 97 } // namespace fileapi |
84 | 98 |
85 #endif // WEBKIT_FILEAPI_SYNCABLE_SYNCABLE_FILE_OPERATION_RUNNER_H_ | 99 #endif // WEBKIT_FILEAPI_SYNCABLE_SYNCABLE_FILE_OPERATION_RUNNER_H_ |
OLD | NEW |