Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / sync_file_system / local / local_file_change_tracker.h
blobb3379e3e502aead118f24cd99ddbb121d81fbbe5
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 "webkit/browser/fileapi/file_observers.h"
21 #include "webkit/browser/fileapi/file_system_url.h"
23 namespace base {
24 class SequencedTaskRunner;
27 namespace fileapi {
28 class FileSystemContext;
29 class FileSystemURL;
32 namespace sync_file_system {
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();
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;
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;
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);
70 // Returns all changes recorded for the given |url|.
71 // Note that this also returns demoted changes.
72 // This should be called after writing is disabled.
73 void GetChangesForURL(const fileapi::FileSystemURL& url,
74 FileChangeList* changes);
76 // Clears the pending changes recorded in this tracker for |url|.
77 void ClearChangesForURL(const fileapi::FileSystemURL& url);
79 // Creates a fresh (empty) in-memory record for |url|.
80 // Note that new changes are recorded to the mirror too.
81 void CreateFreshMirrorForURL(const fileapi::FileSystemURL& url);
83 // Removes a mirror for |url|, and commits the change status to database.
84 void RemoveMirrorAndCommitChangesForURL(const fileapi::FileSystemURL& url);
86 // Resets the changes to the ones recorded in mirror for |url|, and
87 // commits the updated change status to database.
88 void ResetToMirrorAndCommitChangesForURL(const fileapi::FileSystemURL& url);
90 // Re-inserts changes into the separate demoted_changes_ queue. They won't
91 // be fetched by GetNextChangedURLs() unless PromoteDemotedChanges() is
92 // called.
93 void DemoteChangesForURL(const fileapi::FileSystemURL& url);
95 // Promotes all demoted changes to the normal queue. Returns true if it has
96 // promoted any changes.
97 bool PromoteDemotedChanges();
99 // Called by FileSyncService at the startup time to restore last dirty changes
100 // left after the last shutdown (if any).
101 SyncStatusCode Initialize(fileapi::FileSystemContext* file_system_context);
103 // Resets all the changes recorded for the given |origin| and |type|.
104 // TODO(kinuko,nhiroki): Ideally this should be automatically called in
105 // DeleteFileSystem via QuotaUtil::DeleteOriginDataOnFileThread.
106 void ResetForFileSystem(const GURL& origin, fileapi::FileSystemType type);
108 // This method is (exceptionally) thread-safe.
109 int64 num_changes() const {
110 base::AutoLock lock(num_changes_lock_);
111 return num_changes_;
114 private:
115 class TrackerDB;
116 friend class CannedSyncableFileSystem;
117 friend class LocalFileChangeTrackerTest;
118 friend class LocalFileSyncContext;
119 friend class LocalFileSyncContextTest;
120 friend class SyncableFileSystemTest;
122 struct ChangeInfo {
123 ChangeInfo();
124 ~ChangeInfo();
125 FileChangeList change_list;
126 int64 change_seq;
129 typedef std::map<fileapi::FileSystemURL, ChangeInfo,
130 fileapi::FileSystemURL::Comparator>
131 FileChangeMap;
132 typedef std::map<int64, fileapi::FileSystemURL> ChangeSeqMap;
134 void UpdateNumChanges();
136 // This does mostly same as calling GetNextChangedURLs with max_url=0
137 // except that it returns urls in set rather than in deque.
138 // Used only in testings.
139 void GetAllChangedURLs(fileapi::FileSystemURLSet* urls);
141 // Used only in testings.
142 void DropAllChanges();
144 // Database related methods.
145 SyncStatusCode MarkDirtyOnDatabase(const fileapi::FileSystemURL& url);
146 SyncStatusCode ClearDirtyOnDatabase(const fileapi::FileSystemURL& url);
148 SyncStatusCode CollectLastDirtyChanges(
149 fileapi::FileSystemContext* file_system_context);
150 void RecordChange(const fileapi::FileSystemURL& url,
151 const FileChange& change);
153 static void RecordChangeToChangeMaps(const fileapi::FileSystemURL& url,
154 const FileChange& change,
155 int change_seq,
156 FileChangeMap* changes,
157 ChangeSeqMap* change_seqs);
159 bool initialized_;
161 scoped_refptr<base::SequencedTaskRunner> file_task_runner_;
163 FileChangeMap changes_;
164 ChangeSeqMap change_seqs_;
166 FileChangeMap mirror_changes_; // For mirrors.
167 FileChangeMap demoted_changes_; // For demoted changes.
169 scoped_ptr<TrackerDB> tracker_db_;
171 // Change sequence number. Briefly gives a hint about the order of changes,
172 // but they are updated when a new change comes on the same file (as
173 // well as Drive's changestamps).
174 int64 current_change_seq_;
176 // This can be accessed on any threads (with num_changes_lock_).
177 int64 num_changes_;
178 mutable base::Lock num_changes_lock_;
180 DISALLOW_COPY_AND_ASSIGN(LocalFileChangeTracker);
183 } // namespace sync_file_system
185 #endif // CHROME_BROWSER_SYNC_FILE_SYSTEM_LOCAL_LOCAL_FILE_CHANGE_TRACKER_H_