reading_list_entry.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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_ENTRY_H_
  5. #define COMPONENTS_READING_LIST_CORE_READING_LIST_ENTRY_H_
  6. #include <string>
  7. #include "base/files/file_path.h"
  8. #include "base/time/time.h"
  9. #include "net/base/backoff_entry.h"
  10. #include "url/gurl.h"
  11. namespace reading_list {
  12. class ReadingListLocal;
  13. // The different ways a reading list entry is added.
  14. // |ADDED_VIA_CURRENT_APP| is when the entry was added by the user from within
  15. // the current instance of the app.
  16. // |ADDED_VIA_EXTENSION| is when the entry was added via the share extension.
  17. // |ADDED_VIA_SYNC| is when the entry was added with sync.
  18. enum EntrySource { ADDED_VIA_CURRENT_APP, ADDED_VIA_EXTENSION, ADDED_VIA_SYNC };
  19. // Contains additional data used by the ContentSuggestions.
  20. struct ContentSuggestionsExtra {
  21. // Whether the Reading List Entry has been dismissed in the Content
  22. // Suggestions.
  23. bool dismissed = false;
  24. };
  25. }
  26. namespace sync_pb {
  27. class ReadingListSpecifics;
  28. }
  29. class ReadingListEntry;
  30. // An entry in the reading list. The URL is a unique identifier for an entry, as
  31. // such it should not be empty and is the only thing considered when comparing
  32. // entries.
  33. // A word about timestamp usage in this class:
  34. // - The backing store uses int64 values to code timestamps. We use internally
  35. // the same type to avoid useless conversions. This values represent the
  36. // number of micro seconds since Jan 1st 1970.
  37. // - As most timestamp are used to sort entries, operations on int64_t are
  38. // faster than operations on base::Time. So Getter return the int64_t values.
  39. // - However, to ensure all the conversions are done the same way, and because
  40. // the Now time is alway retrieved using base::Time::Now(), all the timestamp
  41. // parameter are passed as base::Time. These parameters are internally
  42. // converted in int64_t.
  43. class ReadingListEntry {
  44. public:
  45. // Creates a ReadingList entry. |url| and |title| are the main fields of the
  46. // entry.
  47. // |now| is used to fill the |creation_time_us_| and all the update timestamp
  48. // fields.
  49. ReadingListEntry(const GURL& url,
  50. const std::string& title,
  51. const base::Time& now);
  52. ReadingListEntry(const GURL& url,
  53. const std::string& title,
  54. const base::Time& now,
  55. std::unique_ptr<net::BackoffEntry> backoff);
  56. ReadingListEntry(ReadingListEntry&& entry);
  57. ReadingListEntry(const ReadingListEntry&) = delete;
  58. ReadingListEntry& operator=(const ReadingListEntry&) = delete;
  59. ~ReadingListEntry();
  60. // Entries are created in WAITING state. At some point they will be PROCESSING
  61. // into one of the three state: PROCESSED, the only state a distilled URL
  62. // would be set, WILL_RETRY, similar to wait, but with exponential delays or
  63. // DISTILLATION_ERROR where the system will not retry at all.
  64. enum DistillationState {
  65. WAITING,
  66. PROCESSING,
  67. PROCESSED,
  68. WILL_RETRY,
  69. DISTILLATION_ERROR
  70. };
  71. static const net::BackoffEntry::Policy kBackoffPolicy;
  72. // The URL of the page the user would like to read later.
  73. const GURL& URL() const;
  74. // The title of the entry. Might be empty.
  75. const std::string& Title() const;
  76. // The estimated time to read of the entry. Zero if none available.
  77. base::TimeDelta EstimatedReadTime() const;
  78. // What state this entry is in.
  79. DistillationState DistilledState() const;
  80. // The local file path for the distilled version of the page. This should only
  81. // be called if the state is "PROCESSED".
  82. const base::FilePath& DistilledPath() const;
  83. // The URL that has been distilled to produce file stored at |DistilledPath|.
  84. const GURL& DistilledURL() const;
  85. // The time distillation was done. The value is in microseconds since Jan 1st
  86. // 1970. Returns 0 if the entry was not distilled.
  87. int64_t DistillationTime() const;
  88. // The size of the stored page in bytes.
  89. int64_t DistillationSize() const;
  90. // The time before the next try. This is automatically increased when the
  91. // state is set to WILL_RETRY or ERROR from a non-error state.
  92. base::TimeDelta TimeUntilNextTry() const;
  93. // The number of time chrome failed to download this entry. This is
  94. // automatically increased when the state is set to WILL_RETRY or ERROR from a
  95. // non-error state.
  96. int FailedDownloadCounter() const;
  97. // The read status of the entry.
  98. bool IsRead() const;
  99. // Returns if an entry has ever been seen.
  100. bool HasBeenSeen() const;
  101. // Extra information about this entry for the Content Suggestions.
  102. const reading_list::ContentSuggestionsExtra* ContentSuggestionsExtra() const;
  103. // The last update time of the entry. This value may be used to sort the
  104. // entries. The value is in microseconds since Jan 1st 1970.
  105. int64_t UpdateTime() const;
  106. // The last update time of the title of the entry. The value is in
  107. // microseconds since Jan 1st 1970.
  108. int64_t UpdateTitleTime() const;
  109. // The creation update time of the entry. The value is in microseconds since
  110. // Jan 1st 1970.
  111. int64_t CreationTime() const;
  112. // The time when the entry was read for the first time. The value is in
  113. // microseconds since Jan 1st 1970.
  114. int64_t FirstReadTime() const;
  115. // Set the update time to |now|.
  116. void MarkEntryUpdated(const base::Time& now);
  117. // Returns a protobuf encoding the content of this ReadingListEntry for local
  118. // storage. Use |now| to serialize the backoff_entry.
  119. std::unique_ptr<reading_list::ReadingListLocal> AsReadingListLocal(
  120. const base::Time& now) const;
  121. // Returns a protobuf encoding the content of this ReadingListEntry for sync.
  122. std::unique_ptr<sync_pb::ReadingListSpecifics> AsReadingListSpecifics() const;
  123. // Created a ReadingListEntry from the protobuf format.
  124. // Use |now| to deserialize the backoff_entry.
  125. static std::unique_ptr<ReadingListEntry> FromReadingListLocal(
  126. const reading_list::ReadingListLocal& pb_entry,
  127. const base::Time& now);
  128. // Created a ReadingListEntry from the protobuf format.
  129. // If creation time is not set, it will be set to |now|.
  130. static std::unique_ptr<ReadingListEntry> FromReadingListSpecifics(
  131. const sync_pb::ReadingListSpecifics& pb_entry,
  132. const base::Time& now);
  133. // Merge |this| and |other| into this.
  134. // Local fields are kept from |this|.
  135. // Each field is merged individually keeping the highest value as defined by
  136. // the |ReadingListStore.CompareEntriesForSync| function.
  137. //
  138. // After calling |MergeLocalStateFrom|, the result must verify
  139. // ReadingListStore.CompareEntriesForSync(old_this.AsReadingListSpecifics(),
  140. // new_this.AsReadingListSpecifics())
  141. // and
  142. // ReadingListStore.CompareEntriesForSync(other.AsReadingListSpecifics(),
  143. // new_this.AsReadingListSpecifics()).
  144. void MergeWithEntry(const ReadingListEntry& other);
  145. ReadingListEntry& operator=(ReadingListEntry&& other);
  146. bool operator==(const ReadingListEntry& other) const;
  147. // Sets |title_| to |title|. Sets |update_title_time_us_| to |now|.
  148. void SetTitle(const std::string& title, const base::Time& now);
  149. // Sets the distilled info (offline path, online URL, size and date of the
  150. // stored files) about distilled page, switch the state to PROCESSED and reset
  151. // the time until the next try.
  152. void SetDistilledInfo(const base::FilePath& path,
  153. const GURL& distilled_url,
  154. int64_t distilation_size,
  155. const base::Time& distilation_time);
  156. // Sets the state to one of PROCESSING, WILL_RETRY or ERROR.
  157. void SetDistilledState(DistillationState distilled_state);
  158. // Sets the read state of the entry. Will set the UpdateTime of the entry.
  159. // If |first_read_time_us_| is 0 and read is READ, sets |first_read_time_us_|
  160. // to |now|.
  161. void SetRead(bool read, const base::Time& now);
  162. // Sets extra information about this entry used by Content Suggestions.
  163. void SetContentSuggestionsExtra(
  164. const reading_list::ContentSuggestionsExtra& extra);
  165. // Sets the estimated time to read of this entry page.
  166. void SetEstimatedReadTime(base::TimeDelta estimated_read_time);
  167. private:
  168. enum State { UNSEEN, UNREAD, READ };
  169. ReadingListEntry(
  170. const GURL& url,
  171. const std::string& title,
  172. base::TimeDelta estimated_read_time,
  173. State state,
  174. int64_t creation_time,
  175. int64_t first_read_time,
  176. int64_t update_time,
  177. int64_t update_title_time,
  178. ReadingListEntry::DistillationState distilled_state,
  179. const base::FilePath& distilled_path,
  180. const GURL& distilled_url,
  181. int64_t distillation_time,
  182. int64_t distillation_size,
  183. int failed_download_counter,
  184. std::unique_ptr<net::BackoffEntry> backoff,
  185. const reading_list::ContentSuggestionsExtra& content_suggestions_extra);
  186. GURL url_;
  187. std::string title_;
  188. base::TimeDelta estimated_read_time_;
  189. State state_;
  190. base::FilePath distilled_path_;
  191. GURL distilled_url_;
  192. DistillationState distilled_state_;
  193. std::unique_ptr<net::BackoffEntry> backoff_;
  194. int failed_download_counter_;
  195. // These value are in microseconds since Jan 1st 1970. They are used for
  196. // sorting the entries from the database. They are kept in int64_t to avoid
  197. // conversion on each save/read event.
  198. int64_t creation_time_us_;
  199. int64_t first_read_time_us_;
  200. int64_t update_time_us_;
  201. int64_t update_title_time_us_;
  202. int64_t distillation_time_us_;
  203. int64_t distillation_size_;
  204. reading_list::ContentSuggestionsExtra content_suggestions_extra_;
  205. };
  206. #endif // COMPONENTS_READING_LIST_CORE_READING_LIST_ENTRY_H_