sync_client.h revision eb525c5499e34cc9c4b825d6d9e75bb07cc06ace
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 17namespace base { 18class SequencedTaskRunner; 19} 20 21namespace drive { 22 23class FileCacheEntry; 24class JobScheduler; 25class ResourceEntry; 26 27namespace file_system { 28class DownloadOperation; 29class OperationObserver; 30class UpdateOperation; 31} 32 33namespace internal { 34 35class FileCache; 36class 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. 44class 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 }; 51 52 SyncClient(base::SequencedTaskRunner* blocking_task_runner, 53 file_system::OperationObserver* observer, 54 JobScheduler* scheduler, 55 ResourceMetadata* metadata, 56 FileCache* cache, 57 const base::FilePath& temporary_file_directory); 58 virtual ~SyncClient(); 59 60 // Adds a fetch task to the queue. 61 void AddFetchTask(const std::string& resource_id); 62 63 // Removes a fetch task from the queue. 64 void RemoveFetchTask(const std::string& resource_id); 65 66 // Adds an upload task to the queue. 67 void AddUploadTask(const std::string& resource_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 resource 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 resource IDs of these files 76 // are added to the queue 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 // Adds the given task to the queue. If the same task is queued, remove the 89 // existing one, and adds a new one to the end of the queue. 90 void AddTaskToQueue(SyncType type, const std::string& resource_id); 91 92 // Called when a task is ready to be added to the queue. 93 void StartTask(SyncType type, const std::string& resource_id); 94 95 // Called when the resource IDs of files in the backlog are obtained. 96 void OnGetResourceIdsOfBacklog(const std::vector<std::string>* to_fetch, 97 const std::vector<std::string>* to_upload); 98 99 // Called when the resource ID of a pinned file is obtained. 100 void OnGetResourceIdOfExistingPinnedFile(const std::string& resource_id, 101 const FileCacheEntry& cache_entry); 102 103 // Called when a file entry is obtained. 104 void OnGetResourceEntryById(const std::string& resource_id, 105 const FileCacheEntry& cache_entry, 106 FileError error, 107 scoped_ptr<ResourceEntry> entry); 108 109 // Called when a cache entry is obtained. 110 void OnGetCacheEntry(const std::string& resource_id, 111 const std::string& latest_md5, 112 bool success, 113 const FileCacheEntry& cache_entry); 114 115 // Called when an existing cache entry and the local files are removed. 116 void OnRemove(const std::string& resource_id, FileError error); 117 118 // Called when a file is pinned. 119 void OnPinned(const std::string& resource_id, FileError error); 120 121 // Called when the file for |resource_id| is fetched. 122 // Calls DoSyncLoop() to go back to the sync loop. 123 void OnFetchFileComplete(const std::string& resource_id, 124 FileError error, 125 const base::FilePath& local_path, 126 scoped_ptr<ResourceEntry> entry); 127 128 // Called when the file for |resource_id| is uploaded. 129 // Calls DoSyncLoop() to go back to the sync loop. 130 void OnUploadFileComplete(const std::string& resource_id, 131 FileError error); 132 133 ResourceMetadata* metadata_; 134 FileCache* cache_; 135 136 // Used to fetch pinned files. 137 scoped_ptr<file_system::DownloadOperation> download_operation_; 138 139 // Used to upload committed files. 140 scoped_ptr<file_system::UpdateOperation> update_operation_; 141 142 // List of the resource ids of resources which have a fetch task created. 143 std::set<std::string> fetch_list_; 144 145 // List of the resource ids of resources which have a upload task created. 146 std::set<std::string> upload_list_; 147 148 // Fetch tasks which have been created, but not started yet. If they are 149 // removed before starting, they will be cancelled. 150 std::set<std::string> pending_fetch_list_; 151 152 // The delay is used for delaying processing SyncTasks in DoSyncLoop(). 153 base::TimeDelta delay_; 154 155 // Note: This should remain the last member so it'll be destroyed and 156 // invalidate its weak pointers before any other members are destroyed. 157 base::WeakPtrFactory<SyncClient> weak_ptr_factory_; 158 159 DISALLOW_COPY_AND_ASSIGN(SyncClient); 160}; 161 162} // namespace internal 163} // namespace drive 164 165#endif // CHROME_BROWSER_CHROMEOS_DRIVE_SYNC_CLIENT_H_ 166