remote_file_sync_service.h revision 1e9bf3e0803691d0a228da41fc608347b6db4340
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 enum UninstallFlag { 95 UNINSTALL_AND_PURGE_REMOTE, 96 UNINSTALL_AND_KEEP_REMOTE, 97 }; 98 99 // For GetOriginStatusMap. 100 typedef std::map<GURL, std::string> OriginStatusMap; 101 102 // For GetRemoteVersions. 103 typedef base::Callback<void(SyncStatusCode status, 104 const std::vector<Version>& versions)> 105 RemoteVersionsCallback; 106 typedef base::Callback<void(SyncStatusCode status, 107 webkit_blob::ScopedFile downloaded)> 108 DownloadVersionCallback; 109 110 RemoteFileSyncService() {} 111 virtual ~RemoteFileSyncService() {} 112 113 // Adds and removes observers. 114 virtual void AddServiceObserver(Observer* observer) = 0; 115 virtual void AddFileStatusObserver(FileStatusObserver* observer) = 0; 116 117 // Registers |origin| to track remote side changes for the |origin|. 118 // Upon completion, invokes |callback|. 119 // The caller may call this method again when the remote service state 120 // migrates to REMOTE_SERVICE_OK state if the error code returned via 121 // |callback| was retriable ones. 122 virtual void RegisterOrigin( 123 const GURL& origin, 124 const SyncStatusCallback& callback) = 0; 125 126 // Re-enables |origin| that was previously disabled. If |origin| is not a 127 // SyncFS app, then the origin is effectively ignored. 128 virtual void EnableOrigin( 129 const GURL& origin, 130 const SyncStatusCallback& callback) = 0; 131 132 virtual void DisableOrigin( 133 const GURL& origin, 134 const SyncStatusCallback& callback) = 0; 135 136 // Uninstalls the |origin| by deleting its remote data copy and then removing 137 // the origin from the metadata store. 138 virtual void UninstallOrigin( 139 const GURL& origin, 140 UninstallFlag flag, 141 const SyncStatusCallback& callback) = 0; 142 143 // Called by the sync engine to process one remote change. 144 // After a change is processed |callback| will be called (to return 145 // the control to the sync engine). 146 // It is invalid to call this before calling SetRemoteChangeProcessor(). 147 virtual void ProcessRemoteChange(const SyncFileCallback& callback) = 0; 148 149 // Sets a remote change processor. This must be called before any 150 // ProcessRemoteChange(). 151 virtual void SetRemoteChangeProcessor( 152 RemoteChangeProcessor* processor) = 0; 153 154 // Returns a LocalChangeProcessor that applies a local change to the remote 155 // storage backed by this service. 156 virtual LocalChangeProcessor* GetLocalChangeProcessor() = 0; 157 158 // Returns true if the file |url| is marked conflicted in the remote service. 159 virtual bool IsConflicting(const fileapi::FileSystemURL& url) = 0; 160 161 // Returns the current remote service state (should equal to the value 162 // returned by the last OnRemoteServiceStateUpdated notification. 163 virtual RemoteServiceState GetCurrentState() const = 0; 164 165 // Returns all origins along with an arbitrary string description of their 166 // corresponding sync statuses. 167 virtual void GetOriginStatusMap(OriginStatusMap* status_map) = 0; 168 169 // Returns file metadata for |origin|. 170 virtual scoped_ptr<base::ListValue> DumpFiles(const GURL& origin) = 0; 171 172 // Enables or disables the background sync. 173 // Setting this to false should disable the synchronization (and make 174 // the service state to REMOTE_SERVICE_DISABLED), while setting this to 175 // true does not necessarily mean the service is actually turned on 176 // (for example if Chrome is offline the service state will become 177 // REMOTE_SERVICE_TEMPORARY_UNAVAILABLE). 178 virtual void SetSyncEnabled(bool enabled) = 0; 179 180 // Sets the conflict resolution policy. Returns SYNC_STATUS_OK on success, 181 // or returns an error code if the given policy is not supported or had 182 // an error. 183 virtual SyncStatusCode SetConflictResolutionPolicy( 184 ConflictResolutionPolicy policy) = 0; 185 186 // Gets the conflict resolution policy. 187 virtual ConflictResolutionPolicy GetConflictResolutionPolicy() const = 0; 188 189 // Returns a list of remote versions with their metadata. 190 // This method is typically called for a file which is in conflicting state. 191 virtual void GetRemoteVersions( 192 const fileapi::FileSystemURL& url, 193 const RemoteVersionsCallback& callback) = 0; 194 195 // Downloads the remote image. The |id| should be the ID string for a 196 // version returned by GetRemoteVersions. 197 virtual void DownloadRemoteVersion( 198 const fileapi::FileSystemURL& url, 199 const std::string& id, 200 const DownloadVersionCallback& callback) = 0; 201 202 private: 203 DISALLOW_COPY_AND_ASSIGN(RemoteFileSyncService); 204}; 205 206} // namespace sync_file_system 207 208#endif // CHROME_BROWSER_SYNC_FILE_SYSTEM_REMOTE_FILE_SYNC_SERVICE_H_ 209