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.
5 #ifndef WEBKIT_FILEAPI_SYNCABLE_LOCAL_FILE_CHANGE_TRACKER_H_
6 #define WEBKIT_FILEAPI_SYNCABLE_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 "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"
25 class SequencedTaskRunner
;
29 class FileSystemContext
;
33 namespace sync_file_system
{
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
{
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
45 LocalFileChangeTracker(const base::FilePath
& base_path
,
46 base::SequencedTaskRunner
* file_task_runner
);
47 virtual ~LocalFileChangeTracker();
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
;
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
;
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
,
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
);
76 // Clears the pending changes recorded in this tracker for |url|.
77 void ClearChangesForURL(const fileapi::FileSystemURL
& url
);
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
);
83 // This method is (exceptionally) thread-safe.
84 int64
num_changes() const {
85 base::AutoLock
lock(num_changes_lock_
);
89 void UpdateNumChanges();
93 friend class CannedSyncableFileSystem
;
94 friend class LocalFileChangeTrackerTest
;
95 friend class LocalFileSyncContext
;
96 friend class SyncableFileSystemTest
;
101 FileChangeList change_list
;
105 typedef std::map
<fileapi::FileSystemURL
, ChangeInfo
,
106 fileapi::FileSystemURL::Comparator
>
108 typedef std::map
<int64
, fileapi::FileSystemURL
> ChangeSeqMap
;
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
);
115 // Used only in testings.
116 void DropAllChanges();
118 // Database related methods.
119 SyncStatusCode
MarkDirtyOnDatabase(const fileapi::FileSystemURL
& url
);
120 SyncStatusCode
ClearDirtyOnDatabase(const fileapi::FileSystemURL
& url
);
122 SyncStatusCode
CollectLastDirtyChanges(
123 fileapi::FileSystemContext
* file_system_context
);
124 void RecordChange(const fileapi::FileSystemURL
& url
,
125 const FileChange
& change
);
129 scoped_refptr
<base::SequencedTaskRunner
> file_task_runner_
;
131 FileChangeMap changes_
;
132 ChangeSeqMap change_seqs_
;
134 scoped_ptr
<TrackerDB
> tracker_db_
;
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_
;
141 // This can be accessed on any threads (with num_changes_lock_).
143 mutable base::Lock num_changes_lock_
;
145 DISALLOW_COPY_AND_ASSIGN(LocalFileChangeTracker
);
148 } // namespace sync_file_system
150 #endif // WEBKIT_FILEAPI_SYNCABLE_LOCAL_FILE_CHANGE_TRACKER_H_