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