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

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

Issue 2369303002: Reading List create protobuf store (Closed)
Patch Set: threads Created 4 years, 2 months 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 "base/bind.h"
8 #include "base/memory/ptr_util.h"
9 #include "components/prefs/pref_service.h"
7 #include "ios/chrome/browser/reading_list/reading_list_model_storage.h" 10 #include "ios/chrome/browser/reading_list/reading_list_model_storage.h"
11 #include "ios/chrome/browser/reading_list/reading_list_pref_names.h"
8 #include "url/gurl.h" 12 #include "url/gurl.h"
9 13
10 ReadingListModelImpl::ReadingListModelImpl() : ReadingListModelImpl(NULL) {} 14 ReadingListModelImpl::ReadingListModelImpl()
15 : ReadingListModelImpl(NULL, NULL) {}
11 16
12 ReadingListModelImpl::ReadingListModelImpl( 17 ReadingListModelImpl::ReadingListModelImpl(
13 std::unique_ptr<ReadingListModelStorage> storage) 18 std::unique_ptr<ReadingListModelStorage> storage,
14 : hasUnseen_(false) { 19 PrefService* pref_service)
20 : pref_service_(pref_service),
21 has_unseen_(false),
22 loaded_(false),
23 weak_ptr_factory_(this) {
15 if (storage) { 24 if (storage) {
16 storageLayer_ = std::move(storage); 25 storage_layer_ = std::move(storage);
17 read_ = storageLayer_->LoadPersistentReadList(); 26 storage_layer_->SetReadingListModel(this);
18 unread_ = storageLayer_->LoadPersistentUnreadList(); 27 storage_layer_->LoadPersistentLists();
19 hasUnseen_ = storageLayer_->LoadPersistentHasUnseen(); 28 } else {
29 loaded_ = true;
30 read_ = base::MakeUnique<ReadingListEntries>();
31 unread_ = base::MakeUnique<ReadingListEntries>();
20 } 32 }
21 loaded_ = true; 33 has_unseen_ = LoadPersistentHasUnseen();
22 } 34 }
23 ReadingListModelImpl::~ReadingListModelImpl() {} 35 ReadingListModelImpl::~ReadingListModelImpl() {}
24 36
37 void ReadingListModelImpl::ModelLoaded(
38 std::unique_ptr<ReadingListEntries> unread,
39 std::unique_ptr<ReadingListEntries> read) {
40 read_ = std::move(read);
41 unread_ = std::move(unread);
42 loaded_ = true;
43 FOR_EACH_OBSERVER(ReadingListModelObserver, observers_,
44 ReadingListModelLoaded(this));
45 }
46
25 void ReadingListModelImpl::Shutdown() { 47 void ReadingListModelImpl::Shutdown() {
26 FOR_EACH_OBSERVER(ReadingListModelObserver, observers_, 48 FOR_EACH_OBSERVER(ReadingListModelObserver, observers_,
27 ReadingListModelBeingDeleted(this)); 49 ReadingListModelBeingDeleted(this));
28 loaded_ = false; 50 loaded_ = false;
29 } 51 }
30 52
31 bool ReadingListModelImpl::loaded() const { 53 bool ReadingListModelImpl::loaded() const {
32 return loaded_; 54 return loaded_;
33 } 55 }
34 56
35 size_t ReadingListModelImpl::unread_size() const { 57 size_t ReadingListModelImpl::unread_size() const {
36 DCHECK(loaded()); 58 if (!loaded())
37 return unread_.size(); 59 return 0;
60 return unread_->size();
38 } 61 }
39 62
40 size_t ReadingListModelImpl::read_size() const { 63 size_t ReadingListModelImpl::read_size() const {
41 DCHECK(loaded()); 64 if (!loaded())
42 return read_.size(); 65 return 0;
66 return read_->size();
43 } 67 }
44 68
45 bool ReadingListModelImpl::HasUnseenEntries() const { 69 bool ReadingListModelImpl::HasUnseenEntries() const {
46 DCHECK(loaded()); 70 if (!loaded())
47 return unread_size() && hasUnseen_; 71 return false;
72 return unread_size() && has_unseen_;
48 } 73 }
49 74
50 void ReadingListModelImpl::ResetUnseenEntries() { 75 void ReadingListModelImpl::ResetUnseenEntries() {
51 DCHECK(loaded()); 76 DCHECK(loaded());
52 hasUnseen_ = false; 77 has_unseen_ = false;
53 if (storageLayer_ && !IsPerformingBatchUpdates()) 78 if (!IsPerformingBatchUpdates())
54 storageLayer_->SavePersistentHasUnseen(false); 79 SavePersistentHasUnseen(false);
55 } 80 }
56 81
57 // Returns a specific entry. 82 // Returns a specific entry.
58 const ReadingListEntry& ReadingListModelImpl::GetUnreadEntryAtIndex( 83 const ReadingListEntry& ReadingListModelImpl::GetUnreadEntryAtIndex(
59 size_t index) const { 84 size_t index) const {
60 DCHECK(loaded()); 85 DCHECK(loaded());
61 return unread_[index]; 86 return unread_->at(index);
62 } 87 }
63 88
64 const ReadingListEntry& ReadingListModelImpl::GetReadEntryAtIndex( 89 const ReadingListEntry& ReadingListModelImpl::GetReadEntryAtIndex(
65 size_t index) const { 90 size_t index) const {
66 DCHECK(loaded()); 91 DCHECK(loaded());
67 return read_[index]; 92 return read_->at(index);
68 } 93 }
69 94
70 bool ReadingListModelImpl::CallbackEntryURL( 95 bool ReadingListModelImpl::CallbackEntryURL(
71 const GURL& url, 96 const GURL& url,
72 base::Callback<void(const ReadingListEntry&)> callback) const { 97 base::Callback<void(const ReadingListEntry&)> callback) const {
73 DCHECK(loaded()); 98 DCHECK(loaded());
74 ReadingListEntry entry(url, std::string()); 99 ReadingListEntry entry(url, std::string());
75 auto resultUnread = std::find(unread_.begin(), unread_.end(), entry); 100 auto resultUnread = std::find(unread_->begin(), unread_->end(), entry);
76 if (resultUnread != unread_.end()) { 101 if (resultUnread != unread_->end()) {
77 callback.Run(*resultUnread); 102 callback.Run(*resultUnread);
78 return true; 103 return true;
79 } 104 }
80 105
81 auto resultRead = std::find(read_.begin(), read_.end(), entry); 106 auto resultRead = std::find(read_->begin(), read_->end(), entry);
82 if (resultRead != read_.end()) { 107 if (resultRead != read_->end()) {
83 callback.Run(*resultRead); 108 callback.Run(*resultRead);
84 return true; 109 return true;
85 } 110 }
86 return false; 111 return false;
87 } 112 }
88 113
89 void ReadingListModelImpl::RemoveEntryByUrl(const GURL& url) { 114 void ReadingListModelImpl::RemoveEntryByUrl(const GURL& url) {
90 DCHECK(loaded()); 115 DCHECK(loaded());
91 const ReadingListEntry entry(url, std::string()); 116 const ReadingListEntry entry(url, std::string());
92 117
93 auto result = std::find(unread_.begin(), unread_.end(), entry); 118 auto result = std::find(unread_->begin(), unread_->end(), entry);
94 if (result != unread_.end()) { 119 if (result != unread_->end()) {
95 FOR_EACH_OBSERVER(ReadingListModelObserver, observers_, 120 FOR_EACH_OBSERVER(ReadingListModelObserver, observers_,
96 ReadingListWillRemoveUnreadEntry( 121 ReadingListWillRemoveUnreadEntry(
97 this, std::distance(unread_.begin(), result))); 122 this, std::distance(unread_->begin(), result)));
98 unread_.erase(result); 123 unread_->erase(result);
99 if (storageLayer_ && !IsPerformingBatchUpdates()) 124
100 storageLayer_->SavePersistentUnreadList(unread_); 125 if (storage_layer_) {
126 storage_layer_->RemoveEntry(*result);
127 }
128
101 FOR_EACH_OBSERVER(ReadingListModelObserver, observers_, 129 FOR_EACH_OBSERVER(ReadingListModelObserver, observers_,
102 ReadingListDidApplyChanges(this)); 130 ReadingListDidApplyChanges(this));
103 return; 131 return;
104 } 132 }
105 133
106 result = std::find(read_.begin(), read_.end(), entry); 134 result = std::find(read_->begin(), read_->end(), entry);
107 if (result != read_.end()) { 135 if (result != read_->end()) {
108 FOR_EACH_OBSERVER(ReadingListModelObserver, observers_, 136 FOR_EACH_OBSERVER(ReadingListModelObserver, observers_,
109 ReadingListWillRemoveReadEntry( 137 ReadingListWillRemoveReadEntry(
110 this, std::distance(read_.begin(), result))); 138 this, std::distance(read_->begin(), result)));
111 read_.erase(result); 139 read_->erase(result);
112 if (storageLayer_ && !IsPerformingBatchUpdates()) 140
113 storageLayer_->SavePersistentReadList(read_); 141 if (storage_layer_) {
142 storage_layer_->RemoveEntry(*result);
143 }
144
114 FOR_EACH_OBSERVER(ReadingListModelObserver, observers_, 145 FOR_EACH_OBSERVER(ReadingListModelObserver, observers_,
115 ReadingListDidApplyChanges(this)); 146 ReadingListDidApplyChanges(this));
116 return; 147 return;
117 } 148 }
118 } 149 }
119 150
120 const ReadingListEntry& ReadingListModelImpl::AddEntry( 151 const ReadingListEntry& ReadingListModelImpl::AddEntry(
121 const GURL& url, 152 const GURL& url,
122 const std::string& title) { 153 const std::string& title) {
123 DCHECK(loaded()); 154 DCHECK(loaded());
124 RemoveEntryByUrl(url); 155 RemoveEntryByUrl(url);
125 ReadingListEntry entry(url, title); 156 ReadingListEntry entry(url, title);
126 FOR_EACH_OBSERVER(ReadingListModelObserver, observers_, 157 FOR_EACH_OBSERVER(ReadingListModelObserver, observers_,
127 ReadingListWillAddUnreadEntry(this, entry)); 158 ReadingListWillAddUnreadEntry(this, entry));
128 unread_.insert(unread_.begin(), std::move(entry)); 159 has_unseen_ = true;
129 hasUnseen_ = true; 160 SavePersistentHasUnseen(true);
130 if (storageLayer_ && !IsPerformingBatchUpdates()) { 161 if (storage_layer_) {
131 storageLayer_->SavePersistentUnreadList(unread_); 162 storage_layer_->SaveEntry(entry, false);
132 storageLayer_->SavePersistentHasUnseen(true);
133 } 163 }
164 unread_->insert(unread_->begin(), std::move(entry));
165
134 FOR_EACH_OBSERVER(ReadingListModelObserver, observers_, 166 FOR_EACH_OBSERVER(ReadingListModelObserver, observers_,
135 ReadingListDidApplyChanges(this)); 167 ReadingListDidApplyChanges(this));
136 return *unread_.begin(); 168 return *unread_->begin();
137 } 169 }
138 170
139 void ReadingListModelImpl::MarkReadByURL(const GURL& url) { 171 void ReadingListModelImpl::MarkReadByURL(const GURL& url) {
140 DCHECK(loaded()); 172 DCHECK(loaded());
141 ReadingListEntry entry(url, std::string()); 173 ReadingListEntry entry(url, std::string());
142 auto result = std::find(unread_.begin(), unread_.end(), entry); 174 auto result = std::find(unread_->begin(), unread_->end(), entry);
143 if (result == unread_.end()) 175 if (result == unread_->end())
144 return; 176 return;
145 177
178 if (storage_layer_) {
179 storage_layer_->SaveEntry(entry, true);
180 }
146 FOR_EACH_OBSERVER( 181 FOR_EACH_OBSERVER(
147 ReadingListModelObserver, observers_, 182 ReadingListModelObserver, observers_,
148 ReadingListWillMoveEntry(this, std::distance(unread_.begin(), result))); 183 ReadingListWillMoveEntry(this, std::distance(unread_->begin(), result)));
149 184
150 read_.insert(read_.begin(), std::move(*result)); 185 result->MarkEntryUpdated();
151 unread_.erase(result); 186 if (storage_layer_) {
187 storage_layer_->SaveEntry(*result, true);
188 }
152 189
153 if (storageLayer_ && !IsPerformingBatchUpdates()) { 190 unread_->erase(result);
154 storageLayer_->SavePersistentUnreadList(unread_); 191 read_->insert(read_->begin(), std::move(*result));
155 storageLayer_->SavePersistentReadList(read_); 192
156 }
157 FOR_EACH_OBSERVER(ReadingListModelObserver, observers_, 193 FOR_EACH_OBSERVER(ReadingListModelObserver, observers_,
158 ReadingListDidApplyChanges(this)); 194 ReadingListDidApplyChanges(this));
159 } 195 }
160 196
161 void ReadingListModelImpl::SetEntryTitle(const GURL& url, 197 void ReadingListModelImpl::SetEntryTitle(const GURL& url,
162 const std::string& title) { 198 const std::string& title) {
163 DCHECK(loaded()); 199 DCHECK(loaded());
164 const ReadingListEntry entry(url, std::string()); 200 const ReadingListEntry entry(url, std::string());
165 201
166 auto result = std::find(unread_.begin(), unread_.end(), entry); 202 auto result = std::find(unread_->begin(), unread_->end(), entry);
167 if (result != unread_.end()) { 203 if (result != unread_->end()) {
168 FOR_EACH_OBSERVER(ReadingListModelObserver, observers_, 204 FOR_EACH_OBSERVER(ReadingListModelObserver, observers_,
169 ReadingListWillUpdateUnreadEntry( 205 ReadingListWillUpdateUnreadEntry(
170 this, std::distance(unread_.begin(), result))); 206 this, std::distance(unread_->begin(), result)));
171 result->SetTitle(title); 207 result->SetTitle(title);
172 if (storageLayer_ && !IsPerformingBatchUpdates()) 208
173 storageLayer_->SavePersistentUnreadList(unread_); 209 if (storage_layer_) {
210 storage_layer_->SaveEntry(*result, false);
211 }
174 FOR_EACH_OBSERVER(ReadingListModelObserver, observers_, 212 FOR_EACH_OBSERVER(ReadingListModelObserver, observers_,
175 ReadingListDidApplyChanges(this)); 213 ReadingListDidApplyChanges(this));
176 return; 214 return;
177 } 215 }
178 216
179 result = std::find(read_.begin(), read_.end(), entry); 217 result = std::find(read_->begin(), read_->end(), entry);
180 if (result != read_.end()) { 218 if (result != read_->end()) {
181 FOR_EACH_OBSERVER(ReadingListModelObserver, observers_, 219 FOR_EACH_OBSERVER(ReadingListModelObserver, observers_,
182 ReadingListWillUpdateReadEntry( 220 ReadingListWillUpdateReadEntry(
183 this, std::distance(read_.begin(), result))); 221 this, std::distance(read_->begin(), result)));
184 result->SetTitle(title); 222 result->SetTitle(title);
185 if (storageLayer_ && !IsPerformingBatchUpdates()) 223 if (storage_layer_) {
186 storageLayer_->SavePersistentReadList(read_); 224 storage_layer_->SaveEntry(*result, true);
225 }
187 FOR_EACH_OBSERVER(ReadingListModelObserver, observers_, 226 FOR_EACH_OBSERVER(ReadingListModelObserver, observers_,
188 ReadingListDidApplyChanges(this)); 227 ReadingListDidApplyChanges(this));
189 return; 228 return;
190 } 229 }
191 } 230 }
192 231
193 void ReadingListModelImpl::SetEntryDistilledURL(const GURL& url, 232 void ReadingListModelImpl::SetEntryDistilledURL(const GURL& url,
194 const GURL& distilled_url) { 233 const GURL& distilled_url) {
195 DCHECK(loaded()); 234 DCHECK(loaded());
196 const ReadingListEntry entry(url, std::string()); 235 const ReadingListEntry entry(url, std::string());
197 236
198 auto result = std::find(unread_.begin(), unread_.end(), entry); 237 auto result = std::find(unread_->begin(), unread_->end(), entry);
199 if (result != unread_.end()) { 238 if (result != unread_->end()) {
200 FOR_EACH_OBSERVER(ReadingListModelObserver, observers_, 239 FOR_EACH_OBSERVER(ReadingListModelObserver, observers_,
201 ReadingListWillUpdateUnreadEntry( 240 ReadingListWillUpdateUnreadEntry(
202 this, std::distance(unread_.begin(), result))); 241 this, std::distance(unread_->begin(), result)));
203 result->SetDistilledURL(distilled_url); 242 result->SetDistilledURL(distilled_url);
204 if (storageLayer_ && !IsPerformingBatchUpdates()) 243 if (storage_layer_) {
205 storageLayer_->SavePersistentUnreadList(unread_); 244 storage_layer_->SaveEntry(*result, false);
245 }
206 FOR_EACH_OBSERVER(ReadingListModelObserver, observers_, 246 FOR_EACH_OBSERVER(ReadingListModelObserver, observers_,
207 ReadingListDidApplyChanges(this)); 247 ReadingListDidApplyChanges(this));
208 return; 248 return;
209 } 249 }
210 250
211 result = std::find(read_.begin(), read_.end(), entry); 251 result = std::find(read_->begin(), read_->end(), entry);
212 if (result != read_.end()) { 252 if (result != read_->end()) {
213 FOR_EACH_OBSERVER(ReadingListModelObserver, observers_, 253 FOR_EACH_OBSERVER(ReadingListModelObserver, observers_,
214 ReadingListWillUpdateReadEntry( 254 ReadingListWillUpdateReadEntry(
215 this, std::distance(read_.begin(), result))); 255 this, std::distance(read_->begin(), result)));
216 result->SetDistilledURL(distilled_url); 256 result->SetDistilledURL(distilled_url);
217 if (storageLayer_ && !IsPerformingBatchUpdates()) 257 if (storage_layer_) {
218 storageLayer_->SavePersistentReadList(read_); 258 storage_layer_->SaveEntry(*result, true);
259 }
219 FOR_EACH_OBSERVER(ReadingListModelObserver, observers_, 260 FOR_EACH_OBSERVER(ReadingListModelObserver, observers_,
220 ReadingListDidApplyChanges(this)); 261 ReadingListDidApplyChanges(this));
221 return; 262 return;
222 } 263 }
223 } 264 }
224 265
225 void ReadingListModelImpl::SetEntryDistilledState( 266 void ReadingListModelImpl::SetEntryDistilledState(
226 const GURL& url, 267 const GURL& url,
227 ReadingListEntry::DistillationState state) { 268 ReadingListEntry::DistillationState state) {
228 DCHECK(loaded()); 269 DCHECK(loaded());
229 const ReadingListEntry entry(url, std::string()); 270 const ReadingListEntry entry(url, std::string());
230 271
231 auto result = std::find(unread_.begin(), unread_.end(), entry); 272 auto result = std::find(unread_->begin(), unread_->end(), entry);
232 if (result != unread_.end()) { 273 if (result != unread_->end()) {
233 FOR_EACH_OBSERVER(ReadingListModelObserver, observers_, 274 FOR_EACH_OBSERVER(ReadingListModelObserver, observers_,
234 ReadingListWillUpdateUnreadEntry( 275 ReadingListWillUpdateUnreadEntry(
235 this, std::distance(unread_.begin(), result))); 276 this, std::distance(unread_->begin(), result)));
236 result->SetDistilledState(state); 277 result->SetDistilledState(state);
237 if (storageLayer_ && !IsPerformingBatchUpdates()) 278 if (storage_layer_) {
238 storageLayer_->SavePersistentUnreadList(unread_); 279 storage_layer_->SaveEntry(*result, false);
280 }
239 FOR_EACH_OBSERVER(ReadingListModelObserver, observers_, 281 FOR_EACH_OBSERVER(ReadingListModelObserver, observers_,
240 ReadingListDidApplyChanges(this)); 282 ReadingListDidApplyChanges(this));
241 return; 283 return;
242 } 284 }
243 285
244 result = std::find(read_.begin(), read_.end(), entry); 286 result = std::find(read_->begin(), read_->end(), entry);
245 if (result != read_.end()) { 287 if (result != read_->end()) {
246 FOR_EACH_OBSERVER(ReadingListModelObserver, observers_, 288 FOR_EACH_OBSERVER(ReadingListModelObserver, observers_,
247 ReadingListWillUpdateReadEntry( 289 ReadingListWillUpdateReadEntry(
248 this, std::distance(read_.begin(), result))); 290 this, std::distance(read_->begin(), result)));
249 result->SetDistilledState(state); 291 result->SetDistilledState(state);
250 if (storageLayer_ && !IsPerformingBatchUpdates()) 292 if (storage_layer_) {
251 storageLayer_->SavePersistentReadList(read_); 293 storage_layer_->SaveEntry(*result, true);
294 }
252 FOR_EACH_OBSERVER(ReadingListModelObserver, observers_, 295 FOR_EACH_OBSERVER(ReadingListModelObserver, observers_,
253 ReadingListDidApplyChanges(this)); 296 ReadingListDidApplyChanges(this));
254 return; 297 return;
255 } 298 }
256 }; 299 };
257 300
258 void ReadingListModelImpl::EndBatchUpdates() { 301 void ReadingListModelImpl::LeavingBatchUpdates() {
259 ReadingListModel::EndBatchUpdates(); 302 ReadingListModel::LeavingBatchUpdates();
260 if (IsPerformingBatchUpdates() || !storageLayer_) { 303 if (storage_layer_) {
304 SavePersistentHasUnseen(has_unseen_);
305 storage_layer_->CommitTransaction();
306 }
307 }
308
309 void ReadingListModelImpl::EnteringBatchUpdates() {
310 if (storage_layer_) {
311 storage_layer_->BeginTransaction();
312 }
313 ReadingListModel::EnteringBatchUpdates();
314 }
315
316 void ReadingListModelImpl::SavePersistentHasUnseen(bool has_unseen) {
317 if (!pref_service_) {
261 return; 318 return;
262 } 319 }
263 storageLayer_->SavePersistentUnreadList(unread_); 320 pref_service_->SetBoolean(reading_list::prefs::kReadingListHasUnseenEntries,
264 storageLayer_->SavePersistentReadList(read_); 321 has_unseen);
265 storageLayer_->SavePersistentHasUnseen(hasUnseen_);
266 } 322 }
323
324 bool ReadingListModelImpl::LoadPersistentHasUnseen() {
325 if (!pref_service_) {
326 return false;
327 }
328 return pref_service_->GetBoolean(
329 reading_list::prefs::kReadingListHasUnseenEntries);
330 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698