| 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_model_impl.h" | 5 #include "components/reading_list/ios/reading_list_model_impl.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/memory/ptr_util.h" | 9 #include "base/memory/ptr_util.h" |
| 10 #include "base/strings/string_util.h" | 10 #include "base/strings/string_util.h" |
| 11 #include "base/time/clock.h" |
| 11 #include "components/prefs/pref_service.h" | 12 #include "components/prefs/pref_service.h" |
| 12 #include "components/reading_list/ios/reading_list_model_storage.h" | 13 #include "components/reading_list/ios/reading_list_model_storage.h" |
| 13 #include "components/reading_list/ios/reading_list_pref_names.h" | 14 #include "components/reading_list/ios/reading_list_pref_names.h" |
| 14 #include "url/gurl.h" | 15 #include "url/gurl.h" |
| 15 | 16 |
| 16 ReadingListModelImpl::ReadingListModelImpl() | |
| 17 : ReadingListModelImpl(nullptr, nullptr) {} | |
| 18 | |
| 19 ReadingListModelImpl::ReadingListModelImpl( | 17 ReadingListModelImpl::ReadingListModelImpl( |
| 20 std::unique_ptr<ReadingListModelStorage> storage, | 18 std::unique_ptr<ReadingListModelStorage> storage, |
| 21 PrefService* pref_service) | 19 PrefService* pref_service, |
| 20 std::unique_ptr<base::Clock> clock) |
| 22 : entries_(base::MakeUnique<ReadingListEntries>()), | 21 : entries_(base::MakeUnique<ReadingListEntries>()), |
| 23 unread_entry_count_(0), | 22 unread_entry_count_(0), |
| 24 read_entry_count_(0), | 23 read_entry_count_(0), |
| 25 unseen_entry_count_(0), | 24 unseen_entry_count_(0), |
| 25 clock_(std::move(clock)), |
| 26 pref_service_(pref_service), | 26 pref_service_(pref_service), |
| 27 has_unseen_(false), | 27 has_unseen_(false), |
| 28 loaded_(false), | 28 loaded_(false), |
| 29 weak_ptr_factory_(this) { | 29 weak_ptr_factory_(this) { |
| 30 DCHECK(CalledOnValidThread()); | 30 DCHECK(CalledOnValidThread()); |
| 31 DCHECK(clock_); |
| 31 if (storage) { | 32 if (storage) { |
| 32 storage_layer_ = std::move(storage); | 33 storage_layer_ = std::move(storage); |
| 33 storage_layer_->SetReadingListModel(this, this); | 34 storage_layer_->SetReadingListModel(this, this, clock_.get()); |
| 34 } else { | 35 } else { |
| 35 loaded_ = true; | 36 loaded_ = true; |
| 36 } | 37 } |
| 37 has_unseen_ = GetPersistentHasUnseen(); | 38 has_unseen_ = GetPersistentHasUnseen(); |
| 38 } | 39 } |
| 39 | 40 |
| 40 ReadingListModelImpl::~ReadingListModelImpl() {} | 41 ReadingListModelImpl::~ReadingListModelImpl() {} |
| 41 | 42 |
| 42 void ReadingListModelImpl::StoreLoaded( | 43 void ReadingListModelImpl::StoreLoaded( |
| 43 std::unique_ptr<ReadingListEntries> entries) { | 44 std::unique_ptr<ReadingListEntries> entries) { |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 127 model_batch_updates = BeginBatchUpdates(); | 128 model_batch_updates = BeginBatchUpdates(); |
| 128 for (auto& iterator : *entries_) { | 129 for (auto& iterator : *entries_) { |
| 129 ReadingListEntry& entry = iterator.second; | 130 ReadingListEntry& entry = iterator.second; |
| 130 if (entry.HasBeenSeen()) { | 131 if (entry.HasBeenSeen()) { |
| 131 continue; | 132 continue; |
| 132 } | 133 } |
| 133 for (auto& observer : observers_) { | 134 for (auto& observer : observers_) { |
| 134 observer.ReadingListWillUpdateEntry(this, iterator.first); | 135 observer.ReadingListWillUpdateEntry(this, iterator.first); |
| 135 } | 136 } |
| 136 UpdateEntryStateCountersOnEntryRemoval(entry); | 137 UpdateEntryStateCountersOnEntryRemoval(entry); |
| 137 entry.SetRead(false); | 138 entry.SetRead(false, clock_->Now()); |
| 138 UpdateEntryStateCountersOnEntryInsertion(entry); | 139 UpdateEntryStateCountersOnEntryInsertion(entry); |
| 139 if (storage_layer_) { | 140 if (storage_layer_) { |
| 140 storage_layer_->SaveEntry(entry); | 141 storage_layer_->SaveEntry(entry); |
| 141 } | 142 } |
| 142 for (auto& observer : observers_) { | 143 for (auto& observer : observers_) { |
| 143 observer.ReadingListDidApplyChanges(this); | 144 observer.ReadingListDidApplyChanges(this); |
| 144 } | 145 } |
| 145 } | 146 } |
| 146 DCHECK(unseen_entry_count_ == 0); | 147 DCHECK(unseen_entry_count_ == 0); |
| 147 } | 148 } |
| (...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 311 const GURL& url, | 312 const GURL& url, |
| 312 const std::string& title, | 313 const std::string& title, |
| 313 reading_list::EntrySource source) { | 314 reading_list::EntrySource source) { |
| 314 DCHECK(CalledOnValidThread()); | 315 DCHECK(CalledOnValidThread()); |
| 315 DCHECK(loaded()); | 316 DCHECK(loaded()); |
| 316 DCHECK(url.SchemeIsHTTPOrHTTPS()); | 317 DCHECK(url.SchemeIsHTTPOrHTTPS()); |
| 317 RemoveEntryByURL(url); | 318 RemoveEntryByURL(url); |
| 318 | 319 |
| 319 std::string trimmed_title = base::CollapseWhitespaceASCII(title, false); | 320 std::string trimmed_title = base::CollapseWhitespaceASCII(title, false); |
| 320 | 321 |
| 321 ReadingListEntry entry(url, trimmed_title); | 322 ReadingListEntry entry(url, trimmed_title, clock_->Now()); |
| 322 for (auto& observer : observers_) | 323 for (auto& observer : observers_) |
| 323 observer.ReadingListWillAddEntry(this, entry); | 324 observer.ReadingListWillAddEntry(this, entry); |
| 324 UpdateEntryStateCountersOnEntryInsertion(entry); | 325 UpdateEntryStateCountersOnEntryInsertion(entry); |
| 325 SetUnseenFlag(); | 326 SetUnseenFlag(); |
| 326 entries_->insert(std::make_pair(url, std::move(entry))); | 327 entries_->insert(std::make_pair(url, std::move(entry))); |
| 327 | 328 |
| 328 if (storage_layer_) { | 329 if (storage_layer_) { |
| 329 storage_layer_->SaveEntry(*GetEntryByURL(url)); | 330 storage_layer_->SaveEntry(*GetEntryByURL(url)); |
| 330 } | 331 } |
| 331 | 332 |
| (...skipping 13 matching lines...) Expand all Loading... |
| 345 return; | 346 return; |
| 346 } | 347 } |
| 347 ReadingListEntry& entry = iterator->second; | 348 ReadingListEntry& entry = iterator->second; |
| 348 if (entry.IsRead() == read) { | 349 if (entry.IsRead() == read) { |
| 349 return; | 350 return; |
| 350 } | 351 } |
| 351 for (ReadingListModelObserver& observer : observers_) { | 352 for (ReadingListModelObserver& observer : observers_) { |
| 352 observer.ReadingListWillMoveEntry(this, url); | 353 observer.ReadingListWillMoveEntry(this, url); |
| 353 } | 354 } |
| 354 UpdateEntryStateCountersOnEntryRemoval(entry); | 355 UpdateEntryStateCountersOnEntryRemoval(entry); |
| 355 entry.SetRead(read); | 356 entry.SetRead(read, clock_->Now()); |
| 356 entry.MarkEntryUpdated(); | 357 entry.MarkEntryUpdated(clock_->Now()); |
| 357 UpdateEntryStateCountersOnEntryInsertion(entry); | 358 UpdateEntryStateCountersOnEntryInsertion(entry); |
| 358 | 359 |
| 359 if (storage_layer_) { | 360 if (storage_layer_) { |
| 360 storage_layer_->SaveEntry(entry); | 361 storage_layer_->SaveEntry(entry); |
| 361 } | 362 } |
| 362 for (ReadingListModelObserver& observer : observers_) { | 363 for (ReadingListModelObserver& observer : observers_) { |
| 363 observer.ReadingListDidMoveEntry(this, url); | 364 observer.ReadingListDidMoveEntry(this, url); |
| 364 observer.ReadingListDidApplyChanges(this); | 365 observer.ReadingListDidApplyChanges(this); |
| 365 } | 366 } |
| 366 } | 367 } |
| 367 | 368 |
| 368 void ReadingListModelImpl::SetEntryTitle(const GURL& url, | 369 void ReadingListModelImpl::SetEntryTitle(const GURL& url, |
| 369 const std::string& title) { | 370 const std::string& title) { |
| 370 DCHECK(CalledOnValidThread()); | 371 DCHECK(CalledOnValidThread()); |
| 371 DCHECK(loaded()); | 372 DCHECK(loaded()); |
| 372 auto iterator = entries_->find(url); | 373 auto iterator = entries_->find(url); |
| 373 if (iterator == entries_->end()) { | 374 if (iterator == entries_->end()) { |
| 374 return; | 375 return; |
| 375 } | 376 } |
| 376 ReadingListEntry& entry = iterator->second; | 377 ReadingListEntry& entry = iterator->second; |
| 377 std::string trimmed_title = base::CollapseWhitespaceASCII(title, false); | 378 std::string trimmed_title = base::CollapseWhitespaceASCII(title, false); |
| 378 if (entry.Title() == trimmed_title) { | 379 if (entry.Title() == trimmed_title) { |
| 379 return; | 380 return; |
| 380 } | 381 } |
| 381 | 382 |
| 382 for (ReadingListModelObserver& observer : observers_) { | 383 for (ReadingListModelObserver& observer : observers_) { |
| 383 observer.ReadingListWillUpdateEntry(this, url); | 384 observer.ReadingListWillUpdateEntry(this, url); |
| 384 } | 385 } |
| 385 entry.SetTitle(trimmed_title); | 386 entry.SetTitle(trimmed_title, clock_->Now()); |
| 386 if (storage_layer_) { | 387 if (storage_layer_) { |
| 387 storage_layer_->SaveEntry(entry); | 388 storage_layer_->SaveEntry(entry); |
| 388 } | 389 } |
| 389 for (ReadingListModelObserver& observer : observers_) { | 390 for (ReadingListModelObserver& observer : observers_) { |
| 390 observer.ReadingListDidApplyChanges(this); | 391 observer.ReadingListDidApplyChanges(this); |
| 391 } | 392 } |
| 392 } | 393 } |
| 393 | 394 |
| 394 void ReadingListModelImpl::SetEntryDistilledInfo( | 395 void ReadingListModelImpl::SetEntryDistilledInfo( |
| 395 const GURL& url, | 396 const GURL& url, |
| 396 const base::FilePath& distilled_path, | 397 const base::FilePath& distilled_path, |
| 397 const GURL& distilled_url, | 398 const GURL& distilled_url, |
| 398 int64_t distillation_size, | 399 int64_t distillation_size, |
| 399 int64_t distillation_date) { | 400 const base::Time& distillation_date) { |
| 400 DCHECK(CalledOnValidThread()); | 401 DCHECK(CalledOnValidThread()); |
| 401 DCHECK(loaded()); | 402 DCHECK(loaded()); |
| 402 auto iterator = entries_->find(url); | 403 auto iterator = entries_->find(url); |
| 403 if (iterator == entries_->end()) { | 404 if (iterator == entries_->end()) { |
| 404 return; | 405 return; |
| 405 } | 406 } |
| 406 ReadingListEntry& entry = iterator->second; | 407 ReadingListEntry& entry = iterator->second; |
| 407 if (entry.DistilledState() == ReadingListEntry::PROCESSED && | 408 if (entry.DistilledState() == ReadingListEntry::PROCESSED && |
| 408 entry.DistilledPath() == distilled_path) { | 409 entry.DistilledPath() == distilled_path) { |
| 409 return; | 410 return; |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 501 | 502 |
| 502 syncer::ModelTypeSyncBridge* ReadingListModelImpl::GetModelTypeSyncBridge() { | 503 syncer::ModelTypeSyncBridge* ReadingListModelImpl::GetModelTypeSyncBridge() { |
| 503 if (!storage_layer_) | 504 if (!storage_layer_) |
| 504 return nullptr; | 505 return nullptr; |
| 505 return storage_layer_.get(); | 506 return storage_layer_.get(); |
| 506 } | 507 } |
| 507 | 508 |
| 508 ReadingListModelStorage* ReadingListModelImpl::StorageLayer() { | 509 ReadingListModelStorage* ReadingListModelImpl::StorageLayer() { |
| 509 return storage_layer_.get(); | 510 return storage_layer_.get(); |
| 510 } | 511 } |
| OLD | NEW |