| OLD | NEW |
| (Empty) |
| 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 WEBKIT_FILEAPI_SYNCABLE_LOCAL_FILE_CHANGE_TRACKER_H_ | |
| 6 #define WEBKIT_FILEAPI_SYNCABLE_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 "webkit/fileapi/file_observers.h" | |
| 19 #include "webkit/fileapi/file_system_url.h" | |
| 20 #include "webkit/fileapi/syncable/file_change.h" | |
| 21 #include "webkit/fileapi/syncable/sync_status_code.h" | |
| 22 #include "webkit/storage/webkit_storage_export.h" | |
| 23 | |
| 24 namespace base { | |
| 25 class SequencedTaskRunner; | |
| 26 } | |
| 27 | |
| 28 namespace fileapi { | |
| 29 class FileSystemContext; | |
| 30 class FileSystemURL; | |
| 31 } | |
| 32 | |
| 33 namespace sync_file_system { | |
| 34 | |
| 35 // Tracks local file changes for cloud-backed file systems. | |
| 36 // All methods must be called on the file_task_runner given to the constructor. | |
| 37 // Owned by FileSystemContext. | |
| 38 class WEBKIT_STORAGE_EXPORT LocalFileChangeTracker | |
| 39 : public fileapi::FileUpdateObserver, | |
| 40 public fileapi::FileChangeObserver { | |
| 41 public: | |
| 42 // |file_task_runner| must be the one where the observee file operations run. | |
| 43 // (So that we can make sure DB operations are done before actual update | |
| 44 // happens) | |
| 45 LocalFileChangeTracker(const base::FilePath& base_path, | |
| 46 base::SequencedTaskRunner* file_task_runner); | |
| 47 virtual ~LocalFileChangeTracker(); | |
| 48 | |
| 49 // FileUpdateObserver overrides. | |
| 50 virtual void OnStartUpdate(const fileapi::FileSystemURL& url) OVERRIDE; | |
| 51 virtual void OnUpdate( | |
| 52 const fileapi::FileSystemURL& url, int64 delta) OVERRIDE {} | |
| 53 virtual void OnEndUpdate(const fileapi::FileSystemURL& url) OVERRIDE; | |
| 54 | |
| 55 // FileChangeObserver overrides. | |
| 56 virtual void OnCreateFile(const fileapi::FileSystemURL& url) OVERRIDE; | |
| 57 virtual void OnCreateFileFrom(const fileapi::FileSystemURL& url, | |
| 58 const fileapi::FileSystemURL& src) OVERRIDE; | |
| 59 virtual void OnRemoveFile(const fileapi::FileSystemURL& url) OVERRIDE; | |
| 60 virtual void OnModifyFile(const fileapi::FileSystemURL& url) OVERRIDE; | |
| 61 virtual void OnCreateDirectory(const fileapi::FileSystemURL& url) OVERRIDE; | |
| 62 virtual void OnRemoveDirectory(const fileapi::FileSystemURL& url) OVERRIDE; | |
| 63 | |
| 64 // Retrieves an array of |url| which have more than one pending changes. | |
| 65 // If |max_urls| is non-zero (recommended in production code) this | |
| 66 // returns URLs up to the number from the ones that have smallest | |
| 67 // change_seq numbers (i.e. older changes). | |
| 68 void GetNextChangedURLs(std::deque<fileapi::FileSystemURL>* urls, | |
| 69 int max_urls); | |
| 70 | |
| 71 // Returns all changes recorded for the given |url|. | |
| 72 // This should be called after writing is disabled. | |
| 73 void GetChangesForURL(const fileapi::FileSystemURL& url, | |
| 74 FileChangeList* changes); | |
| 75 | |
| 76 // Clears the pending changes recorded in this tracker for |url|. | |
| 77 void ClearChangesForURL(const fileapi::FileSystemURL& url); | |
| 78 | |
| 79 // Called by FileSyncService at the startup time to restore last dirty changes | |
| 80 // left after the last shutdown (if any). | |
| 81 SyncStatusCode Initialize(fileapi::FileSystemContext* file_system_context); | |
| 82 | |
| 83 // This method is (exceptionally) thread-safe. | |
| 84 int64 num_changes() const { | |
| 85 base::AutoLock lock(num_changes_lock_); | |
| 86 return num_changes_; | |
| 87 } | |
| 88 | |
| 89 void UpdateNumChanges(); | |
| 90 | |
| 91 private: | |
| 92 class TrackerDB; | |
| 93 friend class CannedSyncableFileSystem; | |
| 94 friend class LocalFileChangeTrackerTest; | |
| 95 friend class LocalFileSyncContext; | |
| 96 friend class SyncableFileSystemTest; | |
| 97 | |
| 98 struct ChangeInfo { | |
| 99 ChangeInfo(); | |
| 100 ~ChangeInfo(); | |
| 101 FileChangeList change_list; | |
| 102 int64 change_seq; | |
| 103 }; | |
| 104 | |
| 105 typedef std::map<fileapi::FileSystemURL, ChangeInfo, | |
| 106 fileapi::FileSystemURL::Comparator> | |
| 107 FileChangeMap; | |
| 108 typedef std::map<int64, fileapi::FileSystemURL> ChangeSeqMap; | |
| 109 | |
| 110 // This does mostly same as calling GetNextChangedURLs with max_url=0 | |
| 111 // except that it returns urls in set rather than in deque. | |
| 112 // Used only in testings. | |
| 113 void GetAllChangedURLs(fileapi::FileSystemURLSet* urls); | |
| 114 | |
| 115 // Used only in testings. | |
| 116 void DropAllChanges(); | |
| 117 | |
| 118 // Database related methods. | |
| 119 SyncStatusCode MarkDirtyOnDatabase(const fileapi::FileSystemURL& url); | |
| 120 SyncStatusCode ClearDirtyOnDatabase(const fileapi::FileSystemURL& url); | |
| 121 | |
| 122 SyncStatusCode CollectLastDirtyChanges( | |
| 123 fileapi::FileSystemContext* file_system_context); | |
| 124 void RecordChange(const fileapi::FileSystemURL& url, | |
| 125 const FileChange& change); | |
| 126 | |
| 127 bool initialized_; | |
| 128 | |
| 129 scoped_refptr<base::SequencedTaskRunner> file_task_runner_; | |
| 130 | |
| 131 FileChangeMap changes_; | |
| 132 ChangeSeqMap change_seqs_; | |
| 133 | |
| 134 scoped_ptr<TrackerDB> tracker_db_; | |
| 135 | |
| 136 // Change sequence number. Briefly gives a hint about the order of changes, | |
| 137 // but they are updated when a new change comes on the same file (as | |
| 138 // well as Drive's changestamps). | |
| 139 int64 current_change_seq_; | |
| 140 | |
| 141 // This can be accessed on any threads (with num_changes_lock_). | |
| 142 int64 num_changes_; | |
| 143 mutable base::Lock num_changes_lock_; | |
| 144 | |
| 145 DISALLOW_COPY_AND_ASSIGN(LocalFileChangeTracker); | |
| 146 }; | |
| 147 | |
| 148 } // namespace sync_file_system | |
| 149 | |
| 150 #endif // WEBKIT_FILEAPI_SYNCABLE_LOCAL_FILE_CHANGE_TRACKER_H_ | |
| OLD | NEW |