Update broken references to image assets
[chromium-blink-merge.git] / chrome / browser / sync_file_system / local / local_file_change_tracker.h
blobd38c8fc4ca52061f08a0c501b99485e38a3b58dd
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.
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_
8 #include <deque>
9 #include <map>
10 #include <string>
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 "storage/browser/fileapi/file_observers.h"
21 #include "storage/browser/fileapi/file_system_url.h"
23 namespace base {
24 class SequencedTaskRunner;
27 namespace storage {
28 class FileSystemContext;
29 class FileSystemURL;
32 namespace leveldb {
33 class Env;
34 class WriteBatch;
37 namespace sync_file_system {
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.
41 // Owned by FileSystemContext.
42 class LocalFileChangeTracker : public storage::FileUpdateObserver,
43 public storage::FileChangeObserver {
44 public:
45 // |file_task_runner| must be the one where the observee file operations run.
46 // (So that we can make sure DB operations are done before actual update
47 // happens)
48 LocalFileChangeTracker(const base::FilePath& base_path,
49 leveldb::Env* env_override,
50 base::SequencedTaskRunner* file_task_runner);
51 ~LocalFileChangeTracker() override;
53 // FileUpdateObserver overrides.
54 void OnStartUpdate(const storage::FileSystemURL& url) override;
55 void OnUpdate(const storage::FileSystemURL& url, int64 delta) override {}
56 void OnEndUpdate(const storage::FileSystemURL& url) override;
58 // FileChangeObserver overrides.
59 void OnCreateFile(const storage::FileSystemURL& url) override;
60 void OnCreateFileFrom(const storage::FileSystemURL& url,
61 const storage::FileSystemURL& src) override;
62 void OnRemoveFile(const storage::FileSystemURL& url) override;
63 void OnModifyFile(const storage::FileSystemURL& url) override;
64 void OnCreateDirectory(const storage::FileSystemURL& url) override;
65 void OnRemoveDirectory(const storage::FileSystemURL& url) override;
67 // Retrieves an array of |url| which have more than one pending changes.
68 // If |max_urls| is non-zero (recommended in production code) this
69 // returns URLs up to the number from the ones that have smallest
70 // change_seq numbers (i.e. older changes).
71 void GetNextChangedURLs(std::deque<storage::FileSystemURL>* urls,
72 int max_urls);
74 // Returns all changes recorded for the given |url|.
75 // Note that this also returns demoted changes.
76 // This should be called after writing is disabled.
77 void GetChangesForURL(const storage::FileSystemURL& url,
78 FileChangeList* changes);
80 // Clears the pending changes recorded in this tracker for |url|.
81 void ClearChangesForURL(const storage::FileSystemURL& url);
83 // Creates a fresh (empty) in-memory record for |url|.
84 // Note that new changes are recorded to the mirror too.
85 void CreateFreshMirrorForURL(const storage::FileSystemURL& url);
87 // Removes a mirror for |url|, and commits the change status to database.
88 void RemoveMirrorAndCommitChangesForURL(const storage::FileSystemURL& url);
90 // Resets the changes to the ones recorded in mirror for |url|, and
91 // commits the updated change status to database.
92 void ResetToMirrorAndCommitChangesForURL(const storage::FileSystemURL& url);
94 // Re-inserts changes into the separate demoted_changes_ queue. They won't
95 // be fetched by GetNextChangedURLs() unless PromoteDemotedChanges() is
96 // called.
97 void DemoteChangesForURL(const storage::FileSystemURL& url);
99 // Promotes demoted changes for |url| to the normal queue.
100 void PromoteDemotedChangesForURL(const storage::FileSystemURL& url);
102 // Promotes all demoted changes to the normal queue. Returns true if it has
103 // promoted any changes.
104 bool PromoteDemotedChanges();
106 // Called by FileSyncService at the startup time to restore last dirty changes
107 // left after the last shutdown (if any).
108 SyncStatusCode Initialize(storage::FileSystemContext* file_system_context);
110 // Resets all the changes recorded for the given |origin| and |type|.
111 // TODO(kinuko,nhiroki): Ideally this should be automatically called in
112 // DeleteFileSystem via QuotaUtil::DeleteOriginDataOnFileThread.
113 void ResetForFileSystem(const GURL& origin, storage::FileSystemType type);
115 // This method is (exceptionally) thread-safe.
116 int64 num_changes() const {
117 base::AutoLock lock(num_changes_lock_);
118 return num_changes_;
121 private:
122 class TrackerDB;
123 friend class CannedSyncableFileSystem;
124 friend class LocalFileChangeTrackerTest;
125 friend class LocalFileSyncContext;
126 friend class LocalFileSyncContextTest;
127 friend class SyncableFileSystemTest;
129 struct ChangeInfo {
130 ChangeInfo();
131 ~ChangeInfo();
132 FileChangeList change_list;
133 int64 change_seq;
136 typedef std::map<storage::FileSystemURL,
137 ChangeInfo,
138 storage::FileSystemURL::Comparator> FileChangeMap;
139 typedef std::map<int64, storage::FileSystemURL> ChangeSeqMap;
141 void UpdateNumChanges();
143 // This does mostly same as calling GetNextChangedURLs with max_url=0
144 // except that it returns urls in set rather than in deque.
145 // Used only in testings.
146 void GetAllChangedURLs(storage::FileSystemURLSet* urls);
148 // Used only in testings.
149 void DropAllChanges();
151 // Database related methods.
152 SyncStatusCode MarkDirtyOnDatabase(const storage::FileSystemURL& url);
153 SyncStatusCode ClearDirtyOnDatabase(const storage::FileSystemURL& url);
155 SyncStatusCode CollectLastDirtyChanges(
156 storage::FileSystemContext* file_system_context);
157 void RecordChange(const storage::FileSystemURL& url,
158 const FileChange& change);
160 static void RecordChangeToChangeMaps(const storage::FileSystemURL& url,
161 const FileChange& change,
162 int change_seq,
163 FileChangeMap* changes,
164 ChangeSeqMap* change_seqs);
166 void ResetForURL(const storage::FileSystemURL& url,
167 int change_seq,
168 leveldb::WriteBatch* batch);
170 bool initialized_;
172 scoped_refptr<base::SequencedTaskRunner> file_task_runner_;
174 FileChangeMap changes_;
175 ChangeSeqMap change_seqs_;
177 FileChangeMap mirror_changes_; // For mirrors.
178 FileChangeMap demoted_changes_; // For demoted changes.
180 scoped_ptr<TrackerDB> tracker_db_;
182 // Change sequence number. Briefly gives a hint about the order of changes,
183 // but they are updated when a new change comes on the same file (as
184 // well as Drive's changestamps).
185 int64 current_change_seq_number_;
187 // This can be accessed on any threads (with num_changes_lock_).
188 int64 num_changes_;
189 mutable base::Lock num_changes_lock_;
191 DISALLOW_COPY_AND_ASSIGN(LocalFileChangeTracker);
194 } // namespace sync_file_system
196 #endif // CHROME_BROWSER_SYNC_FILE_SYSTEM_LOCAL_LOCAL_FILE_CHANGE_TRACKER_H_