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
,
48 bool last_operation_used_network
) = 0;
51 explicit SyncTaskManager(base::WeakPtr
<Client
> client
);
52 virtual ~SyncTaskManager();
54 // This needs to be called to start task scheduling.
55 // If |status| is not SYNC_STATUS_OK calling this may change the
56 // service status. This should not be called more than once.
57 void Initialize(SyncStatusCode status
);
59 // Schedules a task at PRIORITY_MED.
60 void ScheduleTask(const Task
& task
,
61 const SyncStatusCallback
& callback
);
62 void ScheduleSyncTask(scoped_ptr
<SyncTask
> task
,
63 const SyncStatusCallback
& callback
);
65 // Schedules a task at the given priority.
66 void ScheduleTaskAtPriority(const Task
& task
,
68 const SyncStatusCallback
& callback
);
69 void ScheduleSyncTaskAtPriority(scoped_ptr
<SyncTask
> task
,
71 const SyncStatusCallback
& callback
);
73 // Runs the posted task only when we're idle. Returns true if tha task is
75 bool ScheduleTaskIfIdle(const Task
& task
, const SyncStatusCallback
& callback
);
76 bool ScheduleSyncTaskIfIdle(scoped_ptr
<SyncTask
> task
,
77 const SyncStatusCallback
& callback
);
79 void NotifyTaskDone(scoped_ptr
<TaskToken
> token
,
80 SyncStatusCode status
);
89 PendingTask(const base::Closure
& task
, Priority pri
, int seq
);
93 struct PendingTaskComparator
{
94 bool operator()(const PendingTask
& left
,
95 const PendingTask
& right
) const;
98 // This should be called when an async task needs to get a task token.
99 scoped_ptr
<TaskToken
> GetToken(const tracked_objects::Location
& from_here
);
101 // Creates a completion callback that calls NotifyTaskDone.
102 // It is ok to give null |callback|.
103 SyncStatusCallback
CreateCompletionCallback(
104 scoped_ptr
<TaskToken
> token
,
105 const SyncStatusCallback
& callback
);
107 void PushPendingTask(const base::Closure
& closure
, Priority priority
);
109 base::WeakPtr
<Client
> client_
;
111 SyncStatusCode last_operation_status_
;
112 scoped_ptr
<SyncTask
> running_task_
;
113 SyncStatusCallback current_callback_
;
115 std::priority_queue
<PendingTask
, std::vector
<PendingTask
>,
116 PendingTaskComparator
> pending_tasks_
;
117 int64 pending_task_seq_
;
119 // Absence of |token_| implies a task is running. Incoming tasks should
120 // wait for the task to finish in |pending_tasks_| if |token_| is null.
121 // Each task must take TaskToken instance from |token_| and must hold it
122 // until it finished. And the task must return the instance through
123 // NotifyTaskDone when the task finished.
124 scoped_ptr
<TaskToken
> token_
;
126 DISALLOW_COPY_AND_ASSIGN(SyncTaskManager
);
129 } // namespace sync_file_system
131 #endif // CHROME_BROWSER_SYNC_FILE_SYSTEM_SYNC_TASK_MANAGER_H_