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

Side by Side Diff: components/reading_list/ios/reading_list_entry.cc

Issue 2525663002: Refactor Reading List Model to use URL as key. (Closed)
Patch Set: fix Created 4 years 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 "components/reading_list/ios/reading_list_entry.h" 5 #include "components/reading_list/ios/reading_list_entry.h"
6 6
7 #include "base/json/json_string_value_serializer.h" 7 #include "base/json/json_string_value_serializer.h"
8 #include "base/memory/ptr_util.h" 8 #include "base/memory/ptr_util.h"
9 #include "components/reading_list/ios/offline_url_utils.h" 9 #include "components/reading_list/ios/offline_url_utils.h"
10 #include "components/reading_list/ios/proto/reading_list.pb.h" 10 #include "components/reading_list/ios/proto/reading_list.pb.h"
(...skipping 28 matching lines...) Expand all
39 }; 39 };
40 40
41 ReadingListEntry::ReadingListEntry(const GURL& url, const std::string& title) 41 ReadingListEntry::ReadingListEntry(const GURL& url, const std::string& title)
42 : ReadingListEntry(url, title, nullptr){}; 42 : ReadingListEntry(url, title, nullptr){};
43 43
44 ReadingListEntry::ReadingListEntry(const GURL& url, 44 ReadingListEntry::ReadingListEntry(const GURL& url,
45 const std::string& title, 45 const std::string& title,
46 std::unique_ptr<net::BackoffEntry> backoff) 46 std::unique_ptr<net::BackoffEntry> backoff)
47 : ReadingListEntry(url, 47 : ReadingListEntry(url,
48 title, 48 title,
49 false,
49 0, 50 0,
50 0, 51 0,
51 WAITING, 52 WAITING,
52 base::FilePath(), 53 base::FilePath(),
53 0, 54 0,
54 std::move(backoff)) {} 55 std::move(backoff)) {}
55 56
56 ReadingListEntry::ReadingListEntry( 57 ReadingListEntry::ReadingListEntry(
57 const GURL& url, 58 const GURL& url,
58 const std::string& title, 59 const std::string& title,
60 bool read,
59 int64_t creation_time, 61 int64_t creation_time,
60 int64_t update_time, 62 int64_t update_time,
61 ReadingListEntry::DistillationState distilled_state, 63 ReadingListEntry::DistillationState distilled_state,
62 const base::FilePath& distilled_path, 64 const base::FilePath& distilled_path,
63 int failed_download_counter, 65 int failed_download_counter,
64 std::unique_ptr<net::BackoffEntry> backoff) 66 std::unique_ptr<net::BackoffEntry> backoff)
65 : url_(url), 67 : url_(url),
66 title_(title), 68 title_(title),
69 read_(read),
67 distilled_path_(distilled_path), 70 distilled_path_(distilled_path),
68 distilled_state_(distilled_state), 71 distilled_state_(distilled_state),
69 failed_download_counter_(failed_download_counter), 72 failed_download_counter_(failed_download_counter),
70 creation_time_us_(creation_time), 73 creation_time_us_(creation_time),
71 update_time_us_(update_time) { 74 update_time_us_(update_time) {
72 if (backoff) { 75 if (backoff) {
73 backoff_ = std::move(backoff); 76 backoff_ = std::move(backoff);
74 } else { 77 } else {
75 backoff_ = base::MakeUnique<net::BackoffEntry>(&kBackoffPolicy); 78 backoff_ = base::MakeUnique<net::BackoffEntry>(&kBackoffPolicy);
76 } 79 }
77 if (creation_time_us_ == 0) { 80 if (creation_time_us_ == 0) {
78 DCHECK(update_time_us_ == 0); 81 DCHECK(update_time_us_ == 0);
79 creation_time_us_ = 82 creation_time_us_ =
80 (base::Time::Now() - base::Time::UnixEpoch()).InMicroseconds(); 83 (base::Time::Now() - base::Time::UnixEpoch()).InMicroseconds();
81 update_time_us_ = creation_time_us_; 84 update_time_us_ = creation_time_us_;
82 } 85 }
83 DCHECK(!url.is_empty()); 86 DCHECK(!url.is_empty());
84 DCHECK(url.is_valid()); 87 DCHECK(url.is_valid());
85 } 88 }
86 89
87 ReadingListEntry::ReadingListEntry(ReadingListEntry&& entry) 90 ReadingListEntry::ReadingListEntry(ReadingListEntry&& entry)
88 : url_(std::move(entry.url_)), 91 : url_(std::move(entry.url_)),
89 title_(std::move(entry.title_)), 92 title_(std::move(entry.title_)),
93 read_(std::move(entry.read_)),
90 distilled_path_(std::move(entry.distilled_path_)), 94 distilled_path_(std::move(entry.distilled_path_)),
91 distilled_state_(std::move(entry.distilled_state_)), 95 distilled_state_(std::move(entry.distilled_state_)),
92 backoff_(std::move(entry.backoff_)), 96 backoff_(std::move(entry.backoff_)),
93 failed_download_counter_(std::move(entry.failed_download_counter_)), 97 failed_download_counter_(std::move(entry.failed_download_counter_)),
94 creation_time_us_(std::move(entry.creation_time_us_)), 98 creation_time_us_(std::move(entry.creation_time_us_)),
95 update_time_us_(std::move(entry.update_time_us_)) {} 99 update_time_us_(std::move(entry.update_time_us_)) {}
96 100
97 ReadingListEntry::~ReadingListEntry() {} 101 ReadingListEntry::~ReadingListEntry() {}
98 102
99 const GURL& ReadingListEntry::URL() const { 103 const GURL& ReadingListEntry::URL() const {
(...skipping 19 matching lines...) Expand all
119 int ReadingListEntry::FailedDownloadCounter() const { 123 int ReadingListEntry::FailedDownloadCounter() const {
120 return failed_download_counter_; 124 return failed_download_counter_;
121 } 125 }
122 126
123 ReadingListEntry& ReadingListEntry::operator=(ReadingListEntry&& other) { 127 ReadingListEntry& ReadingListEntry::operator=(ReadingListEntry&& other) {
124 url_ = std::move(other.url_); 128 url_ = std::move(other.url_);
125 title_ = std::move(other.title_); 129 title_ = std::move(other.title_);
126 distilled_path_ = std::move(other.distilled_path_); 130 distilled_path_ = std::move(other.distilled_path_);
127 distilled_state_ = std::move(other.distilled_state_); 131 distilled_state_ = std::move(other.distilled_state_);
128 backoff_ = std::move(other.backoff_); 132 backoff_ = std::move(other.backoff_);
133 read_ = std::move(other.read_);
129 failed_download_counter_ = std::move(other.failed_download_counter_); 134 failed_download_counter_ = std::move(other.failed_download_counter_);
130 creation_time_us_ = std::move(other.creation_time_us_); 135 creation_time_us_ = std::move(other.creation_time_us_);
131 update_time_us_ = std::move(other.update_time_us_); 136 update_time_us_ = std::move(other.update_time_us_);
132 return *this; 137 return *this;
133 } 138 }
134 139
135 bool ReadingListEntry::operator==(const ReadingListEntry& other) const { 140 bool ReadingListEntry::operator==(const ReadingListEntry& other) const {
136 return url_ == other.url_; 141 return url_ == other.url_;
137 } 142 }
138 143
139 void ReadingListEntry::SetTitle(const std::string& title) { 144 void ReadingListEntry::SetTitle(const std::string& title) {
140 title_ = title; 145 title_ = title;
141 } 146 }
142 147
148 void ReadingListEntry::SetRead(bool read) {
149 read_ = read;
150 MarkEntryUpdated();
151 }
152
153 bool ReadingListEntry::IsRead() const {
154 return read_;
155 }
156
143 void ReadingListEntry::SetDistilledPath(const base::FilePath& path) { 157 void ReadingListEntry::SetDistilledPath(const base::FilePath& path) {
144 DCHECK(!path.empty()); 158 DCHECK(!path.empty());
145 distilled_path_ = path; 159 distilled_path_ = path;
146 distilled_state_ = PROCESSED; 160 distilled_state_ = PROCESSED;
147 backoff_->Reset(); 161 backoff_->Reset();
148 failed_download_counter_ = 0; 162 failed_download_counter_ = 0;
149 } 163 }
150 164
151 void ReadingListEntry::SetDistilledState(DistillationState distilled_state) { 165 void ReadingListEntry::SetDistilledState(DistillationState distilled_state) {
152 DCHECK(distilled_state != PROCESSED); // use SetDistilledPath instead. 166 DCHECK(distilled_state != PROCESSED); // use SetDistilledPath instead.
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
194 int64_t creation_time_us = 0; 208 int64_t creation_time_us = 0;
195 if (pb_entry.has_creation_time_us()) { 209 if (pb_entry.has_creation_time_us()) {
196 creation_time_us = pb_entry.creation_time_us(); 210 creation_time_us = pb_entry.creation_time_us();
197 } 211 }
198 212
199 int64_t update_time_us = 0; 213 int64_t update_time_us = 0;
200 if (pb_entry.has_update_time_us()) { 214 if (pb_entry.has_update_time_us()) {
201 update_time_us = pb_entry.update_time_us(); 215 update_time_us = pb_entry.update_time_us();
202 } 216 }
203 217
218 bool read = false;
219 if (pb_entry.has_status()) {
220 read = pb_entry.status() == reading_list::ReadingListLocal::READ;
221 }
222
204 ReadingListEntry::DistillationState distillation_state = 223 ReadingListEntry::DistillationState distillation_state =
205 ReadingListEntry::WAITING; 224 ReadingListEntry::WAITING;
206 if (pb_entry.has_distillation_state()) { 225 if (pb_entry.has_distillation_state()) {
207 switch (pb_entry.distillation_state()) { 226 switch (pb_entry.distillation_state()) {
208 case reading_list::ReadingListLocal::WAITING: 227 case reading_list::ReadingListLocal::WAITING:
209 distillation_state = ReadingListEntry::WAITING; 228 distillation_state = ReadingListEntry::WAITING;
210 break; 229 break;
211 case reading_list::ReadingListLocal::PROCESSING: 230 case reading_list::ReadingListLocal::PROCESSING:
212 distillation_state = ReadingListEntry::PROCESSING; 231 distillation_state = ReadingListEntry::PROCESSING;
213 break; 232 break;
(...skipping 24 matching lines...) Expand all
238 JSONStringValueDeserializer deserializer(pb_entry.backoff()); 257 JSONStringValueDeserializer deserializer(pb_entry.backoff());
239 std::unique_ptr<base::Value> value( 258 std::unique_ptr<base::Value> value(
240 deserializer.Deserialize(nullptr, nullptr)); 259 deserializer.Deserialize(nullptr, nullptr));
241 if (value) { 260 if (value) {
242 backoff = net::BackoffEntrySerializer::DeserializeFromValue( 261 backoff = net::BackoffEntrySerializer::DeserializeFromValue(
243 *value, &kBackoffPolicy, nullptr, base::Time::Now()); 262 *value, &kBackoffPolicy, nullptr, base::Time::Now());
244 } 263 }
245 } 264 }
246 265
247 return base::WrapUnique<ReadingListEntry>(new ReadingListEntry( 266 return base::WrapUnique<ReadingListEntry>(new ReadingListEntry(
248 url, title, creation_time_us, update_time_us, distillation_state, 267 url, title, read, creation_time_us, update_time_us, distillation_state,
249 distilled_path, failed_download_counter, std::move(backoff))); 268 distilled_path, failed_download_counter, std::move(backoff)));
250 } 269 }
251 270
252 // static 271 // static
253 std::unique_ptr<ReadingListEntry> ReadingListEntry::FromReadingListSpecifics( 272 std::unique_ptr<ReadingListEntry> ReadingListEntry::FromReadingListSpecifics(
254 const sync_pb::ReadingListSpecifics& pb_entry) { 273 const sync_pb::ReadingListSpecifics& pb_entry) {
255 if (!pb_entry.has_url()) { 274 if (!pb_entry.has_url()) {
256 return nullptr; 275 return nullptr;
257 } 276 }
258 GURL url(pb_entry.url()); 277 GURL url(pb_entry.url());
259 if (url.is_empty() || !url.is_valid()) { 278 if (url.is_empty() || !url.is_valid()) {
260 return nullptr; 279 return nullptr;
261 } 280 }
262 std::string title; 281 std::string title;
263 if (pb_entry.has_title()) { 282 if (pb_entry.has_title()) {
264 title = pb_entry.title(); 283 title = pb_entry.title();
265 } 284 }
266 285
267 int64_t creation_time_us = 0; 286 int64_t creation_time_us = 0;
268 if (pb_entry.has_creation_time_us()) { 287 if (pb_entry.has_creation_time_us()) {
269 creation_time_us = pb_entry.creation_time_us(); 288 creation_time_us = pb_entry.creation_time_us();
270 } 289 }
271 290
272 int64_t update_time_us = 0; 291 int64_t update_time_us = 0;
273 if (pb_entry.has_update_time_us()) { 292 if (pb_entry.has_update_time_us()) {
274 update_time_us = pb_entry.update_time_us(); 293 update_time_us = pb_entry.update_time_us();
275 } 294 }
276 295
296 bool read = false;
297 if (pb_entry.has_status()) {
298 read = pb_entry.status() == sync_pb::ReadingListSpecifics::READ;
299 }
300
277 return base::WrapUnique<ReadingListEntry>( 301 return base::WrapUnique<ReadingListEntry>(
278 new ReadingListEntry(url, title, creation_time_us, update_time_us, 302 new ReadingListEntry(url, title, read, creation_time_us, update_time_us,
279 WAITING, base::FilePath(), 0, nullptr)); 303 WAITING, base::FilePath(), 0, nullptr));
280 } 304 }
281 305
282 void ReadingListEntry::MergeLocalStateFrom(ReadingListEntry& other) { 306 void ReadingListEntry::MergeLocalStateFrom(ReadingListEntry& other) {
283 distilled_path_ = std::move(other.distilled_path_); 307 distilled_path_ = std::move(other.distilled_path_);
284 distilled_state_ = std::move(other.distilled_state_); 308 distilled_state_ = std::move(other.distilled_state_);
285 backoff_ = std::move(other.backoff_); 309 backoff_ = std::move(other.backoff_);
286 failed_download_counter_ = std::move(other.failed_download_counter_); 310 failed_download_counter_ = std::move(other.failed_download_counter_);
287 } 311 }
288 312
289 std::unique_ptr<reading_list::ReadingListLocal> 313 std::unique_ptr<reading_list::ReadingListLocal>
290 ReadingListEntry::AsReadingListLocal(bool read) const { 314 ReadingListEntry::AsReadingListLocal() const {
291 std::unique_ptr<reading_list::ReadingListLocal> pb_entry = 315 std::unique_ptr<reading_list::ReadingListLocal> pb_entry =
292 base::MakeUnique<reading_list::ReadingListLocal>(); 316 base::MakeUnique<reading_list::ReadingListLocal>();
293 317
294 // URL is used as the key for the database and sync as there is only one entry 318 // URL is used as the key for the database and sync as there is only one entry
295 // per URL. 319 // per URL.
296 pb_entry->set_entry_id(URL().spec()); 320 pb_entry->set_entry_id(URL().spec());
297 pb_entry->set_title(Title()); 321 pb_entry->set_title(Title());
298 pb_entry->set_url(URL().spec()); 322 pb_entry->set_url(URL().spec());
299 pb_entry->set_creation_time_us(CreationTime()); 323 pb_entry->set_creation_time_us(CreationTime());
300 pb_entry->set_update_time_us(UpdateTime()); 324 pb_entry->set_update_time_us(UpdateTime());
301 325
302 if (read) { 326 if (read_) {
303 pb_entry->set_status(reading_list::ReadingListLocal::READ); 327 pb_entry->set_status(reading_list::ReadingListLocal::READ);
304 } else { 328 } else {
305 pb_entry->set_status(reading_list::ReadingListLocal::UNREAD); 329 pb_entry->set_status(reading_list::ReadingListLocal::UNREAD);
306 } 330 }
307 331
308 reading_list::ReadingListLocal::DistillationState distilation_state; 332 reading_list::ReadingListLocal::DistillationState distilation_state;
309 switch (DistilledState()) { 333 switch (DistilledState()) {
310 case ReadingListEntry::WAITING: 334 case ReadingListEntry::WAITING:
311 distilation_state = reading_list::ReadingListLocal::WAITING; 335 distilation_state = reading_list::ReadingListLocal::WAITING;
312 break; 336 break;
(...skipping 23 matching lines...) Expand all
336 360
337 std::string output; 361 std::string output;
338 JSONStringValueSerializer serializer(&output); 362 JSONStringValueSerializer serializer(&output);
339 serializer.Serialize(*backoff); 363 serializer.Serialize(*backoff);
340 pb_entry->set_backoff(output); 364 pb_entry->set_backoff(output);
341 } 365 }
342 return pb_entry; 366 return pb_entry;
343 } 367 }
344 368
345 std::unique_ptr<sync_pb::ReadingListSpecifics> 369 std::unique_ptr<sync_pb::ReadingListSpecifics>
346 ReadingListEntry::AsReadingListSpecifics(bool read) const { 370 ReadingListEntry::AsReadingListSpecifics() const {
347 std::unique_ptr<sync_pb::ReadingListSpecifics> pb_entry = 371 std::unique_ptr<sync_pb::ReadingListSpecifics> pb_entry =
348 base::MakeUnique<sync_pb::ReadingListSpecifics>(); 372 base::MakeUnique<sync_pb::ReadingListSpecifics>();
349 373
350 // URL is used as the key for the database and sync as there is only one entry 374 // URL is used as the key for the database and sync as there is only one entry
351 // per URL. 375 // per URL.
352 pb_entry->set_entry_id(URL().spec()); 376 pb_entry->set_entry_id(URL().spec());
353 pb_entry->set_title(Title()); 377 pb_entry->set_title(Title());
354 pb_entry->set_url(URL().spec()); 378 pb_entry->set_url(URL().spec());
355 pb_entry->set_creation_time_us(CreationTime()); 379 pb_entry->set_creation_time_us(CreationTime());
356 pb_entry->set_update_time_us(UpdateTime()); 380 pb_entry->set_update_time_us(UpdateTime());
357 381
358 if (read) { 382 if (read_) {
359 pb_entry->set_status(sync_pb::ReadingListSpecifics::READ); 383 pb_entry->set_status(sync_pb::ReadingListSpecifics::READ);
360 } else { 384 } else {
361 pb_entry->set_status(sync_pb::ReadingListSpecifics::UNREAD); 385 pb_entry->set_status(sync_pb::ReadingListSpecifics::UNREAD);
362 } 386 }
363 387
364 return pb_entry; 388 return pb_entry;
365 } 389 }
366 390
367 bool ReadingListEntry::CompareEntryUpdateTime(const ReadingListEntry& lhs, 391 bool ReadingListEntry::CompareEntryUpdateTime(const ReadingListEntry& lhs,
368 const ReadingListEntry& rhs) { 392 const ReadingListEntry& rhs) {
369 return lhs.UpdateTime() > rhs.UpdateTime(); 393 return lhs.UpdateTime() > rhs.UpdateTime();
370 } 394 }
OLDNEW
« no previous file with comments | « components/reading_list/ios/reading_list_entry.h ('k') | components/reading_list/ios/reading_list_entry_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698