remote_file_sync_service.h revision a1401311d1ab56c4ed0a474bd38c108f75cb0cd9
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.
4
5#ifndef CHROME_BROWSER_SYNC_FILE_SYSTEM_REMOTE_FILE_SYNC_SERVICE_H_
6#define CHROME_BROWSER_SYNC_FILE_SYSTEM_REMOTE_FILE_SYNC_SERVICE_H_
7
8#include <map>
9#include <set>
10#include <string>
11#include <vector>
12
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"
18
19class BrowserContextKeyedServiceFactory;
20class GURL;
21
22namespace base {
23class ListValue;
24}
25
26namespace content {
27class BrowserContext;
28}
29
30namespace webkit_blob {
31class ScopedFile;
32}
33
34namespace sync_file_system {
35
36class FileStatusObserver;
37class LocalChangeProcessor;
38class RemoteChangeProcessor;
39
40enum 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,
45
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,
53
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,
60
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
64  // this state.
65  REMOTE_SERVICE_DISABLED,
66
67  REMOTE_SERVICE_STATE_MAX,
68};
69
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.
74class RemoteFileSyncService {
75 public:
76  enum BackendVersion {
77    V1,
78    V2,
79  };
80
81  class Observer {
82   public:
83    Observer() {}
84    virtual ~Observer() {}
85
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
90    // value.
91    virtual void OnRemoteChangeQueueUpdated(int64 pending_changes_hint) = 0;
92
93    // This is called when RemoteFileSyncService updates its state.
94    virtual void OnRemoteServiceStateUpdated(
95        RemoteServiceState state,
96        const std::string& description) {}
97
98   private:
99    DISALLOW_COPY_AND_ASSIGN(Observer);
100  };
101
102  struct Version {
103    std::string id;
104    SyncFileMetadata metadata;
105  };
106
107  enum UninstallFlag {
108    UNINSTALL_AND_PURGE_REMOTE,
109    UNINSTALL_AND_KEEP_REMOTE,
110  };
111
112  // For GetOriginStatusMap.
113  typedef std::map<GURL, std::string> OriginStatusMap;
114
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;
122
123  // Creates an initialized RemoteFileSyncService for backend |version|
124  // for |context|.
125  static scoped_ptr<RemoteFileSyncService> CreateForBrowserContext(
126      BackendVersion version,
127      content::BrowserContext* context);
128
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);
134
135  RemoteFileSyncService() {}
136  virtual ~RemoteFileSyncService() {}
137
138  // Adds and removes observers.
139  virtual void AddServiceObserver(Observer* observer) = 0;
140  virtual void AddFileStatusObserver(FileStatusObserver* observer) = 0;
141
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(
148      const GURL& origin,
149      const SyncStatusCallback& callback) = 0;
150
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(
154      const GURL& origin,
155      const SyncStatusCallback& callback) = 0;
156
157  virtual void DisableOrigin(
158      const GURL& origin,
159      const SyncStatusCallback& callback) = 0;
160
161  // Uninstalls the |origin| by deleting its remote data copy and then removing
162  // the origin from the metadata store.
163  virtual void UninstallOrigin(
164      const GURL& origin,
165      UninstallFlag flag,
166      const SyncStatusCallback& callback) = 0;
167
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;
173
174  // Sets a remote change processor.  This must be called before any
175  // ProcessRemoteChange().
176  virtual void SetRemoteChangeProcessor(
177      RemoteChangeProcessor* processor) = 0;
178
179  // Returns a LocalChangeProcessor that applies a local change to the remote
180  // storage backed by this service.
181  virtual LocalChangeProcessor* GetLocalChangeProcessor() = 0;
182
183  // Returns true if the file |url| is marked conflicted in the remote service.
184  virtual bool IsConflicting(const fileapi::FileSystemURL& url) = 0;
185
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;
189
190  // Returns all origins along with an arbitrary string description of their
191  // corresponding sync statuses.
192  virtual void GetOriginStatusMap(OriginStatusMap* status_map) = 0;
193
194  // Returns file metadata for |origin|.
195  virtual scoped_ptr<base::ListValue> DumpFiles(const GURL& origin) = 0;
196
197  // Returns the dump of internal database.
198  virtual scoped_ptr<base::ListValue> DumpDatabase() = 0;
199
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;
207
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
210  // an error.
211  virtual SyncStatusCode SetDefaultConflictResolutionPolicy(
212      ConflictResolutionPolicy policy) = 0;
213  virtual SyncStatusCode SetConflictResolutionPolicy(
214      const GURL& origin,
215      ConflictResolutionPolicy policy) = 0;
216
217  // Gets the conflict resolution policy.
218  virtual ConflictResolutionPolicy GetDefaultConflictResolutionPolicy()
219      const = 0;
220  virtual ConflictResolutionPolicy GetConflictResolutionPolicy(
221      const GURL& origin) const = 0;
222
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;
228
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;
235
236  virtual void PromoteDemotedChanges() = 0;
237
238 private:
239  DISALLOW_COPY_AND_ASSIGN(RemoteFileSyncService);
240};
241
242}  // namespace sync_file_system
243
244#endif  // CHROME_BROWSER_SYNC_FILE_SYSTEM_REMOTE_FILE_SYNC_SERVICE_H_
245