model_type_registry.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // Copyright 2014 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_MODEL_TYPE_REGISTRY_H_
  5. #define COMPONENTS_SYNC_ENGINE_MODEL_TYPE_REGISTRY_H_
  6. #include <map>
  7. #include <memory>
  8. #include <string>
  9. #include <vector>
  10. #include "base/memory/raw_ptr.h"
  11. #include "base/memory/weak_ptr.h"
  12. #include "components/sync/base/model_type.h"
  13. #include "components/sync/base/passphrase_enums.h"
  14. #include "components/sync/engine/commit_and_get_updates_types.h"
  15. #include "components/sync/engine/model_type_connector.h"
  16. #include "components/sync/engine/nudge_handler.h"
  17. #include "components/sync/engine/sync_encryption_handler.h"
  18. namespace syncer {
  19. class CancelationSignal;
  20. class CommitContributor;
  21. class SyncEncryptionHandler;
  22. class ModelTypeWorker;
  23. class UpdateHandler;
  24. using UpdateHandlerMap = std::map<ModelType, UpdateHandler*>;
  25. using CommitContributorMap = std::map<ModelType, CommitContributor*>;
  26. // Keeps track of the sets of active update handlers and commit contributors.
  27. class ModelTypeRegistry : public ModelTypeConnector,
  28. public SyncEncryptionHandler::Observer {
  29. public:
  30. // |nudge_handler|, |cancelation_signal| and |sync_encryption_handler| must
  31. // outlive this object.
  32. ModelTypeRegistry(NudgeHandler* nudge_handler,
  33. CancelationSignal* cancelation_signal,
  34. SyncEncryptionHandler* sync_encryption_handler);
  35. ModelTypeRegistry(const ModelTypeRegistry&) = delete;
  36. ModelTypeRegistry& operator=(const ModelTypeRegistry&) = delete;
  37. ~ModelTypeRegistry() override;
  38. // Implementation of ModelTypeConnector.
  39. void ConnectDataType(
  40. ModelType type,
  41. std::unique_ptr<DataTypeActivationResponse> activation_response) override;
  42. void DisconnectDataType(ModelType type) override;
  43. void SetProxyTabsDatatypeEnabled(bool enabled) override;
  44. // Implementation of SyncEncryptionHandler::Observer.
  45. void OnPassphraseRequired(
  46. const KeyDerivationParams& key_derivation_params,
  47. const sync_pb::EncryptedData& pending_keys) override;
  48. void OnPassphraseAccepted() override;
  49. void OnTrustedVaultKeyRequired() override;
  50. void OnTrustedVaultKeyAccepted() override;
  51. void OnEncryptedTypesChanged(ModelTypeSet encrypted_types,
  52. bool encrypt_everything) override;
  53. void OnCryptographerStateChanged(Cryptographer* cryptographer,
  54. bool has_pending_keys) override;
  55. void OnPassphraseTypeChanged(PassphraseType type,
  56. base::Time passphrase_time) override;
  57. // Gets the set of connected types, which is essentially the set of types that
  58. // the sync engine cares about. For each of these, a worker exists to
  59. // propagate changes between the server and the local model's processor.
  60. ModelTypeSet GetConnectedTypes() const;
  61. bool proxy_tabs_datatype_enabled() const;
  62. // Returns set of types for which initial set of updates was downloaded and
  63. // applied.
  64. ModelTypeSet GetInitialSyncEndedTypes() const;
  65. // Returns the update handler for |type|.
  66. const UpdateHandler* GetUpdateHandler(ModelType type) const;
  67. // Simple getters.
  68. UpdateHandlerMap* update_handler_map();
  69. CommitContributorMap* commit_contributor_map();
  70. KeystoreKeysHandler* keystore_keys_handler();
  71. bool HasUnsyncedItems() const;
  72. base::WeakPtr<ModelTypeConnector> AsWeakPtr();
  73. private:
  74. // Whether PROXY_TABS is enabled, which is not enabled for real (e.g. it
  75. // doesn't have a worker).
  76. bool proxy_tabs_datatype_enabled_ = false;
  77. std::vector<std::unique_ptr<ModelTypeWorker>> connected_model_type_workers_;
  78. // Maps of UpdateHandlers and CommitContributors.
  79. // They do not own any of the objects they point to.
  80. UpdateHandlerMap update_handler_map_;
  81. CommitContributorMap commit_contributor_map_;
  82. const raw_ptr<NudgeHandler> nudge_handler_;
  83. // CancelationSignal is signalled on engine shutdown. It is passed to
  84. // ModelTypeWorker to cancel blocking operation.
  85. const raw_ptr<CancelationSignal> cancelation_signal_;
  86. const raw_ptr<SyncEncryptionHandler> sync_encryption_handler_;
  87. base::WeakPtrFactory<ModelTypeRegistry> weak_ptr_factory_{this};
  88. };
  89. } // namespace syncer
  90. #endif // COMPONENTS_SYNC_ENGINE_MODEL_TYPE_REGISTRY_H_