remote_file_sync_service.h revision 2385ea399aae016c0806a4f9ef3c9cfe3d2a39df
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 <string>
10
11#include "base/basictypes.h"
12#include "chrome/browser/sync_file_system/conflict_resolution_policy.h"
13#include "chrome/browser/sync_file_system/sync_callbacks.h"
14#include "webkit/browser/fileapi/file_system_url.h"
15
16class GURL;
17
18namespace base {
19class ListValue;
20}
21
22namespace sync_file_system {
23
24class FileStatusObserver;
25class LocalChangeProcessor;
26class RemoteChangeProcessor;
27
28enum RemoteServiceState {
29  // Remote service is up and running, or has not seen any errors yet.
30  // The consumer of this service can make new requests while the
31  // service is in this state.
32  REMOTE_SERVICE_OK,
33
34  // Remote service is temporarily unavailable due to network,
35  // authentication or some other temporary failure.
36  // This state may be automatically resolved when the underlying
37  // network condition or service condition changes.
38  // The consumer of this service can still make new requests but
39  // they may fail (with recoverable error code).
40  REMOTE_SERVICE_TEMPORARY_UNAVAILABLE,
41
42  // Remote service is temporarily unavailable due to authentication failure.
43  // This state may be automatically resolved when the authentication token
44  // has been refreshed internally (e.g. when the user signed in etc).
45  // The consumer of this service can still make new requests but
46  // they may fail (with recoverable error code).
47  REMOTE_SERVICE_AUTHENTICATION_REQUIRED,
48
49  // Remote service is disabled by configuration change or due to some
50  // unrecoverable errors, e.g. local database corruption.
51  // Any new requests will immediately fail when the service is in
52  // this state.
53  REMOTE_SERVICE_DISABLED,
54};
55
56// This class represents a backing service of the sync filesystem.
57// This also maintains conflict information, i.e. a list of conflicting files
58// (at least in the current design).
59// Owned by SyncFileSystemService.
60class RemoteFileSyncService {
61 public:
62  class Observer {
63   public:
64    Observer() {}
65    virtual ~Observer() {}
66
67    // This is called when RemoteFileSyncService updates its internal queue
68    // of pending remote changes.
69    // |pending_changes_hint| indicates the pending queue length to help sync
70    // scheduling but the value may not be accurately reflect the real-time
71    // value.
72    virtual void OnRemoteChangeQueueUpdated(int64 pending_changes_hint) = 0;
73
74    // This is called when RemoteFileSyncService updates its state.
75    virtual void OnRemoteServiceStateUpdated(
76        RemoteServiceState state,
77        const std::string& description) {}
78
79   private:
80    DISALLOW_COPY_AND_ASSIGN(Observer);
81  };
82
83  RemoteFileSyncService() {}
84  virtual ~RemoteFileSyncService() {}
85
86  // Adds and removes observers.
87  virtual void AddServiceObserver(Observer* observer) = 0;
88  virtual void AddFileStatusObserver(FileStatusObserver* observer) = 0;
89
90  // Registers |origin| to track remote side changes for the |origin|.
91  // Upon completion, invokes |callback|.
92  // The caller may call this method again when the remote service state
93  // migrates to REMOTE_SERVICE_OK state if the error code returned via
94  // |callback| was retriable ones.
95  virtual void RegisterOriginForTrackingChanges(
96      const GURL& origin,
97      const SyncStatusCallback& callback) = 0;
98
99  // Unregisters |origin| to track remote side changes for the |origin|.
100  // Upon completion, invokes |callback|.
101  // The caller may call this method again when the remote service state
102  // migrates to REMOTE_SERVICE_OK state if the error code returned via
103  // |callback| was retriable ones.
104  virtual void UnregisterOriginForTrackingChanges(
105      const GURL& origin,
106      const SyncStatusCallback& callback) = 0;
107
108  // Re-enables |origin| that was previously disabled. If |origin| is not a
109  // SyncFS app, then the origin is effectively ignored.
110  virtual void EnableOriginForTrackingChanges(
111      const GURL& origin,
112      const SyncStatusCallback& callback) = 0;
113
114  virtual void DisableOriginForTrackingChanges(
115      const GURL& origin,
116      const SyncStatusCallback& callback) = 0;
117
118  // Uninstalls the |origin| by deleting its remote data copy and then removing
119  // the origin from the metadata store.
120  virtual void UninstallOrigin(
121      const GURL& origin,
122      const SyncStatusCallback& callback) = 0;
123
124  // Called by the sync engine to process one remote change.
125  // After a change is processed |callback| will be called (to return
126  // the control to the sync engine).
127  // It is invalid to call this before calling SetRemoteChangeProcessor().
128  virtual void ProcessRemoteChange(const SyncFileCallback& callback) = 0;
129
130  // Sets a remote change processor.  This must be called before any
131  // ProcessRemoteChange().
132  virtual void SetRemoteChangeProcessor(
133      RemoteChangeProcessor* processor) = 0;
134
135  // Returns a LocalChangeProcessor that applies a local change to the remote
136  // storage backed by this service.
137  virtual LocalChangeProcessor* GetLocalChangeProcessor() = 0;
138
139  // Returns true if the file |url| is marked conflicted in the remote service.
140  virtual bool IsConflicting(const fileapi::FileSystemURL& url) = 0;
141
142  // Returns the current remote service state (should equal to the value
143  // returned by the last OnRemoteServiceStateUpdated notification.
144  virtual RemoteServiceState GetCurrentState() const = 0;
145
146  // Returns all origins along with an arbitrary string description of their
147  // corresponding sync statuses.
148  typedef std::map<GURL, std::string> OriginStatusMap;
149  virtual void GetOriginStatusMap(OriginStatusMap* status_map) = 0;
150
151  // Returns file metadata for |origin|.
152  virtual scoped_ptr<base::ListValue> DumpFiles(const GURL& origin) = 0;
153
154  // Enables or disables the background sync.
155  // Setting this to false should disable the synchronization (and make
156  // the service state to REMOTE_SERVICE_DISABLED), while setting this to
157  // true does not necessarily mean the service is actually turned on
158  // (for example if Chrome is offline the service state will become
159  // REMOTE_SERVICE_TEMPORARY_UNAVAILABLE).
160  virtual void SetSyncEnabled(bool enabled) = 0;
161
162  // Sets the conflict resolution policy. Returns SYNC_STATUS_OK on success,
163  // or returns an error code if the given policy is not supported or had
164  // an error.
165  virtual SyncStatusCode SetConflictResolutionPolicy(
166      ConflictResolutionPolicy policy) = 0;
167
168  // Gets the conflict resolution policy.
169  virtual ConflictResolutionPolicy GetConflictResolutionPolicy() const = 0;
170
171 private:
172  DISALLOW_COPY_AND_ASSIGN(RemoteFileSyncService);
173};
174
175}  // namespace sync_file_system
176
177#endif  // CHROME_BROWSER_SYNC_FILE_SYSTEM_REMOTE_FILE_SYNC_SERVICE_H_
178