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
3 // found in the LICENSE file.
5 #ifndef CHROME_BROWSER_SYNC_FILE_SYSTEM_SYNC_TASK_MANAGER_H_
6 #define CHROME_BROWSER_SYNC_FILE_SYSTEM_SYNC_TASK_MANAGER_H_
11 #include "base/callback.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/memory/weak_ptr.h"
14 #include "base/threading/non_thread_safe.h"
15 #include "chrome/browser/sync_file_system/sync_callbacks.h"
16 #include "chrome/browser/sync_file_system/sync_status_code.h"
17 #include "chrome/browser/sync_file_system/sync_task.h"
19 namespace tracked_objects {
23 namespace sync_file_system {
26 : public base::NonThreadSafe,
27 public base::SupportsWeakPtr<SyncTaskManager> {
30 typedef base::Callback<void(const SyncStatusCallback& callback)> Task;
42 // Called when the manager is idle.
43 virtual void MaybeScheduleNextTask() = 0;
45 // Called when the manager is notified a task is done.
46 virtual void NotifyLastOperationStatus(
47 SyncStatusCode last_operation_status) = 0;
50 explicit SyncTaskManager(base::WeakPtr<Client> client);
51 virtual ~SyncTaskManager();
53 // This needs to be called to start task scheduling.
54 // If |status| is not SYNC_STATUS_OK calling this may change the
55 // service status. This should not be called more than once.
56 void Initialize(SyncStatusCode status);
58 // Schedules a task at PRIORITY_MED.
59 void ScheduleTask(const Task& task,
60 const SyncStatusCallback& callback);
61 void ScheduleSyncTask(scoped_ptr<SyncTask> task,
62 const SyncStatusCallback& callback);
64 // Schedules a task at the given priority.
65 void ScheduleTaskAtPriority(const Task& task,
67 const SyncStatusCallback& callback);
69 // Runs the posted task only when we're idle. Returns true if tha task is
71 bool ScheduleTaskIfIdle(const Task& task);
72 bool ScheduleSyncTaskIfIdle(scoped_ptr<SyncTask> task);
74 void NotifyTaskDone(scoped_ptr<TaskToken> token,
75 SyncStatusCode status);
84 PendingTask(const base::Closure& task, Priority pri, int seq);
88 struct PendingTaskComparator {
89 bool operator()(const PendingTask& left,
90 const PendingTask& right) const;
93 // This should be called when an async task needs to get a task token.
94 scoped_ptr<TaskToken> GetToken(const tracked_objects::Location& from_here);
96 // Creates a completion callback that calls NotifyTaskDone.
97 // It is ok to give null |callback|.
98 SyncStatusCallback CreateCompletionCallback(
99 scoped_ptr<TaskToken> token,
100 const SyncStatusCallback& callback);
102 void PushPendingTask(const base::Closure& closure, Priority priority);
104 base::WeakPtr<Client> client_;
106 SyncStatusCode last_operation_status_;
107 scoped_ptr<SyncTask> running_task_;
108 SyncStatusCallback current_callback_;
110 std::priority_queue<PendingTask, std::vector<PendingTask>,
111 PendingTaskComparator> pending_tasks_;
112 int64 pending_task_seq_;
114 // Absence of |token_| implies a task is running. Incoming tasks should
115 // wait for the task to finish in |pending_tasks_| if |token_| is null.
116 // Each task must take TaskToken instance from |token_| and must hold it
117 // until it finished. And the task must return the instance through
118 // NotifyTaskDone when the task finished.
119 scoped_ptr<TaskToken> token_;
121 DISALLOW_COPY_AND_ASSIGN(SyncTaskManager);
124 } // namespace sync_file_system
126 #endif // CHROME_BROWSER_SYNC_FILE_SYSTEM_SYNC_TASK_MANAGER_H_