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