remote_file_sync_service.h revision 6e8cce623b6e4fe0c9e4af605d675dd9d0338c38
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 "base/memory/scoped_ptr.h"
15#include "chrome/browser/sync_file_system/conflict_resolution_policy.h"
16#include "chrome/browser/sync_file_system/sync_callbacks.h"
17#include "chrome/browser/sync_file_system/sync_file_metadata.h"
18#include "webkit/browser/fileapi/file_system_url.h"
19
20class BrowserContextKeyedServiceFactory;
21class GURL;
22
23namespace base {
24class ListValue;
25}
26
27namespace content {
28class BrowserContext;
29}
30
31namespace webkit_blob {
32class ScopedFile;
33}
34
35namespace sync_file_system {
36
37class FileStatusObserver;
38class LocalChangeProcessor;
39class RemoteChangeProcessor;
40class TaskLogger;
41
42enum RemoteServiceState {
43  // Remote service is up and running, or has not seen any errors yet.
44  // The consumer of this service can make new requests while the
45  // service is in this state.
46  REMOTE_SERVICE_OK = 0,
47
48  // Remote service is temporarily unavailable due to network,
49  // authentication or some other temporary failure.
50  // This state may be automatically resolved when the underlying
51  // network condition or service condition changes.
52  // The consumer of this service can still make new requests but
53  // they may fail (with recoverable error code).
54  REMOTE_SERVICE_TEMPORARY_UNAVAILABLE,
55
56  // Remote service is temporarily unavailable due to authentication failure.
57  // This state may be automatically resolved when the authentication token
58  // has been refreshed internally (e.g. when the user signed in etc).
59  // The consumer of this service can still make new requests but
60  // they may fail (with recoverable error code).
61  REMOTE_SERVICE_AUTHENTICATION_REQUIRED,
62
63  // Remote service is temporarily unavailable due to lack of API permissions.
64  // This state may be automatically resolved when the API gets right
65  // permissions to access with.
66  // The consumer of this service can still make new requests but
67  // they may fail (with recoverable error code).
68  REMOTE_SERVICE_ACCESS_FORBIDDEN,
69
70  // Remote service is disabled by configuration change or due to some
71  // unrecoverable errors, e.g. local database corruption.
72  // Any new requests will immediately fail when the service is in
73  // this state.
74  REMOTE_SERVICE_DISABLED,
75
76  REMOTE_SERVICE_STATE_MAX,
77};
78
79// This class represents a backing service of the sync filesystem.
80// This also maintains conflict information, i.e. a list of conflicting files
81// (at least in the current design).
82// Owned by SyncFileSystemService.
83class RemoteFileSyncService {
84 public:
85  enum BackendVersion {
86    V1,
87    V2,
88  };
89
90  class Observer {
91   public:
92    Observer() {}
93    virtual ~Observer() {}
94
95    // This is called when RemoteFileSyncService updates its internal queue
96    // of pending remote changes.
97    // |pending_changes_hint| indicates the pending queue length to help sync
98    // scheduling but the value may not be accurately reflect the real-time
99    // value.
100    virtual void OnRemoteChangeQueueUpdated(int64 pending_changes_hint) = 0;
101
102    // This is called when RemoteFileSyncService updates its state.
103    virtual void OnRemoteServiceStateUpdated(
104        RemoteServiceState state,
105        const std::string& description) {}
106
107   private:
108    DISALLOW_COPY_AND_ASSIGN(Observer);
109  };
110
111  struct Version {
112    std::string id;
113    SyncFileMetadata metadata;
114  };
115
116  enum UninstallFlag {
117    UNINSTALL_AND_PURGE_REMOTE,
118    UNINSTALL_AND_KEEP_REMOTE,
119  };
120
121  // For GetOriginStatusMap.
122  typedef std::map<GURL, std::string> OriginStatusMap;
123  typedef base::Callback<void(scoped_ptr<OriginStatusMap> status_map)>
124      StatusMapCallback;
125
126  // For GetRemoteVersions.
127  typedef base::Callback<void(SyncStatusCode status,
128                              const std::vector<Version>& versions)>
129      RemoteVersionsCallback;
130  typedef base::Callback<void(SyncStatusCode status,
131                              webkit_blob::ScopedFile downloaded)>
132      DownloadVersionCallback;
133
134  // For DumpFile.
135  typedef base::Callback<void(scoped_ptr<base::ListValue> list)> ListCallback;
136
137  // Creates an initialized RemoteFileSyncService for backend |version|
138  // for |context|.
139  static scoped_ptr<RemoteFileSyncService> CreateForBrowserContext(
140      BackendVersion version,
141      content::BrowserContext* context,
142      TaskLogger* task_logger);
143
144  // Returns BrowserContextKeyedServiceFactory's an instance of
145  // RemoteFileSyncService for backend |version| depends on.
146  static void AppendDependsOnFactories(
147      BackendVersion version,
148      std::set<BrowserContextKeyedServiceFactory*>* factories);
149
150  RemoteFileSyncService() {}
151  virtual ~RemoteFileSyncService() {}
152
153  // Adds and removes observers.
154  virtual void AddServiceObserver(Observer* observer) = 0;
155  virtual void AddFileStatusObserver(FileStatusObserver* observer) = 0;
156
157  // Registers |origin| to track remote side changes for the |origin|.
158  // Upon completion, invokes |callback|.
159  // The caller may call this method again when the remote service state
160  // migrates to REMOTE_SERVICE_OK state if the error code returned via
161  // |callback| was retriable ones.
162  virtual void RegisterOrigin(
163      const GURL& origin,
164      const SyncStatusCallback& callback) = 0;
165
166  // Re-enables |origin| that was previously disabled. If |origin| is not a
167  // SyncFS app, then the origin is effectively ignored.
168  virtual void EnableOrigin(
169      const GURL& origin,
170      const SyncStatusCallback& callback) = 0;
171
172  virtual void DisableOrigin(
173      const GURL& origin,
174      const SyncStatusCallback& callback) = 0;
175
176  // Uninstalls the |origin| by deleting its remote data copy and then removing
177  // the origin from the metadata store.
178  virtual void UninstallOrigin(
179      const GURL& origin,
180      UninstallFlag flag,
181      const SyncStatusCallback& callback) = 0;
182
183  // Called by the sync engine to process one remote change.
184  // After a change is processed |callback| will be called (to return
185  // the control to the sync engine).
186  // It is invalid to call this before calling SetRemoteChangeProcessor().
187  virtual void ProcessRemoteChange(const SyncFileCallback& callback) = 0;
188
189  // Sets a remote change processor.  This must be called before any
190  // ProcessRemoteChange().
191  virtual void SetRemoteChangeProcessor(
192      RemoteChangeProcessor* processor) = 0;
193
194  // Returns a LocalChangeProcessor that applies a local change to the remote
195  // storage backed by this service.
196  virtual LocalChangeProcessor* GetLocalChangeProcessor() = 0;
197
198  // Returns the current remote service state (should equal to the value
199  // returned by the last OnRemoteServiceStateUpdated notification.
200  virtual RemoteServiceState GetCurrentState() const = 0;
201
202  // Returns all origins along with an arbitrary string description of their
203  // corresponding sync statuses.
204  virtual void GetOriginStatusMap(const StatusMapCallback& callback) = 0;
205
206  // Returns file metadata for |origin| to call |callback|.
207  virtual void DumpFiles(const GURL& origin,
208                         const ListCallback& callback) = 0;
209
210  // Returns the dump of internal database.
211  virtual void DumpDatabase(const ListCallback& callback) = 0;
212
213  // Enables or disables the background sync.
214  // Setting this to false should disable the synchronization (and make
215  // the service state to REMOTE_SERVICE_DISABLED), while setting this to
216  // true does not necessarily mean the service is actually turned on
217  // (for example if Chrome is offline the service state will become
218  // REMOTE_SERVICE_TEMPORARY_UNAVAILABLE).
219  virtual void SetSyncEnabled(bool enabled) = 0;
220
221  virtual void PromoteDemotedChanges(const base::Closure& callback) = 0;
222
223 private:
224  DISALLOW_COPY_AND_ASSIGN(RemoteFileSyncService);
225};
226
227}  // namespace sync_file_system
228
229#endif  // CHROME_BROWSER_SYNC_FILE_SYSTEM_REMOTE_FILE_SYNC_SERVICE_H_
230