local_file_change_tracker.h revision d0247b1b59f9c528cb6df88b4f2b9afaf80d181e
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 23namespace base { 24class SequencedTaskRunner; 25} 26 27namespace fileapi { 28class FileSystemContext; 29class FileSystemURL; 30} 31 32namespace 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. 37class 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 // Called by FileSyncService at the startup time to restore last dirty changes 90 // left after the last shutdown (if any). 91 SyncStatusCode Initialize(fileapi::FileSystemContext* file_system_context); 92 93 // This method is (exceptionally) thread-safe. 94 int64 num_changes() const { 95 base::AutoLock lock(num_changes_lock_); 96 return num_changes_; 97 } 98 99 void UpdateNumChanges(); 100 101 private: 102 class TrackerDB; 103 friend class CannedSyncableFileSystem; 104 friend class LocalFileChangeTrackerTest; 105 friend class LocalFileSyncContext; 106 friend class LocalFileSyncContextTest; 107 friend class SyncableFileSystemTest; 108 109 struct ChangeInfo { 110 ChangeInfo(); 111 ~ChangeInfo(); 112 FileChangeList change_list; 113 int64 change_seq; 114 }; 115 116 typedef std::map<fileapi::FileSystemURL, ChangeInfo, 117 fileapi::FileSystemURL::Comparator> 118 FileChangeMap; 119 typedef std::map<int64, fileapi::FileSystemURL> ChangeSeqMap; 120 121 // This does mostly same as calling GetNextChangedURLs with max_url=0 122 // except that it returns urls in set rather than in deque. 123 // Used only in testings. 124 void GetAllChangedURLs(fileapi::FileSystemURLSet* urls); 125 126 // Used only in testings. 127 void DropAllChanges(); 128 129 // Database related methods. 130 SyncStatusCode MarkDirtyOnDatabase(const fileapi::FileSystemURL& url); 131 SyncStatusCode ClearDirtyOnDatabase(const fileapi::FileSystemURL& url); 132 133 SyncStatusCode CollectLastDirtyChanges( 134 fileapi::FileSystemContext* file_system_context); 135 void RecordChange(const fileapi::FileSystemURL& url, 136 const FileChange& change); 137 138 static void RecordChangeToChangeMaps(const fileapi::FileSystemURL& url, 139 const FileChange& change, 140 int change_seq, 141 FileChangeMap* changes, 142 ChangeSeqMap* change_seqs); 143 144 bool initialized_; 145 146 scoped_refptr<base::SequencedTaskRunner> file_task_runner_; 147 148 FileChangeMap changes_; 149 ChangeSeqMap change_seqs_; 150 151 // For mirrors. 152 FileChangeMap mirror_changes_; 153 154 scoped_ptr<TrackerDB> tracker_db_; 155 156 // Change sequence number. Briefly gives a hint about the order of changes, 157 // but they are updated when a new change comes on the same file (as 158 // well as Drive's changestamps). 159 int64 current_change_seq_; 160 161 // This can be accessed on any threads (with num_changes_lock_). 162 int64 num_changes_; 163 mutable base::Lock num_changes_lock_; 164 165 DISALLOW_COPY_AND_ASSIGN(LocalFileChangeTracker); 166}; 167 168} // namespace sync_file_system 169 170#endif // CHROME_BROWSER_SYNC_FILE_SYSTEM_LOCAL_LOCAL_FILE_CHANGE_TRACKER_H_ 171