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_
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"
26 class FileSystemContext
;
37 namespace sync_file_system
{
40 class LocalChangeProcessor
;
41 class LocalFileSyncContext
;
42 struct LocalFileSyncInfo
;
44 // Maintains local file change tracker and sync status.
45 // Owned by SyncFileSystemService (which is a per-profile object).
46 class LocalFileSyncService
47 : public RemoteChangeProcessor
,
48 public LocalOriginChangeObserver
,
49 public base::SupportsWeakPtr
<LocalFileSyncService
> {
51 typedef base::Callback
<LocalChangeProcessor
*(const GURL
& origin
)>
52 GetLocalChangeProcessorCallback
;
57 virtual ~Observer() {}
59 // This is called when there're one or more local changes available.
60 // |pending_changes_hint| indicates the pending queue length to help sync
61 // scheduling but the value may not be accurately reflect the real-time
63 virtual void OnLocalChangeAvailable(int64 pending_changes_hint
) = 0;
66 DISALLOW_COPY_AND_ASSIGN(Observer
);
69 typedef base::Callback
<void(SyncStatusCode status
,
70 bool has_pending_changes
)>
71 HasPendingLocalChangeCallback
;
73 static scoped_ptr
<LocalFileSyncService
> Create(Profile
* profile
);
74 static scoped_ptr
<LocalFileSyncService
> CreateForTesting(
76 leveldb::Env
* env_override
);
77 ~LocalFileSyncService() override
;
81 void MaybeInitializeFileSystemContext(
82 const GURL
& app_origin
,
83 storage::FileSystemContext
* file_system_context
,
84 const SyncStatusCallback
& callback
);
86 void AddChangeObserver(Observer
* observer
);
88 // Registers |url| to wait until sync is enabled for |url|.
89 // |on_syncable_callback| is to be called when |url| becomes syncable
90 // (i.e. when we have no pending writes and the file is successfully locked
92 // Calling this method again while this already has another URL waiting
93 // for sync will overwrite the previously registered URL.
94 void RegisterURLForWaitingSync(const storage::FileSystemURL
& url
,
95 const base::Closure
& on_syncable_callback
);
97 // Synchronize one (or a set of) local change(s) to the remote server
98 // using local_change_processor given by SetLocalChangeProcessor().
99 // |processor| must have same or longer lifetime than this service.
100 // It is invalid to call this method before calling SetLocalChangeProcessor().
101 void ProcessLocalChange(const SyncFileCallback
& callback
);
103 // Sets a local change processor. The value is ignored if
104 // SetLocalChangeProcessorCallback() is called separately.
105 // Either this or SetLocalChangeProcessorCallback() must be called before
106 // any ProcessLocalChange().
107 void SetLocalChangeProcessor(LocalChangeProcessor
* local_change_processor
);
109 // Sets a closure which gets a local change processor for the given origin.
110 // Note that once this is called it overrides the direct processor setting
111 // done by SetLocalChangeProcessor().
112 // Either this or SetLocalChangeProcessor() must be called before any
113 // ProcessLocalChange().
115 // TODO(kinuko): Remove this method once we stop using multiple backends
116 // (crbug.com/324215), or deprecate the other if we keep doing so.
117 void SetLocalChangeProcessorCallback(
118 const GetLocalChangeProcessorCallback
& get_local_change_processor
);
120 // Returns true via |callback| if the given file |url| has local pending
122 void HasPendingLocalChanges(const storage::FileSystemURL
& url
,
123 const HasPendingLocalChangeCallback
& callback
);
125 void PromoteDemotedChanges(const base::Closure
& callback
);
127 // Returns the metadata of a remote file pointed by |url|.
128 virtual void GetLocalFileMetadata(const storage::FileSystemURL
& url
,
129 const SyncFileMetadataCallback
& callback
);
131 // RemoteChangeProcessor overrides.
132 void PrepareForProcessRemoteChange(
133 const storage::FileSystemURL
& url
,
134 const PrepareChangeCallback
& callback
) override
;
135 void ApplyRemoteChange(const FileChange
& change
,
136 const base::FilePath
& local_path
,
137 const storage::FileSystemURL
& url
,
138 const SyncStatusCallback
& callback
) override
;
139 void FinalizeRemoteSync(const storage::FileSystemURL
& url
,
140 bool clear_local_changes
,
141 const base::Closure
& completion_callback
) override
;
142 void RecordFakeLocalChange(const storage::FileSystemURL
& url
,
143 const FileChange
& change
,
144 const SyncStatusCallback
& callback
) override
;
146 // LocalOriginChangeObserver override.
147 void OnChangesAvailableInOrigins(const std::set
<GURL
>& origins
) override
;
149 // Called when a particular origin (app) is disabled/enabled while
150 // the service is running. This may be called for origins/apps that
151 // are not initialized for the service.
152 void SetOriginEnabled(const GURL
& origin
, bool enabled
);
155 typedef std::map
<GURL
, storage::FileSystemContext
*> OriginToContext
;
156 friend class OriginChangeMapTest
;
158 class OriginChangeMap
{
160 typedef std::map
<GURL
, int64
> Map
;
165 // Sets |origin| to the next origin to process. (For now we simply apply
166 // round-robin to pick the next origin to avoid starvation.)
167 // Returns false if no origins to process.
168 bool NextOriginToProcess(GURL
* origin
);
170 int64
GetTotalChangeCount() const;
172 // Update change_count_map_ for |origin|.
173 void SetOriginChangeCount(const GURL
& origin
, int64 changes
);
175 void SetOriginEnabled(const GURL
& origin
, bool enabled
);
178 // Per-origin changes (cached info, could be stale).
179 Map change_count_map_
;
182 // Holds a set of disabled (but initialized) origins.
183 std::set
<GURL
> disabled_origins_
;
186 LocalFileSyncService(Profile
* profile
, leveldb::Env
* env_override
);
188 void DidInitializeFileSystemContext(
189 const GURL
& app_origin
,
190 storage::FileSystemContext
* file_system_context
,
191 const SyncStatusCallback
& callback
,
192 SyncStatusCode status
);
193 void DidInitializeForRemoteSync(
194 const storage::FileSystemURL
& url
,
195 storage::FileSystemContext
* file_system_context
,
196 const PrepareChangeCallback
& callback
,
197 SyncStatusCode status
);
199 // Callback for ApplyRemoteChange.
200 void DidApplyRemoteChange(
201 const SyncStatusCallback
& callback
,
202 SyncStatusCode status
);
204 // Callbacks for ProcessLocalChange.
205 void DidGetFileForLocalSync(const SyncFileCallback
& callback
,
206 SyncStatusCode status
,
207 const LocalFileSyncInfo
& sync_file_info
,
208 storage::ScopedFile snapshot
);
209 void ProcessNextChangeForURL(const SyncFileCallback
& callback
,
210 storage::ScopedFile snapshot
,
211 const LocalFileSyncInfo
& sync_file_info
,
212 const FileChange
& last_change
,
213 const FileChangeList
& changes
,
214 SyncStatusCode status
);
216 // A thin wrapper of get_local_change_processor_.
217 LocalChangeProcessor
* GetLocalChangeProcessor(
218 const storage::FileSystemURL
& url
);
222 scoped_refptr
<LocalFileSyncContext
> sync_context_
;
224 // Origin to context map. (Assuming that as far as we're in the same
225 // profile single origin wouldn't belong to multiple FileSystemContexts.)
226 OriginToContext origin_to_contexts_
;
228 // Origins which have pending changes but have not been initialized yet.
229 // (Used only for handling dirty files left in the local tracker database
231 std::set
<GURL
> pending_origins_with_changes_
;
233 OriginChangeMap origin_change_map_
;
235 LocalChangeProcessor
* local_change_processor_
;
236 GetLocalChangeProcessorCallback get_local_change_processor_
;
238 base::ObserverList
<Observer
> change_observers_
;
240 DISALLOW_COPY_AND_ASSIGN(LocalFileSyncService
);
243 } // namespace sync_file_system
245 #endif // CHROME_BROWSER_SYNC_FILE_SYSTEM_LOCAL_LOCAL_FILE_SYNC_SERVICE_H_