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