Chromium Code Reviews| 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..6d5d83f179b3e3705686f1fff76e894496a57ab5 100644 |
| --- a/ios/chrome/browser/reading_list/reading_list_model_impl.cc |
| +++ b/ios/chrome/browser/reading_list/reading_list_model_impl.cc |
| @@ -13,13 +13,23 @@ |
| #include "ios/chrome/browser/reading_list/reading_list_pref_names.h" |
| #include "url/gurl.h" |
| +ReadingListModelImpl::Cache::Cache() |
| + : read_entries(std::vector<GURL>()), |
| + unread_entries(std::vector<GURL>()), |
| + dirty(false) {} |
| + |
| +ReadingListModelImpl::Cache::~Cache() {} |
| + |
| ReadingListModelImpl::ReadingListModelImpl() |
| : ReadingListModelImpl(nullptr, nullptr) {} |
| ReadingListModelImpl::ReadingListModelImpl( |
| std::unique_ptr<ReadingListModelStorage> storage, |
| PrefService* pref_service) |
| - : pref_service_(pref_service), |
| + : unread_entry_count_(0), |
| + read_entry_count_(0), |
|
gambard
2016/11/23 12:11:07
I would have only kept the unread entry count. As
Olivier
2016/11/28 14:54:14
I will keep them at the moment, but I may remove o
|
| + cache_(base::MakeUnique<struct Cache>()), |
| + pref_service_(pref_service), |
| has_unseen_(false), |
| loaded_(false), |
| weak_ptr_factory_(this) { |
| @@ -29,8 +39,8 @@ ReadingListModelImpl::ReadingListModelImpl( |
| storage_layer_->SetReadingListModel(this, this); |
| } else { |
| loaded_ = true; |
| - read_ = base::MakeUnique<ReadingListEntries>(); |
| - unread_ = base::MakeUnique<ReadingListEntries>(); |
| + entries_ = base::MakeUnique<ReadingListEntries>(); |
| + cache_->dirty = true; |
| } |
| has_unseen_ = GetPersistentHasUnseen(); |
| } |
| @@ -38,13 +48,19 @@ ReadingListModelImpl::ReadingListModelImpl( |
| ReadingListModelImpl::~ReadingListModelImpl() {} |
| void ReadingListModelImpl::StoreLoaded( |
| - std::unique_ptr<ReadingListEntries> unread, |
| - std::unique_ptr<ReadingListEntries> read) { |
| + std::unique_ptr<ReadingListEntries> entries) { |
| DCHECK(CalledOnValidThread()); |
| - read_ = std::move(read); |
| - unread_ = std::move(unread); |
| + entries_ = std::move(entries); |
| + cache_->dirty = true; |
| + for (auto& iterator : *entries_) { |
| + if (iterator.second.IsRead()) { |
| + read_entry_count_++; |
| + } else { |
| + unread_entry_count_++; |
| + } |
| + } |
| + DCHECK(read_entry_count_ + unread_entry_count_ == entries_->size()); |
| loaded_ = true; |
| - SortEntries(); |
| for (auto& observer : observers_) |
| observer.ReadingListModelLoaded(this); |
| } |
| @@ -61,18 +77,28 @@ bool ReadingListModelImpl::loaded() const { |
| return loaded_; |
| } |
| +size_t ReadingListModelImpl::size() const { |
| + DCHECK(CalledOnValidThread()); |
| + DCHECK(read_entry_count_ + unread_entry_count_ == entries_->size()); |
| + if (!loaded()) |
| + return 0; |
| + return unread_entry_count_ + read_entry_count_; |
|
gambard
2016/11/23 12:11:07
entries_->size() is probably shorter and you are u
Olivier
2016/11/28 14:54:14
Done.
|
| +} |
| + |
| size_t ReadingListModelImpl::unread_size() const { |
| DCHECK(CalledOnValidThread()); |
| + DCHECK(read_entry_count_ + unread_entry_count_ == entries_->size()); |
| if (!loaded()) |
| return 0; |
| - return unread_->size(); |
| + return unread_entry_count_; |
| } |
| size_t ReadingListModelImpl::read_size() const { |
| DCHECK(CalledOnValidThread()); |
| + DCHECK(read_entry_count_ + unread_entry_count_ == entries_->size()); |
| if (!loaded()) |
| return 0; |
| - return read_->size(); |
| + return read_entry_count_; |
| } |
| bool ReadingListModelImpl::HasUnseenEntries() const { |
| @@ -90,47 +116,80 @@ void ReadingListModelImpl::ResetUnseenEntries() { |
| SetPersistentHasUnseen(false); |
| } |
| -const ReadingListEntry& ReadingListModelImpl::GetUnreadEntryAtIndex( |
| - size_t index) const { |
| +const ReadingListEntry* ReadingListModelImpl::GetEntryFromURL( |
|
gambard
2016/11/23 12:11:06
I am not sure this method makes sense.
The order "
Olivier
2016/11/28 14:54:14
I guess this applies to GetEntryAt. Removed.
|
| + const GURL& gurl) const { |
| DCHECK(CalledOnValidThread()); |
| DCHECK(loaded()); |
| - return unread_->at(index); |
| + return GetMutableEntryFromURL(gurl); |
| } |
| -const ReadingListEntry& ReadingListModelImpl::GetReadEntryAtIndex( |
| +const ReadingListEntry* ReadingListModelImpl::GetEntryAt(size_t index) const { |
|
gambard
2016/11/23 12:11:07
Remove this method. You only need the old method f
Olivier
2016/11/28 14:54:14
Done.
|
| + DCHECK(CalledOnValidThread()); |
| + DCHECK(loaded()); |
| + if (index >= read_entry_count_ + unread_entry_count_) { |
| + return nullptr; |
| + } |
| + if (cache_->dirty) { |
| + RebuildIndex(); |
| + } |
| + if (index < unread_entry_count_) { |
| + return GetEntryFromURL(cache_->unread_entries[index]); |
| + } else { |
| + return GetEntryFromURL(cache_->read_entries[index - unread_entry_count_]); |
| + } |
| +} |
| + |
| +const ReadingListEntry* ReadingListModelImpl::GetReadEntryAt( |
|
gambard
2016/11/23 12:11:06
Same as above.
Olivier
2016/11/28 14:54:14
Done.
|
| size_t index) const { |
| DCHECK(CalledOnValidThread()); |
| DCHECK(loaded()); |
| - return read_->at(index); |
| + return GetEntryAt(unread_entry_count_ + index); |
| } |
| -const ReadingListEntry* ReadingListModelImpl::GetEntryFromURL( |
| - const GURL& gurl, |
| - bool* read) const { |
| +const ReadingListEntry* ReadingListModelImpl::GetUnreadEntryAt( |
| + size_t index) const { |
| DCHECK(CalledOnValidThread()); |
| DCHECK(loaded()); |
| - return GetMutableEntryFromURL(gurl, read); |
| + return GetEntryAt(index); |
| } |
| -ReadingListEntry* ReadingListModelImpl::GetMutableEntryFromURL( |
| - const GURL& gurl, |
| - bool* read) const { |
| +void ReadingListModelImpl::RebuildIndex() const { |
| DCHECK(CalledOnValidThread()); |
| DCHECK(loaded()); |
| - bool is_read; |
| - 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()) |
| - return nullptr; |
| + if (!cache_->dirty) { |
| + return; |
| } |
| - if (read) { |
| - *read = is_read; |
| + cache_->dirty = false; |
| + cache_->read_entries.clear(); |
| + cache_->unread_entries.clear(); |
| + for (auto& iterator : *entries_) { |
| + if (iterator.second.IsRead()) { |
| + cache_->read_entries.push_back(iterator.first); |
| + } else { |
| + cache_->unread_entries.push_back(iterator.first); |
| + } |
| } |
| - return &(*it); |
| + DCHECK(read_entry_count_ == cache_->read_entries.size()); |
| + DCHECK(unread_entry_count_ == cache_->unread_entries.size()); |
| + std::sort(cache_->read_entries.begin(), cache_->read_entries.end(), |
| + [this](const GURL& left_url, const GURL& right_url) { |
| + return this->entries_->at(left_url).UpdateTime() > |
| + this->entries_->at(right_url).UpdateTime(); |
| + }); |
| + std::sort(cache_->unread_entries.begin(), cache_->unread_entries.end(), |
| + [this](const GURL& left_url, const GURL& right_url) { |
| + return this->entries_->at(left_url).UpdateTime() > |
| + this->entries_->at(right_url).UpdateTime(); |
| + }); |
| +} |
| + |
| +ReadingListEntry* ReadingListModelImpl::GetMutableEntryFromURL( |
| + const GURL& gurl) const { |
| + DCHECK(CalledOnValidThread()); |
| + DCHECK(loaded()); |
| + if (!entries_->count(gurl)) |
|
jif-google
2016/11/22 18:21:45
instead of calling |->count| and |->at|, just do a
Olivier
2016/11/28 14:54:14
Done.
|
| + return nullptr; |
| + return &entries_->at(gurl); |
| } |
| bool ReadingListModelImpl::CallbackEntryURL( |
| @@ -138,7 +197,7 @@ bool ReadingListModelImpl::CallbackEntryURL( |
| base::Callback<void(const ReadingListEntry&)> callback) const { |
| DCHECK(CalledOnValidThread()); |
| DCHECK(loaded()); |
| - const ReadingListEntry* entry = GetMutableEntryFromURL(url, nullptr); |
| + const ReadingListEntry* entry = GetMutableEntryFromURL(url); |
| if (entry) { |
| callback.Run(*entry); |
| return true; |
| @@ -146,72 +205,59 @@ bool ReadingListModelImpl::CallbackEntryURL( |
| 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) { |
| +void ReadingListModelImpl::SyncAddEntry( |
| + std::unique_ptr<ReadingListEntry> entry) { |
| 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)); |
| + DCHECK(GetMutableEntryFromURL(entry->URL()) == nullptr); |
| + for (auto& observer : observers_) |
| + observer.ReadingListWillAddEntry(this, *entry); |
| + if (entry->IsRead()) { |
| + read_entry_count_++; |
| } else { |
| - for (auto& observer : observers_) |
| - observer.ReadingListWillAddUnreadEntry(this, *entry); |
| - has_unseen_ = true; |
| + unread_entry_count_++; |
| SetPersistentHasUnseen(true); |
| - unread_->insert(unread_->begin(), std::move(*entry)); |
| } |
| + entries_->insert(std::make_pair(entry->URL(), std::move(*entry))); |
|
jif-google
2016/11/22 18:21:45
this is pretty much:
some_func(a, std::move(a));
y
Olivier
2016/11/28 14:54:14
Done.
You cannot use operator[] as there is no def
|
| + cache_->dirty = true; |
| for (auto& observer : observers_) |
| observer.ReadingListDidApplyChanges(this); |
| } |
| ReadingListEntry* ReadingListModelImpl::SyncMergeEntry( |
| - std::unique_ptr<ReadingListEntry> entry, |
| - bool read) { |
| + std::unique_ptr<ReadingListEntry> entry) { |
| DCHECK(CalledOnValidThread()); |
| DCHECK(loaded()); |
| - bool is_existing_entry_read; |
| - ReadingListEntry* existing_entry = |
| - GetMutableEntryFromURL(entry->URL(), &is_existing_entry_read); |
| - |
| + ReadingListEntry* existing_entry = GetMutableEntryFromURL(entry->URL()); |
| DCHECK(existing_entry); |
| DCHECK(existing_entry->UpdateTime() < entry->UpdateTime()); |
| - // Merge local data in new entry. |
| - entry->MergeLocalStateFrom(*existing_entry); |
| + GURL url = entry->URL(); |
|
jif-google
2016/11/22 18:21:45
const GURL & url = ...
Olivier
2016/11/28 14:54:14
I need the url adter the std::move, so I make a co
|
| + |
| + for (auto& observer : observers_) |
| + observer.ReadingListWillMoveEntry(this, url); |
| - if (is_existing_entry_read) { |
| - MoveEntryFrom(read_.get(), *existing_entry, true); |
| + if (existing_entry->IsRead()) { |
| + read_entry_count_--; |
| } else { |
| - MoveEntryFrom(unread_.get(), *existing_entry, false); |
| + unread_entry_count_--; |
| } |
| + // Merge local data in new entry. |
| + entry->MergeLocalStateFrom(*existing_entry); |
| - ReadingListEntries::iterator added_iterator; |
| - if (read) { |
| - read_->push_back(std::move(*entry)); |
| - added_iterator = read_->end() - 1; |
| + entries_->find(url)->second = std::move(*entry); |
| + cache_->dirty = true; |
| + |
| + existing_entry = GetMutableEntryFromURL(entry->URL()); |
| + if (existing_entry->IsRead()) { |
| + read_entry_count_++; |
| } else { |
| - unread_->push_back(std::move(*entry)); |
| - added_iterator = unread_->end() - 1; |
| + unread_entry_count_++; |
| } |
| for (auto& observer : observers_) |
| observer.ReadingListDidApplyChanges(this); |
| - |
| - ReadingListEntry& merged_entry = *added_iterator; |
| - return &merged_entry; |
| + return existing_entry; |
| } |
| void ReadingListModelImpl::SyncRemoveEntry(const GURL& url) { |
| @@ -226,38 +272,24 @@ 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_) |
| - 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); |
| + if (!entries_->count(url)) |
| 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); |
| - for (auto& observer : observers_) |
| - observer.ReadingListDidApplyChanges(this); |
| - return; |
| + for (auto& observer : observers_) |
| + observer.ReadingListWillRemoveEntry(this, url); |
| + const ReadingListEntry* entry = GetEntryFromURL(url); |
| + if (storage_layer_ && !from_sync) { |
| + storage_layer_->RemoveEntry(entry); |
| + } |
| + if (entry->IsRead()) { |
| + read_entry_count_--; |
| + } else { |
| + unread_entry_count_--; |
| } |
| + entries_->erase(url); |
| + cache_->dirty = true; |
| + for (auto& observer : observers_) |
| + observer.ReadingListDidApplyChanges(this); |
| } |
| const ReadingListEntry& ReadingListModelImpl::AddEntry( |
| @@ -272,103 +304,74 @@ const ReadingListEntry& ReadingListModelImpl::AddEntry( |
| ReadingListEntry entry(url, trimmedTitle); |
| for (auto& observer : observers_) |
| - observer.ReadingListWillAddUnreadEntry(this, entry); |
| + observer.ReadingListWillAddEntry(this, entry); |
| has_unseen_ = true; |
| SetPersistentHasUnseen(true); |
| + entries_->insert(std::make_pair(url, std::move(entry))); |
| + unread_entry_count_++; |
| if (storage_layer_) { |
| - storage_layer_->SaveEntry(entry, false); |
| + storage_layer_->SaveEntry(GetEntryFromURL(url)); |
| } |
| - unread_->insert(unread_->begin(), std::move(entry)); |
| + cache_->dirty = true; |
| for (auto& observer : observers_) |
| observer.ReadingListDidApplyChanges(this); |
| - return *unread_->begin(); |
| + return entries_->at(url); |
| } |
| -void ReadingListModelImpl::MarkUnreadByURL(const GURL& url) { |
| +void ReadingListModelImpl::SetReadStatus(const GURL& url, bool read) { |
| DCHECK(CalledOnValidThread()); |
| DCHECK(loaded()); |
| - ReadingListEntry entry(url, std::string()); |
| - auto result = std::find(read_->begin(), read_->end(), entry); |
| - if (result == read_->end()) |
| + if (!entries_->count(url)) { |
| return; |
| - |
| - for (ReadingListModelObserver& observer : observers_) { |
| - observer.ReadingListWillMoveEntry( |
| - this, std::distance(read_->begin(), result), true); |
| } |
| - |
| - result->MarkEntryUpdated(); |
| - if (storage_layer_) { |
| - storage_layer_->SaveEntry(*result, false); |
| + ReadingListEntry& entry = entries_->at(url); |
| + if (entry.IsRead() == read) { |
| + return; |
| } |
| - |
| - unread_->insert(unread_->begin(), std::move(*result)); |
| - read_->erase(result); |
| - |
| for (ReadingListModelObserver& observer : observers_) { |
| - observer.ReadingListDidApplyChanges(this); |
| + observer.ReadingListWillMoveEntry(this, url); |
| } |
| -} |
| - |
| -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 (read) { |
| + read_entry_count_++; |
| + unread_entry_count_--; |
| + } else { |
| + unread_entry_count_++; |
| + read_entry_count_--; |
| + } |
| + entry.SetRead(read); |
| + entry.MarkEntryUpdated(); |
| + cache_->dirty = true; |
| if (storage_layer_) { |
| - storage_layer_->SaveEntry(*result, true); |
| + storage_layer_->SaveEntry(GetEntryFromURL(url)); |
| } |
| - |
| - read_->insert(read_->begin(), std::move(*result)); |
| - unread_->erase(result); |
| - |
| - for (auto& observer : observers_) |
| + for (ReadingListModelObserver& 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_) |
| - observer.ReadingListWillUpdateUnreadEntry( |
| - this, std::distance(unread_->begin(), result)); |
| - result->SetTitle(title); |
| - |
| - if (storage_layer_) { |
| - storage_layer_->SaveEntry(*result, false); |
| - } |
| - for (auto& observer : observers_) |
| - observer.ReadingListDidApplyChanges(this); |
| + if (!entries_->count(url)) { |
| 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)); |
| - result->SetTitle(title); |
| - if (storage_layer_) { |
| - storage_layer_->SaveEntry(*result, true); |
| - } |
| - for (auto& observer : observers_) |
| - observer.ReadingListDidApplyChanges(this); |
| + ReadingListEntry& entry = entries_->at(url); |
| + if (entry.Title() == title) { |
| return; |
| } |
| + |
| + for (ReadingListModelObserver& observer : observers_) { |
| + observer.ReadingListWillUpdateEntry(this, url); |
| + } |
| + entry.SetTitle(title); |
| + if (storage_layer_) { |
| + storage_layer_->SaveEntry(GetEntryFromURL(url)); |
| + } |
| + for (ReadingListModelObserver& observer : observers_) { |
| + observer.ReadingListDidApplyChanges(this); |
| + } |
| } |
| void ReadingListModelImpl::SetEntryDistilledPath( |
| @@ -376,35 +379,25 @@ void ReadingListModelImpl::SetEntryDistilledPath( |
| 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_) |
| - observer.ReadingListWillUpdateUnreadEntry( |
| - 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); |
| + if (!entries_->count(url)) { |
| 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)); |
| - result->SetDistilledPath(distilled_path); |
| - if (storage_layer_) { |
| - storage_layer_->SaveEntry(*result, true); |
| - } |
| - for (auto& observer : observers_) |
| - observer.ReadingListDidApplyChanges(this); |
| + ReadingListEntry& entry = entries_->at(url); |
| + if (entry.DistilledState() == ReadingListEntry::PROCESSED && |
| + entry.DistilledPath() == distilled_path) { |
| return; |
| } |
| + |
| + for (ReadingListModelObserver& observer : observers_) { |
| + observer.ReadingListWillUpdateEntry(this, url); |
| + } |
| + entry.SetDistilledPath(distilled_path); |
| + if (storage_layer_) { |
| + storage_layer_->SaveEntry(GetEntryFromURL(url)); |
| + } |
| + for (ReadingListModelObserver& observer : observers_) { |
| + observer.ReadingListDidApplyChanges(this); |
| + } |
| } |
| void ReadingListModelImpl::SetEntryDistilledState( |
| @@ -412,36 +405,25 @@ void ReadingListModelImpl::SetEntryDistilledState( |
| 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_) |
| - observer.ReadingListWillUpdateUnreadEntry( |
| - this, std::distance(unread_->begin(), result)); |
| - result->SetDistilledState(state); |
| - if (storage_layer_) { |
| - storage_layer_->SaveEntry(*result, false); |
| - } |
| - for (auto& observer : observers_) |
| - observer.ReadingListDidApplyChanges(this); |
| + if (!entries_->count(url)) { |
| 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)); |
| - result->SetDistilledState(state); |
| - if (storage_layer_) { |
| - storage_layer_->SaveEntry(*result, true); |
| - } |
| - for (auto& observer : observers_) |
| - observer.ReadingListDidApplyChanges(this); |
| + ReadingListEntry& entry = entries_->at(url); |
| + if (entry.DistilledState() == state) { |
| return; |
| } |
| -}; |
| + |
| + for (ReadingListModelObserver& observer : observers_) { |
| + observer.ReadingListWillUpdateEntry(this, url); |
| + } |
| + entry.SetDistilledState(state); |
| + if (storage_layer_) { |
| + storage_layer_->SaveEntry(GetEntryFromURL(url)); |
| + } |
| + for (ReadingListModelObserver& observer : observers_) { |
| + observer.ReadingListDidApplyChanges(this); |
| + } |
| +} |
| std::unique_ptr<ReadingListModel::ScopedReadingListBatchUpdate> |
| ReadingListModelImpl::CreateBatchToken() { |
| @@ -467,7 +449,6 @@ void ReadingListModelImpl::LeavingBatchUpdates() { |
| DCHECK(CalledOnValidThread()); |
| if (storage_layer_) { |
| SetPersistentHasUnseen(has_unseen_); |
| - SortEntries(); |
| } |
| ReadingListModel::LeavingBatchUpdates(); |
| } |
| @@ -502,15 +483,6 @@ syncer::ModelTypeSyncBridge* ReadingListModelImpl::GetModelTypeSyncBridge() { |
| 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(); |
| } |