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