reading_list_model_impl.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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_MODEL_IMPL_H_
  5. #define COMPONENTS_READING_LIST_CORE_READING_LIST_MODEL_IMPL_H_
  6. #include <map>
  7. #include <memory>
  8. #include "base/memory/raw_ptr.h"
  9. #include "components/reading_list/core/reading_list_entry.h"
  10. #include "components/reading_list/core/reading_list_model.h"
  11. #include "components/reading_list/core/reading_list_model_storage.h"
  12. #include "components/reading_list/core/reading_list_store_delegate.h"
  13. namespace base {
  14. class Clock;
  15. }
  16. class PrefService;
  17. // Concrete implementation of a reading list model using in memory lists.
  18. class ReadingListModelImpl : public ReadingListModel,
  19. public ReadingListStoreDelegate {
  20. public:
  21. using ReadingListEntries = std::map<GURL, ReadingListEntry>;
  22. // Initialize a ReadingListModelImpl to load and save data in
  23. // |storage_layer|. Passing null to |storage_layer| will create a
  24. // ReadingListModelImpl without persistence. Data will not be persistent
  25. // across sessions.
  26. // |clock| will be used to timestamp all the operations.
  27. ReadingListModelImpl(std::unique_ptr<ReadingListModelStorage> storage_layer,
  28. PrefService* pref_service,
  29. base::Clock* clock_);
  30. syncer::ModelTypeSyncBridge* GetModelTypeSyncBridge() override;
  31. ReadingListModelImpl(const ReadingListModelImpl&) = delete;
  32. ReadingListModelImpl& operator=(const ReadingListModelImpl&) = delete;
  33. ~ReadingListModelImpl() override;
  34. void StoreLoaded(std::unique_ptr<ReadingListEntries> entries) override;
  35. // KeyedService implementation.
  36. void Shutdown() override;
  37. // ReadingListModel implementation.
  38. bool loaded() const override;
  39. size_t size() const override;
  40. size_t unread_size() const override;
  41. size_t unseen_size() const override;
  42. void MarkAllSeen() override;
  43. bool GetLocalUnseenFlag() const override;
  44. void ResetLocalUnseenFlag() override;
  45. const std::vector<GURL> Keys() const override;
  46. const ReadingListEntry* GetEntryByURL(const GURL& gurl) const override;
  47. const ReadingListEntry* GetFirstUnreadEntry(bool distilled) const override;
  48. void RemoveEntryByURL(const GURL& url) override;
  49. bool IsUrlSupported(const GURL& url) override;
  50. const ReadingListEntry& AddEntry(
  51. const GURL& url,
  52. const std::string& title,
  53. reading_list::EntrySource source,
  54. base::TimeDelta estimated_read_time) override;
  55. const ReadingListEntry& AddEntry(const GURL& url,
  56. const std::string& title,
  57. reading_list::EntrySource source) override;
  58. void SetReadStatus(const GURL& url, bool read) override;
  59. void SetEntryTitle(const GURL& url, const std::string& title) override;
  60. void SetEstimatedReadTime(const GURL& url,
  61. base::TimeDelta estimated_read_time) override;
  62. void SetEntryDistilledState(
  63. const GURL& url,
  64. ReadingListEntry::DistillationState state) override;
  65. void SetEntryDistilledInfo(const GURL& url,
  66. const base::FilePath& distilled_path,
  67. const GURL& distilled_url,
  68. int64_t distillation_size,
  69. const base::Time& distillation_date) override;
  70. void SetContentSuggestionsExtra(
  71. const GURL& url,
  72. const reading_list::ContentSuggestionsExtra& extra) override;
  73. void SyncAddEntry(std::unique_ptr<ReadingListEntry> entry) override;
  74. ReadingListEntry* SyncMergeEntry(
  75. std::unique_ptr<ReadingListEntry> entry) override;
  76. void SyncRemoveEntry(const GURL& url) override;
  77. bool DeleteAllEntries() override;
  78. std::unique_ptr<ReadingListModel::ScopedReadingListBatchUpdate>
  79. CreateBatchToken() override;
  80. // Helper class that is used to scope batch updates.
  81. class ScopedReadingListBatchUpdate
  82. : public ReadingListModel::ScopedReadingListBatchUpdate {
  83. public:
  84. explicit ScopedReadingListBatchUpdate(ReadingListModelImpl* model);
  85. ScopedReadingListBatchUpdate(const ScopedReadingListBatchUpdate&) = delete;
  86. ScopedReadingListBatchUpdate& operator=(
  87. const ScopedReadingListBatchUpdate&) = delete;
  88. ~ScopedReadingListBatchUpdate() override;
  89. void ReadingListModelBeingShutdown(const ReadingListModel* model) override;
  90. private:
  91. std::unique_ptr<ReadingListModelStorage::ScopedBatchUpdate> storage_token_;
  92. };
  93. protected:
  94. void EnteringBatchUpdates() override;
  95. void LeavingBatchUpdates() override;
  96. private:
  97. // Sets/Loads the pref flag that indicate if some entries have never been seen
  98. // since being added to the store.
  99. void SetPersistentHasUnseen(bool has_unseen);
  100. bool GetPersistentHasUnseen();
  101. // Returns a mutable pointer to the entry with URL |url|. Return nullptr if
  102. // no entry is found.
  103. ReadingListEntry* GetMutableEntryFromURL(const GURL& url) const;
  104. // Returns the |storage_layer_| of the model.
  105. ReadingListModelStorage* StorageLayer();
  106. // Remove entry |url| and propagate to store if |from_sync| is false.
  107. void RemoveEntryByURLImpl(const GURL& url, bool from_sync);
  108. void RebuildIndex() const;
  109. std::unique_ptr<ReadingListEntries> entries_;
  110. size_t unread_entry_count_;
  111. size_t read_entry_count_;
  112. size_t unseen_entry_count_;
  113. // Update the 3 counts above considering addition/removal of |entry|.
  114. void UpdateEntryStateCountersOnEntryRemoval(const ReadingListEntry& entry);
  115. void UpdateEntryStateCountersOnEntryInsertion(const ReadingListEntry& entry);
  116. // Set the unseen flag to true.
  117. void SetUnseenFlag();
  118. raw_ptr<base::Clock> clock_;
  119. std::unique_ptr<ReadingListModelStorage> storage_layer_;
  120. raw_ptr<PrefService> pref_service_;
  121. bool has_unseen_;
  122. bool loaded_;
  123. base::WeakPtrFactory<ReadingListModelImpl> weak_ptr_factory_{this};
  124. };
  125. #endif // COMPONENTS_READING_LIST_CORE_READING_LIST_MODEL_IMPL_H_