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_
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"
24 class SequencedTaskRunner
;
28 class FileSystemContext
;
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
{
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
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
,
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
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_
);
116 friend class CannedSyncableFileSystem
;
117 friend class LocalFileChangeTrackerTest
;
118 friend class LocalFileSyncContext
;
119 friend class LocalFileSyncContextTest
;
120 friend class SyncableFileSystemTest
;
125 FileChangeList change_list
;
129 typedef std::map
<fileapi::FileSystemURL
, ChangeInfo
,
130 fileapi::FileSystemURL::Comparator
>
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
,
156 FileChangeMap
* changes
,
157 ChangeSeqMap
* change_seqs
);
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_).
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_