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.
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
,
68 // This class represents a backing service of the sync filesystem.
69 // This also maintains conflict information, i.e. a list of conflicting files
70 // (at least in the current design).
71 // Owned by SyncFileSystemService.
72 class RemoteFileSyncService
{
82 virtual ~Observer() {}
84 // This is called when RemoteFileSyncService updates its internal queue
85 // of pending remote changes.
86 // |pending_changes_hint| indicates the pending queue length to help sync
87 // scheduling but the value may not be accurately reflect the real-time
89 virtual void OnRemoteChangeQueueUpdated(int64 pending_changes_hint
) = 0;
91 // This is called when RemoteFileSyncService updates its state.
92 virtual void OnRemoteServiceStateUpdated(
93 RemoteServiceState state
,
94 const std::string
& description
) {}
97 DISALLOW_COPY_AND_ASSIGN(Observer
);
102 SyncFileMetadata metadata
;
106 UNINSTALL_AND_PURGE_REMOTE
,
107 UNINSTALL_AND_KEEP_REMOTE
,
110 // For GetOriginStatusMap.
111 typedef std::map
<GURL
, std::string
> OriginStatusMap
;
113 // For GetRemoteVersions.
114 typedef base::Callback
<void(SyncStatusCode status
,
115 const std::vector
<Version
>& versions
)>
116 RemoteVersionsCallback
;
117 typedef base::Callback
<void(SyncStatusCode status
,
118 webkit_blob::ScopedFile downloaded
)>
119 DownloadVersionCallback
;
121 // Creates an initialized RemoteFileSyncService for backend |version|
123 static scoped_ptr
<RemoteFileSyncService
> CreateForBrowserContext(
124 BackendVersion version
,
125 content::BrowserContext
* context
);
127 // Returns BrowserContextKeyedServiceFactory's an instance of
128 // RemoteFileSyncService for backend |version| depends on.
129 static void AppendDependsOnFactories(
130 BackendVersion version
,
131 std::set
<BrowserContextKeyedServiceFactory
*>* factories
);
133 RemoteFileSyncService() {}
134 virtual ~RemoteFileSyncService() {}
136 // Adds and removes observers.
137 virtual void AddServiceObserver(Observer
* observer
) = 0;
138 virtual void AddFileStatusObserver(FileStatusObserver
* observer
) = 0;
140 // Registers |origin| to track remote side changes for the |origin|.
141 // Upon completion, invokes |callback|.
142 // The caller may call this method again when the remote service state
143 // migrates to REMOTE_SERVICE_OK state if the error code returned via
144 // |callback| was retriable ones.
145 virtual void RegisterOrigin(
147 const SyncStatusCallback
& callback
) = 0;
149 // Re-enables |origin| that was previously disabled. If |origin| is not a
150 // SyncFS app, then the origin is effectively ignored.
151 virtual void EnableOrigin(
153 const SyncStatusCallback
& callback
) = 0;
155 virtual void DisableOrigin(
157 const SyncStatusCallback
& callback
) = 0;
159 // Uninstalls the |origin| by deleting its remote data copy and then removing
160 // the origin from the metadata store.
161 virtual void UninstallOrigin(
164 const SyncStatusCallback
& callback
) = 0;
166 // Called by the sync engine to process one remote change.
167 // After a change is processed |callback| will be called (to return
168 // the control to the sync engine).
169 // It is invalid to call this before calling SetRemoteChangeProcessor().
170 virtual void ProcessRemoteChange(const SyncFileCallback
& callback
) = 0;
172 // Sets a remote change processor. This must be called before any
173 // ProcessRemoteChange().
174 virtual void SetRemoteChangeProcessor(
175 RemoteChangeProcessor
* processor
) = 0;
177 // Returns a LocalChangeProcessor that applies a local change to the remote
178 // storage backed by this service.
179 virtual LocalChangeProcessor
* GetLocalChangeProcessor() = 0;
181 // Returns true if the file |url| is marked conflicted in the remote service.
182 virtual bool IsConflicting(const fileapi::FileSystemURL
& url
) = 0;
184 // Returns the current remote service state (should equal to the value
185 // returned by the last OnRemoteServiceStateUpdated notification.
186 virtual RemoteServiceState
GetCurrentState() const = 0;
188 // Returns all origins along with an arbitrary string description of their
189 // corresponding sync statuses.
190 virtual void GetOriginStatusMap(OriginStatusMap
* status_map
) = 0;
192 // Returns file metadata for |origin|.
193 virtual scoped_ptr
<base::ListValue
> DumpFiles(const GURL
& origin
) = 0;
195 // Returns the dump of internal database.
196 virtual scoped_ptr
<base::ListValue
> DumpDatabase() = 0;
198 // Enables or disables the background sync.
199 // Setting this to false should disable the synchronization (and make
200 // the service state to REMOTE_SERVICE_DISABLED), while setting this to
201 // true does not necessarily mean the service is actually turned on
202 // (for example if Chrome is offline the service state will become
203 // REMOTE_SERVICE_TEMPORARY_UNAVAILABLE).
204 virtual void SetSyncEnabled(bool enabled
) = 0;
206 // Sets the conflict resolution policy. Returns SYNC_STATUS_OK on success,
207 // or returns an error code if the given policy is not supported or had
209 virtual SyncStatusCode
SetConflictResolutionPolicy(
210 ConflictResolutionPolicy policy
) = 0;
212 // Gets the conflict resolution policy.
213 virtual ConflictResolutionPolicy
GetConflictResolutionPolicy() const = 0;
215 // Returns a list of remote versions with their metadata.
216 // This method is typically called for a file which is in conflicting state.
217 virtual void GetRemoteVersions(
218 const fileapi::FileSystemURL
& url
,
219 const RemoteVersionsCallback
& callback
) = 0;
221 // Downloads the remote image. The |id| should be the ID string for a
222 // version returned by GetRemoteVersions.
223 virtual void DownloadRemoteVersion(
224 const fileapi::FileSystemURL
& url
,
225 const std::string
& id
,
226 const DownloadVersionCallback
& callback
) = 0;
228 virtual void PromoteDemotedChanges() = 0;
231 DISALLOW_COPY_AND_ASSIGN(RemoteFileSyncService
);
234 } // namespace sync_file_system
236 #endif // CHROME_BROWSER_SYNC_FILE_SYSTEM_REMOTE_FILE_SYNC_SERVICE_H_