Index: ios/chrome/browser/reading_list/reading_list_model_impl.cc |
diff --git a/ios/chrome/browser/reading_list/reading_list_model_impl.cc b/ios/chrome/browser/reading_list/reading_list_model_impl.cc |
index 78453a9cabdd585b8467c34909c4d93ab5db9eda..e67ea6e8440b16affd4014d783b4ffd875fc606b 100644 |
--- a/ios/chrome/browser/reading_list/reading_list_model_impl.cc |
+++ b/ios/chrome/browser/reading_list/reading_list_model_impl.cc |
@@ -4,9 +4,6 @@ |
#include "ios/chrome/browser/reading_list/reading_list_model_impl.h" |
-#include "base/bind.h" |
-#include "base/logging.h" |
-#include "base/memory/ptr_util.h" |
#include "base/strings/string_util.h" |
#include "components/prefs/pref_service.h" |
#include "ios/chrome/browser/reading_list/reading_list_model_storage.h" |
@@ -19,116 +16,71 @@ |
ReadingListModelImpl::ReadingListModelImpl( |
std::unique_ptr<ReadingListModelStorage> storage, |
PrefService* pref_service) |
- : pref_service_(pref_service), |
- has_unseen_(false), |
- loaded_(false), |
- weak_ptr_factory_(this) { |
- DCHECK(CalledOnValidThread()); |
+ : pref_service_(pref_service), has_unseen_(false) { |
if (storage) { |
storage_layer_ = std::move(storage); |
- storage_layer_->SetReadingListModel(this, this); |
- } else { |
- loaded_ = true; |
- read_ = base::MakeUnique<ReadingListEntries>(); |
- unread_ = base::MakeUnique<ReadingListEntries>(); |
- } |
- has_unseen_ = GetPersistentHasUnseen(); |
+ read_ = storage_layer_->LoadPersistentReadList(); |
+ unread_ = storage_layer_->LoadPersistentUnreadList(); |
+ has_unseen_ = GetPersistentHasUnseen(); |
+ } |
+ loaded_ = true; |
} |
ReadingListModelImpl::~ReadingListModelImpl() {} |
-void ReadingListModelImpl::StoreLoaded( |
- std::unique_ptr<ReadingListEntries> unread, |
- std::unique_ptr<ReadingListEntries> read) { |
- DCHECK(CalledOnValidThread()); |
- read_ = std::move(read); |
- unread_ = std::move(unread); |
- loaded_ = true; |
- SortEntries(); |
- for (auto& observer : observers_) |
- observer.ReadingListModelLoaded(this); |
-} |
- |
void ReadingListModelImpl::Shutdown() { |
- DCHECK(CalledOnValidThread()); |
for (auto& observer : observers_) |
observer.ReadingListModelBeingDeleted(this); |
loaded_ = false; |
} |
bool ReadingListModelImpl::loaded() const { |
- DCHECK(CalledOnValidThread()); |
return loaded_; |
} |
size_t ReadingListModelImpl::unread_size() const { |
- DCHECK(CalledOnValidThread()); |
- if (!loaded()) |
- return 0; |
- return unread_->size(); |
+ DCHECK(loaded()); |
+ return unread_.size(); |
} |
size_t ReadingListModelImpl::read_size() const { |
- DCHECK(CalledOnValidThread()); |
- if (!loaded()) |
- return 0; |
- return read_->size(); |
+ DCHECK(loaded()); |
+ return read_.size(); |
} |
bool ReadingListModelImpl::HasUnseenEntries() const { |
- DCHECK(CalledOnValidThread()); |
- if (!loaded()) |
- return false; |
+ DCHECK(loaded()); |
return unread_size() && has_unseen_; |
} |
void ReadingListModelImpl::ResetUnseenEntries() { |
- DCHECK(CalledOnValidThread()); |
DCHECK(loaded()); |
has_unseen_ = false; |
- if (!IsPerformingBatchUpdates()) |
+ if (storage_layer_ && !IsPerformingBatchUpdates()) |
SetPersistentHasUnseen(false); |
} |
const ReadingListEntry& ReadingListModelImpl::GetUnreadEntryAtIndex( |
size_t index) const { |
- DCHECK(CalledOnValidThread()); |
- DCHECK(loaded()); |
- return unread_->at(index); |
+ DCHECK(loaded()); |
+ return unread_[index]; |
} |
const ReadingListEntry& ReadingListModelImpl::GetReadEntryAtIndex( |
size_t index) const { |
- DCHECK(CalledOnValidThread()); |
- DCHECK(loaded()); |
- return read_->at(index); |
+ DCHECK(loaded()); |
+ return read_[index]; |
} |
const ReadingListEntry* ReadingListModelImpl::GetEntryFromURL( |
- const GURL& gurl, |
- bool* read) const { |
- DCHECK(CalledOnValidThread()); |
- DCHECK(loaded()); |
- return GetMutableEntryFromURL(gurl, read); |
-} |
- |
-ReadingListEntry* ReadingListModelImpl::GetMutableEntryFromURL( |
- const GURL& gurl, |
- bool* read) const { |
- DCHECK(CalledOnValidThread()); |
- DCHECK(loaded()); |
- bool is_read; |
+ const GURL& gurl) const { |
+ DCHECK(loaded()); |
ReadingListEntry entry(gurl, std::string()); |
- auto it = std::find(read_->begin(), read_->end(), entry); |
- is_read = true; |
- if (it == read_->end()) { |
- it = std::find(unread_->begin(), unread_->end(), entry); |
- is_read = false; |
- if (it == unread_->end()) |
+ auto it = std::find(read_.begin(), read_.end(), entry); |
+ if (it == read_.end()) { |
+ it = std::find(unread_.begin(), unread_.end(), entry); |
+ if (it == unread_.end()) |
return nullptr; |
- } |
- if (read) { |
- *read = is_read; |
} |
return &(*it); |
} |
@@ -136,9 +88,8 @@ |
bool ReadingListModelImpl::CallbackEntryURL( |
const GURL& url, |
base::Callback<void(const ReadingListEntry&)> callback) const { |
- DCHECK(CalledOnValidThread()); |
- DCHECK(loaded()); |
- const ReadingListEntry* entry = GetMutableEntryFromURL(url, nullptr); |
+ DCHECK(loaded()); |
+ const ReadingListEntry* entry = GetEntryFromURL(url); |
if (entry) { |
callback.Run(*entry); |
return true; |
@@ -146,114 +97,33 @@ |
return false; |
} |
-void ReadingListModelImpl::MoveEntryFrom(ReadingListEntries* entries, |
- const ReadingListEntry& entry, |
- bool read) { |
- auto result = std::find(entries->begin(), entries->end(), entry); |
- DCHECK(result != entries->end()); |
- int index = std::distance(entries->begin(), result); |
- for (auto& observer : observers_) |
- observer.ReadingListWillMoveEntry(this, index, read); |
- entries->erase(result); |
-} |
- |
-void ReadingListModelImpl::SyncAddEntry(std::unique_ptr<ReadingListEntry> entry, |
- bool read) { |
- DCHECK(CalledOnValidThread()); |
- DCHECK(loaded()); |
- // entry must not already exist. |
- DCHECK(GetMutableEntryFromURL(entry->URL(), nullptr) == nullptr); |
- if (read) { |
- for (auto& observer : observers_) |
- observer.ReadingListWillAddReadEntry(this, *entry); |
- read_->insert(read_->begin(), std::move(*entry)); |
- } else { |
- for (auto& observer : observers_) |
- observer.ReadingListWillAddUnreadEntry(this, *entry); |
- has_unseen_ = true; |
- SetPersistentHasUnseen(true); |
- unread_->insert(unread_->begin(), std::move(*entry)); |
- } |
- for (auto& observer : observers_) |
- observer.ReadingListDidApplyChanges(this); |
-} |
- |
-ReadingListEntry* ReadingListModelImpl::SyncMergeEntry( |
- std::unique_ptr<ReadingListEntry> entry, |
- bool read) { |
- DCHECK(CalledOnValidThread()); |
- DCHECK(loaded()); |
- bool is_existing_entry_read; |
- ReadingListEntry* existing_entry = |
- GetMutableEntryFromURL(entry->URL(), &is_existing_entry_read); |
- |
- DCHECK(existing_entry); |
- DCHECK(existing_entry->UpdateTime() < entry->UpdateTime()); |
- |
- // Merge local data in new entry. |
- entry->MergeLocalStateFrom(*existing_entry); |
- |
- if (is_existing_entry_read) { |
- MoveEntryFrom(read_.get(), *existing_entry, true); |
- } else { |
- MoveEntryFrom(unread_.get(), *existing_entry, false); |
- } |
- |
- ReadingListEntries::iterator added_iterator; |
- if (read) { |
- read_->push_back(std::move(*entry)); |
- added_iterator = read_->end() - 1; |
- } else { |
- unread_->push_back(std::move(*entry)); |
- added_iterator = unread_->end() - 1; |
- } |
- for (auto& observer : observers_) |
- observer.ReadingListDidApplyChanges(this); |
- |
- ReadingListEntry& merged_entry = *added_iterator; |
- return &merged_entry; |
-} |
- |
-void ReadingListModelImpl::SyncRemoveEntry(const GURL& url) { |
- RemoveEntryByURLImpl(url, true); |
-} |
- |
void ReadingListModelImpl::RemoveEntryByURL(const GURL& url) { |
- RemoveEntryByURLImpl(url, false); |
-} |
- |
-void ReadingListModelImpl::RemoveEntryByURLImpl(const GURL& url, |
- bool from_sync) { |
- DCHECK(CalledOnValidThread()); |
DCHECK(loaded()); |
const ReadingListEntry entry(url, std::string()); |
- auto result = std::find(unread_->begin(), unread_->end(), entry); |
- if (result != unread_->end()) { |
- for (auto& observer : observers_) |
+ auto result = std::find(unread_.begin(), unread_.end(), entry); |
+ if (result != unread_.end()) { |
+ for (auto& observer : observers_) { |
observer.ReadingListWillRemoveUnreadEntry( |
- this, std::distance(unread_->begin(), result)); |
- |
- if (storage_layer_ && !from_sync) { |
- storage_layer_->RemoveEntry(*result); |
- } |
- unread_->erase(result); |
- |
- for (auto& observer : observers_) |
- observer.ReadingListDidApplyChanges(this); |
- return; |
- } |
- |
- result = std::find(read_->begin(), read_->end(), entry); |
- if (result != read_->end()) { |
- for (auto& observer : observers_) |
+ this, std::distance(unread_.begin(), result)); |
+ } |
+ unread_.erase(result); |
+ if (storage_layer_ && !IsPerformingBatchUpdates()) |
+ storage_layer_->SavePersistentUnreadList(unread_); |
+ for (auto& observer : observers_) |
+ observer.ReadingListDidApplyChanges(this); |
+ return; |
+ } |
+ |
+ result = std::find(read_.begin(), read_.end(), entry); |
+ if (result != read_.end()) { |
+ for (auto& observer : observers_) { |
observer.ReadingListWillRemoveReadEntry( |
- this, std::distance(read_->begin(), result)); |
- if (storage_layer_ && !from_sync) { |
- storage_layer_->RemoveEntry(*result); |
- } |
- read_->erase(result); |
- |
+ this, std::distance(read_.begin(), result)); |
+ } |
+ read_.erase(result); |
+ if (storage_layer_ && !IsPerformingBatchUpdates()) |
+ storage_layer_->SavePersistentReadList(read_); |
for (auto& observer : observers_) |
observer.ReadingListDidApplyChanges(this); |
return; |
@@ -263,7 +133,6 @@ |
const ReadingListEntry& ReadingListModelImpl::AddEntry( |
const GURL& url, |
const std::string& title) { |
- DCHECK(CalledOnValidThread()); |
DCHECK(loaded()); |
RemoveEntryByURL(url); |
@@ -273,98 +142,92 @@ |
ReadingListEntry entry(url, trimmedTitle); |
for (auto& observer : observers_) |
observer.ReadingListWillAddUnreadEntry(this, entry); |
+ unread_.insert(unread_.begin(), std::move(entry)); |
has_unseen_ = true; |
- SetPersistentHasUnseen(true); |
- if (storage_layer_) { |
- storage_layer_->SaveEntry(entry, false); |
- } |
- unread_->insert(unread_->begin(), std::move(entry)); |
- |
+ if (storage_layer_ && !IsPerformingBatchUpdates()) { |
+ storage_layer_->SavePersistentUnreadList(unread_); |
+ SetPersistentHasUnseen(true); |
+ } |
for (auto& observer : observers_) |
observer.ReadingListDidApplyChanges(this); |
- return *unread_->begin(); |
+ return *unread_.begin(); |
} |
void ReadingListModelImpl::MarkUnreadByURL(const GURL& url) { |
- DCHECK(CalledOnValidThread()); |
DCHECK(loaded()); |
ReadingListEntry entry(url, std::string()); |
- auto result = std::find(read_->begin(), read_->end(), entry); |
- if (result == read_->end()) |
+ auto result = std::find(read_.begin(), read_.end(), entry); |
+ if (result == read_.end()) |
return; |
for (ReadingListModelObserver& observer : observers_) { |
- observer.ReadingListWillMoveEntry( |
- this, std::distance(read_->begin(), result), true); |
- } |
- |
- result->MarkEntryUpdated(); |
- if (storage_layer_) { |
- storage_layer_->SaveEntry(*result, false); |
- } |
- |
- unread_->insert(unread_->begin(), std::move(*result)); |
- read_->erase(result); |
- |
+ observer.ReadingListWillMoveEntry(this, |
+ std::distance(read_.begin(), result)); |
+ } |
+ |
+ unread_.insert(unread_.begin(), std::move(*result)); |
+ read_.erase(result); |
+ |
+ if (storage_layer_ && !IsPerformingBatchUpdates()) { |
+ storage_layer_->SavePersistentUnreadList(read_); |
+ storage_layer_->SavePersistentReadList(unread_); |
+ } |
for (ReadingListModelObserver& observer : observers_) { |
observer.ReadingListDidApplyChanges(this); |
} |
} |
void ReadingListModelImpl::MarkReadByURL(const GURL& url) { |
- DCHECK(CalledOnValidThread()); |
DCHECK(loaded()); |
ReadingListEntry entry(url, std::string()); |
- auto result = std::find(unread_->begin(), unread_->end(), entry); |
- if (result == unread_->end()) |
- return; |
- |
- for (auto& observer : observers_) |
- observer.ReadingListWillMoveEntry( |
- this, std::distance(unread_->begin(), result), false); |
- |
- result->MarkEntryUpdated(); |
- if (storage_layer_) { |
- storage_layer_->SaveEntry(*result, true); |
- } |
- |
- read_->insert(read_->begin(), std::move(*result)); |
- unread_->erase(result); |
- |
+ auto result = std::find(unread_.begin(), unread_.end(), entry); |
+ if (result == unread_.end()) |
+ return; |
+ |
+ for (auto& observer : observers_) { |
+ observer.ReadingListWillMoveEntry(this, |
+ std::distance(unread_.begin(), result)); |
+ } |
+ |
+ read_.insert(read_.begin(), std::move(*result)); |
+ unread_.erase(result); |
+ |
+ if (storage_layer_ && !IsPerformingBatchUpdates()) { |
+ storage_layer_->SavePersistentUnreadList(unread_); |
+ storage_layer_->SavePersistentReadList(read_); |
+ } |
for (auto& observer : observers_) |
observer.ReadingListDidApplyChanges(this); |
} |
void ReadingListModelImpl::SetEntryTitle(const GURL& url, |
const std::string& title) { |
- DCHECK(CalledOnValidThread()); |
DCHECK(loaded()); |
const ReadingListEntry entry(url, std::string()); |
- auto result = std::find(unread_->begin(), unread_->end(), entry); |
- if (result != unread_->end()) { |
- for (auto& observer : observers_) |
+ auto result = std::find(unread_.begin(), unread_.end(), entry); |
+ if (result != unread_.end()) { |
+ for (auto& observer : observers_) { |
observer.ReadingListWillUpdateUnreadEntry( |
- this, std::distance(unread_->begin(), result)); |
+ this, std::distance(unread_.begin(), result)); |
+ } |
result->SetTitle(title); |
- |
- if (storage_layer_) { |
- storage_layer_->SaveEntry(*result, false); |
- } |
- for (auto& observer : observers_) |
- observer.ReadingListDidApplyChanges(this); |
- return; |
- } |
- |
- result = std::find(read_->begin(), read_->end(), entry); |
- if (result != read_->end()) { |
- for (auto& observer : observers_) |
+ if (storage_layer_ && !IsPerformingBatchUpdates()) |
+ storage_layer_->SavePersistentUnreadList(unread_); |
+ for (auto& observer : observers_) |
+ observer.ReadingListDidApplyChanges(this); |
+ return; |
+ } |
+ |
+ result = std::find(read_.begin(), read_.end(), entry); |
+ if (result != read_.end()) { |
+ for (auto& observer : observers_) { |
observer.ReadingListWillUpdateReadEntry( |
- this, std::distance(read_->begin(), result)); |
+ this, std::distance(read_.begin(), result)); |
+ } |
result->SetTitle(title); |
- if (storage_layer_) { |
- storage_layer_->SaveEntry(*result, true); |
- } |
+ if (storage_layer_ && !IsPerformingBatchUpdates()) |
+ storage_layer_->SavePersistentReadList(read_); |
for (auto& observer : observers_) |
observer.ReadingListDidApplyChanges(this); |
return; |
@@ -374,33 +237,32 @@ |
void ReadingListModelImpl::SetEntryDistilledPath( |
const GURL& url, |
const base::FilePath& distilled_path) { |
- DCHECK(CalledOnValidThread()); |
DCHECK(loaded()); |
const ReadingListEntry entry(url, std::string()); |
- auto result = std::find(unread_->begin(), unread_->end(), entry); |
- if (result != unread_->end()) { |
- for (auto& observer : observers_) |
+ auto result = std::find(unread_.begin(), unread_.end(), entry); |
+ if (result != unread_.end()) { |
+ for (auto& observer : observers_) { |
observer.ReadingListWillUpdateUnreadEntry( |
- this, std::distance(unread_->begin(), result)); |
+ this, std::distance(unread_.begin(), result)); |
+ } |
result->SetDistilledPath(distilled_path); |
- if (storage_layer_) { |
- storage_layer_->SaveEntry(*result, false); |
- } |
- for (auto& observer : observers_) |
- observer.ReadingListDidApplyChanges(this); |
- return; |
- } |
- |
- result = std::find(read_->begin(), read_->end(), entry); |
- if (result != read_->end()) { |
- for (auto& observer : observers_) |
+ if (storage_layer_ && !IsPerformingBatchUpdates()) |
+ storage_layer_->SavePersistentUnreadList(unread_); |
+ for (auto& observer : observers_) |
+ observer.ReadingListDidApplyChanges(this); |
+ return; |
+ } |
+ |
+ result = std::find(read_.begin(), read_.end(), entry); |
+ if (result != read_.end()) { |
+ for (auto& observer : observers_) { |
observer.ReadingListWillUpdateReadEntry( |
- this, std::distance(read_->begin(), result)); |
+ this, std::distance(read_.begin(), result)); |
+ } |
result->SetDistilledPath(distilled_path); |
- if (storage_layer_) { |
- storage_layer_->SaveEntry(*result, true); |
- } |
+ if (storage_layer_ && !IsPerformingBatchUpdates()) |
+ storage_layer_->SavePersistentReadList(read_); |
for (auto& observer : observers_) |
observer.ReadingListDidApplyChanges(this); |
return; |
@@ -410,75 +272,49 @@ |
void ReadingListModelImpl::SetEntryDistilledState( |
const GURL& url, |
ReadingListEntry::DistillationState state) { |
- DCHECK(CalledOnValidThread()); |
DCHECK(loaded()); |
const ReadingListEntry entry(url, std::string()); |
- auto result = std::find(unread_->begin(), unread_->end(), entry); |
- if (result != unread_->end()) { |
- for (auto& observer : observers_) |
+ auto result = std::find(unread_.begin(), unread_.end(), entry); |
+ if (result != unread_.end()) { |
+ for (auto& observer : observers_) { |
observer.ReadingListWillUpdateUnreadEntry( |
- this, std::distance(unread_->begin(), result)); |
+ this, std::distance(unread_.begin(), result)); |
+ } |
result->SetDistilledState(state); |
- if (storage_layer_) { |
- storage_layer_->SaveEntry(*result, false); |
- } |
- for (auto& observer : observers_) |
- observer.ReadingListDidApplyChanges(this); |
- return; |
- } |
- |
- result = std::find(read_->begin(), read_->end(), entry); |
- if (result != read_->end()) { |
- for (auto& observer : observers_) |
+ if (storage_layer_ && !IsPerformingBatchUpdates()) |
+ storage_layer_->SavePersistentUnreadList(unread_); |
+ for (auto& observer : observers_) |
+ observer.ReadingListDidApplyChanges(this); |
+ return; |
+ } |
+ |
+ result = std::find(read_.begin(), read_.end(), entry); |
+ if (result != read_.end()) { |
+ for (auto& observer : observers_) { |
observer.ReadingListWillUpdateReadEntry( |
- this, std::distance(read_->begin(), result)); |
+ this, std::distance(read_.begin(), result)); |
+ } |
result->SetDistilledState(state); |
- if (storage_layer_) { |
- storage_layer_->SaveEntry(*result, true); |
- } |
+ if (storage_layer_ && !IsPerformingBatchUpdates()) |
+ storage_layer_->SavePersistentReadList(read_); |
for (auto& observer : observers_) |
observer.ReadingListDidApplyChanges(this); |
return; |
} |
}; |
-std::unique_ptr<ReadingListModel::ScopedReadingListBatchUpdate> |
-ReadingListModelImpl::CreateBatchToken() { |
- return base::MakeUnique<ReadingListModelImpl::ScopedReadingListBatchUpdate>( |
- this); |
-} |
- |
-ReadingListModelImpl::ScopedReadingListBatchUpdate:: |
- ScopedReadingListBatchUpdate(ReadingListModelImpl* model) |
- : ReadingListModel::ScopedReadingListBatchUpdate:: |
- ScopedReadingListBatchUpdate(model) { |
- if (model->StorageLayer()) { |
- storage_token_ = model->StorageLayer()->EnsureBatchCreated(); |
- } |
-} |
- |
-ReadingListModelImpl::ScopedReadingListBatchUpdate:: |
- ~ScopedReadingListBatchUpdate() { |
- storage_token_.reset(); |
-} |
- |
-void ReadingListModelImpl::LeavingBatchUpdates() { |
- DCHECK(CalledOnValidThread()); |
- if (storage_layer_) { |
- SetPersistentHasUnseen(has_unseen_); |
- SortEntries(); |
- } |
- ReadingListModel::LeavingBatchUpdates(); |
-} |
- |
-void ReadingListModelImpl::EnteringBatchUpdates() { |
- DCHECK(CalledOnValidThread()); |
- ReadingListModel::EnteringBatchUpdates(); |
+void ReadingListModelImpl::EndBatchUpdates() { |
+ ReadingListModel::EndBatchUpdates(); |
+ if (IsPerformingBatchUpdates() || !storage_layer_) { |
+ return; |
+ } |
+ storage_layer_->SavePersistentUnreadList(unread_); |
+ storage_layer_->SavePersistentReadList(read_); |
+ SetPersistentHasUnseen(has_unseen_); |
} |
void ReadingListModelImpl::SetPersistentHasUnseen(bool has_unseen) { |
- DCHECK(CalledOnValidThread()); |
if (!pref_service_) { |
return; |
} |
@@ -487,30 +323,9 @@ |
} |
bool ReadingListModelImpl::GetPersistentHasUnseen() { |
- DCHECK(CalledOnValidThread()); |
if (!pref_service_) { |
return false; |
} |
return pref_service_->GetBoolean( |
reading_list::prefs::kReadingListHasUnseenEntries); |
} |
- |
-syncer::ModelTypeSyncBridge* ReadingListModelImpl::GetModelTypeSyncBridge() { |
- DCHECK(loaded()); |
- if (!storage_layer_) |
- return nullptr; |
- return storage_layer_->GetModelTypeSyncBridge(); |
-} |
- |
-void ReadingListModelImpl::SortEntries() { |
- DCHECK(CalledOnValidThread()); |
- DCHECK(loaded()); |
- std::sort(read_->begin(), read_->end(), |
- ReadingListEntry::CompareEntryUpdateTime); |
- std::sort(unread_->begin(), unread_->end(), |
- ReadingListEntry::CompareEntryUpdateTime); |
-} |
- |
-ReadingListModelStorage* ReadingListModelImpl::StorageLayer() { |
- return storage_layer_.get(); |
-} |