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 "ios/chrome/browser/reading_list/reading_list_model_impl.h" | 5 #include "ios/chrome/browser/reading_list/reading_list_model_impl.h" |
6 | 6 |
7 #include "ios/chrome/browser/reading_list/reading_list_model_storage.h" | 7 #include "ios/chrome/browser/reading_list/reading_list_model_storage.h" |
8 #include "url/gurl.h" | 8 #include "url/gurl.h" |
9 | 9 |
10 ReadingListModelImpl::ReadingListModelImpl() : ReadingListModelImpl(NULL) {} | 10 ReadingListModelImpl::ReadingListModelImpl() : ReadingListModelImpl(NULL) {} |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
47 return unread_size() && hasUnseen_; | 47 return unread_size() && hasUnseen_; |
48 } | 48 } |
49 | 49 |
50 void ReadingListModelImpl::ResetUnseenEntries() { | 50 void ReadingListModelImpl::ResetUnseenEntries() { |
51 DCHECK(loaded()); | 51 DCHECK(loaded()); |
52 hasUnseen_ = false; | 52 hasUnseen_ = false; |
53 if (storageLayer_ && !IsPerformingBatchUpdates()) | 53 if (storageLayer_ && !IsPerformingBatchUpdates()) |
54 storageLayer_->SavePersistentHasUnseen(false); | 54 storageLayer_->SavePersistentHasUnseen(false); |
55 } | 55 } |
56 | 56 |
57 // Returns a specific entry. | 57 const ReadingListEntry* ReadingListModelImpl::GetUnreadEntryAtIndex( |
58 const ReadingListEntry& ReadingListModelImpl::GetUnreadEntryAtIndex( | |
59 size_t index) const { | 58 size_t index) const { |
60 DCHECK(loaded()); | 59 DCHECK(loaded()); |
61 return unread_[index]; | 60 if (index >= unread_.size()) |
61 return nullptr; | |
62 return &unread_[index]; | |
62 } | 63 } |
63 | 64 |
64 const ReadingListEntry& ReadingListModelImpl::GetReadEntryAtIndex( | 65 const ReadingListEntry* ReadingListModelImpl::GetReadEntryAtIndex( |
65 size_t index) const { | 66 size_t index) const { |
66 DCHECK(loaded()); | 67 DCHECK(loaded()); |
67 return read_[index]; | 68 if (index >= read_.size()) |
69 return nullptr; | |
70 return &read_[index]; | |
71 } | |
72 | |
73 const ReadingListEntry* ReadingListModelImpl::GetEntryFromURL( | |
74 const GURL& gurl) const { | |
75 auto it = std::find_if( | |
gambard
2016/10/03 09:04:45
Use std::find with a entry created for the find.
| |
76 read_.begin(), read_.end(), | |
77 [&gurl](const ReadingListEntry& entry) { return gurl == entry.URL(); }); | |
78 if (it == read_.end()) { | |
79 it = std::find_if( | |
80 unread_.begin(), unread_.end(), | |
81 [&gurl](const ReadingListEntry& entry) { return gurl == entry.URL(); }); | |
82 if (it == unread_.end()) | |
83 return nullptr; | |
84 } | |
85 return &(*it); | |
68 } | 86 } |
69 | 87 |
70 bool ReadingListModelImpl::CallbackEntryURL( | 88 bool ReadingListModelImpl::CallbackEntryURL( |
71 const GURL& url, | 89 const GURL& url, |
72 base::Callback<void(const ReadingListEntry&)> callback) const { | 90 base::Callback<void(const ReadingListEntry&)> callback) const { |
73 DCHECK(loaded()); | 91 DCHECK(loaded()); |
74 ReadingListEntry entry(url, std::string()); | 92 ReadingListEntry entry(url, std::string()); |
75 auto resultUnread = std::find(unread_.begin(), unread_.end(), entry); | 93 auto resultUnread = std::find(unread_.begin(), unread_.end(), entry); |
76 if (resultUnread != unread_.end()) { | 94 if (resultUnread != unread_.end()) { |
77 callback.Run(*resultUnread); | 95 callback.Run(*resultUnread); |
(...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
257 | 275 |
258 void ReadingListModelImpl::EndBatchUpdates() { | 276 void ReadingListModelImpl::EndBatchUpdates() { |
259 ReadingListModel::EndBatchUpdates(); | 277 ReadingListModel::EndBatchUpdates(); |
260 if (IsPerformingBatchUpdates() || !storageLayer_) { | 278 if (IsPerformingBatchUpdates() || !storageLayer_) { |
261 return; | 279 return; |
262 } | 280 } |
263 storageLayer_->SavePersistentUnreadList(unread_); | 281 storageLayer_->SavePersistentUnreadList(unread_); |
264 storageLayer_->SavePersistentReadList(read_); | 282 storageLayer_->SavePersistentReadList(read_); |
265 storageLayer_->SavePersistentHasUnseen(hasUnseen_); | 283 storageLayer_->SavePersistentHasUnseen(hasUnseen_); |
266 } | 284 } |
OLD | NEW |