| 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 28 matching lines...) Expand all Loading... |
| 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 UNSEEN, |
| 50 0, | 50 0, |
| 51 0, | 51 0, |
| 52 WAITING, | 52 WAITING, |
| 53 base::FilePath(), | 53 base::FilePath(), |
| 54 0, | 54 0, |
| 55 std::move(backoff)) {} | 55 std::move(backoff)) {} |
| 56 | 56 |
| 57 ReadingListEntry::ReadingListEntry( | 57 ReadingListEntry::ReadingListEntry( |
| 58 const GURL& url, | 58 const GURL& url, |
| 59 const std::string& title, | 59 const std::string& title, |
| 60 bool read, | 60 State state, |
| 61 int64_t creation_time, | 61 int64_t creation_time, |
| 62 int64_t update_time, | 62 int64_t update_time, |
| 63 ReadingListEntry::DistillationState distilled_state, | 63 ReadingListEntry::DistillationState distilled_state, |
| 64 const base::FilePath& distilled_path, | 64 const base::FilePath& distilled_path, |
| 65 int failed_download_counter, | 65 int failed_download_counter, |
| 66 std::unique_ptr<net::BackoffEntry> backoff) | 66 std::unique_ptr<net::BackoffEntry> backoff) |
| 67 : url_(url), | 67 : url_(url), |
| 68 title_(title), | 68 title_(title), |
| 69 read_(read), | 69 state_(state), |
| 70 distilled_path_(distilled_path), | 70 distilled_path_(distilled_path), |
| 71 distilled_state_(distilled_state), | 71 distilled_state_(distilled_state), |
| 72 failed_download_counter_(failed_download_counter), | 72 failed_download_counter_(failed_download_counter), |
| 73 creation_time_us_(creation_time), | 73 creation_time_us_(creation_time), |
| 74 update_time_us_(update_time) { | 74 update_time_us_(update_time) { |
| 75 if (backoff) { | 75 if (backoff) { |
| 76 backoff_ = std::move(backoff); | 76 backoff_ = std::move(backoff); |
| 77 } else { | 77 } else { |
| 78 backoff_ = base::MakeUnique<net::BackoffEntry>(&kBackoffPolicy); | 78 backoff_ = base::MakeUnique<net::BackoffEntry>(&kBackoffPolicy); |
| 79 } | 79 } |
| 80 if (creation_time_us_ == 0) { | 80 if (creation_time_us_ == 0) { |
| 81 DCHECK(update_time_us_ == 0); | 81 DCHECK(update_time_us_ == 0); |
| 82 creation_time_us_ = | 82 creation_time_us_ = |
| 83 (base::Time::Now() - base::Time::UnixEpoch()).InMicroseconds(); | 83 (base::Time::Now() - base::Time::UnixEpoch()).InMicroseconds(); |
| 84 update_time_us_ = creation_time_us_; | 84 update_time_us_ = creation_time_us_; |
| 85 } | 85 } |
| 86 DCHECK(!url.is_empty()); | 86 DCHECK(!url.is_empty()); |
| 87 DCHECK(url.is_valid()); | 87 DCHECK(url.is_valid()); |
| 88 } | 88 } |
| 89 | 89 |
| 90 ReadingListEntry::ReadingListEntry(ReadingListEntry&& entry) | 90 ReadingListEntry::ReadingListEntry(ReadingListEntry&& entry) |
| 91 : url_(std::move(entry.url_)), | 91 : url_(std::move(entry.url_)), |
| 92 title_(std::move(entry.title_)), | 92 title_(std::move(entry.title_)), |
| 93 read_(std::move(entry.read_)), | 93 state_(std::move(entry.state_)), |
| 94 distilled_path_(std::move(entry.distilled_path_)), | 94 distilled_path_(std::move(entry.distilled_path_)), |
| 95 distilled_state_(std::move(entry.distilled_state_)), | 95 distilled_state_(std::move(entry.distilled_state_)), |
| 96 backoff_(std::move(entry.backoff_)), | 96 backoff_(std::move(entry.backoff_)), |
| 97 failed_download_counter_(std::move(entry.failed_download_counter_)), | 97 failed_download_counter_(std::move(entry.failed_download_counter_)), |
| 98 creation_time_us_(std::move(entry.creation_time_us_)), | 98 creation_time_us_(std::move(entry.creation_time_us_)), |
| 99 update_time_us_(std::move(entry.update_time_us_)) {} | 99 update_time_us_(std::move(entry.update_time_us_)) {} |
| 100 | 100 |
| 101 ReadingListEntry::~ReadingListEntry() {} | 101 ReadingListEntry::~ReadingListEntry() {} |
| 102 | 102 |
| 103 const GURL& ReadingListEntry::URL() const { | 103 const GURL& ReadingListEntry::URL() const { |
| (...skipping 19 matching lines...) Expand all Loading... |
| 123 int ReadingListEntry::FailedDownloadCounter() const { | 123 int ReadingListEntry::FailedDownloadCounter() const { |
| 124 return failed_download_counter_; | 124 return failed_download_counter_; |
| 125 } | 125 } |
| 126 | 126 |
| 127 ReadingListEntry& ReadingListEntry::operator=(ReadingListEntry&& other) { | 127 ReadingListEntry& ReadingListEntry::operator=(ReadingListEntry&& other) { |
| 128 url_ = std::move(other.url_); | 128 url_ = std::move(other.url_); |
| 129 title_ = std::move(other.title_); | 129 title_ = std::move(other.title_); |
| 130 distilled_path_ = std::move(other.distilled_path_); | 130 distilled_path_ = std::move(other.distilled_path_); |
| 131 distilled_state_ = std::move(other.distilled_state_); | 131 distilled_state_ = std::move(other.distilled_state_); |
| 132 backoff_ = std::move(other.backoff_); | 132 backoff_ = std::move(other.backoff_); |
| 133 read_ = std::move(other.read_); | 133 state_ = std::move(other.state_); |
| 134 failed_download_counter_ = std::move(other.failed_download_counter_); | 134 failed_download_counter_ = std::move(other.failed_download_counter_); |
| 135 creation_time_us_ = std::move(other.creation_time_us_); | 135 creation_time_us_ = std::move(other.creation_time_us_); |
| 136 update_time_us_ = std::move(other.update_time_us_); | 136 update_time_us_ = std::move(other.update_time_us_); |
| 137 return *this; | 137 return *this; |
| 138 } | 138 } |
| 139 | 139 |
| 140 bool ReadingListEntry::operator==(const ReadingListEntry& other) const { | 140 bool ReadingListEntry::operator==(const ReadingListEntry& other) const { |
| 141 return url_ == other.url_; | 141 return url_ == other.url_; |
| 142 } | 142 } |
| 143 | 143 |
| 144 void ReadingListEntry::SetTitle(const std::string& title) { | 144 void ReadingListEntry::SetTitle(const std::string& title) { |
| 145 title_ = title; | 145 title_ = title; |
| 146 } | 146 } |
| 147 | 147 |
| 148 void ReadingListEntry::SetRead(bool read) { | 148 void ReadingListEntry::SetRead(bool read) { |
| 149 read_ = read; | 149 state_ = read ? READ : UNREAD; |
| 150 MarkEntryUpdated(); | 150 MarkEntryUpdated(); |
| 151 } | 151 } |
| 152 | 152 |
| 153 bool ReadingListEntry::IsRead() const { | 153 bool ReadingListEntry::IsRead() const { |
| 154 return read_; | 154 return state_ == READ; |
| 155 } |
| 156 |
| 157 bool ReadingListEntry::HasBeenSeen() const { |
| 158 return state_ != UNSEEN; |
| 155 } | 159 } |
| 156 | 160 |
| 157 void ReadingListEntry::SetDistilledPath(const base::FilePath& path) { | 161 void ReadingListEntry::SetDistilledPath(const base::FilePath& path) { |
| 158 DCHECK(!path.empty()); | 162 DCHECK(!path.empty()); |
| 159 distilled_path_ = path; | 163 distilled_path_ = path; |
| 160 distilled_state_ = PROCESSED; | 164 distilled_state_ = PROCESSED; |
| 161 backoff_->Reset(); | 165 backoff_->Reset(); |
| 162 failed_download_counter_ = 0; | 166 failed_download_counter_ = 0; |
| 163 } | 167 } |
| 164 | 168 |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 208 int64_t creation_time_us = 0; | 212 int64_t creation_time_us = 0; |
| 209 if (pb_entry.has_creation_time_us()) { | 213 if (pb_entry.has_creation_time_us()) { |
| 210 creation_time_us = pb_entry.creation_time_us(); | 214 creation_time_us = pb_entry.creation_time_us(); |
| 211 } | 215 } |
| 212 | 216 |
| 213 int64_t update_time_us = 0; | 217 int64_t update_time_us = 0; |
| 214 if (pb_entry.has_update_time_us()) { | 218 if (pb_entry.has_update_time_us()) { |
| 215 update_time_us = pb_entry.update_time_us(); | 219 update_time_us = pb_entry.update_time_us(); |
| 216 } | 220 } |
| 217 | 221 |
| 218 bool read = false; | 222 State state = UNSEEN; |
| 219 if (pb_entry.has_status()) { | 223 if (pb_entry.has_status()) { |
| 220 read = pb_entry.status() == reading_list::ReadingListLocal::READ; | 224 switch (pb_entry.status()) { |
| 225 case reading_list::ReadingListLocal::READ: |
| 226 state = READ; |
| 227 break; |
| 228 case reading_list::ReadingListLocal::UNREAD: |
| 229 state = UNREAD; |
| 230 break; |
| 231 case reading_list::ReadingListLocal::UNSEEN: |
| 232 state = UNSEEN; |
| 233 break; |
| 234 } |
| 221 } | 235 } |
| 222 | 236 |
| 223 ReadingListEntry::DistillationState distillation_state = | 237 ReadingListEntry::DistillationState distillation_state = |
| 224 ReadingListEntry::WAITING; | 238 ReadingListEntry::WAITING; |
| 225 if (pb_entry.has_distillation_state()) { | 239 if (pb_entry.has_distillation_state()) { |
| 226 switch (pb_entry.distillation_state()) { | 240 switch (pb_entry.distillation_state()) { |
| 227 case reading_list::ReadingListLocal::WAITING: | 241 case reading_list::ReadingListLocal::WAITING: |
| 228 distillation_state = ReadingListEntry::WAITING; | 242 distillation_state = ReadingListEntry::WAITING; |
| 229 break; | 243 break; |
| 230 case reading_list::ReadingListLocal::PROCESSING: | 244 case reading_list::ReadingListLocal::PROCESSING: |
| (...skipping 26 matching lines...) Expand all Loading... |
| 257 JSONStringValueDeserializer deserializer(pb_entry.backoff()); | 271 JSONStringValueDeserializer deserializer(pb_entry.backoff()); |
| 258 std::unique_ptr<base::Value> value( | 272 std::unique_ptr<base::Value> value( |
| 259 deserializer.Deserialize(nullptr, nullptr)); | 273 deserializer.Deserialize(nullptr, nullptr)); |
| 260 if (value) { | 274 if (value) { |
| 261 backoff = net::BackoffEntrySerializer::DeserializeFromValue( | 275 backoff = net::BackoffEntrySerializer::DeserializeFromValue( |
| 262 *value, &kBackoffPolicy, nullptr, base::Time::Now()); | 276 *value, &kBackoffPolicy, nullptr, base::Time::Now()); |
| 263 } | 277 } |
| 264 } | 278 } |
| 265 | 279 |
| 266 return base::WrapUnique<ReadingListEntry>(new ReadingListEntry( | 280 return base::WrapUnique<ReadingListEntry>(new ReadingListEntry( |
| 267 url, title, read, creation_time_us, update_time_us, distillation_state, | 281 url, title, state, creation_time_us, update_time_us, distillation_state, |
| 268 distilled_path, failed_download_counter, std::move(backoff))); | 282 distilled_path, failed_download_counter, std::move(backoff))); |
| 269 } | 283 } |
| 270 | 284 |
| 271 // static | 285 // static |
| 272 std::unique_ptr<ReadingListEntry> ReadingListEntry::FromReadingListSpecifics( | 286 std::unique_ptr<ReadingListEntry> ReadingListEntry::FromReadingListSpecifics( |
| 273 const sync_pb::ReadingListSpecifics& pb_entry) { | 287 const sync_pb::ReadingListSpecifics& pb_entry) { |
| 274 if (!pb_entry.has_url()) { | 288 if (!pb_entry.has_url()) { |
| 275 return nullptr; | 289 return nullptr; |
| 276 } | 290 } |
| 277 GURL url(pb_entry.url()); | 291 GURL url(pb_entry.url()); |
| 278 if (url.is_empty() || !url.is_valid()) { | 292 if (url.is_empty() || !url.is_valid()) { |
| 279 return nullptr; | 293 return nullptr; |
| 280 } | 294 } |
| 281 std::string title; | 295 std::string title; |
| 282 if (pb_entry.has_title()) { | 296 if (pb_entry.has_title()) { |
| 283 title = pb_entry.title(); | 297 title = pb_entry.title(); |
| 284 } | 298 } |
| 285 | 299 |
| 286 int64_t creation_time_us = 0; | 300 int64_t creation_time_us = 0; |
| 287 if (pb_entry.has_creation_time_us()) { | 301 if (pb_entry.has_creation_time_us()) { |
| 288 creation_time_us = pb_entry.creation_time_us(); | 302 creation_time_us = pb_entry.creation_time_us(); |
| 289 } | 303 } |
| 290 | 304 |
| 291 int64_t update_time_us = 0; | 305 int64_t update_time_us = 0; |
| 292 if (pb_entry.has_update_time_us()) { | 306 if (pb_entry.has_update_time_us()) { |
| 293 update_time_us = pb_entry.update_time_us(); | 307 update_time_us = pb_entry.update_time_us(); |
| 294 } | 308 } |
| 295 | 309 |
| 296 bool read = false; | 310 State state = UNSEEN; |
| 297 if (pb_entry.has_status()) { | 311 if (pb_entry.has_status()) { |
| 298 read = pb_entry.status() == sync_pb::ReadingListSpecifics::READ; | 312 switch (pb_entry.status()) { |
| 313 case sync_pb::ReadingListSpecifics::READ: |
| 314 state = READ; |
| 315 break; |
| 316 case sync_pb::ReadingListSpecifics::UNREAD: |
| 317 state = UNREAD; |
| 318 break; |
| 319 case sync_pb::ReadingListSpecifics::UNSEEN: |
| 320 state = UNSEEN; |
| 321 break; |
| 322 } |
| 299 } | 323 } |
| 300 | 324 |
| 301 return base::WrapUnique<ReadingListEntry>( | 325 return base::WrapUnique<ReadingListEntry>( |
| 302 new ReadingListEntry(url, title, read, creation_time_us, update_time_us, | 326 new ReadingListEntry(url, title, state, creation_time_us, update_time_us, |
| 303 WAITING, base::FilePath(), 0, nullptr)); | 327 WAITING, base::FilePath(), 0, nullptr)); |
| 304 } | 328 } |
| 305 | 329 |
| 306 void ReadingListEntry::MergeLocalStateFrom(ReadingListEntry& other) { | 330 void ReadingListEntry::MergeLocalStateFrom(ReadingListEntry& other) { |
| 307 distilled_path_ = std::move(other.distilled_path_); | 331 distilled_path_ = std::move(other.distilled_path_); |
| 308 distilled_state_ = std::move(other.distilled_state_); | 332 distilled_state_ = std::move(other.distilled_state_); |
| 309 backoff_ = std::move(other.backoff_); | 333 backoff_ = std::move(other.backoff_); |
| 310 failed_download_counter_ = std::move(other.failed_download_counter_); | 334 failed_download_counter_ = std::move(other.failed_download_counter_); |
| 311 } | 335 } |
| 312 | 336 |
| 313 std::unique_ptr<reading_list::ReadingListLocal> | 337 std::unique_ptr<reading_list::ReadingListLocal> |
| 314 ReadingListEntry::AsReadingListLocal() const { | 338 ReadingListEntry::AsReadingListLocal() const { |
| 315 std::unique_ptr<reading_list::ReadingListLocal> pb_entry = | 339 std::unique_ptr<reading_list::ReadingListLocal> pb_entry = |
| 316 base::MakeUnique<reading_list::ReadingListLocal>(); | 340 base::MakeUnique<reading_list::ReadingListLocal>(); |
| 317 | 341 |
| 318 // URL is used as the key for the database and sync as there is only one entry | 342 // URL is used as the key for the database and sync as there is only one entry |
| 319 // per URL. | 343 // per URL. |
| 320 pb_entry->set_entry_id(URL().spec()); | 344 pb_entry->set_entry_id(URL().spec()); |
| 321 pb_entry->set_title(Title()); | 345 pb_entry->set_title(Title()); |
| 322 pb_entry->set_url(URL().spec()); | 346 pb_entry->set_url(URL().spec()); |
| 323 pb_entry->set_creation_time_us(CreationTime()); | 347 pb_entry->set_creation_time_us(CreationTime()); |
| 324 pb_entry->set_update_time_us(UpdateTime()); | 348 pb_entry->set_update_time_us(UpdateTime()); |
| 325 | 349 |
| 326 if (read_) { | 350 switch (state_) { |
| 327 pb_entry->set_status(reading_list::ReadingListLocal::READ); | 351 case READ: |
| 328 } else { | 352 pb_entry->set_status(reading_list::ReadingListLocal::READ); |
| 329 pb_entry->set_status(reading_list::ReadingListLocal::UNREAD); | 353 break; |
| 354 case UNREAD: |
| 355 pb_entry->set_status(reading_list::ReadingListLocal::UNREAD); |
| 356 break; |
| 357 case UNSEEN: |
| 358 pb_entry->set_status(reading_list::ReadingListLocal::UNSEEN); |
| 359 break; |
| 330 } | 360 } |
| 331 | 361 |
| 332 reading_list::ReadingListLocal::DistillationState distilation_state; | 362 reading_list::ReadingListLocal::DistillationState distilation_state; |
| 333 switch (DistilledState()) { | 363 switch (DistilledState()) { |
| 334 case ReadingListEntry::WAITING: | 364 case ReadingListEntry::WAITING: |
| 335 distilation_state = reading_list::ReadingListLocal::WAITING; | 365 distilation_state = reading_list::ReadingListLocal::WAITING; |
| 336 break; | 366 break; |
| 337 case ReadingListEntry::PROCESSING: | 367 case ReadingListEntry::PROCESSING: |
| 338 distilation_state = reading_list::ReadingListLocal::PROCESSING; | 368 distilation_state = reading_list::ReadingListLocal::PROCESSING; |
| 339 break; | 369 break; |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 372 base::MakeUnique<sync_pb::ReadingListSpecifics>(); | 402 base::MakeUnique<sync_pb::ReadingListSpecifics>(); |
| 373 | 403 |
| 374 // URL is used as the key for the database and sync as there is only one entry | 404 // URL is used as the key for the database and sync as there is only one entry |
| 375 // per URL. | 405 // per URL. |
| 376 pb_entry->set_entry_id(URL().spec()); | 406 pb_entry->set_entry_id(URL().spec()); |
| 377 pb_entry->set_title(Title()); | 407 pb_entry->set_title(Title()); |
| 378 pb_entry->set_url(URL().spec()); | 408 pb_entry->set_url(URL().spec()); |
| 379 pb_entry->set_creation_time_us(CreationTime()); | 409 pb_entry->set_creation_time_us(CreationTime()); |
| 380 pb_entry->set_update_time_us(UpdateTime()); | 410 pb_entry->set_update_time_us(UpdateTime()); |
| 381 | 411 |
| 382 if (read_) { | 412 switch (state_) { |
| 383 pb_entry->set_status(sync_pb::ReadingListSpecifics::READ); | 413 case READ: |
| 384 } else { | 414 pb_entry->set_status(sync_pb::ReadingListSpecifics::READ); |
| 385 pb_entry->set_status(sync_pb::ReadingListSpecifics::UNREAD); | 415 break; |
| 416 case UNREAD: |
| 417 pb_entry->set_status(sync_pb::ReadingListSpecifics::UNREAD); |
| 418 break; |
| 419 case UNSEEN: |
| 420 pb_entry->set_status(sync_pb::ReadingListSpecifics::UNSEEN); |
| 421 break; |
| 386 } | 422 } |
| 387 | 423 |
| 388 return pb_entry; | 424 return pb_entry; |
| 389 } | 425 } |
| 390 | 426 |
| 391 bool ReadingListEntry::CompareEntryUpdateTime(const ReadingListEntry& lhs, | 427 bool ReadingListEntry::CompareEntryUpdateTime(const ReadingListEntry& lhs, |
| 392 const ReadingListEntry& rhs) { | 428 const ReadingListEntry& rhs) { |
| 393 return lhs.UpdateTime() > rhs.UpdateTime(); | 429 return lhs.UpdateTime() > rhs.UpdateTime(); |
| 394 } | 430 } |
| OLD | NEW |