Home | History | Annotate | Download | only in drive
      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.
      4 
      5 #ifndef CHROME_BROWSER_CHROMEOS_DRIVE_SYNC_CLIENT_H_
      6 #define CHROME_BROWSER_CHROMEOS_DRIVE_SYNC_CLIENT_H_
      7 
      8 #include <map>
      9 #include <string>
     10 #include <vector>
     11 
     12 #include "base/callback.h"
     13 #include "base/memory/scoped_ptr.h"
     14 #include "base/memory/weak_ptr.h"
     15 #include "base/time/time.h"
     16 #include "chrome/browser/chromeos/drive/file_errors.h"
     17 #include "chrome/browser/chromeos/drive/resource_metadata.h"
     18 
     19 namespace base {
     20 class SequencedTaskRunner;
     21 }
     22 
     23 namespace drive {
     24 
     25 class FileCacheEntry;
     26 class JobScheduler;
     27 class ResourceEntry;
     28 struct ClientContext;
     29 
     30 namespace file_system {
     31 class DownloadOperation;
     32 class OperationObserver;
     33 }
     34 
     35 namespace internal {
     36 
     37 class ChangeListLoader;
     38 class EntryUpdatePerformer;
     39 class FileCache;
     40 class LoaderController;
     41 class ResourceMetadata;
     42 
     43 // The SyncClient is used to synchronize pinned files on Drive and the
     44 // cache on the local drive.
     45 //
     46 // If the user logs out before fetching of the pinned files is complete, this
     47 // client resumes fetching operations next time the user logs in, based on
     48 // the states left in the cache.
     49 class SyncClient {
     50  public:
     51   SyncClient(base::SequencedTaskRunner* blocking_task_runner,
     52              file_system::OperationObserver* observer,
     53              JobScheduler* scheduler,
     54              ResourceMetadata* metadata,
     55              FileCache* cache,
     56              LoaderController* loader_controller,
     57              const base::FilePath& temporary_file_directory);
     58   virtual ~SyncClient();
     59 
     60   // Adds a fetch task.
     61   void AddFetchTask(const std::string& local_id);
     62 
     63   // Removes a fetch task.
     64   void RemoveFetchTask(const std::string& local_id);
     65 
     66   // Adds a update task.
     67   void AddUpdateTask(const ClientContext& context, const std::string& local_id);
     68 
     69   // Starts processing the backlog (i.e. pinned-but-not-filed files and
     70   // dirty-but-not-uploaded files). Kicks off retrieval of the local
     71   // IDs of these files, and then starts the sync loop.
     72   void StartProcessingBacklog();
     73 
     74   // Starts checking the existing pinned files to see if these are
     75   // up-to-date. If stale files are detected, the local IDs of these files
     76   // are added and the sync loop is started.
     77   void StartCheckingExistingPinnedFiles();
     78 
     79   // Sets a delay for testing.
     80   void set_delay_for_testing(const base::TimeDelta& delay) {
     81     delay_ = delay;
     82   }
     83 
     84   // Starts the sync loop if it's not running.
     85   void StartSyncLoop();
     86 
     87  private:
     88   // Types of sync tasks.
     89   enum SyncType {
     90     FETCH,  // Fetch a file from the Drive server.
     91     UPDATE,  // Updates an entry's metadata or content on the Drive server.
     92   };
     93 
     94   // States of sync tasks.
     95   enum SyncState {
     96     PENDING,
     97     RUNNING,
     98   };
     99 
    100   struct SyncTask {
    101     SyncTask();
    102     ~SyncTask();
    103     SyncState state;
    104     base::Callback<base::Closure()> task;
    105     bool should_run_again;
    106     base::Closure cancel_closure;
    107   };
    108 
    109   typedef std::map<std::pair<SyncType, std::string>, SyncTask> SyncTasks;
    110 
    111   // Adds a FETCH task.
    112   void AddFetchTaskInternal(const std::string& local_id,
    113                             const base::TimeDelta& delay);
    114 
    115   // Adds a UPDATE task.
    116   void AddUpdateTaskInternal(const ClientContext& context,
    117                              const std::string& local_id,
    118                              const base::TimeDelta& delay);
    119 
    120   // Adds the given task. If the same task is found, does nothing.
    121   void AddTask(const SyncTasks::key_type& key,
    122                const SyncTask& task,
    123                const base::TimeDelta& delay);
    124 
    125   // Called when a task is ready to start.
    126   void StartTask(const SyncTasks::key_type& key);
    127 
    128   // Called when the local IDs of files in the backlog are obtained.
    129   void OnGetLocalIdsOfBacklog(const std::vector<std::string>* to_fetch,
    130                               const std::vector<std::string>* to_update);
    131 
    132   // Adds fetch tasks.
    133   void AddFetchTasks(const std::vector<std::string>* local_ids);
    134 
    135   // Erases the task and returns true if task is completed.
    136   bool OnTaskComplete(SyncType type, const std::string& local_id);
    137 
    138   // Called when the file for |local_id| is fetched.
    139   void OnFetchFileComplete(const std::string& local_id,
    140                            FileError error,
    141                            const base::FilePath& local_path,
    142                            scoped_ptr<ResourceEntry> entry);
    143 
    144   // Called when the entry is updated.
    145   void OnUpdateComplete(const std::string& local_id, FileError error);
    146 
    147   // Adds update tasks for |entries|.
    148   void AddChildUpdateTasks(const ResourceEntryVector* entries,
    149                            FileError error);
    150 
    151   scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_;
    152   file_system::OperationObserver* operation_observer_;
    153   ResourceMetadata* metadata_;
    154   FileCache* cache_;
    155 
    156   // Used to fetch pinned files.
    157   scoped_ptr<file_system::DownloadOperation> download_operation_;
    158 
    159   // Used to update entry metadata.
    160   scoped_ptr<EntryUpdatePerformer> entry_update_performer_;
    161 
    162   // Sync tasks to be processed.
    163   SyncTasks tasks_;
    164 
    165   // The delay is used for delaying processing tasks in AddTask().
    166   base::TimeDelta delay_;
    167 
    168   // The delay is used for delaying retry of tasks on server errors.
    169   base::TimeDelta long_delay_;
    170 
    171   // Note: This should remain the last member so it'll be destroyed and
    172   // invalidate its weak pointers before any other members are destroyed.
    173   base::WeakPtrFactory<SyncClient> weak_ptr_factory_;
    174 
    175   DISALLOW_COPY_AND_ASSIGN(SyncClient);
    176 };
    177 
    178 }  // namespace internal
    179 }  // namespace drive
    180 
    181 #endif  // CHROME_BROWSER_CHROMEOS_DRIVE_SYNC_CLIENT_H_
    182