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

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

Issue 2525663002: Refactor Reading List Model to use URL as key. (Closed)
Patch Set: format 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 "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/json/json_string_value_serializer.h"
8 #include "base/memory/ptr_util.h" 8 #include "base/memory/ptr_util.h"
9 #include "components/sync/protocol/reading_list_specifics.pb.h" 9 #include "components/sync/protocol/reading_list_specifics.pb.h"
10 #include "ios/chrome/browser/reading_list/offline_url_utils.h" 10 #include "ios/chrome/browser/reading_list/offline_url_utils.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 23 matching lines...) Expand all
123 int ReadingListEntry::FailedDownloadCounter() const { 127 int ReadingListEntry::FailedDownloadCounter() const {
124 return failed_download_counter_; 128 return failed_download_counter_;
125 } 129 }
126 130
127 ReadingListEntry& ReadingListEntry::operator=(ReadingListEntry&& other) { 131 ReadingListEntry& ReadingListEntry::operator=(ReadingListEntry&& other) {
128 url_ = std::move(other.url_); 132 url_ = std::move(other.url_);
129 title_ = std::move(other.title_); 133 title_ = std::move(other.title_);
130 distilled_path_ = std::move(other.distilled_path_); 134 distilled_path_ = std::move(other.distilled_path_);
131 distilled_state_ = std::move(other.distilled_state_); 135 distilled_state_ = std::move(other.distilled_state_);
132 backoff_ = std::move(other.backoff_); 136 backoff_ = std::move(other.backoff_);
137 read_ = std::move(other.read_);
133 failed_download_counter_ = std::move(other.failed_download_counter_); 138 failed_download_counter_ = std::move(other.failed_download_counter_);
134 creation_time_us_ = std::move(other.creation_time_us_); 139 creation_time_us_ = std::move(other.creation_time_us_);
135 update_time_us_ = std::move(other.update_time_us_); 140 update_time_us_ = std::move(other.update_time_us_);
136 return *this; 141 return *this;
137 } 142 }
138 143
139 bool ReadingListEntry::operator==(const ReadingListEntry& other) const { 144 bool ReadingListEntry::operator==(const ReadingListEntry& other) const {
140 return url_ == other.url_; 145 return url_ == other.url_;
141 } 146 }
142 147
143 void ReadingListEntry::SetTitle(const std::string& title) { 148 void ReadingListEntry::SetTitle(const std::string& title) {
144 title_ = title; 149 title_ = title;
145 } 150 }
146 151
152 void ReadingListEntry::SetRead(bool read) {
153 read_ = read;
154 MarkEntryUpdated();
155 }
156
157 bool ReadingListEntry::IsRead() const {
158 return read_;
159 }
160
147 void ReadingListEntry::SetDistilledPath(const base::FilePath& path) { 161 void ReadingListEntry::SetDistilledPath(const base::FilePath& path) {
148 DCHECK(!path.empty()); 162 DCHECK(!path.empty());
149 distilled_path_ = path; 163 distilled_path_ = path;
150 distilled_state_ = PROCESSED; 164 distilled_state_ = PROCESSED;
151 backoff_->Reset(); 165 backoff_->Reset();
152 failed_download_counter_ = 0; 166 failed_download_counter_ = 0;
153 } 167 }
154 168
155 void ReadingListEntry::SetDistilledState(DistillationState distilled_state) { 169 void ReadingListEntry::SetDistilledState(DistillationState distilled_state) {
156 DCHECK(distilled_state != PROCESSED); // use SetDistilledPath instead. 170 DCHECK(distilled_state != PROCESSED); // use SetDistilledPath instead.
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
198 int64_t creation_time_us = 0; 212 int64_t creation_time_us = 0;
199 if (pb_entry.has_creation_time_us()) { 213 if (pb_entry.has_creation_time_us()) {
200 creation_time_us = pb_entry.creation_time_us(); 214 creation_time_us = pb_entry.creation_time_us();
201 } 215 }
202 216
203 int64_t update_time_us = 0; 217 int64_t update_time_us = 0;
204 if (pb_entry.has_update_time_us()) { 218 if (pb_entry.has_update_time_us()) {
205 update_time_us = pb_entry.update_time_us(); 219 update_time_us = pb_entry.update_time_us();
206 } 220 }
207 221
222 bool read = false;
223 if (pb_entry.has_status()) {
224 read = pb_entry.status() == reading_list::ReadingListLocal::READ;
225 }
226
208 ReadingListEntry::DistillationState distillation_state = 227 ReadingListEntry::DistillationState distillation_state =
209 ReadingListEntry::WAITING; 228 ReadingListEntry::WAITING;
210 if (pb_entry.has_distillation_state()) { 229 if (pb_entry.has_distillation_state()) {
211 switch (pb_entry.distillation_state()) { 230 switch (pb_entry.distillation_state()) {
212 case reading_list::ReadingListLocal::WAITING: 231 case reading_list::ReadingListLocal::WAITING:
213 distillation_state = ReadingListEntry::WAITING; 232 distillation_state = ReadingListEntry::WAITING;
214 break; 233 break;
215 case reading_list::ReadingListLocal::PROCESSING: 234 case reading_list::ReadingListLocal::PROCESSING:
216 distillation_state = ReadingListEntry::PROCESSING; 235 distillation_state = ReadingListEntry::PROCESSING;
217 break; 236 break;
(...skipping 24 matching lines...) Expand all
242 JSONStringValueDeserializer deserializer(pb_entry.backoff()); 261 JSONStringValueDeserializer deserializer(pb_entry.backoff());
243 std::unique_ptr<base::Value> value( 262 std::unique_ptr<base::Value> value(
244 deserializer.Deserialize(nullptr, nullptr)); 263 deserializer.Deserialize(nullptr, nullptr));
245 if (value) { 264 if (value) {
246 backoff = net::BackoffEntrySerializer::DeserializeFromValue( 265 backoff = net::BackoffEntrySerializer::DeserializeFromValue(
247 *value, &kBackoffPolicy, nullptr, base::Time::Now()); 266 *value, &kBackoffPolicy, nullptr, base::Time::Now());
248 } 267 }
249 } 268 }
250 269
251 return base::WrapUnique<ReadingListEntry>(new ReadingListEntry( 270 return base::WrapUnique<ReadingListEntry>(new ReadingListEntry(
252 url, title, creation_time_us, update_time_us, distillation_state, 271 url, title, read, creation_time_us, update_time_us, distillation_state,
253 distilled_path, failed_download_counter, std::move(backoff))); 272 distilled_path, failed_download_counter, std::move(backoff)));
254 } 273 }
255 274
256 // static 275 // static
257 std::unique_ptr<ReadingListEntry> ReadingListEntry::FromReadingListSpecifics( 276 std::unique_ptr<ReadingListEntry> ReadingListEntry::FromReadingListSpecifics(
258 const sync_pb::ReadingListSpecifics& pb_entry) { 277 const sync_pb::ReadingListSpecifics& pb_entry) {
259 if (!pb_entry.has_url()) { 278 if (!pb_entry.has_url()) {
260 return nullptr; 279 return nullptr;
261 } 280 }
262 GURL url(pb_entry.url()); 281 GURL url(pb_entry.url());
263 if (url.is_empty() || !url.is_valid()) { 282 if (url.is_empty() || !url.is_valid()) {
264 return nullptr; 283 return nullptr;
265 } 284 }
266 std::string title; 285 std::string title;
267 if (pb_entry.has_title()) { 286 if (pb_entry.has_title()) {
268 title = pb_entry.title(); 287 title = pb_entry.title();
269 } 288 }
270 289
271 int64_t creation_time_us = 0; 290 int64_t creation_time_us = 0;
272 if (pb_entry.has_creation_time_us()) { 291 if (pb_entry.has_creation_time_us()) {
273 creation_time_us = pb_entry.creation_time_us(); 292 creation_time_us = pb_entry.creation_time_us();
274 } 293 }
275 294
276 int64_t update_time_us = 0; 295 int64_t update_time_us = 0;
277 if (pb_entry.has_update_time_us()) { 296 if (pb_entry.has_update_time_us()) {
278 update_time_us = pb_entry.update_time_us(); 297 update_time_us = pb_entry.update_time_us();
279 } 298 }
280 299
300 bool read = false;
301 if (pb_entry.has_status()) {
302 read = pb_entry.status() == sync_pb::ReadingListSpecifics::READ;
303 }
304
281 return base::WrapUnique<ReadingListEntry>( 305 return base::WrapUnique<ReadingListEntry>(
282 new ReadingListEntry(url, title, creation_time_us, update_time_us, 306 new ReadingListEntry(url, title, read, creation_time_us, update_time_us,
283 WAITING, base::FilePath(), 0, nullptr)); 307 WAITING, base::FilePath(), 0, nullptr));
284 } 308 }
285 309
286 void ReadingListEntry::MergeLocalStateFrom(ReadingListEntry& other) { 310 void ReadingListEntry::MergeLocalStateFrom(ReadingListEntry& other) {
287 distilled_path_ = std::move(other.distilled_path_); 311 distilled_path_ = std::move(other.distilled_path_);
288 distilled_state_ = std::move(other.distilled_state_); 312 distilled_state_ = std::move(other.distilled_state_);
289 backoff_ = std::move(other.backoff_); 313 backoff_ = std::move(other.backoff_);
290 failed_download_counter_ = std::move(other.failed_download_counter_); 314 failed_download_counter_ = std::move(other.failed_download_counter_);
291 } 315 }
292 316
293 std::unique_ptr<reading_list::ReadingListLocal> 317 std::unique_ptr<reading_list::ReadingListLocal>
294 ReadingListEntry::AsReadingListLocal(bool read) const { 318 ReadingListEntry::AsReadingListLocal() const {
295 std::unique_ptr<reading_list::ReadingListLocal> pb_entry = 319 std::unique_ptr<reading_list::ReadingListLocal> pb_entry =
296 base::MakeUnique<reading_list::ReadingListLocal>(); 320 base::MakeUnique<reading_list::ReadingListLocal>();
297 321
298 // URL is used as the key for the database and sync as there is only one entry 322 // URL is used as the key for the database and sync as there is only one entry
299 // per URL. 323 // per URL.
300 pb_entry->set_entry_id(URL().spec()); 324 pb_entry->set_entry_id(URL().spec());
301 pb_entry->set_title(Title()); 325 pb_entry->set_title(Title());
302 pb_entry->set_url(URL().spec()); 326 pb_entry->set_url(URL().spec());
303 pb_entry->set_creation_time_us(CreationTime()); 327 pb_entry->set_creation_time_us(CreationTime());
304 pb_entry->set_update_time_us(UpdateTime()); 328 pb_entry->set_update_time_us(UpdateTime());
305 329
306 if (read) { 330 if (read_) {
307 pb_entry->set_status(reading_list::ReadingListLocal::READ); 331 pb_entry->set_status(reading_list::ReadingListLocal::READ);
308 } else { 332 } else {
309 pb_entry->set_status(reading_list::ReadingListLocal::UNREAD); 333 pb_entry->set_status(reading_list::ReadingListLocal::UNREAD);
310 } 334 }
311 335
312 reading_list::ReadingListLocal::DistillationState distilation_state; 336 reading_list::ReadingListLocal::DistillationState distilation_state;
313 switch (DistilledState()) { 337 switch (DistilledState()) {
314 case ReadingListEntry::WAITING: 338 case ReadingListEntry::WAITING:
315 distilation_state = reading_list::ReadingListLocal::WAITING; 339 distilation_state = reading_list::ReadingListLocal::WAITING;
316 break; 340 break;
(...skipping 23 matching lines...) Expand all
340 364
341 std::string output; 365 std::string output;
342 JSONStringValueSerializer serializer(&output); 366 JSONStringValueSerializer serializer(&output);
343 serializer.Serialize(*backoff); 367 serializer.Serialize(*backoff);
344 pb_entry->set_backoff(output); 368 pb_entry->set_backoff(output);
345 } 369 }
346 return pb_entry; 370 return pb_entry;
347 } 371 }
348 372
349 std::unique_ptr<sync_pb::ReadingListSpecifics> 373 std::unique_ptr<sync_pb::ReadingListSpecifics>
350 ReadingListEntry::AsReadingListSpecifics(bool read) const { 374 ReadingListEntry::AsReadingListSpecifics() const {
351 std::unique_ptr<sync_pb::ReadingListSpecifics> pb_entry = 375 std::unique_ptr<sync_pb::ReadingListSpecifics> pb_entry =
352 base::MakeUnique<sync_pb::ReadingListSpecifics>(); 376 base::MakeUnique<sync_pb::ReadingListSpecifics>();
353 377
354 // URL is used as the key for the database and sync as there is only one entry 378 // URL is used as the key for the database and sync as there is only one entry
355 // per URL. 379 // per URL.
356 pb_entry->set_entry_id(URL().spec()); 380 pb_entry->set_entry_id(URL().spec());
357 pb_entry->set_title(Title()); 381 pb_entry->set_title(Title());
358 pb_entry->set_url(URL().spec()); 382 pb_entry->set_url(URL().spec());
359 pb_entry->set_creation_time_us(CreationTime()); 383 pb_entry->set_creation_time_us(CreationTime());
360 pb_entry->set_update_time_us(UpdateTime()); 384 pb_entry->set_update_time_us(UpdateTime());
361 385
362 if (read) { 386 if (read_) {
363 pb_entry->set_status(sync_pb::ReadingListSpecifics::READ); 387 pb_entry->set_status(sync_pb::ReadingListSpecifics::READ);
364 } else { 388 } else {
365 pb_entry->set_status(sync_pb::ReadingListSpecifics::UNREAD); 389 pb_entry->set_status(sync_pb::ReadingListSpecifics::UNREAD);
366 } 390 }
367 391
368 return pb_entry; 392 return pb_entry;
369 } 393 }
370 394
371 bool ReadingListEntry::CompareEntryUpdateTime(const ReadingListEntry& lhs, 395 bool ReadingListEntry::CompareEntryUpdateTime(const ReadingListEntry& lhs,
372 const ReadingListEntry& rhs) { 396 const ReadingListEntry& rhs) {
373 return lhs.UpdateTime() > rhs.UpdateTime(); 397 return lhs.UpdateTime() > rhs.UpdateTime();
374 } 398 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698