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

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

Issue 2369303002: Reading List create protobuf store (Closed)
Patch Set: cleaning 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 "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/09/28 09:19:08 Do you need with? You are always using the default
Olivier 2016/09/28 11:20:06 This is needed for the serialization (not sure why
15 static base::TickClock* clock = new base::DefaultTickClock();
jif-google 2016/09/27 16:39:03 Wouldn't that work: static base::DefaultTickClock
Olivier 2016/09/28 11:20:06 Done.
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
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
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 std::unique_ptr<ReadingListEntry> ReadingListEntry::FromReadingListLocal(
gambard 2016/09/28 09:19:08 Please add // static.
Olivier 2016/09/28 11:20:06 Done.
169 const sync_pb::ReadingListLocal& pb_entry) {
170 if (!pb_entry.has_entry() || !pb_entry.entry().has_url()) {
171 return nullptr;
172 }
173 GURL url(pb_entry.entry().url());
174 if (url.is_empty() || !url.is_valid()) {
175 return nullptr;
176 }
177 std::string title;
178 if (pb_entry.entry().has_title()) {
179 title = pb_entry.entry().title();
180 }
181
182 int64_t creation_time_us = 0;
183 if (pb_entry.entry().has_creation_time_us()) {
184 creation_time_us = pb_entry.entry().creation_time_us();
185 }
186
187 int64_t update_time_us = 0;
188 if (pb_entry.entry().has_update_time_us()) {
189 update_time_us = pb_entry.entry().update_time_us();
190 }
191
192 ReadingListEntry::DistillationState distillation_state =
193 ReadingListEntry::WAITING;
194 if (pb_entry.has_distillation_state()) {
195 switch (pb_entry.distillation_state()) {
196 case sync_pb::ReadingListLocal::WAITING:
197 distillation_state = ReadingListEntry::WAITING;
198 case sync_pb::ReadingListLocal::PROCESSING:
199 distillation_state = ReadingListEntry::PROCESSING;
200 case sync_pb::ReadingListLocal::PROCESSED:
201 distillation_state = ReadingListEntry::PROCESSED;
202 case sync_pb::ReadingListLocal::WILL_RETRY:
203 distillation_state = ReadingListEntry::WILL_RETRY;
204 case sync_pb::ReadingListLocal::ERROR:
205 distillation_state = ReadingListEntry::ERROR;
206 }
207 }
208
209 GURL distilled_url;
210 if (pb_entry.has_distilled_url()) {
211 distilled_url = GURL(pb_entry.distilled_url());
212 }
213
214 std::unique_ptr<net::BackoffEntry> backoff;
215 if (pb_entry.has_backoff()) {
216 JSONStringValueDeserializer deserializer(pb_entry.backoff());
217 std::unique_ptr<base::Value> value(
218 deserializer.Deserialize(nullptr, nullptr));
219 if (value) {
220 backoff = net::BackoffEntrySerializer::DeserializeFromValue(
221 *value, &kBackoffPolicy, ReadingListTickClock(), base::Time::Now());
222 }
223 }
224
225 std::unique_ptr<ReadingListEntry> entry(base::MakeUnique<ReadingListEntry>(
jif-google 2016/09/27 16:39:03 this would be slightly more readable: std::unique
Olivier 2016/09/28 11:20:06 Done.
226 url, title, creation_time_us, update_time_us, distillation_state,
227 distilled_url, std::move(backoff)));
228 return entry;
229 }
230
231 std::unique_ptr<sync_pb::ReadingListLocal> ReadingListEntry::AsReadingListLocal(
232 bool read) const {
233 std::unique_ptr<sync_pb::ReadingListLocal> pb_entry =
234 base::MakeUnique<sync_pb::ReadingListLocal>();
235
236 pb_entry->mutable_entry()->set_title(Title());
237 pb_entry->mutable_entry()->set_url(URL().spec());
238 pb_entry->mutable_entry()->set_entry_id(URL().spec());
jif-google 2016/09/27 16:39:03 How about adding a comment such as "there can only
Olivier 2016/09/28 11:20:06 Done.
239 pb_entry->mutable_entry()->set_creation_time_us(creation_time_us_);
240 pb_entry->mutable_entry()->set_update_time_us(UpdateTime());
241
242 if (read) {
243 pb_entry->mutable_entry()->set_status(sync_pb::ReadingListSpecifics::READ);
244 } else {
245 pb_entry->mutable_entry()->set_status(
246 sync_pb::ReadingListSpecifics::UNREAD);
247 }
248
249 sync_pb::ReadingListLocal::DistillationState distilation_state;
250 switch (DistilledState()) {
251 case ReadingListEntry::WAITING:
252 distilation_state = sync_pb::ReadingListLocal::WAITING;
253 case ReadingListEntry::PROCESSING:
254 distilation_state = sync_pb::ReadingListLocal::PROCESSING;
255 case ReadingListEntry::PROCESSED:
256 distilation_state = sync_pb::ReadingListLocal::PROCESSED;
257 case ReadingListEntry::WILL_RETRY:
258 distilation_state = sync_pb::ReadingListLocal::WILL_RETRY;
259 case ReadingListEntry::ERROR:
260 distilation_state = sync_pb::ReadingListLocal::ERROR;
261 }
262 pb_entry->set_distillation_state(distilation_state);
263 if (DistilledURL().is_valid()) {
264 pb_entry->set_distilled_url(DistilledURL().spec());
265 }
266
267 if (backoff_) {
268 std::unique_ptr<base::Value> backoff =
269 net::BackoffEntrySerializer::SerializeToValue(*backoff_,
270 base::Time::Now());
271
272 std::string output;
273 JSONStringValueSerializer serializer(&output);
274 serializer.Serialize(*backoff);
275 pb_entry->set_backoff(output);
276 }
277 return pb_entry;
278 }
279
280 bool ReadingListEntry::CompareEntryUpdateTime(const ReadingListEntry& lhs,
281 const ReadingListEntry& rhs) {
282 return lhs.UpdateTime() > rhs.UpdateTime();
283 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698