| 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" |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 49 title, | 49 title, |
| 50 UNSEEN, | 50 UNSEEN, |
| 51 0, | 51 0, |
| 52 0, | 52 0, |
| 53 0, | 53 0, |
| 54 0, | 54 0, |
| 55 WAITING, | 55 WAITING, |
| 56 base::FilePath(), | 56 base::FilePath(), |
| 57 GURL(), | 57 GURL(), |
| 58 0, | 58 0, |
| 59 0, |
| 60 0, |
| 59 std::move(backoff)) {} | 61 std::move(backoff)) {} |
| 60 | 62 |
| 61 ReadingListEntry::ReadingListEntry( | 63 ReadingListEntry::ReadingListEntry( |
| 62 const GURL& url, | 64 const GURL& url, |
| 63 const std::string& title, | 65 const std::string& title, |
| 64 State state, | 66 State state, |
| 65 int64_t creation_time, | 67 int64_t creation_time, |
| 66 int64_t first_read_time, | 68 int64_t first_read_time, |
| 67 int64_t update_time, | 69 int64_t update_time, |
| 68 int64_t update_title_time, | 70 int64_t update_title_time, |
| 69 ReadingListEntry::DistillationState distilled_state, | 71 ReadingListEntry::DistillationState distilled_state, |
| 70 const base::FilePath& distilled_path, | 72 const base::FilePath& distilled_path, |
| 71 const GURL& distilled_url, | 73 const GURL& distilled_url, |
| 74 int64_t distillation_time, |
| 75 int64_t distillation_size, |
| 72 int failed_download_counter, | 76 int failed_download_counter, |
| 73 std::unique_ptr<net::BackoffEntry> backoff) | 77 std::unique_ptr<net::BackoffEntry> backoff) |
| 74 : url_(url), | 78 : url_(url), |
| 75 title_(title), | 79 title_(title), |
| 76 state_(state), | 80 state_(state), |
| 77 distilled_path_(distilled_path), | 81 distilled_path_(distilled_path), |
| 78 distilled_url_(distilled_url), | 82 distilled_url_(distilled_url), |
| 79 distilled_state_(distilled_state), | 83 distilled_state_(distilled_state), |
| 80 failed_download_counter_(failed_download_counter), | 84 failed_download_counter_(failed_download_counter), |
| 81 creation_time_us_(creation_time), | 85 creation_time_us_(creation_time), |
| 82 first_read_time_us_(first_read_time), | 86 first_read_time_us_(first_read_time), |
| 83 update_time_us_(update_time), | 87 update_time_us_(update_time), |
| 84 update_title_time_us_(update_title_time) { | 88 update_title_time_us_(update_title_time), |
| 89 distillation_time_us_(distillation_time), |
| 90 distillation_size_(distillation_size) { |
| 85 if (backoff) { | 91 if (backoff) { |
| 86 backoff_ = std::move(backoff); | 92 backoff_ = std::move(backoff); |
| 87 } else { | 93 } else { |
| 88 backoff_ = base::MakeUnique<net::BackoffEntry>(&kBackoffPolicy); | 94 backoff_ = base::MakeUnique<net::BackoffEntry>(&kBackoffPolicy); |
| 89 } | 95 } |
| 90 if (creation_time_us_ == 0) { | 96 if (creation_time_us_ == 0) { |
| 91 DCHECK(update_time_us_ == 0); | 97 DCHECK(update_time_us_ == 0); |
| 92 DCHECK(update_title_time_us_ == 0); | 98 DCHECK(update_title_time_us_ == 0); |
| 93 creation_time_us_ = | 99 creation_time_us_ = |
| 94 (base::Time::Now() - base::Time::UnixEpoch()).InMicroseconds(); | 100 (base::Time::Now() - base::Time::UnixEpoch()).InMicroseconds(); |
| 95 update_time_us_ = creation_time_us_; | 101 update_time_us_ = creation_time_us_; |
| 96 update_title_time_us_ = creation_time_us_; | 102 update_title_time_us_ = creation_time_us_; |
| 97 } | 103 } |
| 98 DCHECK(!url.is_empty()); | 104 DCHECK(!url.is_empty()); |
| 99 DCHECK(url.is_valid()); | 105 DCHECK(url.is_valid()); |
| 100 } | 106 } |
| 101 | 107 |
| 102 ReadingListEntry::ReadingListEntry(ReadingListEntry&& entry) | 108 ReadingListEntry::ReadingListEntry(ReadingListEntry&& entry) |
| 103 : url_(std::move(entry.url_)), | 109 : url_(std::move(entry.url_)), |
| 104 title_(std::move(entry.title_)), | 110 title_(std::move(entry.title_)), |
| 105 state_(std::move(entry.state_)), | 111 state_(std::move(entry.state_)), |
| 106 distilled_path_(std::move(entry.distilled_path_)), | 112 distilled_path_(std::move(entry.distilled_path_)), |
| 107 distilled_url_(std::move(entry.distilled_url_)), | 113 distilled_url_(std::move(entry.distilled_url_)), |
| 108 distilled_state_(std::move(entry.distilled_state_)), | 114 distilled_state_(std::move(entry.distilled_state_)), |
| 109 backoff_(std::move(entry.backoff_)), | 115 backoff_(std::move(entry.backoff_)), |
| 110 failed_download_counter_(std::move(entry.failed_download_counter_)), | 116 failed_download_counter_(std::move(entry.failed_download_counter_)), |
| 111 creation_time_us_(std::move(entry.creation_time_us_)), | 117 creation_time_us_(std::move(entry.creation_time_us_)), |
| 112 first_read_time_us_(std::move(entry.first_read_time_us_)), | 118 first_read_time_us_(std::move(entry.first_read_time_us_)), |
| 113 update_time_us_(std::move(entry.update_time_us_)), | 119 update_time_us_(std::move(entry.update_time_us_)), |
| 114 update_title_time_us_(std::move(entry.update_title_time_us_)) {} | 120 update_title_time_us_(std::move(entry.update_title_time_us_)), |
| 121 distillation_time_us_(std::move(entry.distillation_time_us_)), |
| 122 distillation_size_(std::move(entry.distillation_size_)) {} |
| 115 | 123 |
| 116 ReadingListEntry::~ReadingListEntry() {} | 124 ReadingListEntry::~ReadingListEntry() {} |
| 117 | 125 |
| 118 const GURL& ReadingListEntry::URL() const { | 126 const GURL& ReadingListEntry::URL() const { |
| 119 return url_; | 127 return url_; |
| 120 } | 128 } |
| 121 | 129 |
| 122 const std::string& ReadingListEntry::Title() const { | 130 const std::string& ReadingListEntry::Title() const { |
| 123 return title_; | 131 return title_; |
| 124 } | 132 } |
| 125 | 133 |
| 126 ReadingListEntry::DistillationState ReadingListEntry::DistilledState() const { | 134 ReadingListEntry::DistillationState ReadingListEntry::DistilledState() const { |
| 127 return distilled_state_; | 135 return distilled_state_; |
| 128 } | 136 } |
| 129 | 137 |
| 130 const base::FilePath& ReadingListEntry::DistilledPath() const { | 138 const base::FilePath& ReadingListEntry::DistilledPath() const { |
| 131 return distilled_path_; | 139 return distilled_path_; |
| 132 } | 140 } |
| 133 | 141 |
| 134 const GURL& ReadingListEntry::DistilledURL() const { | 142 const GURL& ReadingListEntry::DistilledURL() const { |
| 135 return distilled_url_; | 143 return distilled_url_; |
| 136 } | 144 } |
| 137 | 145 |
| 146 int64_t ReadingListEntry::DistillationTime() const { |
| 147 return distillation_time_us_; |
| 148 } |
| 149 |
| 150 int64_t ReadingListEntry::DistillationSize() const { |
| 151 return distillation_size_; |
| 152 } |
| 153 |
| 138 base::TimeDelta ReadingListEntry::TimeUntilNextTry() const { | 154 base::TimeDelta ReadingListEntry::TimeUntilNextTry() const { |
| 139 return backoff_->GetTimeUntilRelease(); | 155 return backoff_->GetTimeUntilRelease(); |
| 140 } | 156 } |
| 141 | 157 |
| 142 int ReadingListEntry::FailedDownloadCounter() const { | 158 int ReadingListEntry::FailedDownloadCounter() const { |
| 143 return failed_download_counter_; | 159 return failed_download_counter_; |
| 144 } | 160 } |
| 145 | 161 |
| 146 ReadingListEntry& ReadingListEntry::operator=(ReadingListEntry&& other) { | 162 ReadingListEntry& ReadingListEntry::operator=(ReadingListEntry&& other) { |
| 147 url_ = std::move(other.url_); | 163 url_ = std::move(other.url_); |
| 148 title_ = std::move(other.title_); | 164 title_ = std::move(other.title_); |
| 149 distilled_path_ = std::move(other.distilled_path_); | 165 distilled_path_ = std::move(other.distilled_path_); |
| 150 distilled_url_ = std::move(other.distilled_url_); | 166 distilled_url_ = std::move(other.distilled_url_); |
| 151 distilled_state_ = std::move(other.distilled_state_); | 167 distilled_state_ = std::move(other.distilled_state_); |
| 152 backoff_ = std::move(other.backoff_); | 168 backoff_ = std::move(other.backoff_); |
| 153 state_ = std::move(other.state_); | 169 state_ = std::move(other.state_); |
| 154 failed_download_counter_ = std::move(other.failed_download_counter_); | 170 failed_download_counter_ = std::move(other.failed_download_counter_); |
| 155 creation_time_us_ = std::move(other.creation_time_us_); | 171 creation_time_us_ = std::move(other.creation_time_us_); |
| 156 first_read_time_us_ = std::move(other.first_read_time_us_); | 172 first_read_time_us_ = std::move(other.first_read_time_us_); |
| 157 update_time_us_ = std::move(other.update_time_us_); | 173 update_time_us_ = std::move(other.update_time_us_); |
| 158 update_title_time_us_ = std::move(other.update_title_time_us_); | 174 update_title_time_us_ = std::move(other.update_title_time_us_); |
| 175 distillation_time_us_ = std::move(other.distillation_time_us_); |
| 176 distillation_size_ = std::move(other.distillation_size_); |
| 159 return *this; | 177 return *this; |
| 160 } | 178 } |
| 161 | 179 |
| 162 bool ReadingListEntry::operator==(const ReadingListEntry& other) const { | 180 bool ReadingListEntry::operator==(const ReadingListEntry& other) const { |
| 163 return url_ == other.url_; | 181 return url_ == other.url_; |
| 164 } | 182 } |
| 165 | 183 |
| 166 void ReadingListEntry::SetTitle(const std::string& title) { | 184 void ReadingListEntry::SetTitle(const std::string& title) { |
| 167 title_ = title; | 185 title_ = title; |
| 168 update_title_time_us_ = | 186 update_title_time_us_ = |
| (...skipping 19 matching lines...) Expand all Loading... |
| 188 | 206 |
| 189 bool ReadingListEntry::IsRead() const { | 207 bool ReadingListEntry::IsRead() const { |
| 190 return state_ == READ; | 208 return state_ == READ; |
| 191 } | 209 } |
| 192 | 210 |
| 193 bool ReadingListEntry::HasBeenSeen() const { | 211 bool ReadingListEntry::HasBeenSeen() const { |
| 194 return state_ != UNSEEN; | 212 return state_ != UNSEEN; |
| 195 } | 213 } |
| 196 | 214 |
| 197 void ReadingListEntry::SetDistilledInfo(const base::FilePath& path, | 215 void ReadingListEntry::SetDistilledInfo(const base::FilePath& path, |
| 198 const GURL& distilled_url) { | 216 const GURL& distilled_url, |
| 217 int64_t size) { |
| 199 DCHECK(!path.empty()); | 218 DCHECK(!path.empty()); |
| 200 DCHECK(distilled_url.is_valid()); | 219 DCHECK(distilled_url.is_valid()); |
| 201 distilled_path_ = path; | 220 distilled_path_ = path; |
| 202 distilled_state_ = PROCESSED; | 221 distilled_state_ = PROCESSED; |
| 203 distilled_url_ = distilled_url; | 222 distilled_url_ = distilled_url; |
| 223 distillation_time_us_ = |
| 224 (base::Time::Now() - base::Time::UnixEpoch()).InMicroseconds(); |
| 225 distillation_size_ = size; |
| 204 backoff_->Reset(); | 226 backoff_->Reset(); |
| 205 failed_download_counter_ = 0; | 227 failed_download_counter_ = 0; |
| 206 } | 228 } |
| 207 | 229 |
| 208 void ReadingListEntry::SetDistilledState(DistillationState distilled_state) { | 230 void ReadingListEntry::SetDistilledState(DistillationState distilled_state) { |
| 209 DCHECK(distilled_state != PROCESSED); // use SetDistilledPath instead. | 231 DCHECK(distilled_state != PROCESSED); // use SetDistilledPath instead. |
| 210 DCHECK(distilled_state != WAITING); | 232 DCHECK(distilled_state != WAITING); |
| 211 // Increase time until next retry exponentially if the state change from a | 233 // Increase time until next retry exponentially if the state change from a |
| 212 // non-error state to an error state. | 234 // non-error state to an error state. |
| 213 if ((distilled_state == WILL_RETRY || distilled_state == ERROR) && | 235 if ((distilled_state == WILL_RETRY || distilled_state == ERROR) && |
| (...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 317 base::FilePath distilled_path; | 339 base::FilePath distilled_path; |
| 318 if (pb_entry.has_distilled_path()) { | 340 if (pb_entry.has_distilled_path()) { |
| 319 distilled_path = base::FilePath(pb_entry.distilled_path()); | 341 distilled_path = base::FilePath(pb_entry.distilled_path()); |
| 320 } | 342 } |
| 321 | 343 |
| 322 GURL distilled_url; | 344 GURL distilled_url; |
| 323 if (pb_entry.has_distilled_url()) { | 345 if (pb_entry.has_distilled_url()) { |
| 324 distilled_url = GURL(pb_entry.distilled_url()); | 346 distilled_url = GURL(pb_entry.distilled_url()); |
| 325 } | 347 } |
| 326 | 348 |
| 349 int64_t distillation_time_us = 0; |
| 350 if (pb_entry.has_distillation_time_us()) { |
| 351 distillation_time_us = pb_entry.distillation_time_us(); |
| 352 } |
| 353 |
| 354 int64_t distillation_size = 0; |
| 355 if (pb_entry.has_distillation_size()) { |
| 356 distillation_size = pb_entry.distillation_size(); |
| 357 } |
| 358 |
| 327 int64_t failed_download_counter = 0; | 359 int64_t failed_download_counter = 0; |
| 328 if (pb_entry.has_failed_download_counter()) { | 360 if (pb_entry.has_failed_download_counter()) { |
| 329 failed_download_counter = pb_entry.failed_download_counter(); | 361 failed_download_counter = pb_entry.failed_download_counter(); |
| 330 } | 362 } |
| 331 | 363 |
| 332 std::unique_ptr<net::BackoffEntry> backoff; | 364 std::unique_ptr<net::BackoffEntry> backoff; |
| 333 if (pb_entry.has_backoff()) { | 365 if (pb_entry.has_backoff()) { |
| 334 JSONStringValueDeserializer deserializer(pb_entry.backoff()); | 366 JSONStringValueDeserializer deserializer(pb_entry.backoff()); |
| 335 std::unique_ptr<base::Value> value( | 367 std::unique_ptr<base::Value> value( |
| 336 deserializer.Deserialize(nullptr, nullptr)); | 368 deserializer.Deserialize(nullptr, nullptr)); |
| 337 if (value) { | 369 if (value) { |
| 338 backoff = net::BackoffEntrySerializer::DeserializeFromValue( | 370 backoff = net::BackoffEntrySerializer::DeserializeFromValue( |
| 339 *value, &kBackoffPolicy, nullptr, base::Time::Now()); | 371 *value, &kBackoffPolicy, nullptr, base::Time::Now()); |
| 340 } | 372 } |
| 341 } | 373 } |
| 342 | 374 |
| 343 return base::WrapUnique<ReadingListEntry>(new ReadingListEntry( | 375 return base::WrapUnique<ReadingListEntry>(new ReadingListEntry( |
| 344 url, title, state, creation_time_us, first_read_time_us, update_time_us, | 376 url, title, state, creation_time_us, first_read_time_us, update_time_us, |
| 345 update_title_time_us, distillation_state, distilled_path, distilled_url, | 377 update_title_time_us, distillation_state, distilled_path, distilled_url, |
| 346 failed_download_counter, std::move(backoff))); | 378 distillation_time_us, distillation_size, failed_download_counter, |
| 379 std::move(backoff))); |
| 347 } | 380 } |
| 348 | 381 |
| 349 // static | 382 // static |
| 350 std::unique_ptr<ReadingListEntry> ReadingListEntry::FromReadingListSpecifics( | 383 std::unique_ptr<ReadingListEntry> ReadingListEntry::FromReadingListSpecifics( |
| 351 const sync_pb::ReadingListSpecifics& pb_entry) { | 384 const sync_pb::ReadingListSpecifics& pb_entry) { |
| 352 if (!pb_entry.has_url()) { | 385 if (!pb_entry.has_url()) { |
| 353 return nullptr; | 386 return nullptr; |
| 354 } | 387 } |
| 355 GURL url(pb_entry.url()); | 388 GURL url(pb_entry.url()); |
| 356 if (url.is_empty() || !url.is_valid()) { | 389 if (url.is_empty() || !url.is_valid()) { |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 391 state = UNREAD; | 424 state = UNREAD; |
| 392 break; | 425 break; |
| 393 case sync_pb::ReadingListSpecifics::UNSEEN: | 426 case sync_pb::ReadingListSpecifics::UNSEEN: |
| 394 state = UNSEEN; | 427 state = UNSEEN; |
| 395 break; | 428 break; |
| 396 } | 429 } |
| 397 } | 430 } |
| 398 | 431 |
| 399 return base::WrapUnique<ReadingListEntry>(new ReadingListEntry( | 432 return base::WrapUnique<ReadingListEntry>(new ReadingListEntry( |
| 400 url, title, state, creation_time_us, first_read_time_us, update_time_us, | 433 url, title, state, creation_time_us, first_read_time_us, update_time_us, |
| 401 update_title_time_us, WAITING, base::FilePath(), GURL(), 0, nullptr)); | 434 update_title_time_us, WAITING, base::FilePath(), GURL(), 0, 0, 0, |
| 435 nullptr)); |
| 402 } | 436 } |
| 403 | 437 |
| 404 void ReadingListEntry::MergeWithEntry(const ReadingListEntry& other) { | 438 void ReadingListEntry::MergeWithEntry(const ReadingListEntry& other) { |
| 405 #if !defined(NDEBUG) | 439 #if !defined(NDEBUG) |
| 406 // Checks that the result entry respects the sync order. | 440 // Checks that the result entry respects the sync order. |
| 407 std::unique_ptr<sync_pb::ReadingListSpecifics> old_this_pb( | 441 std::unique_ptr<sync_pb::ReadingListSpecifics> old_this_pb( |
| 408 AsReadingListSpecifics()); | 442 AsReadingListSpecifics()); |
| 409 std::unique_ptr<sync_pb::ReadingListSpecifics> other_pb( | 443 std::unique_ptr<sync_pb::ReadingListSpecifics> other_pb( |
| 410 other.AsReadingListSpecifics()); | 444 other.AsReadingListSpecifics()); |
| 411 #endif | 445 #endif |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 498 distilation_state = reading_list::ReadingListLocal::ERROR; | 532 distilation_state = reading_list::ReadingListLocal::ERROR; |
| 499 break; | 533 break; |
| 500 } | 534 } |
| 501 pb_entry->set_distillation_state(distilation_state); | 535 pb_entry->set_distillation_state(distilation_state); |
| 502 if (!DistilledPath().empty()) { | 536 if (!DistilledPath().empty()) { |
| 503 pb_entry->set_distilled_path(DistilledPath().value()); | 537 pb_entry->set_distilled_path(DistilledPath().value()); |
| 504 } | 538 } |
| 505 if (DistilledURL().is_valid()) { | 539 if (DistilledURL().is_valid()) { |
| 506 pb_entry->set_distilled_url(DistilledURL().spec()); | 540 pb_entry->set_distilled_url(DistilledURL().spec()); |
| 507 } | 541 } |
| 542 if (DistillationTime()) { |
| 543 pb_entry->set_distillation_time_us(DistillationTime()); |
| 544 } |
| 545 if (DistillationSize()) { |
| 546 pb_entry->set_distillation_size(DistillationSize()); |
| 547 } |
| 548 |
| 508 pb_entry->set_failed_download_counter(failed_download_counter_); | 549 pb_entry->set_failed_download_counter(failed_download_counter_); |
| 509 | 550 |
| 510 if (backoff_) { | 551 if (backoff_) { |
| 511 std::unique_ptr<base::Value> backoff = | 552 std::unique_ptr<base::Value> backoff = |
| 512 net::BackoffEntrySerializer::SerializeToValue(*backoff_, | 553 net::BackoffEntrySerializer::SerializeToValue(*backoff_, |
| 513 base::Time::Now()); | 554 base::Time::Now()); |
| 514 | 555 |
| 515 std::string output; | 556 std::string output; |
| 516 JSONStringValueSerializer serializer(&output); | 557 JSONStringValueSerializer serializer(&output); |
| 517 serializer.Serialize(*backoff); | 558 serializer.Serialize(*backoff); |
| (...skipping 24 matching lines...) Expand all Loading... |
| 542 case UNREAD: | 583 case UNREAD: |
| 543 pb_entry->set_status(sync_pb::ReadingListSpecifics::UNREAD); | 584 pb_entry->set_status(sync_pb::ReadingListSpecifics::UNREAD); |
| 544 break; | 585 break; |
| 545 case UNSEEN: | 586 case UNSEEN: |
| 546 pb_entry->set_status(sync_pb::ReadingListSpecifics::UNSEEN); | 587 pb_entry->set_status(sync_pb::ReadingListSpecifics::UNSEEN); |
| 547 break; | 588 break; |
| 548 } | 589 } |
| 549 | 590 |
| 550 return pb_entry; | 591 return pb_entry; |
| 551 } | 592 } |
| OLD | NEW |