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