Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(328)

Side by Side Diff: ios/chrome/browser/reading_list/reading_list_model_impl.cc

Issue 2436743002: Add GetEntryFromURL method to the ReadingListModel. (Closed)
Patch Set: 2 Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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.
58 const ReadingListEntry& ReadingListModelImpl::GetUnreadEntryAtIndex( 57 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 return unread_[index];
62 } 61 }
63 62
64 const ReadingListEntry& ReadingListModelImpl::GetReadEntryAtIndex( 63 const ReadingListEntry& ReadingListModelImpl::GetReadEntryAtIndex(
65 size_t index) const { 64 size_t index) const {
66 DCHECK(loaded()); 65 DCHECK(loaded());
67 return read_[index]; 66 return read_[index];
68 } 67 }
69 68
69 const ReadingListEntry* ReadingListModelImpl::GetEntryFromURL(
70 const GURL& gurl) const {
71 auto it = std::find_if(
Olivier 2016/10/25 13:10:09 DCHECK(loaded())
jif 2016/10/25 13:18:45 Done.
72 read_.begin(), read_.end(),
73 [&gurl](const ReadingListEntry& entry) { return gurl == entry.URL(); });
Olivier 2016/10/25 13:10:09 This should not be needed as there is already a te
gambard 2016/10/25 13:18:34 I don't mind lambda but we already have a == opera
jif 2016/10/25 13:18:44 CallbackEntryURL requires creating a fake ReadingL
Olivier 2016/10/25 13:21:59 I don't think creating one ReadingListEntry is mor
jif-google 2016/10/25 13:51:28 The lambda would be compiled as a struct containin
Olivier 2016/10/25 15:21:09 IMO, find_if should be used if you want to find an
74 if (it == read_.end()) {
75 it = std::find_if(
76 unread_.begin(), unread_.end(),
77 [&gurl](const ReadingListEntry& entry) { return gurl == entry.URL(); });
78 if (it == unread_.end())
79 return nullptr;
80 }
81 return &(*it);
82 }
83
70 bool ReadingListModelImpl::CallbackEntryURL( 84 bool ReadingListModelImpl::CallbackEntryURL(
71 const GURL& url, 85 const GURL& url,
72 base::Callback<void(const ReadingListEntry&)> callback) const { 86 base::Callback<void(const ReadingListEntry&)> callback) const {
73 DCHECK(loaded()); 87 DCHECK(loaded());
74 ReadingListEntry entry(url, std::string()); 88 const ReadingListEntry* entry = GetEntryFromURL(url);
75 auto resultUnread = std::find(unread_.begin(), unread_.end(), entry); 89 if (entry) {
76 if (resultUnread != unread_.end()) { 90 callback.Run(*entry);
77 callback.Run(*resultUnread);
78 return true;
79 }
80
81 auto resultRead = std::find(read_.begin(), read_.end(), entry);
82 if (resultRead != read_.end()) {
83 callback.Run(*resultRead);
84 return true; 91 return true;
85 } 92 }
86 return false; 93 return false;
87 } 94 }
88 95
89 void ReadingListModelImpl::RemoveEntryByUrl(const GURL& url) { 96 void ReadingListModelImpl::RemoveEntryByUrl(const GURL& url) {
90 DCHECK(loaded()); 97 DCHECK(loaded());
91 const ReadingListEntry entry(url, std::string()); 98 const ReadingListEntry entry(url, std::string());
92 99
93 auto result = std::find(unread_.begin(), unread_.end(), entry); 100 auto result = std::find(unread_.begin(), unread_.end(), entry);
(...skipping 196 matching lines...) Expand 10 before | Expand all | Expand 10 after
290 297
291 void ReadingListModelImpl::EndBatchUpdates() { 298 void ReadingListModelImpl::EndBatchUpdates() {
292 ReadingListModel::EndBatchUpdates(); 299 ReadingListModel::EndBatchUpdates();
293 if (IsPerformingBatchUpdates() || !storageLayer_) { 300 if (IsPerformingBatchUpdates() || !storageLayer_) {
294 return; 301 return;
295 } 302 }
296 storageLayer_->SavePersistentUnreadList(unread_); 303 storageLayer_->SavePersistentUnreadList(unread_);
297 storageLayer_->SavePersistentReadList(read_); 304 storageLayer_->SavePersistentReadList(read_);
298 storageLayer_->SavePersistentHasUnseen(hasUnseen_); 305 storageLayer_->SavePersistentHasUnseen(hasUnseen_);
299 } 306 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698