desk_sync_bridge.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. // Copyright 2021 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_DESKS_STORAGE_CORE_DESK_SYNC_BRIDGE_H_
  5. #define COMPONENTS_DESKS_STORAGE_CORE_DESK_SYNC_BRIDGE_H_
  6. #include <memory>
  7. #include <string>
  8. #include <vector>
  9. #include "base/guid.h"
  10. #include "base/memory/weak_ptr.h"
  11. #include "base/scoped_observation.h"
  12. #include "base/time/time.h"
  13. #include "components/account_id/account_id.h"
  14. #include "components/desks_storage/core/desk_model.h"
  15. #include "components/sync/base/model_type.h"
  16. #include "components/sync/model/model_type_store.h"
  17. #include "components/sync/model/model_type_sync_bridge.h"
  18. namespace syncer {
  19. class ModelTypeChangeProcessor;
  20. } // namespace syncer
  21. namespace sync_pb {
  22. class WorkspaceDeskSpecifics;
  23. } // namespace sync_pb
  24. namespace ash {
  25. class DeskTemplate;
  26. enum class DeskTemplateType;
  27. } // namespace ash
  28. namespace desks_storage {
  29. // A Sync-backed persistence layer for Workspace Desk.
  30. class DeskSyncBridge : public syncer::ModelTypeSyncBridge, public DeskModel {
  31. public:
  32. DeskSyncBridge(
  33. std::unique_ptr<syncer::ModelTypeChangeProcessor> change_processor,
  34. syncer::OnceModelTypeStoreFactory create_store_callback,
  35. const AccountId& account_id);
  36. DeskSyncBridge(const DeskSyncBridge&) = delete;
  37. DeskSyncBridge& operator=(const DeskSyncBridge&) = delete;
  38. ~DeskSyncBridge() override;
  39. // Converts a WorkspaceDesk proto to its corresponding ash::DeskTemplate.
  40. static std::unique_ptr<ash::DeskTemplate> FromSyncProto(
  41. const sync_pb::WorkspaceDeskSpecifics& pb_entry);
  42. // syncer::ModelTypeSyncBridge overrides.
  43. std::unique_ptr<syncer::MetadataChangeList> CreateMetadataChangeList()
  44. override;
  45. absl::optional<syncer::ModelError> MergeSyncData(
  46. std::unique_ptr<syncer::MetadataChangeList> metadata_change_list,
  47. syncer::EntityChangeList entity_data) override;
  48. absl::optional<syncer::ModelError> ApplySyncChanges(
  49. std::unique_ptr<syncer::MetadataChangeList> metadata_change_list,
  50. syncer::EntityChangeList entity_changes) override;
  51. void GetData(StorageKeyList storage_keys, DataCallback callback) override;
  52. void GetAllDataForDebugging(DataCallback callback) override;
  53. std::string GetClientTag(const syncer::EntityData& entity_data) override;
  54. std::string GetStorageKey(const syncer::EntityData& entity_data) override;
  55. // DeskModel overrides.
  56. DeskModel::GetAllEntriesResult GetAllEntries() override;
  57. void GetEntryByUUID(const std::string& uuid,
  58. GetEntryByUuidCallback callback) override;
  59. void AddOrUpdateEntry(std::unique_ptr<ash::DeskTemplate> new_entry,
  60. AddOrUpdateEntryCallback callback) override;
  61. void DeleteEntry(const std::string& uuid,
  62. DeleteEntryCallback callback) override;
  63. void DeleteAllEntries(DeleteEntryCallback callback) override;
  64. std::size_t GetEntryCount() const override;
  65. std::size_t GetMaxEntryCount() const override;
  66. std::size_t GetSaveAndRecallDeskEntryCount() const override;
  67. std::size_t GetDeskTemplateEntryCount() const override;
  68. std::size_t GetMaxSaveAndRecallDeskEntryCount() const override;
  69. std::size_t GetMaxDeskTemplateEntryCount() const override;
  70. std::vector<base::GUID> GetAllEntryUuids() const override;
  71. bool IsReady() const override;
  72. // Whether this sync bridge is syncing local data to sync. This sync bridge
  73. // still allows user to save desk templates locally when users disable syncing
  74. // for Workspace Desk model type.
  75. bool IsSyncing() const override;
  76. ash::DeskTemplate* FindOtherEntryWithName(
  77. const std::u16string& name,
  78. ash::DeskTemplateType type,
  79. const base::GUID& uuid) const override;
  80. // Other helper methods.
  81. // Converts an ash::DeskTemplate to its corresponding WorkspaceDesk proto.
  82. sync_pb::WorkspaceDeskSpecifics ToSyncProto(
  83. const ash::DeskTemplate* desk_template);
  84. bool HasUuid(const std::string& uuid_str) const;
  85. const ash::DeskTemplate* GetUserEntryByUUID(const base::GUID& uuid) const;
  86. DeskModel::GetAllEntriesStatus GetAllEntries(
  87. std::vector<const ash::DeskTemplate*>& entries);
  88. DeskModel::DeleteEntryStatus DeleteAllEntries();
  89. private:
  90. using DeskEntries = std::map<base::GUID, std::unique_ptr<ash::DeskTemplate>>;
  91. // Notify all observers that the model is loaded;
  92. void NotifyDeskModelLoaded();
  93. // Notify all observers of any `new_entries` when they are added/updated via
  94. // sync.
  95. void NotifyRemoteDeskTemplateAddedOrUpdated(
  96. const std::vector<const ash::DeskTemplate*>& new_entries);
  97. // Notify all observers when the entries with `uuids` have been removed via
  98. // sync or disabling sync locally.
  99. void NotifyRemoteDeskTemplateDeleted(const std::vector<std::string>& uuids);
  100. // Methods used as callbacks given to DataTypeStore.
  101. void OnStoreCreated(const absl::optional<syncer::ModelError>& error,
  102. std::unique_ptr<syncer::ModelTypeStore> store);
  103. void OnReadAllData(std::unique_ptr<DeskEntries> initial_entries,
  104. const absl::optional<syncer::ModelError>& error);
  105. void OnReadAllMetadata(const absl::optional<syncer::ModelError>& error,
  106. std::unique_ptr<syncer::MetadataBatch> metadata_batch);
  107. void OnCommit(const absl::optional<syncer::ModelError>& error);
  108. // Persists changes in sync store.
  109. void Commit(std::unique_ptr<syncer::ModelTypeStore::WriteBatch> batch);
  110. // Uploads data that only exists locally to Sync during MergeSyncData().
  111. void UploadLocalOnlyData(syncer::MetadataChangeList* metadata_change_list,
  112. const syncer::EntityChangeList& entity_data);
  113. // Returns true if `templates_` contains a desk template with `name`.
  114. bool HasUserTemplateWithName(const std::u16string& name);
  115. // `desk_template_entries_` is keyed by UUIDs.
  116. DeskEntries desk_template_entries_;
  117. // Whether local data and metadata have finished loading and this sync bridge
  118. // is ready to be accessed.
  119. bool is_ready_;
  120. // In charge of actually persisting changes to disk, or loading previous data.
  121. std::unique_ptr<syncer::ModelTypeStore> store_;
  122. // Account ID of the user this class will sync data for.
  123. const AccountId account_id_;
  124. base::WeakPtrFactory<DeskSyncBridge> weak_ptr_factory_{this};
  125. };
  126. } // namespace desks_storage
  127. #endif // COMPONENTS_DESKS_STORAGE_CORE_DESK_SYNC_BRIDGE_H_