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 <set> 9 #include <string> 10 #include <vector> 11 12 #include "base/memory/scoped_ptr.h" 13 #include "base/memory/weak_ptr.h" 14 #include "base/time/time.h" 15 #include "chrome/browser/chromeos/drive/file_errors.h" 16 17 namespace base { 18 class SequencedTaskRunner; 19 } 20 21 namespace drive { 22 23 class FileCacheEntry; 24 class JobScheduler; 25 class ResourceEntry; 26 27 namespace file_system { 28 class DownloadOperation; 29 class OperationObserver; 30 class UpdateOperation; 31 } 32 33 namespace internal { 34 35 class FileCache; 36 class ResourceMetadata; 37 38 // The SyncClient is used to synchronize pinned files on Drive and the 39 // cache on the local drive. 40 // 41 // If the user logs out before fetching of the pinned files is complete, this 42 // client resumes fetching operations next time the user logs in, based on 43 // the states left in the cache. 44 class SyncClient { 45 public: 46 // Types of sync tasks. 47 enum SyncType { 48 FETCH, // Fetch a file from the Drive server. 49 UPLOAD, // Upload a file to the Drive server. 50 UPLOAD_NO_CONTENT_CHECK, // Upload a file without checking if the file is 51 // really modified. This type is used for upload 52 // retry tasks that should have already run the 53 // check at least once. 54 }; 55 56 SyncClient(base::SequencedTaskRunner* blocking_task_runner, 57 file_system::OperationObserver* observer, 58 JobScheduler* scheduler, 59 ResourceMetadata* metadata, 60 FileCache* cache, 61 const base::FilePath& temporary_file_directory); 62 virtual ~SyncClient(); 63 64 // Adds a fetch task to the queue. 65 void AddFetchTask(const std::string& resource_id); 66 67 // Removes a fetch task from the queue. 68 void RemoveFetchTask(const std::string& resource_id); 69 70 // Adds an upload task to the queue. 71 void AddUploadTask(const std::string& resource_id); 72 73 // Starts processing the backlog (i.e. pinned-but-not-filed files and 74 // dirty-but-not-uploaded files). Kicks off retrieval of the resource 75 // IDs of these files, and then starts the sync loop. 76 void StartProcessingBacklog(); 77 78 // Starts checking the existing pinned files to see if these are 79 // up-to-date. If stale files are detected, the resource IDs of these files 80 // are added to the queue and the sync loop is started. 81 void StartCheckingExistingPinnedFiles(); 82 83 // Sets a delay for testing. 84 void set_delay_for_testing(const base::TimeDelta& delay) { 85 delay_ = delay; 86 } 87 88 // Starts the sync loop if it's not running. 89 void StartSyncLoop(); 90 91 private: 92 // Adds the given task to the queue. If the same task is queued, remove the 93 // existing one, and adds a new one to the end of the queue. 94 void AddTaskToQueue(SyncType type, 95 const std::string& resource_id, 96 const base::TimeDelta& delay); 97 98 // Called when a task is ready to be added to the queue. 99 void StartTask(SyncType type, const std::string& resource_id); 100 101 // Called when the resource IDs of files in the backlog are obtained. 102 void OnGetResourceIdsOfBacklog(const std::vector<std::string>* to_fetch, 103 const std::vector<std::string>* to_upload); 104 105 // Called when the resource ID of a pinned file is obtained. 106 void OnGetResourceIdOfExistingPinnedFile(const std::string& resource_id, 107 const FileCacheEntry& cache_entry); 108 109 // Called when a file entry is obtained. 110 void OnGetResourceEntryById(const std::string& resource_id, 111 const FileCacheEntry& cache_entry, 112 FileError error, 113 scoped_ptr<ResourceEntry> entry); 114 115 // Called when a cache entry is obtained. 116 void OnGetCacheEntry(const std::string& resource_id, 117 const std::string& latest_md5, 118 bool success, 119 const FileCacheEntry& cache_entry); 120 121 // Called when an existing cache entry and the local files are removed. 122 void OnRemove(const std::string& resource_id, FileError error); 123 124 // Called when a file is pinned. 125 void OnPinned(const std::string& resource_id, FileError error); 126 127 // Called when the file for |resource_id| is fetched. 128 // Calls DoSyncLoop() to go back to the sync loop. 129 void OnFetchFileComplete(const std::string& resource_id, 130 FileError error, 131 const base::FilePath& local_path, 132 scoped_ptr<ResourceEntry> entry); 133 134 // Called when the file for |resource_id| is uploaded. 135 // Calls DoSyncLoop() to go back to the sync loop. 136 void OnUploadFileComplete(const std::string& resource_id, 137 FileError error); 138 139 ResourceMetadata* metadata_; 140 FileCache* cache_; 141 142 // Used to fetch pinned files. 143 scoped_ptr<file_system::DownloadOperation> download_operation_; 144 145 // Used to upload committed files. 146 scoped_ptr<file_system::UpdateOperation> update_operation_; 147 148 // List of the resource ids of resources which have a fetch task created. 149 std::set<std::string> fetch_list_; 150 151 // List of the resource ids of resources which have a upload task created. 152 std::set<std::string> upload_list_; 153 154 // Fetch tasks which have been created, but not started yet. If they are 155 // removed before starting, they will be cancelled. 156 std::set<std::string> pending_fetch_list_; 157 158 // The delay is used for delaying processing tasks in AddTaskToQueue(). 159 base::TimeDelta delay_; 160 161 // The delay is used for delaying retry of tasks on server errors. 162 base::TimeDelta long_delay_; 163 164 // Note: This should remain the last member so it'll be destroyed and 165 // invalidate its weak pointers before any other members are destroyed. 166 base::WeakPtrFactory<SyncClient> weak_ptr_factory_; 167 168 DISALLOW_COPY_AND_ASSIGN(SyncClient); 169 }; 170 171 } // namespace internal 172 } // namespace drive 173 174 #endif // CHROME_BROWSER_CHROMEOS_DRIVE_SYNC_CLIENT_H_ 175