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 CHROME_BROWSER_SYNC_FILE_SYSTEM_REMOTE_FILE_SYNC_SERVICE_H_
6 #define CHROME_BROWSER_SYNC_FILE_SYSTEM_REMOTE_FILE_SYNC_SERVICE_H_
13 #include "base/basictypes.h"
14 #include "chrome/browser/sync_file_system/conflict_resolution_policy.h"
15 #include "chrome/browser/sync_file_system/sync_callbacks.h"
16 #include "chrome/browser/sync_file_system/sync_file_metadata.h"
17 #include "webkit/browser/fileapi/file_system_url.h"
19 class BrowserContextKeyedServiceFactory
;
30 namespace webkit_blob
{
34 namespace sync_file_system
{
36 class FileStatusObserver
;
37 class LocalChangeProcessor
;
38 class RemoteChangeProcessor
;
40 enum RemoteServiceState
{
41 // Remote service is up and running, or has not seen any errors yet.
42 // The consumer of this service can make new requests while the
43 // service is in this state.
44 REMOTE_SERVICE_OK
= 0,
46 // Remote service is temporarily unavailable due to network,
47 // authentication or some other temporary failure.
48 // This state may be automatically resolved when the underlying
49 // network condition or service condition changes.
50 // The consumer of this service can still make new requests but
51 // they may fail (with recoverable error code).
52 REMOTE_SERVICE_TEMPORARY_UNAVAILABLE
,
54 // Remote service is temporarily unavailable due to authentication failure.
55 // This state may be automatically resolved when the authentication token
56 // has been refreshed internally (e.g. when the user signed in etc).
57 // The consumer of this service can still make new requests but
58 // they may fail (with recoverable error code).
59 REMOTE_SERVICE_AUTHENTICATION_REQUIRED
,
61 // Remote service is disabled by configuration change or due to some
62 // unrecoverable errors, e.g. local database corruption.
63 // Any new requests will immediately fail when the service is in
65 REMOTE_SERVICE_DISABLED
,
67 REMOTE_SERVICE_STATE_MAX
,
70 // This class represents a backing service of the sync filesystem.
71 // This also maintains conflict information, i.e. a list of conflicting files
72 // (at least in the current design).
73 // Owned by SyncFileSystemService.
74 class RemoteFileSyncService
{
84 virtual ~Observer() {}
86 // This is called when RemoteFileSyncService updates its internal queue
87 // of pending remote changes.
88 // |pending_changes_hint| indicates the pending queue length to help sync
89 // scheduling but the value may not be accurately reflect the real-time
91 virtual void OnRemoteChangeQueueUpdated(int64 pending_changes_hint
) = 0;
93 // This is called when RemoteFileSyncService updates its state.
94 virtual void OnRemoteServiceStateUpdated(
95 RemoteServiceState state
,
96 const std::string
& description
) {}
99 DISALLOW_COPY_AND_ASSIGN(Observer
);
104 SyncFileMetadata metadata
;
108 UNINSTALL_AND_PURGE_REMOTE
,
109 UNINSTALL_AND_KEEP_REMOTE
,
112 // For GetOriginStatusMap.
113 typedef std::map
<GURL
, std::string
> OriginStatusMap
;
115 // For GetRemoteVersions.
116 typedef base::Callback
<void(SyncStatusCode status
,
117 const std::vector
<Version
>& versions
)>
118 RemoteVersionsCallback
;
119 typedef base::Callback
<void(SyncStatusCode status
,
120 webkit_blob::ScopedFile downloaded
)>
121 DownloadVersionCallback
;
123 // Creates an initialized RemoteFileSyncService for backend |version|
125 static scoped_ptr
<RemoteFileSyncService
> CreateForBrowserContext(
126 BackendVersion version
,
127 content::BrowserContext
* context
);
129 // Returns BrowserContextKeyedServiceFactory's an instance of
130 // RemoteFileSyncService for backend |version| depends on.
131 static void AppendDependsOnFactories(
132 BackendVersion version
,
133 std::set
<BrowserContextKeyedServiceFactory
*>* factories
);
135 RemoteFileSyncService() {}
136 virtual ~RemoteFileSyncService() {}
138 // Adds and removes observers.
139 virtual void AddServiceObserver(Observer
* observer
) = 0;
140 virtual void AddFileStatusObserver(FileStatusObserver
* observer
) = 0;
142 // Registers |origin| to track remote side changes for the |origin|.
143 // Upon completion, invokes |callback|.
144 // The caller may call this method again when the remote service state
145 // migrates to REMOTE_SERVICE_OK state if the error code returned via
146 // |callback| was retriable ones.
147 virtual void RegisterOrigin(
149 const SyncStatusCallback
& callback
) = 0;
151 // Re-enables |origin| that was previously disabled. If |origin| is not a
152 // SyncFS app, then the origin is effectively ignored.
153 virtual void EnableOrigin(
155 const SyncStatusCallback
& callback
) = 0;
157 virtual void DisableOrigin(
159 const SyncStatusCallback
& callback
) = 0;
161 // Uninstalls the |origin| by deleting its remote data copy and then removing
162 // the origin from the metadata store.
163 virtual void UninstallOrigin(
166 const SyncStatusCallback
& callback
) = 0;
168 // Called by the sync engine to process one remote change.
169 // After a change is processed |callback| will be called (to return
170 // the control to the sync engine).
171 // It is invalid to call this before calling SetRemoteChangeProcessor().
172 virtual void ProcessRemoteChange(const SyncFileCallback
& callback
) = 0;
174 // Sets a remote change processor. This must be called before any
175 // ProcessRemoteChange().
176 virtual void SetRemoteChangeProcessor(
177 RemoteChangeProcessor
* processor
) = 0;
179 // Returns a LocalChangeProcessor that applies a local change to the remote
180 // storage backed by this service.
181 virtual LocalChangeProcessor
* GetLocalChangeProcessor() = 0;
183 // Returns true if the file |url| is marked conflicted in the remote service.
184 virtual bool IsConflicting(const fileapi::FileSystemURL
& url
) = 0;
186 // Returns the current remote service state (should equal to the value
187 // returned by the last OnRemoteServiceStateUpdated notification.
188 virtual RemoteServiceState
GetCurrentState() const = 0;
190 // Returns all origins along with an arbitrary string description of their
191 // corresponding sync statuses.
192 virtual void GetOriginStatusMap(OriginStatusMap
* status_map
) = 0;
194 // Returns file metadata for |origin|.
195 virtual scoped_ptr
<base::ListValue
> DumpFiles(const GURL
& origin
) = 0;
197 // Returns the dump of internal database.
198 virtual scoped_ptr
<base::ListValue
> DumpDatabase() = 0;
200 // Enables or disables the background sync.
201 // Setting this to false should disable the synchronization (and make
202 // the service state to REMOTE_SERVICE_DISABLED), while setting this to
203 // true does not necessarily mean the service is actually turned on
204 // (for example if Chrome is offline the service state will become
205 // REMOTE_SERVICE_TEMPORARY_UNAVAILABLE).
206 virtual void SetSyncEnabled(bool enabled
) = 0;
208 // Sets the conflict resolution policy. Returns SYNC_STATUS_OK on success,
209 // or returns an error code if the given policy is not supported or had
211 virtual SyncStatusCode
SetDefaultConflictResolutionPolicy(
212 ConflictResolutionPolicy policy
) = 0;
213 virtual SyncStatusCode
SetConflictResolutionPolicy(
215 ConflictResolutionPolicy policy
) = 0;
217 // Gets the conflict resolution policy.
218 virtual ConflictResolutionPolicy
GetDefaultConflictResolutionPolicy()
220 virtual ConflictResolutionPolicy
GetConflictResolutionPolicy(
221 const GURL
& origin
) const = 0;
223 // Returns a list of remote versions with their metadata.
224 // This method is typically called for a file which is in conflicting state.
225 virtual void GetRemoteVersions(
226 const fileapi::FileSystemURL
& url
,
227 const RemoteVersionsCallback
& callback
) = 0;
229 // Downloads the remote image. The |id| should be the ID string for a
230 // version returned by GetRemoteVersions.
231 virtual void DownloadRemoteVersion(
232 const fileapi::FileSystemURL
& url
,
233 const std::string
& id
,
234 const DownloadVersionCallback
& callback
) = 0;
236 virtual void PromoteDemotedChanges() = 0;
239 DISALLOW_COPY_AND_ASSIGN(RemoteFileSyncService
);
242 } // namespace sync_file_system
244 #endif // CHROME_BROWSER_SYNC_FILE_SYSTEM_REMOTE_FILE_SYNC_SERVICE_H_