Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(45)

Side by Side Diff: ios/chrome/browser/reading_list/reading_list_entry.cc

Issue 2369303002: Reading List create protobuf store (Closed)
Patch Set: split proto Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "ios/chrome/browser/reading_list/reading_list_entry.h" 5 #include "ios/chrome/browser/reading_list/reading_list_entry.h"
6 6
7 #include "base/json/json_string_value_serializer.h"
7 #include "base/memory/ptr_util.h" 8 #include "base/memory/ptr_util.h"
9 #include "ios/chrome/browser/reading_list/proto/reading_list.pb.h"
10 #include "net/base/backoff_entry_serializer.h"
11
12 namespace {
13 // Default Tick clock for |net::BackoffEntry|.
14 base::TickClock* ReadingListTickClock() {
15 static base::DefaultTickClock clock;
16 return &clock;
17 }
18 }
8 19
9 // The backoff time is the following: 10min, 10min, 1h, 2h, 2h..., starting 20 // The backoff time is the following: 10min, 10min, 1h, 2h, 2h..., starting
10 // after the first failure. 21 // after the first failure.
11 const net::BackoffEntry::Policy ReadingListEntry::kBackoffPolicy = { 22 const net::BackoffEntry::Policy ReadingListEntry::kBackoffPolicy = {
12 // Number of initial errors (in sequence) to ignore before applying 23 // Number of initial errors (in sequence) to ignore before applying
13 // exponential back-off rules. 24 // exponential back-off rules.
14 2, 25 2,
15 26
16 // Initial delay for exponential back-off in ms. 27 // Initial delay for exponential back-off in ms.
17 10 * 60 * 1000, // 10 minutes. 28 10 * 60 * 1000, // 10 minutes.
(...skipping 14 matching lines...) Expand all
32 43
33 true, // Don't use initial delay unless the last request was an error. 44 true, // Don't use initial delay unless the last request was an error.
34 }; 45 };
35 46
36 ReadingListEntry::ReadingListEntry(const GURL& url, const std::string& title) 47 ReadingListEntry::ReadingListEntry(const GURL& url, const std::string& title)
37 : ReadingListEntry(url, title, nullptr){}; 48 : ReadingListEntry(url, title, nullptr){};
38 49
39 ReadingListEntry::ReadingListEntry(const GURL& url, 50 ReadingListEntry::ReadingListEntry(const GURL& url,
40 const std::string& title, 51 const std::string& title,
41 std::unique_ptr<net::BackoffEntry> backoff) 52 std::unique_ptr<net::BackoffEntry> backoff)
53 : ReadingListEntry(url, title, 0, 0, WAITING, GURL(), std::move(backoff)) {}
54
55 ReadingListEntry::ReadingListEntry(
56 const GURL& url,
57 const std::string& title,
58 int64_t creation_time,
59 int64_t update_time,
60 ReadingListEntry::DistillationState distilled_state,
61 const GURL& distilled_url,
62 std::unique_ptr<net::BackoffEntry> backoff)
42 : url_(url), 63 : url_(url),
43 title_(title), 64 title_(title),
44 distilled_state_(WAITING), 65 distilled_url_(distilled_url),
45 failed_download_counter_(0) { 66 distilled_state_(distilled_state),
67 failed_download_counter_(0),
68 creation_time_us_(creation_time),
69 update_time_us_(update_time) {
46 if (backoff) { 70 if (backoff) {
47 backoff_ = std::move(backoff); 71 backoff_ = std::move(backoff);
48 } else { 72 } else {
49 backoff_ = base::MakeUnique<net::BackoffEntry>(&kBackoffPolicy); 73 backoff_ = base::MakeUnique<net::BackoffEntry>(&kBackoffPolicy,
74 ReadingListTickClock());
75 }
76 if (creation_time_us_ == 0) {
77 DCHECK(update_time_us_ == 0);
78 creation_time_us_ =
79 (base::Time::Now() - base::Time::UnixEpoch()).InMicroseconds();
80 update_time_us_ = creation_time_us_;
50 } 81 }
51 DCHECK(!url.is_empty()); 82 DCHECK(!url.is_empty());
52 DCHECK(url.is_valid()); 83 DCHECK(url.is_valid());
53 } 84 }
54 85
55 ReadingListEntry::ReadingListEntry(ReadingListEntry&& entry) 86 ReadingListEntry::ReadingListEntry(ReadingListEntry&& entry)
56 : url_(std::move(entry.url_)), 87 : url_(std::move(entry.url_)),
57 title_(std::move(entry.title_)), 88 title_(std::move(entry.title_)),
58 distilled_url_(std::move(entry.distilled_url_)), 89 distilled_url_(std::move(entry.distilled_url_)),
59 distilled_state_(std::move(entry.distilled_state_)), 90 distilled_state_(std::move(entry.distilled_state_)),
60 backoff_(std::move(entry.backoff_)), 91 backoff_(std::move(entry.backoff_)),
61 failed_download_counter_(std::move(entry.failed_download_counter_)) {} 92 failed_download_counter_(std::move(entry.failed_download_counter_)),
93 creation_time_us_(std::move(entry.creation_time_us_)),
94 update_time_us_(std::move(entry.update_time_us_)) {}
62 95
63 ReadingListEntry::~ReadingListEntry() {} 96 ReadingListEntry::~ReadingListEntry() {}
64 97
65 const GURL& ReadingListEntry::URL() const { 98 const GURL& ReadingListEntry::URL() const {
66 return url_; 99 return url_;
67 } 100 }
68 101
69 const std::string& ReadingListEntry::Title() const { 102 const std::string& ReadingListEntry::Title() const {
70 return title_; 103 return title_;
71 } 104 }
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
119 // non-error state to an error state. 152 // non-error state to an error state.
120 if ((distilled_state == WILL_RETRY || distilled_state == ERROR) && 153 if ((distilled_state == WILL_RETRY || distilled_state == ERROR) &&
121 distilled_state_ != WILL_RETRY && distilled_state_ != ERROR) { 154 distilled_state_ != WILL_RETRY && distilled_state_ != ERROR) {
122 backoff_->InformOfRequest(false); 155 backoff_->InformOfRequest(false);
123 failed_download_counter_++; 156 failed_download_counter_++;
124 } 157 }
125 158
126 distilled_state_ = distilled_state; 159 distilled_state_ = distilled_state;
127 distilled_url_ = GURL(); 160 distilled_url_ = GURL();
128 } 161 }
162
163 int64_t ReadingListEntry::UpdateTime() const {
164 return update_time_us_;
165 }
166
167 void ReadingListEntry::MarkEntryUpdated() {
168 update_time_us_ =
169 (base::Time::Now() - base::Time::UnixEpoch()).InMicroseconds();
170 }
171
172 // static
173 std::unique_ptr<ReadingListEntry> ReadingListEntry::FromReadingListLocal(
174 const reading_list::ReadingListLocal& pb_entry) {
175 if (!pb_entry.has_entry() || !pb_entry.entry().has_url()) {
176 return nullptr;
177 }
178 GURL url(pb_entry.entry().url());
179 if (url.is_empty() || !url.is_valid()) {
180 return nullptr;
181 }
182 std::string title;
183 if (pb_entry.entry().has_title()) {
184 title = pb_entry.entry().title();
185 }
186
187 int64_t creation_time_us = 0;
188 if (pb_entry.entry().has_creation_time_us()) {
189 creation_time_us = pb_entry.entry().creation_time_us();
190 }
191
192 int64_t update_time_us = 0;
193 if (pb_entry.entry().has_update_time_us()) {
194 update_time_us = pb_entry.entry().update_time_us();
195 }
196
197 ReadingListEntry::DistillationState distillation_state =
198 ReadingListEntry::WAITING;
199 if (pb_entry.has_distillation_state()) {
200 switch (pb_entry.distillation_state()) {
201 case reading_list::ReadingListLocal::WAITING:
202 distillation_state = ReadingListEntry::WAITING;
203 case reading_list::ReadingListLocal::PROCESSING:
204 distillation_state = ReadingListEntry::PROCESSING;
205 case reading_list::ReadingListLocal::PROCESSED:
206 distillation_state = ReadingListEntry::PROCESSED;
207 case reading_list::ReadingListLocal::WILL_RETRY:
208 distillation_state = ReadingListEntry::WILL_RETRY;
209 case reading_list::ReadingListLocal::ERROR:
210 distillation_state = ReadingListEntry::ERROR;
211 }
212 }
213
214 GURL distilled_url;
215 if (pb_entry.has_distilled_url()) {
216 distilled_url = GURL(pb_entry.distilled_url());
217 }
218
219 std::unique_ptr<net::BackoffEntry> backoff;
220 if (pb_entry.has_backoff()) {
221 JSONStringValueDeserializer deserializer(pb_entry.backoff());
222 std::unique_ptr<base::Value> value(
223 deserializer.Deserialize(nullptr, nullptr));
224 if (value) {
225 backoff = net::BackoffEntrySerializer::DeserializeFromValue(
226 *value, &kBackoffPolicy, ReadingListTickClock(), base::Time::Now());
227 }
228 }
229
230 std::unique_ptr<ReadingListEntry> entry = base::MakeUnique<ReadingListEntry>(
231 url, title, creation_time_us, update_time_us, distillation_state,
232 distilled_url, std::move(backoff));
233 return entry;
234 }
235
236 std::unique_ptr<reading_list::ReadingListLocal>
237 ReadingListEntry::AsReadingListLocal(bool read) const {
238 std::unique_ptr<reading_list::ReadingListLocal> pb_entry =
239 base::MakeUnique<reading_list::ReadingListLocal>();
240
241 // URL is used as the key for the database and sync as there is only one entry
242 // per URL.
243 pb_entry->mutable_entry()->set_entry_id(URL().spec());
244 pb_entry->mutable_entry()->set_title(Title());
245 pb_entry->mutable_entry()->set_url(URL().spec());
246 DCHECK(creation_time_us_ > 100000000);
247 pb_entry->mutable_entry()->set_creation_time_us(creation_time_us_);
248 pb_entry->mutable_entry()->set_update_time_us(UpdateTime());
249
250 if (read) {
251 pb_entry->mutable_entry()->set_status(sync_pb::ReadingListSpecifics::READ);
252 } else {
253 pb_entry->mutable_entry()->set_status(
254 sync_pb::ReadingListSpecifics::UNREAD);
255 }
256
257 reading_list::ReadingListLocal::DistillationState distilation_state;
258 switch (DistilledState()) {
259 case ReadingListEntry::WAITING:
260 distilation_state = reading_list::ReadingListLocal::WAITING;
261 case ReadingListEntry::PROCESSING:
262 distilation_state = reading_list::ReadingListLocal::PROCESSING;
263 case ReadingListEntry::PROCESSED:
264 distilation_state = reading_list::ReadingListLocal::PROCESSED;
265 case ReadingListEntry::WILL_RETRY:
266 distilation_state = reading_list::ReadingListLocal::WILL_RETRY;
267 case ReadingListEntry::ERROR:
268 distilation_state = reading_list::ReadingListLocal::ERROR;
269 }
270 pb_entry->set_distillation_state(distilation_state);
271 if (DistilledURL().is_valid()) {
272 pb_entry->set_distilled_url(DistilledURL().spec());
273 }
274
275 if (backoff_) {
276 std::unique_ptr<base::Value> backoff =
277 net::BackoffEntrySerializer::SerializeToValue(*backoff_,
278 base::Time::Now());
279
280 std::string output;
281 JSONStringValueSerializer serializer(&output);
282 serializer.Serialize(*backoff);
283 pb_entry->set_backoff(output);
284 }
285 return pb_entry;
286 }
287
288 bool ReadingListEntry::CompareEntryUpdateTime(const ReadingListEntry& lhs,
289 const ReadingListEntry& rhs) {
290 return lhs.UpdateTime() > rhs.UpdateTime();
291 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698