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