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