reading_list_model.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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_H_
  5. #define COMPONENTS_READING_LIST_CORE_READING_LIST_MODEL_H_
  6. #include <memory>
  7. #include <string>
  8. #include <vector>
  9. #include "base/callback.h"
  10. #include "base/memory/raw_ptr.h"
  11. #include "base/observer_list.h"
  12. #include "base/sequence_checker.h"
  13. #include "components/keyed_service/core/keyed_service.h"
  14. #include "components/reading_list/core/reading_list_entry.h"
  15. #include "components/reading_list/core/reading_list_model_observer.h"
  16. class GURL;
  17. class ReadingListModel;
  18. class ScopedReadingListBatchUpdate;
  19. namespace syncer {
  20. class ModelTypeSyncBridge;
  21. }
  22. // The reading list model contains two list of entries: one of unread urls, the
  23. // other of read ones. This object should only be accessed from one thread
  24. // (Usually the main thread). The observers callbacks are also sent on the main
  25. // thread.
  26. class ReadingListModel : public KeyedService {
  27. public:
  28. class ScopedReadingListBatchUpdate;
  29. ReadingListModel(const ReadingListModel&) = delete;
  30. ReadingListModel& operator=(const ReadingListModel&) = delete;
  31. // Returns true if the model finished loading. Until this returns true the
  32. // reading list is not ready for use.
  33. virtual bool loaded() const = 0;
  34. // Returns true if the model is performing batch updates right now.
  35. bool IsPerformingBatchUpdates() const;
  36. // Returns the ModelTypeSyncBridge responsible for handling sync message.
  37. virtual syncer::ModelTypeSyncBridge* GetModelTypeSyncBridge() = 0;
  38. // Tells model to prepare for batch updates.
  39. // This method is reentrant, i.e. several batch updates may take place at the
  40. // same time.
  41. // Returns a scoped batch update object that should be retained while the
  42. // batch update is performed. Deallocating this object will inform model that
  43. // the batch update has completed.
  44. std::unique_ptr<ScopedReadingListBatchUpdate> BeginBatchUpdates();
  45. // Creates a batch token that will freeze the model while in scope.
  46. virtual std::unique_ptr<ScopedReadingListBatchUpdate> CreateBatchToken();
  47. // Returns a vector of URLs in the model. The order of the URL is not
  48. // specified and can vary on successive calls.
  49. virtual const std::vector<GURL> Keys() const = 0;
  50. // Returns the total number of entries in the model.
  51. virtual size_t size() const = 0;
  52. // Returns the total number of unread entries in the model.
  53. virtual size_t unread_size() const = 0;
  54. // Returns the total number of unseen entries in the model. Note: These
  55. // entries are also unread so unseen_size() <= unread_size().
  56. virtual size_t unseen_size() const = 0;
  57. // Mark all unseen entries as unread.
  58. virtual void MarkAllSeen() = 0;
  59. // Delete all the Reading List entries. Return true if entries where indeed
  60. // deleted.
  61. virtual bool DeleteAllEntries() = 0;
  62. // Returns the flag about unseen entries on the device.
  63. // This flag is raised if some unseen items are added on this device.
  64. // The flag is reset if |ResetLocalUnseenFlag| is called or if all unseen
  65. // entries are removed.
  66. // This is a local flag and it can have different values on different devices,
  67. // even if they are synced.
  68. // (unseen_size() == 0 => GetLocalUnseenFlag() == false)
  69. virtual bool GetLocalUnseenFlag() const = 0;
  70. // Set the unseen flag to false.
  71. virtual void ResetLocalUnseenFlag() = 0;
  72. // Returns a specific entry. Returns null if the entry does not exist.
  73. virtual const ReadingListEntry* GetEntryByURL(const GURL& gurl) const = 0;
  74. // Returns the first unread entry. If |distilled| is true, prioritize the
  75. // entries available offline.
  76. virtual const ReadingListEntry* GetFirstUnreadEntry(bool distilled) const = 0;
  77. // Returns true if |url| can be added to the reading list.
  78. virtual bool IsUrlSupported(const GURL& url) = 0;
  79. // Adds |url| at the top of the unread entries, and removes entries with the
  80. // same |url| from everywhere else if they exist. The entry title will be a
  81. // trimmed copy of |title|. |time_to_read_minutes| is the estimated time to
  82. // read the page. The addition may be asynchronous, and the data will be
  83. // available only once the observers are notified.
  84. virtual const ReadingListEntry& AddEntry(
  85. const GURL& url,
  86. const std::string& title,
  87. reading_list::EntrySource source,
  88. base::TimeDelta estimated_read_time) = 0;
  89. virtual const ReadingListEntry& AddEntry(
  90. const GURL& url,
  91. const std::string& title,
  92. reading_list::EntrySource source) = 0;
  93. // Removes an entry. The removal may be asynchronous, and not happen
  94. // immediately.
  95. virtual void RemoveEntryByURL(const GURL& url) = 0;
  96. // If the |url| is in the reading list and entry(|url|).read != |read|, sets
  97. // the read state of the URL to read. This will also update the update time of
  98. // the entry.
  99. virtual void SetReadStatus(const GURL& url, bool read) = 0;
  100. // Methods to mutate an entry. Will locate the relevant entry by URL. Does
  101. // nothing if the entry is not found.
  102. virtual void SetEntryTitle(const GURL& url, const std::string& title) = 0;
  103. virtual void SetEstimatedReadTime(const GURL& url,
  104. base::TimeDelta estimated_read_time) = 0;
  105. virtual void SetEntryDistilledState(
  106. const GURL& url,
  107. ReadingListEntry::DistillationState state) = 0;
  108. // Sets the Distilled info for the entry |url|. This method sets the
  109. // DistillationState of the entry to PROCESSED and sets the |distilled_path|
  110. // (path of the file on disk), the |distilled_url| (url of the page that
  111. // was distilled, the |distillation_size| (the size of the offline data) and
  112. // the |distillation_date| (date of distillation in microseconds since Jan 1st
  113. // 1970.
  114. virtual void SetEntryDistilledInfo(const GURL& url,
  115. const base::FilePath& distilled_path,
  116. const GURL& distilled_url,
  117. int64_t distilation_size,
  118. const base::Time& distilation_time) = 0;
  119. // Sets the extra info for the entry |url|.
  120. virtual void SetContentSuggestionsExtra(
  121. const GURL& url,
  122. const reading_list::ContentSuggestionsExtra& extra) = 0;
  123. // Observer registration methods. The model will remove all observers upon
  124. // destruction automatically.
  125. void AddObserver(ReadingListModelObserver* observer);
  126. void RemoveObserver(ReadingListModelObserver* observer);
  127. // Helper class that is used to scope batch updates.
  128. class ScopedReadingListBatchUpdate : public ReadingListModelObserver {
  129. public:
  130. explicit ScopedReadingListBatchUpdate(ReadingListModel* model);
  131. ScopedReadingListBatchUpdate(const ScopedReadingListBatchUpdate&) = delete;
  132. ScopedReadingListBatchUpdate& operator=(
  133. const ScopedReadingListBatchUpdate&) = delete;
  134. ~ScopedReadingListBatchUpdate() override;
  135. void ReadingListModelLoaded(const ReadingListModel* model) override;
  136. void ReadingListModelBeingShutdown(const ReadingListModel* model) override;
  137. private:
  138. raw_ptr<ReadingListModel> model_;
  139. };
  140. protected:
  141. ReadingListModel();
  142. ~ReadingListModel() override;
  143. // The observers.
  144. base::ObserverList<ReadingListModelObserver>::Unchecked observers_;
  145. // Tells model that batch updates have completed. Called from
  146. // ReadingListBatchUpdateToken dtor.
  147. virtual void EndBatchUpdates();
  148. // Called when model is entering batch update mode.
  149. virtual void EnteringBatchUpdates();
  150. // Called when model is leaving batch update mode.
  151. virtual void LeavingBatchUpdates();
  152. SEQUENCE_CHECKER(sequence_checker_);
  153. private:
  154. unsigned int current_batch_updates_count_;
  155. };
  156. #endif // COMPONENTS_READING_LIST_CORE_READING_LIST_MODEL_H_