reading_list_store.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. // Copyright 2016 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_READING_LIST_CORE_READING_LIST_STORE_H_
  5. #define COMPONENTS_READING_LIST_CORE_READING_LIST_STORE_H_
  6. #include <memory>
  7. #include <string>
  8. #include "base/memory/raw_ptr.h"
  9. #include "base/memory/weak_ptr.h"
  10. #include "base/sequence_checker.h"
  11. #include "components/reading_list/core/reading_list_model_storage.h"
  12. #include "components/reading_list/core/reading_list_store_delegate.h"
  13. #include "components/sync/model/model_error.h"
  14. #include "components/sync/model/model_type_store.h"
  15. namespace syncer {
  16. class MutableDataBatch;
  17. }
  18. class ReadingListModel;
  19. // A ReadingListModelStorage storing and syncing data in protobufs.
  20. class ReadingListStore : public ReadingListModelStorage {
  21. public:
  22. ReadingListStore(
  23. syncer::OnceModelTypeStoreFactory create_store_callback,
  24. std::unique_ptr<syncer::ModelTypeChangeProcessor> change_processor);
  25. ReadingListStore(const ReadingListStore&) = delete;
  26. ReadingListStore& operator=(const ReadingListStore&) = delete;
  27. ~ReadingListStore() override;
  28. std::unique_ptr<ScopedBatchUpdate> EnsureBatchCreated() override;
  29. // ReadingListModelStorage implementation
  30. void SetReadingListModel(ReadingListModel* model,
  31. ReadingListStoreDelegate* delegate,
  32. base::Clock* clock) override;
  33. void SaveEntry(const ReadingListEntry& entry) override;
  34. void RemoveEntry(const ReadingListEntry& entry) override;
  35. // Creates an object used to communicate changes in the sync metadata to the
  36. // model type store.
  37. std::unique_ptr<syncer::MetadataChangeList> CreateMetadataChangeList()
  38. override;
  39. // Perform the initial merge between local and sync data. This should only be
  40. // called when a data type is first enabled to start syncing, and there is no
  41. // sync metadata. Best effort should be made to match local and sync data. The
  42. // storage keys in the |entity_data| are populated with GetStorageKey(...),
  43. // local and sync copies of the same entity should resolve to the same storage
  44. // key. Any local pieces of data that are not present in sync should
  45. // immediately be Put(...) to the processor before returning. The same
  46. // MetadataChangeList that was passed into this function can be passed to
  47. // Put(...) calls. Delete(...) can also be called but should not be needed for
  48. // most model types. Durable storage writes, if not able to combine all change
  49. // atomically, should save the metadata after the data changes, so that this
  50. // merge will be re-driven by sync if is not completely saved during the
  51. // current run.
  52. absl::optional<syncer::ModelError> MergeSyncData(
  53. std::unique_ptr<syncer::MetadataChangeList> metadata_change_list,
  54. syncer::EntityChangeList entity_changes) override;
  55. // Apply changes from the sync server locally.
  56. // Please note that |entity_changes| might have fewer entries than
  57. // |metadata_change_list| in case when some of the data changes are filtered
  58. // out, or even be empty in case when a commit confirmation is processed and
  59. // only the metadata needs to persisted.
  60. absl::optional<syncer::ModelError> ApplySyncChanges(
  61. std::unique_ptr<syncer::MetadataChangeList> metadata_change_list,
  62. syncer::EntityChangeList entity_changes) override;
  63. // Returns whether entries respect a strict order for sync and if |rhs| can be
  64. // submitted to sync after |lhs| has been received.
  65. // The order should ensure that there is no sync loop in sync and should be
  66. // submitted to sync in strictly increasing order.
  67. // Entries are in increasing order if all the fields respect increasing order.
  68. // - URL must be the same.
  69. // - update_title_time_us:
  70. // rhs.update_title_time_us >= lhs.update_title_time_us
  71. // - title:
  72. // if rhs.update_title_time_us > lhs.update_title_time_us
  73. // title can be anything
  74. // if rhs.update_title_time_us == lhs.update_title_time_us
  75. // title must verify rhs.title.compare(lhs.title) >= 0
  76. // - creation_time_us:
  77. // rhs.creation_time_us >= lhs.creation_time_us
  78. // - rhs.first_read_time_us:
  79. // if rhs.creation_time_us > lhs.creation_time_us,
  80. // rhs.first_read_time_us can be anything.
  81. // if rhs.creation_time_us == lhs.creation_time_us
  82. // and rhs.first_read_time_us == 0
  83. // rhs.first_read_time_us can be anything.
  84. // if rhs.creation_time_us == lhs.creation_time_us,
  85. // rhs.first_read_time_us <= lhs.first_read_time_us
  86. // - update_time_us:
  87. // rhs.update_time_us >= lhs.update_time_us
  88. // - state:
  89. // if rhs.update_time_us > lhs.update_time_us
  90. // rhs.state can be anything.
  91. // if rhs.update_time_us == lhs.update_time_us
  92. // rhs.state >= lhs.state in the order UNSEEN, UNREAD, READ.
  93. static bool CompareEntriesForSync(const sync_pb::ReadingListSpecifics& lhs,
  94. const sync_pb::ReadingListSpecifics& rhs);
  95. // Asynchronously retrieve the corresponding sync data for |storage_keys|.
  96. void GetData(StorageKeyList storage_keys, DataCallback callback) override;
  97. // Asynchronously retrieve all of the local sync data.
  98. void GetAllDataForDebugging(DataCallback callback) override;
  99. // Get or generate a client tag for |entity_data|. This must be the same tag
  100. // that was/would have been generated in the SyncableService/Directory world
  101. // for backward compatibility with pre-USS clients. The only time this
  102. // theoretically needs to be called is on the creation of local data, however
  103. // it is also used to verify the hash of remote data. If a data type was never
  104. // launched pre-USS, then method does not need to be different from
  105. // GetStorageKey().
  106. std::string GetClientTag(const syncer::EntityData& entity_data) override;
  107. // Get or generate a storage key for |entity_data|. This will only ever be
  108. // called once when first encountering a remote entity. Local changes will
  109. // provide their storage keys directly to Put instead of using this method.
  110. // Theoretically this function doesn't need to be stable across multiple calls
  111. // on the same or different clients, but to keep things simple, it probably
  112. // should be.
  113. std::string GetStorageKey(const syncer::EntityData& entity_data) override;
  114. // Methods used as callbacks given to DataTypeStore.
  115. void OnStoreCreated(const absl::optional<syncer::ModelError>& error,
  116. std::unique_ptr<syncer::ModelTypeStore> store);
  117. class ScopedBatchUpdate : public ReadingListModelStorage::ScopedBatchUpdate {
  118. public:
  119. explicit ScopedBatchUpdate(ReadingListStore* store);
  120. ScopedBatchUpdate(const ScopedBatchUpdate&) = delete;
  121. ScopedBatchUpdate& operator=(const ScopedBatchUpdate&) = delete;
  122. ~ScopedBatchUpdate() override;
  123. private:
  124. raw_ptr<ReadingListStore> store_;
  125. };
  126. private:
  127. void BeginTransaction();
  128. void CommitTransaction();
  129. // Callbacks needed for the database handling.
  130. void OnDatabaseLoad(
  131. const absl::optional<syncer::ModelError>& error,
  132. std::unique_ptr<syncer::ModelTypeStore::RecordList> entries);
  133. void OnDatabaseSave(const absl::optional<syncer::ModelError>& error);
  134. void OnReadAllMetadata(const absl::optional<syncer::ModelError>& error,
  135. std::unique_ptr<syncer::MetadataBatch> metadata_batch);
  136. void AddEntryToBatch(syncer::MutableDataBatch* batch,
  137. const ReadingListEntry& entry);
  138. std::unique_ptr<syncer::ModelTypeStore> store_;
  139. raw_ptr<ReadingListModel> model_;
  140. raw_ptr<ReadingListStoreDelegate> delegate_;
  141. syncer::OnceModelTypeStoreFactory create_store_callback_;
  142. int pending_transaction_count_;
  143. std::unique_ptr<syncer::ModelTypeStore::WriteBatch> batch_;
  144. raw_ptr<base::Clock> clock_;
  145. SEQUENCE_CHECKER(sequence_checker_);
  146. base::WeakPtrFactory<ReadingListStore> weak_ptr_factory_{this};
  147. };
  148. #endif // COMPONENTS_READING_LIST_CORE_READING_LIST_STORE_H_