1 // Copyright 2013 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_SYNC_FILE_SYSTEM_LOCAL_LOCAL_FILE_CHANGE_TRACKER_H_ 6 #define CHROME_BROWSER_SYNC_FILE_SYSTEM_LOCAL_LOCAL_FILE_CHANGE_TRACKER_H_ 7 8 #include <deque> 9 #include <map> 10 #include <string> 11 12 #include "base/basictypes.h" 13 #include "base/compiler_specific.h" 14 #include "base/files/file_path.h" 15 #include "base/memory/ref_counted.h" 16 #include "base/memory/scoped_ptr.h" 17 #include "base/synchronization/lock.h" 18 #include "chrome/browser/sync_file_system/file_change.h" 19 #include "chrome/browser/sync_file_system/sync_status_code.h" 20 #include "webkit/browser/fileapi/file_observers.h" 21 #include "webkit/browser/fileapi/file_system_url.h" 22 23 namespace base { 24 class SequencedTaskRunner; 25 } 26 27 namespace fileapi { 28 class FileSystemContext; 29 class FileSystemURL; 30 } 31 32 namespace sync_file_system { 33 34 // Tracks local file changes for cloud-backed file systems. 35 // All methods must be called on the file_task_runner given to the constructor. 36 // Owned by FileSystemContext. 37 class LocalFileChangeTracker 38 : public fileapi::FileUpdateObserver, 39 public fileapi::FileChangeObserver { 40 public: 41 // |file_task_runner| must be the one where the observee file operations run. 42 // (So that we can make sure DB operations are done before actual update 43 // happens) 44 LocalFileChangeTracker(const base::FilePath& base_path, 45 base::SequencedTaskRunner* file_task_runner); 46 virtual ~LocalFileChangeTracker(); 47 48 // FileUpdateObserver overrides. 49 virtual void OnStartUpdate(const fileapi::FileSystemURL& url) OVERRIDE; 50 virtual void OnUpdate( 51 const fileapi::FileSystemURL& url, int64 delta) OVERRIDE {} 52 virtual void OnEndUpdate(const fileapi::FileSystemURL& url) OVERRIDE; 53 54 // FileChangeObserver overrides. 55 virtual void OnCreateFile(const fileapi::FileSystemURL& url) OVERRIDE; 56 virtual void OnCreateFileFrom(const fileapi::FileSystemURL& url, 57 const fileapi::FileSystemURL& src) OVERRIDE; 58 virtual void OnRemoveFile(const fileapi::FileSystemURL& url) OVERRIDE; 59 virtual void OnModifyFile(const fileapi::FileSystemURL& url) OVERRIDE; 60 virtual void OnCreateDirectory(const fileapi::FileSystemURL& url) OVERRIDE; 61 virtual void OnRemoveDirectory(const fileapi::FileSystemURL& url) OVERRIDE; 62 63 // Retrieves an array of |url| which have more than one pending changes. 64 // If |max_urls| is non-zero (recommended in production code) this 65 // returns URLs up to the number from the ones that have smallest 66 // change_seq numbers (i.e. older changes). 67 void GetNextChangedURLs(std::deque<fileapi::FileSystemURL>* urls, 68 int max_urls); 69 70 // Returns all changes recorded for the given |url|. 71 // This should be called after writing is disabled. 72 void GetChangesForURL(const fileapi::FileSystemURL& url, 73 FileChangeList* changes); 74 75 // Clears the pending changes recorded in this tracker for |url|. 76 void ClearChangesForURL(const fileapi::FileSystemURL& url); 77 78 // Creates a fresh (empty) in-memory record for |url|. 79 // Note that new changes are recorded to the mirror too. 80 void CreateFreshMirrorForURL(const fileapi::FileSystemURL& url); 81 82 // Removes a mirror for |url|, and commits the change status to database. 83 void RemoveMirrorAndCommitChangesForURL(const fileapi::FileSystemURL& url); 84 85 // Resets the changes to the ones recorded in mirror for |url|, and 86 // commits the updated change status to database. 87 void ResetToMirrorAndCommitChangesForURL(const fileapi::FileSystemURL& url); 88 89 // Re-insert changes for the file with newer (bigger) sequence numbers, 90 // so that they won't be fetched by GetChangesForURL() soon. This could be 91 // useful for changes that have been failed to apply but would need to be 92 // retried again later. 93 void DemoteChangesForURL(const fileapi::FileSystemURL& url); 94 95 // Called by FileSyncService at the startup time to restore last dirty changes 96 // left after the last shutdown (if any). 97 SyncStatusCode Initialize(fileapi::FileSystemContext* file_system_context); 98 99 // Resets all the changes recorded for the given |origin| and |type|. 100 // TODO(kinuko,nhiroki): Ideally this should be automatically called in 101 // DeleteFileSystem via QuotaUtil::DeleteOriginDataOnFileThread. 102 void ResetForFileSystem(const GURL& origin, fileapi::FileSystemType type); 103 104 // This method is (exceptionally) thread-safe. 105 int64 num_changes() const { 106 base::AutoLock lock(num_changes_lock_); 107 return num_changes_; 108 } 109 110 private: 111 class TrackerDB; 112 friend class CannedSyncableFileSystem; 113 friend class LocalFileChangeTrackerTest; 114 friend class LocalFileSyncContext; 115 friend class LocalFileSyncContextTest; 116 friend class SyncableFileSystemTest; 117 118 struct ChangeInfo { 119 ChangeInfo(); 120 ~ChangeInfo(); 121 FileChangeList change_list; 122 int64 change_seq; 123 }; 124 125 typedef std::map<fileapi::FileSystemURL, ChangeInfo, 126 fileapi::FileSystemURL::Comparator> 127 FileChangeMap; 128 typedef std::map<int64, fileapi::FileSystemURL> ChangeSeqMap; 129 130 void UpdateNumChanges(); 131 132 // This does mostly same as calling GetNextChangedURLs with max_url=0 133 // except that it returns urls in set rather than in deque. 134 // Used only in testings. 135 void GetAllChangedURLs(fileapi::FileSystemURLSet* urls); 136 137 // Used only in testings. 138 void DropAllChanges(); 139 140 // Database related methods. 141 SyncStatusCode MarkDirtyOnDatabase(const fileapi::FileSystemURL& url); 142 SyncStatusCode ClearDirtyOnDatabase(const fileapi::FileSystemURL& url); 143 144 SyncStatusCode CollectLastDirtyChanges( 145 fileapi::FileSystemContext* file_system_context); 146 void RecordChange(const fileapi::FileSystemURL& url, 147 const FileChange& change); 148 149 static void RecordChangeToChangeMaps(const fileapi::FileSystemURL& url, 150 const FileChange& change, 151 int change_seq, 152 FileChangeMap* changes, 153 ChangeSeqMap* change_seqs); 154 155 bool initialized_; 156 157 scoped_refptr<base::SequencedTaskRunner> file_task_runner_; 158 159 FileChangeMap changes_; 160 ChangeSeqMap change_seqs_; 161 162 // For mirrors. 163 FileChangeMap mirror_changes_; 164 165 scoped_ptr<TrackerDB> tracker_db_; 166 167 // Change sequence number. Briefly gives a hint about the order of changes, 168 // but they are updated when a new change comes on the same file (as 169 // well as Drive's changestamps). 170 int64 current_change_seq_; 171 172 // This can be accessed on any threads (with num_changes_lock_). 173 int64 num_changes_; 174 mutable base::Lock num_changes_lock_; 175 176 DISALLOW_COPY_AND_ASSIGN(LocalFileChangeTracker); 177 }; 178 179 } // namespace sync_file_system 180 181 #endif // CHROME_BROWSER_SYNC_FILE_SYSTEM_LOCAL_LOCAL_FILE_CHANGE_TRACKER_H_ 182