OLD | NEW |
---|---|
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 "components/sync/protocol/reading_list_specifics.pb.h" | |
10 #include "net/base/backoff_entry_serializer.h" | |
11 | |
12 namespace { | |
13 // Default Tick clock for |net::BackoffEntry|. | |
14 base::TickClock* ReadingListTickClock() { | |
gambard
2016/10/03 08:08:43
I really think it should be ok to pass nullptr ins
| |
15 static base::DefaultTickClock clock; | |
16 return &clock; | |
17 } | |
18 } | |
8 | 19 |
9 const net::BackoffEntry::Policy ReadingListEntry::kBackoffPolicy = { | 20 const net::BackoffEntry::Policy ReadingListEntry::kBackoffPolicy = { |
10 // Number of initial errors (in sequence) to ignore before applying | 21 // Number of initial errors (in sequence) to ignore before applying |
11 // exponential back-off rules. | 22 // exponential back-off rules. |
12 0, | 23 0, |
13 | 24 |
14 // Initial delay for exponential back-off in ms. | 25 // Initial delay for exponential back-off in ms. |
15 1000, // 1 second. | 26 1000, // 1 second. |
16 | 27 |
17 // Factor by which the waiting time will be multiplied. | 28 // Factor by which the waiting time will be multiplied. |
(...skipping 12 matching lines...) Expand all Loading... | |
30 | 41 |
31 false, // Don't use initial delay unless the last request was an error. | 42 false, // Don't use initial delay unless the last request was an error. |
32 }; | 43 }; |
33 | 44 |
34 ReadingListEntry::ReadingListEntry(const GURL& url, const std::string& title) | 45 ReadingListEntry::ReadingListEntry(const GURL& url, const std::string& title) |
35 : ReadingListEntry(url, title, nullptr){}; | 46 : ReadingListEntry(url, title, nullptr){}; |
36 | 47 |
37 ReadingListEntry::ReadingListEntry(const GURL& url, | 48 ReadingListEntry::ReadingListEntry(const GURL& url, |
38 const std::string& title, | 49 const std::string& title, |
39 std::unique_ptr<net::BackoffEntry> backoff) | 50 std::unique_ptr<net::BackoffEntry> backoff) |
51 : ReadingListEntry(url, title, 0, 0, WAITING, GURL(), std::move(backoff)) {} | |
52 | |
53 ReadingListEntry::ReadingListEntry( | |
54 const GURL& url, | |
55 const std::string& title, | |
56 int64_t creation_time, | |
57 int64_t update_time, | |
58 ReadingListEntry::DistillationState distilled_state, | |
59 const GURL& distilled_url, | |
60 std::unique_ptr<net::BackoffEntry> backoff) | |
40 : url_(url), | 61 : url_(url), |
41 title_(title), | 62 title_(title), |
42 distilled_state_(WAITING), | 63 distilled_url_(distilled_url), |
43 failed_download_counter_(0) { | 64 distilled_state_(distilled_state), |
65 failed_download_counter_(0), | |
66 creation_time_us_(creation_time), | |
67 update_time_us_(update_time) { | |
44 if (backoff) { | 68 if (backoff) { |
45 backoff_ = std::move(backoff); | 69 backoff_ = std::move(backoff); |
46 } else { | 70 } else { |
47 backoff_ = base::MakeUnique<net::BackoffEntry>(&kBackoffPolicy); | 71 backoff_ = base::MakeUnique<net::BackoffEntry>(&kBackoffPolicy, |
72 ReadingListTickClock()); | |
73 } | |
74 if (creation_time_us_ == 0) { | |
75 DCHECK(update_time_us_ == 0); | |
76 creation_time_us_ = | |
77 (base::Time::Now() - base::Time::UnixEpoch()).InMicroseconds(); | |
78 update_time_us_ = creation_time_us_; | |
48 } | 79 } |
49 DCHECK(!url.is_empty()); | 80 DCHECK(!url.is_empty()); |
50 DCHECK(url.is_valid()); | 81 DCHECK(url.is_valid()); |
51 } | 82 } |
52 | 83 |
53 ReadingListEntry::ReadingListEntry(ReadingListEntry&& entry) | 84 ReadingListEntry::ReadingListEntry(ReadingListEntry&& entry) |
54 : url_(std::move(entry.url_)), | 85 : url_(std::move(entry.url_)), |
55 title_(std::move(entry.title_)), | 86 title_(std::move(entry.title_)), |
56 distilled_url_(std::move(entry.distilled_url_)), | 87 distilled_url_(std::move(entry.distilled_url_)), |
57 distilled_state_(std::move(entry.distilled_state_)), | 88 distilled_state_(std::move(entry.distilled_state_)), |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
117 // non-error state to an error state. | 148 // non-error state to an error state. |
118 if ((distilled_state == WILL_RETRY || distilled_state == ERROR) && | 149 if ((distilled_state == WILL_RETRY || distilled_state == ERROR) && |
119 distilled_state_ != WILL_RETRY && distilled_state_ != ERROR) { | 150 distilled_state_ != WILL_RETRY && distilled_state_ != ERROR) { |
120 backoff_->InformOfRequest(false); | 151 backoff_->InformOfRequest(false); |
121 failed_download_counter_++; | 152 failed_download_counter_++; |
122 } | 153 } |
123 | 154 |
124 distilled_state_ = distilled_state; | 155 distilled_state_ = distilled_state; |
125 distilled_url_ = GURL(); | 156 distilled_url_ = GURL(); |
126 } | 157 } |
158 | |
159 int64_t ReadingListEntry::UpdateTime() const { | |
160 return update_time_us_; | |
161 } | |
162 | |
163 void ReadingListEntry::MarkEntryUpdated() { | |
164 update_time_us_ = | |
165 (base::Time::Now() - base::Time::UnixEpoch()).InMicroseconds(); | |
166 } | |
167 | |
168 // static | |
169 std::unique_ptr<ReadingListEntry> ReadingListEntry::FromReadingListLocal( | |
170 const sync_pb::ReadingListLocal& pb_entry) { | |
171 if (!pb_entry.has_entry() || !pb_entry.entry().has_url()) { | |
172 return nullptr; | |
173 } | |
174 GURL url(pb_entry.entry().url()); | |
175 if (url.is_empty() || !url.is_valid()) { | |
176 return nullptr; | |
177 } | |
178 std::string title; | |
179 if (pb_entry.entry().has_title()) { | |
180 title = pb_entry.entry().title(); | |
181 } | |
182 | |
183 int64_t creation_time_us = 0; | |
184 if (pb_entry.entry().has_creation_time_us()) { | |
185 creation_time_us = pb_entry.entry().creation_time_us(); | |
186 } | |
187 | |
188 int64_t update_time_us = 0; | |
189 if (pb_entry.entry().has_update_time_us()) { | |
190 update_time_us = pb_entry.entry().update_time_us(); | |
191 } | |
192 | |
193 ReadingListEntry::DistillationState distillation_state = | |
194 ReadingListEntry::WAITING; | |
195 if (pb_entry.has_distillation_state()) { | |
196 switch (pb_entry.distillation_state()) { | |
197 case sync_pb::ReadingListLocal::WAITING: | |
198 distillation_state = ReadingListEntry::WAITING; | |
199 case sync_pb::ReadingListLocal::PROCESSING: | |
200 distillation_state = ReadingListEntry::PROCESSING; | |
201 case sync_pb::ReadingListLocal::PROCESSED: | |
202 distillation_state = ReadingListEntry::PROCESSED; | |
203 case sync_pb::ReadingListLocal::WILL_RETRY: | |
204 distillation_state = ReadingListEntry::WILL_RETRY; | |
205 case sync_pb::ReadingListLocal::ERROR: | |
206 distillation_state = ReadingListEntry::ERROR; | |
207 } | |
208 } | |
209 | |
210 GURL distilled_url; | |
211 if (pb_entry.has_distilled_url()) { | |
212 distilled_url = GURL(pb_entry.distilled_url()); | |
213 } | |
214 | |
215 std::unique_ptr<net::BackoffEntry> backoff; | |
216 if (pb_entry.has_backoff()) { | |
217 JSONStringValueDeserializer deserializer(pb_entry.backoff()); | |
218 std::unique_ptr<base::Value> value( | |
219 deserializer.Deserialize(nullptr, nullptr)); | |
220 if (value) { | |
221 backoff = net::BackoffEntrySerializer::DeserializeFromValue( | |
222 *value, &kBackoffPolicy, ReadingListTickClock(), base::Time::Now()); | |
223 } | |
224 } | |
225 | |
226 std::unique_ptr<ReadingListEntry> entry = base::MakeUnique<ReadingListEntry>( | |
227 url, title, creation_time_us, update_time_us, distillation_state, | |
228 distilled_url, std::move(backoff)); | |
229 return entry; | |
230 } | |
231 | |
232 std::unique_ptr<sync_pb::ReadingListLocal> ReadingListEntry::AsReadingListLocal( | |
233 bool read) const { | |
234 std::unique_ptr<sync_pb::ReadingListLocal> pb_entry = | |
235 base::MakeUnique<sync_pb::ReadingListLocal>(); | |
236 | |
237 // URL is used as the key for the database and sync as there is only one entry | |
238 // per URL. | |
239 pb_entry->mutable_entry()->set_entry_id(URL().spec()); | |
240 pb_entry->mutable_entry()->set_title(Title()); | |
241 pb_entry->mutable_entry()->set_url(URL().spec()); | |
242 pb_entry->mutable_entry()->set_creation_time_us(creation_time_us_); | |
243 pb_entry->mutable_entry()->set_update_time_us(UpdateTime()); | |
244 | |
245 if (read) { | |
246 pb_entry->mutable_entry()->set_status(sync_pb::ReadingListSpecifics::READ); | |
247 } else { | |
248 pb_entry->mutable_entry()->set_status( | |
249 sync_pb::ReadingListSpecifics::UNREAD); | |
250 } | |
251 | |
252 sync_pb::ReadingListLocal::DistillationState distilation_state; | |
253 switch (DistilledState()) { | |
254 case ReadingListEntry::WAITING: | |
255 distilation_state = sync_pb::ReadingListLocal::WAITING; | |
256 case ReadingListEntry::PROCESSING: | |
257 distilation_state = sync_pb::ReadingListLocal::PROCESSING; | |
258 case ReadingListEntry::PROCESSED: | |
259 distilation_state = sync_pb::ReadingListLocal::PROCESSED; | |
260 case ReadingListEntry::WILL_RETRY: | |
261 distilation_state = sync_pb::ReadingListLocal::WILL_RETRY; | |
262 case ReadingListEntry::ERROR: | |
263 distilation_state = sync_pb::ReadingListLocal::ERROR; | |
264 } | |
265 pb_entry->set_distillation_state(distilation_state); | |
266 if (DistilledURL().is_valid()) { | |
267 pb_entry->set_distilled_url(DistilledURL().spec()); | |
268 } | |
269 | |
270 if (backoff_) { | |
271 std::unique_ptr<base::Value> backoff = | |
272 net::BackoffEntrySerializer::SerializeToValue(*backoff_, | |
273 base::Time::Now()); | |
274 | |
275 std::string output; | |
276 JSONStringValueSerializer serializer(&output); | |
277 serializer.Serialize(*backoff); | |
278 pb_entry->set_backoff(output); | |
279 } | |
280 return pb_entry; | |
281 } | |
282 | |
283 bool ReadingListEntry::CompareEntryUpdateTime(const ReadingListEntry& lhs, | |
284 const ReadingListEntry& rhs) { | |
285 return lhs.UpdateTime() > rhs.UpdateTime(); | |
286 } | |
OLD | NEW |