| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef COMPONENTS_SYNC_DRIVER_GLUE_SYNC_BACKEND_HOST_IMPL_H_ | 5 #ifndef COMPONENTS_SYNC_DRIVER_GLUE_SYNC_BACKEND_HOST_IMPL_H_ |
| 6 #define COMPONENTS_SYNC_DRIVER_GLUE_SYNC_BACKEND_HOST_IMPL_H_ | 6 #define COMPONENTS_SYNC_DRIVER_GLUE_SYNC_BACKEND_HOST_IMPL_H_ |
| 7 | 7 |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <map> | 10 #include <map> |
| 11 #include <memory> | 11 #include <memory> |
| 12 #include <string> | 12 #include <string> |
| 13 #include <vector> | 13 #include <vector> |
| 14 | 14 |
| 15 #include "base/callback.h" | 15 #include "base/callback.h" |
| 16 #include "base/compiler_specific.h" | 16 #include "base/compiler_specific.h" |
| 17 #include "base/macros.h" | 17 #include "base/macros.h" |
| 18 #include "base/memory/ref_counted.h" | 18 #include "base/memory/ref_counted.h" |
| 19 #include "base/memory/weak_ptr.h" | 19 #include "base/memory/weak_ptr.h" |
| 20 #include "base/single_thread_task_runner.h" | 20 #include "base/threading/thread_checker.h" |
| 21 #include "base/threading/thread.h" | |
| 22 #include "components/invalidation/public/invalidation_handler.h" | 21 #include "components/invalidation/public/invalidation_handler.h" |
| 23 #include "components/sync/base/extensions_activity.h" | 22 #include "components/sync/base/extensions_activity.h" |
| 24 #include "components/sync/base/model_type.h" | 23 #include "components/sync/base/model_type.h" |
| 25 #include "components/sync/base/weak_handle.h" | 24 #include "components/sync/base/weak_handle.h" |
| 26 #include "components/sync/driver/backend_data_type_configurer.h" | |
| 27 #include "components/sync/driver/glue/sync_backend_host.h" | |
| 28 #include "components/sync/engine/configure_reason.h" | 25 #include "components/sync/engine/configure_reason.h" |
| 29 #include "components/sync/engine/cycle/sync_cycle_snapshot.h" | 26 #include "components/sync/engine/cycle/sync_cycle_snapshot.h" |
| 30 #include "components/sync/engine/cycle/type_debug_info_observer.h" | 27 #include "components/sync/engine/cycle/type_debug_info_observer.h" |
| 28 #include "components/sync/engine/model_type_configurer.h" |
| 29 #include "components/sync/engine/sync_engine.h" |
| 31 #include "components/sync/engine/sync_manager.h" | 30 #include "components/sync/engine/sync_manager.h" |
| 32 #include "components/sync/protocol/encryption.pb.h" | 31 #include "components/sync/protocol/encryption.pb.h" |
| 33 #include "components/sync/protocol/sync_protocol_error.h" | 32 #include "components/sync/protocol/sync_protocol_error.h" |
| 34 | 33 |
| 35 class GURL; | 34 class GURL; |
| 36 | 35 |
| 37 namespace invalidation { | 36 namespace invalidation { |
| 38 class InvalidationService; | 37 class InvalidationService; |
| 39 } // namespace invalidation | 38 } // namespace invalidation |
| 40 | 39 |
| 41 namespace syncer { | 40 namespace syncer { |
| 42 | 41 |
| 43 class ChangeProcessor; | 42 class ChangeProcessor; |
| 44 class SyncBackendHostCore; | 43 class SyncBackendHostCore; |
| 45 class SyncBackendRegistrar; | 44 class SyncBackendRegistrar; |
| 46 class SyncClient; | 45 class SyncClient; |
| 47 class SyncManagerFactory; | 46 class SyncManagerFactory; |
| 48 class SyncPrefs; | 47 class SyncPrefs; |
| 49 class UnrecoverableErrorHandler; | 48 class UnrecoverableErrorHandler; |
| 50 struct DoInitializeOptions; | 49 struct DoInitializeOptions; |
| 51 | 50 |
| 52 // The only real implementation of the SyncBackendHost. See that interface's | 51 // The only real implementation of the SyncEngine. See that interface's |
| 53 // definition for documentation of public methods. | 52 // definition for documentation of public methods. |
| 54 class SyncBackendHostImpl : public SyncBackendHost, public InvalidationHandler { | 53 class SyncBackendHostImpl : public SyncEngine, public InvalidationHandler { |
| 55 public: | 54 public: |
| 56 typedef SyncStatus Status; | 55 typedef SyncStatus Status; |
| 57 | 56 |
| 58 // Create a SyncBackendHost with a reference to the |frontend| that | |
| 59 // it serves and communicates to via the SyncFrontend interface (on | |
| 60 // the same thread it used to call the constructor). Must outlive | |
| 61 // |sync_prefs|. | |
| 62 SyncBackendHostImpl( | 57 SyncBackendHostImpl( |
| 63 const std::string& name, | 58 const std::string& name, |
| 64 SyncClient* sync_client, | 59 SyncClient* sync_client, |
| 65 invalidation::InvalidationService* invalidator, | 60 invalidation::InvalidationService* invalidator, |
| 66 const base::WeakPtr<SyncPrefs>& sync_prefs, | 61 const base::WeakPtr<SyncPrefs>& sync_prefs, |
| 67 const base::FilePath& sync_folder); | 62 const base::FilePath& sync_folder); |
| 68 ~SyncBackendHostImpl() override; | 63 ~SyncBackendHostImpl() override; |
| 69 | 64 |
| 70 // SyncBackendHost implementation. | 65 // SyncEngine implementation. |
| 71 void Initialize( | 66 void Initialize( |
| 72 SyncFrontend* frontend, | 67 SyncEngineHost* host, |
| 73 scoped_refptr<base::SingleThreadTaskRunner> sync_task_runner, | 68 scoped_refptr<base::SingleThreadTaskRunner> sync_task_runner, |
| 74 const WeakHandle<JsEventHandler>& event_handler, | 69 const WeakHandle<JsEventHandler>& event_handler, |
| 75 const GURL& service_url, | 70 const GURL& service_url, |
| 76 const std::string& sync_user_agent, | 71 const std::string& sync_user_agent, |
| 77 const SyncCredentials& credentials, | 72 const SyncCredentials& credentials, |
| 78 bool delete_sync_data_folder, | 73 bool delete_sync_data_folder, |
| 79 bool enable_local_sync_backend, | 74 bool enable_local_sync_backend, |
| 80 const base::FilePath& local_sync_backend_folder, | 75 const base::FilePath& local_sync_backend_folder, |
| 81 std::unique_ptr<SyncManagerFactory> sync_manager_factory, | 76 std::unique_ptr<SyncManagerFactory> sync_manager_factory, |
| 82 const WeakHandle<UnrecoverableErrorHandler>& unrecoverable_error_handler, | 77 const WeakHandle<UnrecoverableErrorHandler>& unrecoverable_error_handler, |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 163 // manager initialization to be passed back to the UI thread. | 158 // manager initialization to be passed back to the UI thread. |
| 164 // | 159 // |
| 165 // |model_type_connector| is our ModelTypeConnector, which is owned because in | 160 // |model_type_connector| is our ModelTypeConnector, which is owned because in |
| 166 // production it is a proxy object to the real ModelTypeConnector. | 161 // production it is a proxy object to the real ModelTypeConnector. |
| 167 virtual void HandleInitializationSuccessOnFrontendLoop( | 162 virtual void HandleInitializationSuccessOnFrontendLoop( |
| 168 const WeakHandle<JsBackend> js_backend, | 163 const WeakHandle<JsBackend> js_backend, |
| 169 const WeakHandle<DataTypeDebugInfoListener> debug_info_listener, | 164 const WeakHandle<DataTypeDebugInfoListener> debug_info_listener, |
| 170 std::unique_ptr<ModelTypeConnector> model_type_connector, | 165 std::unique_ptr<ModelTypeConnector> model_type_connector, |
| 171 const std::string& cache_guid); | 166 const std::string& cache_guid); |
| 172 | 167 |
| 173 // Forwards a ProtocolEvent to the frontend. Will not be called unless a | 168 // Forwards a ProtocolEvent to the host. Will not be called unless a call to |
| 174 // call to SetForwardProtocolEvents() explicitly requested that we start | 169 // SetForwardProtocolEvents() explicitly requested that we start forwarding |
| 175 // forwarding these events. | 170 // these events. |
| 176 void HandleProtocolEventOnFrontendLoop(std::unique_ptr<ProtocolEvent> event); | 171 void HandleProtocolEventOnFrontendLoop(std::unique_ptr<ProtocolEvent> event); |
| 177 | 172 |
| 178 // Forwards a directory commit counter update to the frontend loop. Will not | 173 // Forwards a directory commit counter update to the frontend loop. Will not |
| 179 // be called unless a call to EnableDirectoryTypeDebugInfoForwarding() | 174 // be called unless a call to EnableDirectoryTypeDebugInfoForwarding() |
| 180 // explicitly requested that we start forwarding these events. | 175 // explicitly requested that we start forwarding these events. |
| 181 void HandleDirectoryCommitCountersUpdatedOnFrontendLoop( | 176 void HandleDirectoryCommitCountersUpdatedOnFrontendLoop( |
| 182 ModelType type, | 177 ModelType type, |
| 183 const CommitCounters& counters); | 178 const CommitCounters& counters); |
| 184 | 179 |
| 185 // Forwards a directory update counter update to the frontend loop. Will not | 180 // Forwards a directory update counter update to the frontend loop. Will not |
| 186 // be called unless a call to EnableDirectoryTypeDebugInfoForwarding() | 181 // be called unless a call to EnableDirectoryTypeDebugInfoForwarding() |
| 187 // explicitly requested that we start forwarding these events. | 182 // explicitly requested that we start forwarding these events. |
| 188 void HandleDirectoryUpdateCountersUpdatedOnFrontendLoop( | 183 void HandleDirectoryUpdateCountersUpdatedOnFrontendLoop( |
| 189 ModelType type, | 184 ModelType type, |
| 190 const UpdateCounters& counters); | 185 const UpdateCounters& counters); |
| 191 | 186 |
| 192 // Forwards a directory status counter update to the frontend loop. Will not | 187 // Forwards a directory status counter update to the frontend loop. Will not |
| 193 // be called unless a call to EnableDirectoryTypeDebugInfoForwarding() | 188 // be called unless a call to EnableDirectoryTypeDebugInfoForwarding() |
| 194 // explicitly requested that we start forwarding these events. | 189 // explicitly requested that we start forwarding these events. |
| 195 void HandleDirectoryStatusCountersUpdatedOnFrontendLoop( | 190 void HandleDirectoryStatusCountersUpdatedOnFrontendLoop( |
| 196 ModelType type, | 191 ModelType type, |
| 197 const StatusCounters& counters); | 192 const StatusCounters& counters); |
| 198 | 193 |
| 199 // Overwrites the kSyncInvalidationVersions preference with the most recent | 194 // Overwrites the kSyncInvalidationVersions preference with the most recent |
| 200 // set of invalidation versions for each type. | 195 // set of invalidation versions for each type. |
| 201 void UpdateInvalidationVersions( | 196 void UpdateInvalidationVersions( |
| 202 const std::map<ModelType, int64_t>& invalidation_versions); | 197 const std::map<ModelType, int64_t>& invalidation_versions); |
| 203 | 198 |
| 204 SyncFrontend* frontend() { return frontend_; } | 199 SyncEngineHost* host() { return host_; } |
| 205 | 200 |
| 206 private: | 201 private: |
| 207 friend class SyncBackendHostCore; | 202 friend class SyncBackendHostCore; |
| 208 | 203 |
| 209 // Checks if we have received a notice to turn on experimental datatypes | 204 // Checks if we have received a notice to turn on experimental datatypes |
| 210 // (via the nigori node) and informs the frontend if that is the case. | 205 // (via the nigori node) and informs the frontend if that is the case. |
| 211 // Note: it is illegal to call this before the backend is initialized. | 206 // Note: it is illegal to call this before the backend is initialized. |
| 212 void AddExperimentalTypes(); | 207 void AddExperimentalTypes(); |
| 213 | 208 |
| 214 // Handles backend initialization failure. | 209 // Handles backend initialization failure. |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 297 | 292 |
| 298 // Our core, which communicates directly to the syncapi. Use refptr instead | 293 // Our core, which communicates directly to the syncapi. Use refptr instead |
| 299 // of WeakHandle because |core_| is created on UI loop but released on | 294 // of WeakHandle because |core_| is created on UI loop but released on |
| 300 // sync loop. | 295 // sync loop. |
| 301 scoped_refptr<SyncBackendHostCore> core_; | 296 scoped_refptr<SyncBackendHostCore> core_; |
| 302 | 297 |
| 303 // A handle referencing the main interface for non-blocking sync types. This | 298 // A handle referencing the main interface for non-blocking sync types. This |
| 304 // object is owned because in production code it is a proxy object. | 299 // object is owned because in production code it is a proxy object. |
| 305 std::unique_ptr<ModelTypeConnector> model_type_connector_; | 300 std::unique_ptr<ModelTypeConnector> model_type_connector_; |
| 306 | 301 |
| 307 bool initialized_; | 302 bool initialized_ = false; |
| 308 | 303 |
| 309 const base::WeakPtr<SyncPrefs> sync_prefs_; | 304 const base::WeakPtr<SyncPrefs> sync_prefs_; |
| 310 | 305 |
| 311 std::unique_ptr<SyncBackendRegistrar> registrar_; | 306 std::unique_ptr<SyncBackendRegistrar> registrar_; |
| 312 | 307 |
| 313 // The frontend which we serve (and are owned by). | 308 // The host which we serve (and are owned by). Set in Initialize() and nulled |
| 314 SyncFrontend* frontend_; | 309 // out in StopSyncingForShutdown(). |
| 310 SyncEngineHost* host_ = nullptr; |
| 315 | 311 |
| 316 // We cache the cryptographer's pending keys whenever NotifyPassphraseRequired | 312 // We cache the cryptographer's pending keys whenever NotifyPassphraseRequired |
| 317 // is called. This way, before the UI calls SetDecryptionPassphrase on the | 313 // is called. This way, before the UI calls SetDecryptionPassphrase on the |
| 318 // syncer, it can avoid the overhead of an asynchronous decryption call and | 314 // syncer, it can avoid the overhead of an asynchronous decryption call and |
| 319 // give the user immediate feedback about the passphrase entered by first | 315 // give the user immediate feedback about the passphrase entered by first |
| 320 // trying to decrypt the cached pending keys on the UI thread. Note that | 316 // trying to decrypt the cached pending keys on the UI thread. Note that |
| 321 // SetDecryptionPassphrase can still fail after the cached pending keys are | 317 // SetDecryptionPassphrase can still fail after the cached pending keys are |
| 322 // successfully decrypted if the pending keys have changed since the time they | 318 // successfully decrypted if the pending keys have changed since the time they |
| 323 // were cached. | 319 // were cached. |
| 324 sync_pb::EncryptedData cached_pending_keys_; | 320 sync_pb::EncryptedData cached_pending_keys_; |
| 325 | 321 |
| 326 // The state of the passphrase required to decrypt the bag of encryption keys | 322 // The state of the passphrase required to decrypt the bag of encryption keys |
| 327 // in the nigori node. Updated whenever a new nigori node arrives or the user | 323 // in the nigori node. Updated whenever a new nigori node arrives or the user |
| 328 // manually changes their passphrase state. Cached so we can synchronously | 324 // manually changes their passphrase state. Cached so we can synchronously |
| 329 // check it from the UI thread. | 325 // check it from the UI thread. |
| 330 PassphraseType cached_passphrase_type_; | 326 PassphraseType cached_passphrase_type_ = PassphraseType::IMPLICIT_PASSPHRASE; |
| 331 | 327 |
| 332 // If an explicit passphrase is in use, the time at which the passphrase was | 328 // If an explicit passphrase is in use, the time at which the passphrase was |
| 333 // first set (if available). | 329 // first set (if available). |
| 334 base::Time cached_explicit_passphrase_time_; | 330 base::Time cached_explicit_passphrase_time_; |
| 335 | 331 |
| 336 // UI-thread cache of the last SyncCycleSnapshot received from syncapi. | 332 // UI-thread cache of the last SyncCycleSnapshot received from syncapi. |
| 337 SyncCycleSnapshot last_snapshot_; | 333 SyncCycleSnapshot last_snapshot_; |
| 338 | 334 |
| 339 invalidation::InvalidationService* invalidator_; | 335 invalidation::InvalidationService* invalidator_; |
| 340 bool invalidation_handler_registered_; | 336 bool invalidation_handler_registered_ = false; |
| 341 | 337 |
| 342 // Checks that we're on the same thread this was constructed on (UI thread). | 338 // Checks that we're on the same thread this was constructed on (UI thread). |
| 343 base::ThreadChecker thread_checker_; | 339 base::ThreadChecker thread_checker_; |
| 344 | 340 |
| 345 base::WeakPtrFactory<SyncBackendHostImpl> weak_ptr_factory_; | 341 base::WeakPtrFactory<SyncBackendHostImpl> weak_ptr_factory_; |
| 346 | 342 |
| 347 DISALLOW_COPY_AND_ASSIGN(SyncBackendHostImpl); | 343 DISALLOW_COPY_AND_ASSIGN(SyncBackendHostImpl); |
| 348 }; | 344 }; |
| 349 | 345 |
| 350 } // namespace syncer | 346 } // namespace syncer |
| 351 | 347 |
| 352 #endif // COMPONENTS_SYNC_DRIVER_GLUE_SYNC_BACKEND_HOST_IMPL_H_ | 348 #endif // COMPONENTS_SYNC_DRIVER_GLUE_SYNC_BACKEND_HOST_IMPL_H_ |
| OLD | NEW |