Home | History | Annotate | Download | only in sync_file_system
      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 <deque>
      9 
     10 #include "base/callback.h"
     11 #include "base/memory/scoped_ptr.h"
     12 #include "base/memory/weak_ptr.h"
     13 #include "base/threading/non_thread_safe.h"
     14 #include "chrome/browser/sync_file_system/sync_callbacks.h"
     15 #include "chrome/browser/sync_file_system/sync_status_code.h"
     16 
     17 class Profile;
     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   class Client {
     33    public:
     34     virtual ~Client() {}
     35 
     36     // Called when the manager is idle.
     37     virtual void MaybeScheduleNextTask() = 0;
     38 
     39     // Called when the manager is notified a task is done.
     40     virtual void NotifyLastOperationStatus(
     41         SyncStatusCode last_operation_status) = 0;
     42   };
     43 
     44   explicit SyncTaskManager(base::WeakPtr<Client> client);
     45   virtual ~SyncTaskManager();
     46 
     47   // This needs to be called to start task scheduling.
     48   // If |status| is not SYNC_STATUS_OK calling this may change the
     49   // service status. This should not be called more than once.
     50   void Initialize(SyncStatusCode status);
     51 
     52   void ScheduleTask(const Task& task,
     53                     const SyncStatusCallback& callback);
     54 
     55   // Runs the posted task only when we're idle.
     56   void ScheduleTaskIfIdle(const Task& task);
     57 
     58   void NotifyTaskDone(scoped_ptr<TaskToken> token,
     59                       const SyncStatusCallback& callback,
     60                       SyncStatusCode status);
     61 
     62  private:
     63   // This should be called when an async task needs to get a task token.
     64   scoped_ptr<TaskToken> GetToken(const tracked_objects::Location& from_here);
     65 
     66   // Creates a completion callback that calls NotifyTaskDone.
     67   // It is ok to give null |callback|.
     68   SyncStatusCallback CreateCompletionCallback(
     69       scoped_ptr<TaskToken> token,
     70       const SyncStatusCallback& callback);
     71 
     72   base::WeakPtr<Client> client_;
     73 
     74   SyncStatusCode last_operation_status_;
     75   std::deque<base::Closure> pending_tasks_;
     76 
     77   // Absence of |token_| implies a task is running. Incoming tasks should
     78   // wait for the task to finish in |pending_tasks_| if |token_| is null.
     79   // Each task must take TaskToken instance from |token_| and must hold it
     80   // until it finished. And the task must return the instance through
     81   // NotifyTaskDone when the task finished.
     82   scoped_ptr<TaskToken> token_;
     83 
     84   DISALLOW_COPY_AND_ASSIGN(SyncTaskManager);
     85 };
     86 
     87 }  // namespace sync_file_system
     88 
     89 #endif  // CHROME_BROWSER_SYNC_FILE_SYSTEM_SYNC_TASK_MANAGER_H_
     90