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_
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"
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
{
33 // Represents an operation task (which usually wraps one FileSystemOperation).
39 // Only one of Run() or Cancel() is called.
40 virtual void Run() = 0;
41 virtual void Cancel() = 0;
44 // This is never called after Run() or Cancel() is called.
45 virtual const std::vector
<fileapi::FileSystemURL
>& target_paths() const = 0;
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_
; }
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_