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