sync_engine.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. #ifndef COMPONENTS_SYNC_ENGINE_SYNC_ENGINE_H_
  5. #define COMPONENTS_SYNC_ENGINE_SYNC_ENGINE_H_
  6. #include <memory>
  7. #include <string>
  8. #include <vector>
  9. #include "base/callback_forward.h"
  10. #include "base/files/file_path.h"
  11. #include "base/memory/raw_ptr.h"
  12. #include "base/memory/scoped_refptr.h"
  13. #include "base/time/time.h"
  14. #include "base/values.h"
  15. #include "components/signin/public/identity_manager/account_info.h"
  16. #include "components/sync/base/extensions_activity.h"
  17. #include "components/sync/base/model_type.h"
  18. #include "components/sync/base/weak_handle.h"
  19. #include "components/sync/engine/configure_reason.h"
  20. #include "components/sync/engine/cycle/sync_cycle_snapshot.h"
  21. #include "components/sync/engine/model_type_configurer.h"
  22. #include "components/sync/engine/shutdown_reason.h"
  23. #include "components/sync/engine/sync_credentials.h"
  24. #include "components/sync/engine/sync_encryption_handler.h"
  25. #include "components/sync/engine/sync_manager_factory.h"
  26. #include "url/gurl.h"
  27. namespace syncer {
  28. class EngineComponentsFactory;
  29. class HttpPostProviderFactory;
  30. class KeyDerivationParams;
  31. class Nigori;
  32. class SyncEngineHost;
  33. struct SyncStatus;
  34. // The interface into the sync engine, which is the part of sync that performs
  35. // communication between model types and the sync server. In prod the engine
  36. // will always live on the sync thread and the object implementing this
  37. // interface will handle crossing threads if necessary.
  38. class SyncEngine : public ModelTypeConfigurer {
  39. public:
  40. using AllNodesCallback =
  41. base::OnceCallback<void(ModelType, base::Value::List)>;
  42. using HttpPostProviderFactoryGetter =
  43. base::OnceCallback<std::unique_ptr<HttpPostProviderFactory>()>;
  44. // Utility struct for holding initialization options.
  45. struct InitParams {
  46. InitParams();
  47. InitParams(const InitParams&) = delete;
  48. InitParams& operator=(const InitParams&) = delete;
  49. InitParams(InitParams&& other);
  50. ~InitParams();
  51. raw_ptr<SyncEngineHost> host = nullptr;
  52. std::unique_ptr<SyncEncryptionHandler::Observer> encryption_observer_proxy;
  53. scoped_refptr<ExtensionsActivity> extensions_activity;
  54. GURL service_url;
  55. SyncEngine::HttpPostProviderFactoryGetter http_factory_getter;
  56. CoreAccountInfo authenticated_account_info;
  57. std::string invalidator_client_id;
  58. std::unique_ptr<SyncManagerFactory> sync_manager_factory;
  59. bool enable_local_sync_backend = false;
  60. base::FilePath local_sync_backend_folder;
  61. std::unique_ptr<EngineComponentsFactory> engine_components_factory;
  62. };
  63. SyncEngine();
  64. SyncEngine(const SyncEngine&) = delete;
  65. SyncEngine& operator=(const SyncEngine&) = delete;
  66. ~SyncEngine() override;
  67. // Kicks off asynchronous initialization. Optionally deletes sync data during
  68. // init in order to make sure we're starting fresh.
  69. //
  70. // |saved_nigori_state| is optional nigori state to restore from a previous
  71. // engine instance. May be null.
  72. virtual void Initialize(InitParams params) = 0;
  73. // Returns whether the asynchronous initialization process has finished.
  74. virtual bool IsInitialized() const = 0;
  75. // Inform the engine to trigger a sync cycle for |types|.
  76. virtual void TriggerRefresh(const ModelTypeSet& types) = 0;
  77. // Updates the engine's SyncCredentials. The credentials must be fully
  78. // specified (account ID, email, and sync token). To invalidate the
  79. // credentials, use InvalidateCredentials() instead.
  80. virtual void UpdateCredentials(const SyncCredentials& credentials) = 0;
  81. // Invalidates the SyncCredentials.
  82. virtual void InvalidateCredentials() = 0;
  83. // Transport metadata getters.
  84. virtual std::string GetCacheGuid() const = 0;
  85. virtual std::string GetBirthday() const = 0;
  86. virtual base::Time GetLastSyncedTimeForDebugging() const = 0;
  87. // Switches sync engine into configuration mode. In this mode only initial
  88. // data for newly enabled types is downloaded from server. No local changes
  89. // are committed to server.
  90. virtual void StartConfiguration() = 0;
  91. // This starts the sync engine running a Syncer object to communicate with
  92. // sync servers. Until this is called, no changes will leave or enter this
  93. // browser from the cloud / sync servers.
  94. virtual void StartSyncingWithServer() = 0;
  95. // Starts handling incoming standalone invalidations. This method must be
  96. // called when data types are configured.
  97. virtual void StartHandlingInvalidations() = 0;
  98. // Asynchronously set a new passphrase for encryption. Note that it is an
  99. // error to call SetEncryptionPassphrase under the following circumstances:
  100. // - An explicit passphrase has already been set
  101. // - We have pending keys.
  102. virtual void SetEncryptionPassphrase(
  103. const std::string& passphrase,
  104. const KeyDerivationParams& key_derivation_params) = 0;
  105. // Use the provided decryption key to asynchronously attempt decryption. If
  106. // new encrypted keys arrive during the asynchronous call,
  107. // OnPassphraseRequired may be triggered at a later time. It is an error to
  108. // call this when there are no pending keys.
  109. virtual void SetExplicitPassphraseDecryptionKey(
  110. std::unique_ptr<Nigori> key) = 0;
  111. // Analogous to SetExplicitPassphraseDecryptionKey() but specifically for
  112. // TRUSTED_VAULT_PASSPHRASE: it provides new decryption keys that could
  113. // allow decrypting pending Nigori keys. Notifies observers of the result of
  114. // the operation via OnTrustedVaultKeyAccepted if the provided keys
  115. // successfully decrypted pending keys. |done_cb| is invoked at the very end.
  116. virtual void AddTrustedVaultDecryptionKeys(
  117. const std::vector<std::vector<uint8_t>>& keys,
  118. base::OnceClosure done_cb) = 0;
  119. // Kick off shutdown procedure. Attempts to cut short any long-lived or
  120. // blocking sync thread tasks so that the shutdown on sync thread task that
  121. // we're about to post won't have to wait very long.
  122. virtual void StopSyncingForShutdown() = 0;
  123. // See the implementation and Core::DoShutdown for details.
  124. // Must be called *after* StopSyncingForShutdown.
  125. virtual void Shutdown(ShutdownReason reason) = 0;
  126. // Returns current detailed status information.
  127. virtual const SyncStatus& GetDetailedStatus() const = 0;
  128. // Determines if the underlying sync engine has made any local changes to
  129. // items that have not yet been synced with the server.
  130. // ONLY CALL THIS IF OnInitializationComplete was called!
  131. virtual void HasUnsyncedItemsForTest(
  132. base::OnceCallback<void(bool)> cb) const = 0;
  133. // Returns datatypes that are currently throttled.
  134. virtual void GetThrottledDataTypesForTest(
  135. base::OnceCallback<void(ModelTypeSet)> cb) const = 0;
  136. // Requests that the backend forward to the fronent any protocol events in
  137. // its buffer and begin forwarding automatically from now on. Repeated calls
  138. // to this function may result in the same events being emitted several
  139. // times.
  140. virtual void RequestBufferedProtocolEventsAndEnableForwarding() = 0;
  141. // Disables protocol event forwarding.
  142. virtual void DisableProtocolEventForwarding() = 0;
  143. // Notify the syncer that the cookie jar has changed.
  144. // See SyncManager::OnCookieJarChanged.
  145. virtual void OnCookieJarChanged(bool account_mismatch,
  146. base::OnceClosure callback) = 0;
  147. // Enables/Disables invalidations for session sync related datatypes.
  148. virtual void SetInvalidationsForSessionsEnabled(bool enabled) = 0;
  149. // Returns a ListValue representing Nigori node.
  150. virtual void GetNigoriNodeForDebugging(AllNodesCallback callback) = 0;
  151. };
  152. } // namespace syncer
  153. #endif // COMPONENTS_SYNC_ENGINE_SYNC_ENGINE_H_