remote_file_sync_service.h revision 03b57e008b61dfcb1fbad3aea950ae0e001748b0
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 storage {
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  class Observer {
86   public:
87    Observer() {}
88    virtual ~Observer() {}
89
90    // This is called when RemoteFileSyncService updates its internal queue
91    // of pending remote changes.
92    // |pending_changes_hint| indicates the pending queue length to help sync
93    // scheduling but the value may not be accurately reflect the real-time
94    // value.
95    virtual void OnRemoteChangeQueueUpdated(int64 pending_changes_hint) = 0;
96
97    // This is called when RemoteFileSyncService updates its state.
98    virtual void OnRemoteServiceStateUpdated(
99        RemoteServiceState state,
100        const std::string& description) {}
101
102   private:
103    DISALLOW_COPY_AND_ASSIGN(Observer);
104  };
105
106  struct Version {
107    std::string id;
108    SyncFileMetadata metadata;
109  };
110
111  enum UninstallFlag {
112    UNINSTALL_AND_PURGE_REMOTE,
113    UNINSTALL_AND_KEEP_REMOTE,
114  };
115
116  // For GetOriginStatusMap.
117  typedef std::map<GURL, std::string> OriginStatusMap;
118  typedef base::Callback<void(scoped_ptr<OriginStatusMap> status_map)>
119      StatusMapCallback;
120
121  // For GetRemoteVersions.
122  typedef base::Callback<void(SyncStatusCode status,
123                              const std::vector<Version>& versions)>
124      RemoteVersionsCallback;
125  typedef base::Callback<
126      void(SyncStatusCode status, storage::ScopedFile downloaded)>
127      DownloadVersionCallback;
128
129  // For DumpFile.
130  typedef base::Callback<void(scoped_ptr<base::ListValue> list)> ListCallback;
131
132  // Creates an initialized RemoteFileSyncService for backend |version|
133  // for |context|.
134  static scoped_ptr<RemoteFileSyncService> CreateForBrowserContext(
135      content::BrowserContext* context,
136      TaskLogger* task_logger);
137
138  // Returns BrowserContextKeyedServiceFactory's an instance of
139  // RemoteFileSyncService for backend |version| depends on.
140  static void AppendDependsOnFactories(
141      std::set<BrowserContextKeyedServiceFactory*>* factories);
142
143  RemoteFileSyncService() {}
144  virtual ~RemoteFileSyncService() {}
145
146  // Adds and removes observers.
147  virtual void AddServiceObserver(Observer* observer) = 0;
148  virtual void AddFileStatusObserver(FileStatusObserver* observer) = 0;
149
150  // Registers |origin| to track remote side changes for the |origin|.
151  // Upon completion, invokes |callback|.
152  // The caller may call this method again when the remote service state
153  // migrates to REMOTE_SERVICE_OK state if the error code returned via
154  // |callback| was retriable ones.
155  virtual void RegisterOrigin(
156      const GURL& origin,
157      const SyncStatusCallback& callback) = 0;
158
159  // Re-enables |origin| that was previously disabled. If |origin| is not a
160  // SyncFS app, then the origin is effectively ignored.
161  virtual void EnableOrigin(
162      const GURL& origin,
163      const SyncStatusCallback& callback) = 0;
164
165  virtual void DisableOrigin(
166      const GURL& origin,
167      const SyncStatusCallback& callback) = 0;
168
169  // Uninstalls the |origin| by deleting its remote data copy and then removing
170  // the origin from the metadata store.
171  virtual void UninstallOrigin(
172      const GURL& origin,
173      UninstallFlag flag,
174      const SyncStatusCallback& callback) = 0;
175
176  // Called by the sync engine to process one remote change.
177  // After a change is processed |callback| will be called (to return
178  // the control to the sync engine).
179  // It is invalid to call this before calling SetRemoteChangeProcessor().
180  virtual void ProcessRemoteChange(const SyncFileCallback& callback) = 0;
181
182  // Sets a remote change processor.  This must be called before any
183  // ProcessRemoteChange().
184  virtual void SetRemoteChangeProcessor(
185      RemoteChangeProcessor* processor) = 0;
186
187  // Returns a LocalChangeProcessor that applies a local change to the remote
188  // storage backed by this service.
189  virtual LocalChangeProcessor* GetLocalChangeProcessor() = 0;
190
191  // Returns the current remote service state (should equal to the value
192  // returned by the last OnRemoteServiceStateUpdated notification.
193  virtual RemoteServiceState GetCurrentState() const = 0;
194
195  // Returns all origins along with an arbitrary string description of their
196  // corresponding sync statuses.
197  virtual void GetOriginStatusMap(const StatusMapCallback& callback) = 0;
198
199  // Returns file metadata for |origin| to call |callback|.
200  virtual void DumpFiles(const GURL& origin,
201                         const ListCallback& callback) = 0;
202
203  // Returns the dump of internal database.
204  virtual void DumpDatabase(const ListCallback& callback) = 0;
205
206  // Enables or disables the background sync.
207  // Setting this to false should disable the synchronization (and make
208  // the service state to REMOTE_SERVICE_DISABLED), while setting this to
209  // true does not necessarily mean the service is actually turned on
210  // (for example if Chrome is offline the service state will become
211  // REMOTE_SERVICE_TEMPORARY_UNAVAILABLE).
212  virtual void SetSyncEnabled(bool enabled) = 0;
213
214  virtual void PromoteDemotedChanges(const base::Closure& callback) = 0;
215
216 private:
217  DISALLOW_COPY_AND_ASSIGN(RemoteFileSyncService);
218};
219
220}  // namespace sync_file_system
221
222#endif  // CHROME_BROWSER_SYNC_FILE_SYSTEM_REMOTE_FILE_SYNC_SERVICE_H_
223