Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / sync_file_system / local / local_file_sync_service.h
blobfdffa4ac6644afc0c1b00a0cd3bade5e12987f0d
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_SYNC_SERVICE_H_
6 #define CHROME_BROWSER_SYNC_FILE_SYSTEM_LOCAL_LOCAL_FILE_SYNC_SERVICE_H_
8 #include <map>
9 #include <set>
10 #include <string>
12 #include "base/basictypes.h"
13 #include "base/callback.h"
14 #include "base/memory/ref_counted.h"
15 #include "base/memory/weak_ptr.h"
16 #include "base/observer_list.h"
17 #include "chrome/browser/sync_file_system/local/local_origin_change_observer.h"
18 #include "chrome/browser/sync_file_system/remote_change_processor.h"
19 #include "chrome/browser/sync_file_system/sync_callbacks.h"
20 #include "chrome/browser/sync_file_system/sync_status_code.h"
22 class GURL;
23 class Profile;
25 namespace fileapi {
26 class FileSystemContext;
29 namespace webkit_blob {
30 class ScopedFile;
33 namespace sync_file_system {
35 class FileChange;
36 class LocalChangeProcessor;
37 class LocalFileSyncContext;
38 struct LocalFileSyncInfo;
40 // Maintains local file change tracker and sync status.
41 // Owned by SyncFileSystemService (which is a per-profile object).
42 class LocalFileSyncService
43 : public RemoteChangeProcessor,
44 public LocalOriginChangeObserver,
45 public base::SupportsWeakPtr<LocalFileSyncService> {
46 public:
47 typedef base::Callback<LocalChangeProcessor*(const GURL& origin)>
48 GetLocalChangeProcessorCallback;
50 class Observer {
51 public:
52 Observer() {}
53 virtual ~Observer() {}
55 // This is called when there're one or more local changes available.
56 // |pending_changes_hint| indicates the pending queue length to help sync
57 // scheduling but the value may not be accurately reflect the real-time
58 // value.
59 virtual void OnLocalChangeAvailable(int64 pending_changes_hint) = 0;
61 private:
62 DISALLOW_COPY_AND_ASSIGN(Observer);
65 typedef base::Callback<void(SyncStatusCode status,
66 bool has_pending_changes)>
67 HasPendingLocalChangeCallback;
69 explicit LocalFileSyncService(Profile* profile);
70 virtual ~LocalFileSyncService();
72 void Shutdown();
74 void MaybeInitializeFileSystemContext(
75 const GURL& app_origin,
76 fileapi::FileSystemContext* file_system_context,
77 const SyncStatusCallback& callback);
79 void AddChangeObserver(Observer* observer);
81 // Registers |url| to wait until sync is enabled for |url|.
82 // |on_syncable_callback| is to be called when |url| becomes syncable
83 // (i.e. when we have no pending writes and the file is successfully locked
84 // for sync).
85 // Calling this method again while this already has another URL waiting
86 // for sync will overwrite the previously registered URL.
87 void RegisterURLForWaitingSync(const fileapi::FileSystemURL& url,
88 const base::Closure& on_syncable_callback);
90 // Synchronize one (or a set of) local change(s) to the remote server
91 // using local_change_processor given by SetLocalChangeProcessor().
92 // |processor| must have same or longer lifetime than this service.
93 // It is invalid to call this method before calling SetLocalChangeProcessor().
94 void ProcessLocalChange(const SyncFileCallback& callback);
96 // Sets a local change processor. The value is ignored if
97 // SetLocalChangeProcessorCallback() is called separately.
98 // Either this or SetLocalChangeProcessorCallback() must be called before
99 // any ProcessLocalChange().
100 void SetLocalChangeProcessor(LocalChangeProcessor* local_change_processor);
102 // Sets a closure which gets a local change processor for the given origin.
103 // Note that once this is called it overrides the direct processor setting
104 // done by SetLocalChangeProcessor().
105 // Either this or SetLocalChangeProcessor() must be called before any
106 // ProcessLocalChange().
108 // TODO(kinuko): Remove this method once we stop using multiple backends
109 // (crbug.com/324215), or deprecate the other if we keep doing so.
110 void SetLocalChangeProcessorCallback(
111 const GetLocalChangeProcessorCallback& get_local_change_processor);
113 // Returns true via |callback| if the given file |url| has local pending
114 // changes.
115 void HasPendingLocalChanges(
116 const fileapi::FileSystemURL& url,
117 const HasPendingLocalChangeCallback& callback);
119 void PromoteDemotedChanges();
121 // Returns the metadata of a remote file pointed by |url|.
122 virtual void GetLocalFileMetadata(
123 const fileapi::FileSystemURL& url,
124 const SyncFileMetadataCallback& callback);
126 // RemoteChangeProcessor overrides.
127 virtual void PrepareForProcessRemoteChange(
128 const fileapi::FileSystemURL& url,
129 const PrepareChangeCallback& callback) OVERRIDE;
130 virtual void ApplyRemoteChange(
131 const FileChange& change,
132 const base::FilePath& local_path,
133 const fileapi::FileSystemURL& url,
134 const SyncStatusCallback& callback) OVERRIDE;
135 virtual void FinalizeRemoteSync(
136 const fileapi::FileSystemURL& url,
137 bool clear_local_changes,
138 const base::Closure& completion_callback) OVERRIDE;
139 virtual void RecordFakeLocalChange(
140 const fileapi::FileSystemURL& url,
141 const FileChange& change,
142 const SyncStatusCallback& callback) OVERRIDE;
144 // LocalOriginChangeObserver override.
145 virtual void OnChangesAvailableInOrigins(
146 const std::set<GURL>& origins) OVERRIDE;
148 // Called when a particular origin (app) is disabled/enabled while
149 // the service is running. This may be called for origins/apps that
150 // are not initialized for the service.
151 void SetOriginEnabled(const GURL& origin, bool enabled);
153 private:
154 typedef std::map<GURL, fileapi::FileSystemContext*> OriginToContext;
155 friend class OriginChangeMapTest;
157 class OriginChangeMap {
158 public:
159 typedef std::map<GURL, int64> Map;
161 OriginChangeMap();
162 ~OriginChangeMap();
164 // Sets |origin| to the next origin to process. (For now we simply apply
165 // round-robin to pick the next origin to avoid starvation.)
166 // Returns false if no origins to process.
167 bool NextOriginToProcess(GURL* origin);
169 int64 GetTotalChangeCount() const;
171 // Update change_count_map_ for |origin|.
172 void SetOriginChangeCount(const GURL& origin, int64 changes);
174 void SetOriginEnabled(const GURL& origin, bool enabled);
176 private:
177 // Per-origin changes (cached info, could be stale).
178 Map change_count_map_;
179 Map::iterator next_;
181 // Holds a set of disabled (but initialized) origins.
182 std::set<GURL> disabled_origins_;
185 void DidInitializeFileSystemContext(
186 const GURL& app_origin,
187 fileapi::FileSystemContext* file_system_context,
188 const SyncStatusCallback& callback,
189 SyncStatusCode status);
190 void DidInitializeForRemoteSync(
191 const fileapi::FileSystemURL& url,
192 fileapi::FileSystemContext* file_system_context,
193 const PrepareChangeCallback& callback,
194 SyncStatusCode status);
196 // Runs local_sync_callback_ and resets it.
197 void RunLocalSyncCallback(
198 SyncStatusCode status,
199 const fileapi::FileSystemURL& url);
201 // Callback for ApplyRemoteChange.
202 void DidApplyRemoteChange(
203 const SyncStatusCallback& callback,
204 SyncStatusCode status);
206 // Callbacks for ProcessLocalChange.
207 void DidGetFileForLocalSync(
208 SyncStatusCode status,
209 const LocalFileSyncInfo& sync_file_info,
210 webkit_blob::ScopedFile snapshot);
211 void ProcessNextChangeForURL(
212 webkit_blob::ScopedFile snapshot,
213 const LocalFileSyncInfo& sync_file_info,
214 const FileChange& last_change,
215 const FileChangeList& changes,
216 SyncStatusCode status);
218 // A thin wrapper of get_local_change_processor_.
219 LocalChangeProcessor* GetLocalChangeProcessor(
220 const fileapi::FileSystemURL& url);
222 Profile* profile_;
224 scoped_refptr<LocalFileSyncContext> sync_context_;
226 // Origin to context map. (Assuming that as far as we're in the same
227 // profile single origin wouldn't belong to multiple FileSystemContexts.)
228 OriginToContext origin_to_contexts_;
230 // Origins which have pending changes but have not been initialized yet.
231 // (Used only for handling dirty files left in the local tracker database
232 // after a restart.)
233 std::set<GURL> pending_origins_with_changes_;
235 OriginChangeMap origin_change_map_;
237 // This callback is non-null while a local sync is running (i.e.
238 // ProcessLocalChange has been called and has not been returned yet).
239 SyncFileCallback local_sync_callback_;
241 LocalChangeProcessor* local_change_processor_;
242 GetLocalChangeProcessorCallback get_local_change_processor_;
244 ObserverList<Observer> change_observers_;
246 DISALLOW_COPY_AND_ASSIGN(LocalFileSyncService);
249 } // namespace sync_file_system
251 #endif // CHROME_BROWSER_SYNC_FILE_SYSTEM_LOCAL_LOCAL_FILE_SYNC_SERVICE_H_