- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / sync_file_system / sync_task_manager.h
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.
4
5 #ifndef CHROME_BROWSER_SYNC_FILE_SYSTEM_SYNC_TASK_MANAGER_H_
6 #define CHROME_BROWSER_SYNC_FILE_SYSTEM_SYNC_TASK_MANAGER_H_
7
8 #include <queue>
9 #include <vector>
10
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"
18
19 namespace tracked_objects {
20 class Location;
21 }
22
23 namespace sync_file_system {
24
25 class SyncTaskManager
26     : public base::NonThreadSafe,
27       public base::SupportsWeakPtr<SyncTaskManager> {
28  public:
29   class TaskToken;
30   typedef base::Callback<void(const SyncStatusCallback& callback)> Task;
31
32   enum Priority {
33     PRIORITY_LOW,
34     PRIORITY_MED,
35     PRIORITY_HIGH,
36   };
37
38   class Client {
39    public:
40     virtual ~Client() {}
41
42     // Called when the manager is idle.
43     virtual void MaybeScheduleNextTask() = 0;
44
45     // Called when the manager is notified a task is done.
46     virtual void NotifyLastOperationStatus(
47         SyncStatusCode last_operation_status) = 0;
48   };
49
50   explicit SyncTaskManager(base::WeakPtr<Client> client);
51   virtual ~SyncTaskManager();
52
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);
57
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);
63
64   // Schedules a task at the given priority.
65   void ScheduleTaskAtPriority(const Task& task,
66                               Priority priority,
67                               const SyncStatusCallback& callback);
68
69   // Runs the posted task only when we're idle.  Returns true if tha task is
70   // scheduled.
71   bool ScheduleTaskIfIdle(const Task& task);
72   bool ScheduleSyncTaskIfIdle(scoped_ptr<SyncTask> task);
73
74   void NotifyTaskDone(scoped_ptr<TaskToken> token,
75                       SyncStatusCode status);
76
77  private:
78   struct PendingTask {
79     base::Closure task;
80     Priority priority;
81     int64 seq;
82
83     PendingTask();
84     PendingTask(const base::Closure& task, Priority pri, int seq);
85     ~PendingTask();
86   };
87
88   struct PendingTaskComparator {
89     bool operator()(const PendingTask& left,
90                     const PendingTask& right) const;
91   };
92
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);
95
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);
101
102   void PushPendingTask(const base::Closure& closure, Priority priority);
103
104   base::WeakPtr<Client> client_;
105
106   SyncStatusCode last_operation_status_;
107   scoped_ptr<SyncTask> running_task_;
108   SyncStatusCallback current_callback_;
109
110   std::priority_queue<PendingTask, std::vector<PendingTask>,
111                       PendingTaskComparator> pending_tasks_;
112   int64 pending_task_seq_;
113
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_;
120
121   DISALLOW_COPY_AND_ASSIGN(SyncTaskManager);
122 };
123
124 }  // namespace sync_file_system
125
126 #endif  // CHROME_BROWSER_SYNC_FILE_SYSTEM_SYNC_TASK_MANAGER_H_