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