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 "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" |
| 11 #include "components/reading_list/ios/reading_list_store.h" |
11 #include "components/sync/protocol/reading_list_specifics.pb.h" | 12 #include "components/sync/protocol/reading_list_specifics.pb.h" |
12 #include "net/base/backoff_entry_serializer.h" | 13 #include "net/base/backoff_entry_serializer.h" |
13 | 14 |
14 // The backoff time is the following: 10min, 10min, 1h, 2h, 2h..., starting | 15 // The backoff time is the following: 10min, 10min, 1h, 2h, 2h..., starting |
15 // after the first failure. | 16 // after the first failure. |
16 const net::BackoffEntry::Policy ReadingListEntry::kBackoffPolicy = { | 17 const net::BackoffEntry::Policy ReadingListEntry::kBackoffPolicy = { |
17 // Number of initial errors (in sequence) to ignore before applying | 18 // Number of initial errors (in sequence) to ignore before applying |
18 // exponential back-off rules. | 19 // exponential back-off rules. |
19 2, | 20 2, |
20 | 21 |
(...skipping 323 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
344 state = UNSEEN; | 345 state = UNSEEN; |
345 break; | 346 break; |
346 } | 347 } |
347 } | 348 } |
348 | 349 |
349 return base::WrapUnique<ReadingListEntry>(new ReadingListEntry( | 350 return base::WrapUnique<ReadingListEntry>(new ReadingListEntry( |
350 url, title, state, creation_time_us, first_read_time_us, update_time_us, | 351 url, title, state, creation_time_us, first_read_time_us, update_time_us, |
351 WAITING, base::FilePath(), 0, nullptr)); | 352 WAITING, base::FilePath(), 0, nullptr)); |
352 } | 353 } |
353 | 354 |
354 void ReadingListEntry::MergeLocalStateFrom(ReadingListEntry& other) { | 355 void ReadingListEntry::MergeWithEntry(const ReadingListEntry& other) { |
355 distilled_path_ = std::move(other.distilled_path_); | 356 #if !defined(NDEBUG) |
356 distilled_state_ = std::move(other.distilled_state_); | 357 // Checks that the result entry respects the sync order. |
357 backoff_ = std::move(other.backoff_); | 358 std::unique_ptr<sync_pb::ReadingListSpecifics> old_this_pb( |
358 failed_download_counter_ = std::move(other.failed_download_counter_); | 359 AsReadingListSpecifics()); |
| 360 std::unique_ptr<sync_pb::ReadingListSpecifics> other_pb( |
| 361 other.AsReadingListSpecifics()); |
| 362 #endif |
| 363 DCHECK(url_ == other.url_); |
| 364 if (title_.compare(other.title_) < 0) { |
| 365 // Take the last in alphabetical order or the longer one. |
| 366 // This ensure empty string is replaced. |
| 367 title_ = std::move(other.title_); |
| 368 } |
| 369 if (creation_time_us_ < other.creation_time_us_) { |
| 370 creation_time_us_ = std::move(other.creation_time_us_); |
| 371 } |
| 372 if (state_ == UNSEEN) { |
| 373 state_ = std::move(other.state_); |
| 374 } else if (other.state_ != UNSEEN) { |
| 375 // Both are not UNSEEN, take the newer one. |
| 376 if (update_time_us_ < other.update_time_us_) { |
| 377 state_ = std::move(other.state_); |
| 378 } else if (update_time_us_ == other.update_time_us_) { |
| 379 // Both states are likely the same, but if they are not, READ should win. |
| 380 if (other.state_ == READ) { |
| 381 state_ = std::move(other.state_); |
| 382 } |
| 383 } |
| 384 } |
| 385 if (update_time_us_ < other.update_time_us_) { |
| 386 update_time_us_ = std::move(other.update_time_us_); |
| 387 } |
| 388 #if !defined(NDEBUG) |
| 389 std::unique_ptr<sync_pb::ReadingListSpecifics> new_this_pb( |
| 390 AsReadingListSpecifics()); |
| 391 DCHECK(ReadingListStore::CompareEntriesForSync(*old_this_pb, *new_this_pb)); |
| 392 DCHECK(ReadingListStore::CompareEntriesForSync(*other_pb, *new_this_pb)); |
| 393 #endif |
359 } | 394 } |
360 | 395 |
361 std::unique_ptr<reading_list::ReadingListLocal> | 396 std::unique_ptr<reading_list::ReadingListLocal> |
362 ReadingListEntry::AsReadingListLocal() const { | 397 ReadingListEntry::AsReadingListLocal() const { |
363 std::unique_ptr<reading_list::ReadingListLocal> pb_entry = | 398 std::unique_ptr<reading_list::ReadingListLocal> pb_entry = |
364 base::MakeUnique<reading_list::ReadingListLocal>(); | 399 base::MakeUnique<reading_list::ReadingListLocal>(); |
365 | 400 |
366 // URL is used as the key for the database and sync as there is only one entry | 401 // URL is used as the key for the database and sync as there is only one entry |
367 // per URL. | 402 // per URL. |
368 pb_entry->set_entry_id(URL().spec()); | 403 pb_entry->set_entry_id(URL().spec()); |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
442 case UNREAD: | 477 case UNREAD: |
443 pb_entry->set_status(sync_pb::ReadingListSpecifics::UNREAD); | 478 pb_entry->set_status(sync_pb::ReadingListSpecifics::UNREAD); |
444 break; | 479 break; |
445 case UNSEEN: | 480 case UNSEEN: |
446 pb_entry->set_status(sync_pb::ReadingListSpecifics::UNSEEN); | 481 pb_entry->set_status(sync_pb::ReadingListSpecifics::UNSEEN); |
447 break; | 482 break; |
448 } | 483 } |
449 | 484 |
450 return pb_entry; | 485 return pb_entry; |
451 } | 486 } |
452 | |
453 bool ReadingListEntry::CompareEntryUpdateTime(const ReadingListEntry& lhs, | |
454 const ReadingListEntry& rhs) { | |
455 return lhs.UpdateTime() > rhs.UpdateTime(); | |
456 } | |
OLD | NEW |