| OLD | NEW |
| (Empty) |
| 1 // Copyright 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 COMPONENTS_SYNC_CORE_SYNC_MANAGER_H_ | |
| 6 #define COMPONENTS_SYNC_CORE_SYNC_MANAGER_H_ | |
| 7 | |
| 8 #include <stdint.h> | |
| 9 | |
| 10 #include <memory> | |
| 11 #include <string> | |
| 12 #include <vector> | |
| 13 | |
| 14 #include "base/callback.h" | |
| 15 #include "base/files/file_path.h" | |
| 16 #include "base/memory/ref_counted.h" | |
| 17 #include "base/task_runner.h" | |
| 18 #include "base/threading/thread_checker.h" | |
| 19 #include "components/sync/base/invalidation_interface.h" | |
| 20 #include "components/sync/base/model_type.h" | |
| 21 #include "components/sync/base/weak_handle.h" | |
| 22 #include "components/sync/core/configure_reason.h" | |
| 23 #include "components/sync/core/connection_status.h" | |
| 24 #include "components/sync/core/internal_components_factory.h" | |
| 25 #include "components/sync/core/model_type_connector.h" | |
| 26 #include "components/sync/core/shutdown_reason.h" | |
| 27 #include "components/sync/core/sync_encryption_handler.h" | |
| 28 #include "components/sync/engine/events/protocol_event.h" | |
| 29 #include "components/sync/engine/model_safe_worker.h" | |
| 30 #include "components/sync/engine/net/http_post_provider_factory.h" | |
| 31 #include "components/sync/engine/sync_status.h" | |
| 32 #include "components/sync/protocol/sync_protocol_error.h" | |
| 33 #include "components/sync/syncable/change_record.h" | |
| 34 #include "google_apis/gaia/oauth2_token_service.h" | |
| 35 | |
| 36 class GURL; | |
| 37 | |
| 38 namespace sync_pb { | |
| 39 class EncryptedData; | |
| 40 } // namespace sync_pb | |
| 41 | |
| 42 namespace syncer { | |
| 43 | |
| 44 class BaseTransaction; | |
| 45 class CancelationSignal; | |
| 46 class DataTypeDebugInfoListener; | |
| 47 class Encryptor; | |
| 48 class ExtensionsActivity; | |
| 49 class InternalComponentsFactory; | |
| 50 class JsBackend; | |
| 51 class JsEventHandler; | |
| 52 class ProtocolEvent; | |
| 53 class SyncCycleSnapshot; | |
| 54 class SyncEncryptionHandler; | |
| 55 class SyncScheduler; | |
| 56 class TypeDebugInfoObserver; | |
| 57 class UnrecoverableErrorHandler; | |
| 58 struct Experiments; | |
| 59 struct UserShare; | |
| 60 | |
| 61 // Contains everything needed to talk to and identify a user account. | |
| 62 struct SyncCredentials { | |
| 63 SyncCredentials(); | |
| 64 SyncCredentials(const SyncCredentials& other); | |
| 65 ~SyncCredentials(); | |
| 66 | |
| 67 // Account_id of signed in account. | |
| 68 std::string account_id; | |
| 69 | |
| 70 // The email associated with this account. | |
| 71 std::string email; | |
| 72 | |
| 73 // The raw authentication token's bytes. | |
| 74 std::string sync_token; | |
| 75 | |
| 76 // The set of scopes to use when talking to sync server. | |
| 77 OAuth2TokenService::ScopeSet scope_set; | |
| 78 }; | |
| 79 | |
| 80 // SyncManager encapsulates syncable::Directory and serves as the parent of all | |
| 81 // other objects in the sync API. If multiple threads interact with the same | |
| 82 // local sync repository (i.e. the same sqlite database), they should share a | |
| 83 // single SyncManager instance. The caller should typically create one | |
| 84 // SyncManager for the lifetime of a user session. | |
| 85 // | |
| 86 // Unless stated otherwise, all methods of SyncManager should be called on the | |
| 87 // same thread. | |
| 88 class SyncManager { | |
| 89 public: | |
| 90 // An interface the embedding application implements to be notified | |
| 91 // on change events. Note that these methods may be called on *any* | |
| 92 // thread. | |
| 93 class ChangeDelegate { | |
| 94 public: | |
| 95 // Notify the delegate that changes have been applied to the sync model. | |
| 96 // | |
| 97 // This will be invoked on the same thread as on which ApplyChanges was | |
| 98 // called. |changes| is an array of size |change_count|, and contains the | |
| 99 // ID of each individual item that was changed. |changes| exists only for | |
| 100 // the duration of the call. If items of multiple data types change at | |
| 101 // the same time, this method is invoked once per data type and |changes| | |
| 102 // is restricted to items of the ModelType indicated by |model_type|. | |
| 103 // Because the observer is passed a |trans|, the observer can assume a | |
| 104 // read lock on the sync model that will be released after the function | |
| 105 // returns. | |
| 106 // | |
| 107 // The SyncManager constructs |changes| in the following guaranteed order: | |
| 108 // | |
| 109 // 1. Deletions, from leaves up to parents. | |
| 110 // 2. Updates to existing items with synced parents & predecessors. | |
| 111 // 3. New items with synced parents & predecessors. | |
| 112 // 4. Items with parents & predecessors in |changes|. | |
| 113 // 5. Repeat #4 until all items are in |changes|. | |
| 114 // | |
| 115 // Thus, an implementation of OnChangesApplied should be able to | |
| 116 // process the change records in the order without having to worry about | |
| 117 // forward dependencies. But since deletions come before reparent | |
| 118 // operations, a delete may temporarily orphan a node that is | |
| 119 // updated later in the list. | |
| 120 virtual void OnChangesApplied(ModelType model_type, | |
| 121 int64_t model_version, | |
| 122 const BaseTransaction* trans, | |
| 123 const ImmutableChangeRecordList& changes) = 0; | |
| 124 | |
| 125 // OnChangesComplete gets called when the TransactionComplete event is | |
| 126 // posted (after OnChangesApplied finishes), after the transaction lock | |
| 127 // and the change channel mutex are released. | |
| 128 // | |
| 129 // The purpose of this function is to support processors that require | |
| 130 // split-transactions changes. For example, if a model processor wants to | |
| 131 // perform blocking I/O due to a change, it should calculate the changes | |
| 132 // while holding the transaction lock (from within OnChangesApplied), buffer | |
| 133 // those changes, let the transaction fall out of scope, and then commit | |
| 134 // those changes from within OnChangesComplete (postponing the blocking | |
| 135 // I/O to when it no longer holds any lock). | |
| 136 virtual void OnChangesComplete(ModelType model_type) = 0; | |
| 137 | |
| 138 protected: | |
| 139 virtual ~ChangeDelegate(); | |
| 140 }; | |
| 141 | |
| 142 // Like ChangeDelegate, except called only on the sync thread and | |
| 143 // not while a transaction is held. For objects that want to know | |
| 144 // when changes happen, but don't need to process them. | |
| 145 class ChangeObserver { | |
| 146 public: | |
| 147 // Ids referred to in |changes| may or may not be in the write | |
| 148 // transaction specified by |write_transaction_id|. If they're | |
| 149 // not, that means that the node didn't actually change, but we | |
| 150 // marked them as changed for some other reason (e.g., siblings of | |
| 151 // re-ordered nodes). | |
| 152 // | |
| 153 // TODO(sync, long-term): Ideally, ChangeDelegate/Observer would | |
| 154 // be passed a transformed version of EntryKernelMutation instead | |
| 155 // of a transaction that would have to be used to look up the | |
| 156 // changed nodes. That is, ChangeDelegate::OnChangesApplied() | |
| 157 // would still be called under the transaction, but all the needed | |
| 158 // data will be passed down. | |
| 159 // | |
| 160 // Even more ideally, we would have sync semantics such that we'd | |
| 161 // be able to apply changes without being under a transaction. | |
| 162 // But that's a ways off... | |
| 163 virtual void OnChangesApplied(ModelType model_type, | |
| 164 int64_t write_transaction_id, | |
| 165 const ImmutableChangeRecordList& changes) = 0; | |
| 166 | |
| 167 virtual void OnChangesComplete(ModelType model_type) = 0; | |
| 168 | |
| 169 protected: | |
| 170 virtual ~ChangeObserver(); | |
| 171 }; | |
| 172 | |
| 173 // An interface the embedding application implements to receive | |
| 174 // notifications from the SyncManager. Register an observer via | |
| 175 // SyncManager::AddObserver. All methods are called only on the | |
| 176 // sync thread. | |
| 177 class Observer { | |
| 178 public: | |
| 179 // A round-trip sync-cycle took place and the syncer has resolved any | |
| 180 // conflicts that may have arisen. | |
| 181 virtual void OnSyncCycleCompleted(const SyncCycleSnapshot& snapshot) = 0; | |
| 182 | |
| 183 // Called when the status of the connection to the sync server has | |
| 184 // changed. | |
| 185 virtual void OnConnectionStatusChange(ConnectionStatus status) = 0; | |
| 186 | |
| 187 // Called when initialization is complete to the point that SyncManager can | |
| 188 // process changes. This does not necessarily mean authentication succeeded | |
| 189 // or that the SyncManager is online. | |
| 190 // IMPORTANT: Creating any type of transaction before receiving this | |
| 191 // notification is illegal! | |
| 192 // WARNING: Calling methods on the SyncManager before receiving this | |
| 193 // message, unless otherwise specified, produces undefined behavior. | |
| 194 | |
| 195 virtual void OnInitializationComplete( | |
| 196 const WeakHandle<JsBackend>& js_backend, | |
| 197 const WeakHandle<DataTypeDebugInfoListener>& debug_info_listener, | |
| 198 bool success, | |
| 199 ModelTypeSet restored_types) = 0; | |
| 200 | |
| 201 virtual void OnActionableError( | |
| 202 const SyncProtocolError& sync_protocol_error) = 0; | |
| 203 | |
| 204 virtual void OnMigrationRequested(ModelTypeSet types) = 0; | |
| 205 | |
| 206 virtual void OnProtocolEvent(const ProtocolEvent& event) = 0; | |
| 207 | |
| 208 protected: | |
| 209 virtual ~Observer(); | |
| 210 }; | |
| 211 | |
| 212 // Arguments for initializing SyncManager. | |
| 213 struct InitArgs { | |
| 214 InitArgs(); | |
| 215 ~InitArgs(); | |
| 216 | |
| 217 // Path in which to create or open sync's sqlite database (aka the | |
| 218 // directory). | |
| 219 base::FilePath database_location; | |
| 220 | |
| 221 // Used to propagate events to chrome://sync-internals. Optional. | |
| 222 WeakHandle<JsEventHandler> event_handler; | |
| 223 | |
| 224 // URL of the sync server. | |
| 225 GURL service_url; | |
| 226 | |
| 227 // Used to communicate with the sync server. | |
| 228 std::unique_ptr<HttpPostProviderFactory> post_factory; | |
| 229 | |
| 230 std::vector<scoped_refptr<ModelSafeWorker>> workers; | |
| 231 | |
| 232 // Must outlive SyncManager. | |
| 233 ExtensionsActivity* extensions_activity; | |
| 234 | |
| 235 // Must outlive SyncManager. | |
| 236 ChangeDelegate* change_delegate; | |
| 237 | |
| 238 // Credentials to be used when talking to the sync server. | |
| 239 SyncCredentials credentials; | |
| 240 | |
| 241 // Unqiuely identifies this client to the invalidation notification server. | |
| 242 std::string invalidator_client_id; | |
| 243 | |
| 244 // Used to boostrap the cryptographer. | |
| 245 std::string restored_key_for_bootstrapping; | |
| 246 std::string restored_keystore_key_for_bootstrapping; | |
| 247 | |
| 248 std::unique_ptr<InternalComponentsFactory> internal_components_factory; | |
| 249 | |
| 250 // Must outlive SyncManager. | |
| 251 Encryptor* encryptor; | |
| 252 | |
| 253 WeakHandle<UnrecoverableErrorHandler> unrecoverable_error_handler; | |
| 254 base::Closure report_unrecoverable_error_function; | |
| 255 | |
| 256 // Carries shutdown requests across threads and will be used to cut short | |
| 257 // any network I/O and tell the syncer to exit early. | |
| 258 // | |
| 259 // Must outlive SyncManager. | |
| 260 CancelationSignal* cancelation_signal; | |
| 261 | |
| 262 // Optional nigori state to be restored. | |
| 263 std::unique_ptr<SyncEncryptionHandler::NigoriState> saved_nigori_state; | |
| 264 }; | |
| 265 | |
| 266 typedef base::Callback<void(void)> ClearServerDataCallback; | |
| 267 | |
| 268 SyncManager(); | |
| 269 virtual ~SyncManager(); | |
| 270 | |
| 271 // Initialize the sync manager using arguments from |args|. | |
| 272 // | |
| 273 // Note, args is passed by non-const pointer because it contains objects like | |
| 274 // unique_ptr. | |
| 275 virtual void Init(InitArgs* args) = 0; | |
| 276 | |
| 277 virtual ModelTypeSet InitialSyncEndedTypes() = 0; | |
| 278 | |
| 279 // Returns those types within |types| that have an empty progress marker | |
| 280 // token. | |
| 281 virtual ModelTypeSet GetTypesWithEmptyProgressMarkerToken( | |
| 282 ModelTypeSet types) = 0; | |
| 283 | |
| 284 // Purge from the directory those types with non-empty progress markers | |
| 285 // but without initial synced ended set. | |
| 286 // Returns false if an error occurred, true otherwise. | |
| 287 virtual bool PurgePartiallySyncedTypes() = 0; | |
| 288 | |
| 289 // Update tokens that we're using in Sync. Email must stay the same. | |
| 290 virtual void UpdateCredentials(const SyncCredentials& credentials) = 0; | |
| 291 | |
| 292 // Put the syncer in normal mode ready to perform nudges and polls. | |
| 293 virtual void StartSyncingNormally(const ModelSafeRoutingInfo& routing_info, | |
| 294 base::Time last_poll_time) = 0; | |
| 295 | |
| 296 // Switches the mode of operation to CONFIGURATION_MODE and performs | |
| 297 // any configuration tasks needed as determined by the params. Once complete, | |
| 298 // syncer will remain in CONFIGURATION_MODE until StartSyncingNormally is | |
| 299 // called. | |
| 300 // Data whose types are not in |new_routing_info| are purged from sync | |
| 301 // directory, unless they're part of |to_ignore|, in which case they're left | |
| 302 // untouched. The purged data is backed up in delete journal for recovery in | |
| 303 // next session if its type is in |to_journal|. If in |to_unapply| | |
| 304 // only the local data is removed; the server data is preserved. | |
| 305 // |ready_task| is invoked when the configuration completes. | |
| 306 // |retry_task| is invoked if the configuration job could not immediately | |
| 307 // execute. |ready_task| will still be called when it eventually | |
| 308 // does finish. | |
| 309 virtual void ConfigureSyncer(ConfigureReason reason, | |
| 310 ModelTypeSet to_download, | |
| 311 ModelTypeSet to_purge, | |
| 312 ModelTypeSet to_journal, | |
| 313 ModelTypeSet to_unapply, | |
| 314 const ModelSafeRoutingInfo& new_routing_info, | |
| 315 const base::Closure& ready_task, | |
| 316 const base::Closure& retry_task) = 0; | |
| 317 | |
| 318 // Inform the syncer of a change in the invalidator's state. | |
| 319 virtual void SetInvalidatorEnabled(bool invalidator_enabled) = 0; | |
| 320 | |
| 321 // Inform the syncer that its cached information about a type is obsolete. | |
| 322 virtual void OnIncomingInvalidation( | |
| 323 ModelType type, | |
| 324 std::unique_ptr<InvalidationInterface> invalidation) = 0; | |
| 325 | |
| 326 // Adds a listener to be notified of sync events. | |
| 327 // NOTE: It is OK (in fact, it's probably a good idea) to call this before | |
| 328 // having received OnInitializationCompleted. | |
| 329 virtual void AddObserver(Observer* observer) = 0; | |
| 330 | |
| 331 // Remove the given observer. Make sure to call this if the | |
| 332 // Observer is being destroyed so the SyncManager doesn't | |
| 333 // potentially dereference garbage. | |
| 334 virtual void RemoveObserver(Observer* observer) = 0; | |
| 335 | |
| 336 // Status-related getter. May be called on any thread. | |
| 337 virtual SyncStatus GetDetailedStatus() const = 0; | |
| 338 | |
| 339 // Call periodically from a database-safe thread to persist recent changes | |
| 340 // to the syncapi model. | |
| 341 virtual void SaveChanges() = 0; | |
| 342 | |
| 343 // Issue a final SaveChanges, and close sqlite handles. | |
| 344 virtual void ShutdownOnSyncThread(ShutdownReason reason) = 0; | |
| 345 | |
| 346 // May be called from any thread. | |
| 347 virtual UserShare* GetUserShare() = 0; | |
| 348 | |
| 349 // Returns an instance of the main interface for non-blocking sync types. | |
| 350 virtual std::unique_ptr<ModelTypeConnector> GetModelTypeConnectorProxy() = 0; | |
| 351 | |
| 352 // Returns the cache_guid of the currently open database. | |
| 353 // Requires that the SyncManager be initialized. | |
| 354 virtual const std::string cache_guid() = 0; | |
| 355 | |
| 356 // Reads the nigori node to determine if any experimental features should | |
| 357 // be enabled. | |
| 358 // Note: opens a transaction. May be called on any thread. | |
| 359 virtual bool ReceivedExperiment(Experiments* experiments) = 0; | |
| 360 | |
| 361 // Uses a read-only transaction to determine if the directory being synced has | |
| 362 // any remaining unsynced items. May be called on any thread. | |
| 363 virtual bool HasUnsyncedItems() = 0; | |
| 364 | |
| 365 // Returns the SyncManager's encryption handler. | |
| 366 virtual SyncEncryptionHandler* GetEncryptionHandler() = 0; | |
| 367 | |
| 368 // Ask the SyncManager to fetch updates for the given types. | |
| 369 virtual void RefreshTypes(ModelTypeSet types) = 0; | |
| 370 | |
| 371 // Returns any buffered protocol events. Does not clear the buffer. | |
| 372 virtual std::vector<std::unique_ptr<ProtocolEvent>> | |
| 373 GetBufferedProtocolEvents() = 0; | |
| 374 | |
| 375 // Functions to manage registrations of DebugInfoObservers. | |
| 376 virtual void RegisterDirectoryTypeDebugInfoObserver( | |
| 377 TypeDebugInfoObserver* observer) = 0; | |
| 378 virtual void UnregisterDirectoryTypeDebugInfoObserver( | |
| 379 TypeDebugInfoObserver* observer) = 0; | |
| 380 virtual bool HasDirectoryTypeDebugInfoObserver( | |
| 381 TypeDebugInfoObserver* observer) = 0; | |
| 382 | |
| 383 // Request that all current counter values be emitted as though they had just | |
| 384 // been updated. Useful for initializing new observers' state. | |
| 385 virtual void RequestEmitDebugInfo() = 0; | |
| 386 | |
| 387 // Clears server data and invokes |callback| when complete. | |
| 388 // | |
| 389 // This is an asynchronous operation that requires interaction with the sync | |
| 390 // server. The operation will automatically be retried with backoff until it | |
| 391 // completes successfully or sync is shutdown. | |
| 392 virtual void ClearServerData(const ClearServerDataCallback& callback) = 0; | |
| 393 | |
| 394 // Updates Sync's tracking of whether the cookie jar has a mismatch with the | |
| 395 // chrome account. See ClientConfigParams proto message for more info. | |
| 396 // Note: this does not trigger a sync cycle. It just updates the sync context. | |
| 397 virtual void OnCookieJarChanged(bool account_mismatch, bool empty_jar) = 0; | |
| 398 }; | |
| 399 | |
| 400 } // namespace syncer | |
| 401 | |
| 402 #endif // COMPONENTS_SYNC_CORE_SYNC_MANAGER_H_ | |
| OLD | NEW |