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

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

Issue 2451843002: Add Store+Sync to reading list. (Closed)
Patch Set: reading_list_model_unittests 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 "base/bind.h"
8 #include "base/logging.h"
9 #include "base/memory/ptr_util.h"
10 #include "components/prefs/pref_service.h"
7 #include "ios/chrome/browser/reading_list/reading_list_model_storage.h" 11 #include "ios/chrome/browser/reading_list/reading_list_model_storage.h"
12 #include "ios/chrome/browser/reading_list/reading_list_pref_names.h"
13 #include "ios/web/public/web_thread.h"
8 #include "url/gurl.h" 14 #include "url/gurl.h"
9 15
10 ReadingListModelImpl::ReadingListModelImpl() : ReadingListModelImpl(NULL) {} 16 ReadingListModelImpl::ReadingListModelImpl()
17 : ReadingListModelImpl(NULL, NULL) {}
jif 2016/11/07 12:30:52 s/NULL/nullptr/ (TIL that we can chain constructo
Olivier 2016/11/07 18:18:46 Done.
11 18
12 ReadingListModelImpl::ReadingListModelImpl( 19 ReadingListModelImpl::ReadingListModelImpl(
13 std::unique_ptr<ReadingListModelStorage> storage) 20 std::unique_ptr<ReadingListModelStorage> storage,
14 : hasUnseen_(false) { 21 PrefService* pref_service)
22 : pref_service_(pref_service),
23 has_unseen_(false),
24 loaded_(false),
25 weak_ptr_factory_(this) {
26 DCHECK(CalledOnValidThread());
15 if (storage) { 27 if (storage) {
16 storageLayer_ = std::move(storage); 28 storage_layer_ = std::move(storage);
17 read_ = storageLayer_->LoadPersistentReadList(); 29 storage_layer_->SetReadingListModel(this);
18 unread_ = storageLayer_->LoadPersistentUnreadList(); 30 } else {
19 hasUnseen_ = storageLayer_->LoadPersistentHasUnseen(); 31 loaded_ = true;
32 read_ = base::MakeUnique<ReadingListEntries>();
33 unread_ = base::MakeUnique<ReadingListEntries>();
20 } 34 }
21 loaded_ = true; 35 has_unseen_ = LoadPersistentHasUnseen();
22 } 36 }
23 ReadingListModelImpl::~ReadingListModelImpl() {} 37 ReadingListModelImpl::~ReadingListModelImpl() {}
24 38
39 void ReadingListModelImpl::ModelLoaded(
40 std::unique_ptr<ReadingListEntries> unread,
41 std::unique_ptr<ReadingListEntries> read) {
42 DCHECK(CalledOnValidThread());
43 read_ = std::move(read);
44 unread_ = std::move(unread);
45 loaded_ = true;
46 SortEntries();
47 for (auto& observer : observers_)
48 observer.ReadingListModelLoaded(this);
49 }
50
25 void ReadingListModelImpl::Shutdown() { 51 void ReadingListModelImpl::Shutdown() {
52 DCHECK(CalledOnValidThread());
26 for (auto& observer : observers_) 53 for (auto& observer : observers_)
27 observer.ReadingListModelBeingDeleted(this); 54 observer.ReadingListModelBeingDeleted(this);
28 loaded_ = false; 55 loaded_ = false;
29 } 56 }
30 57
31 bool ReadingListModelImpl::loaded() const { 58 bool ReadingListModelImpl::loaded() const {
59 DCHECK(CalledOnValidThread());
32 return loaded_; 60 return loaded_;
33 } 61 }
34 62
35 size_t ReadingListModelImpl::unread_size() const { 63 size_t ReadingListModelImpl::unread_size() const {
36 DCHECK(loaded()); 64 DCHECK(CalledOnValidThread());
37 return unread_.size(); 65 if (!loaded())
66 return 0;
67 return unread_->size();
38 } 68 }
39 69
40 size_t ReadingListModelImpl::read_size() const { 70 size_t ReadingListModelImpl::read_size() const {
41 DCHECK(loaded()); 71 DCHECK(CalledOnValidThread());
42 return read_.size(); 72 if (!loaded())
73 return 0;
74 return read_->size();
43 } 75 }
44 76
45 bool ReadingListModelImpl::HasUnseenEntries() const { 77 bool ReadingListModelImpl::HasUnseenEntries() const {
46 DCHECK(loaded()); 78 DCHECK(CalledOnValidThread());
47 return unread_size() && hasUnseen_; 79 if (!loaded())
80 return false;
81 return unread_size() && has_unseen_;
48 } 82 }
49 83
50 void ReadingListModelImpl::ResetUnseenEntries() { 84 void ReadingListModelImpl::ResetUnseenEntries() {
85 DCHECK(CalledOnValidThread());
51 DCHECK(loaded()); 86 DCHECK(loaded());
52 hasUnseen_ = false; 87 has_unseen_ = false;
53 if (storageLayer_ && !IsPerformingBatchUpdates()) 88 if (!IsPerformingBatchUpdates())
54 storageLayer_->SavePersistentHasUnseen(false); 89 SavePersistentHasUnseen(false);
55 } 90 }
56 91
57 const ReadingListEntry& ReadingListModelImpl::GetUnreadEntryAtIndex( 92 const ReadingListEntry& ReadingListModelImpl::GetUnreadEntryAtIndex(
58 size_t index) const { 93 size_t index) const {
94 DCHECK(CalledOnValidThread());
59 DCHECK(loaded()); 95 DCHECK(loaded());
60 return unread_[index]; 96 return unread_->at(index);
61 } 97 }
62 98
63 const ReadingListEntry& ReadingListModelImpl::GetReadEntryAtIndex( 99 const ReadingListEntry& ReadingListModelImpl::GetReadEntryAtIndex(
64 size_t index) const { 100 size_t index) const {
101 DCHECK(CalledOnValidThread());
65 DCHECK(loaded()); 102 DCHECK(loaded());
66 return read_[index]; 103 return read_->at(index);
67 } 104 }
68 105
69 const ReadingListEntry* ReadingListModelImpl::GetEntryFromURL( 106 const ReadingListEntry* ReadingListModelImpl::GetEntryFromURL(
70 const GURL& gurl) const { 107 const GURL& gurl) const {
108 DCHECK(CalledOnValidThread());
71 DCHECK(loaded()); 109 DCHECK(loaded());
110 bool read;
111 return GetMutableEntryFromURL(gurl, &read);
jif 2016/11/07 12:30:52 s/&read/nullptr/ ?
Olivier 2016/11/07 18:18:46 Done.
112 }
113
114 ReadingListEntry* ReadingListModelImpl::GetMutableEntryFromURL(
115 const GURL& gurl,
116 bool* read) const {
117 DCHECK(CalledOnValidThread());
118 DCHECK(loaded());
119 bool is_read;
72 ReadingListEntry entry(gurl, std::string()); 120 ReadingListEntry entry(gurl, std::string());
73 auto it = std::find(read_.begin(), read_.end(), entry); 121 auto it = std::find(read_->begin(), read_->end(), entry);
74 if (it == read_.end()) { 122 is_read = true;
75 it = std::find(unread_.begin(), unread_.end(), entry); 123 if (it == read_->end()) {
76 if (it == unread_.end()) 124 it = std::find(unread_->begin(), unread_->end(), entry);
125 is_read = false;
126 if (it == unread_->end())
77 return nullptr; 127 return nullptr;
78 } 128 }
129 if (read) {
jif 2016/11/07 12:30:52 I really dislike the fact that we are forbidden to
130 *read = is_read;
131 }
79 return &(*it); 132 return &(*it);
80 } 133 }
81 134
82 bool ReadingListModelImpl::CallbackEntryURL( 135 bool ReadingListModelImpl::CallbackEntryURL(
83 const GURL& url, 136 const GURL& url,
84 base::Callback<void(const ReadingListEntry&)> callback) const { 137 base::Callback<void(const ReadingListEntry&)> callback) const {
138 DCHECK(CalledOnValidThread());
85 DCHECK(loaded()); 139 DCHECK(loaded());
86 const ReadingListEntry* entry = GetEntryFromURL(url); 140 const ReadingListEntry* entry = GetMutableEntryFromURL(url, nullptr);
87 if (entry) { 141 if (entry) {
88 callback.Run(*entry); 142 callback.Run(*entry);
89 return true; 143 return true;
90 } 144 }
91 return false; 145 return false;
92 } 146 }
93 147
94 void ReadingListModelImpl::RemoveEntryByUrl(const GURL& url) { 148 bool ReadingListModelImpl::CallbackEntryReadStatusURL(
149 const GURL& url,
150 base::Callback<void(const ReadingListEntry&, bool read)> callback) const {
151 DCHECK(CalledOnValidThread());
95 DCHECK(loaded()); 152 DCHECK(loaded());
96 const ReadingListEntry entry(url, std::string()); 153 bool read;
154 const ReadingListEntry* entry = GetMutableEntryFromURL(url, &read);
155 if (entry) {
156 callback.Run(*entry, read);
157 return true;
158 }
159 return false;
160 }
97 161
98 auto result = std::find(unread_.begin(), unread_.end(), entry); 162 void ReadingListModelImpl::SyncAddEntry(std::unique_ptr<ReadingListEntry> entry,
99 if (result != unread_.end()) { 163 bool read) {
100 for (auto& observer : observers_) { 164 DCHECK(CalledOnValidThread());
101 observer.ReadingListWillRemoveUnreadEntry( 165 DCHECK(loaded());
102 this, std::distance(unread_.begin(), result)); 166 bool is_existing_entry_read;
167 ReadingListEntry* existing_entry =
168 GetMutableEntryFromURL(entry->URL(), &is_existing_entry_read);
169 if (!existing_entry) {
170 if (storage_layer_) {
171 storage_layer_->SaveEntry(*entry, read, true);
103 } 172 }
104 unread_.erase(result); 173 if (read) {
105 if (storageLayer_ && !IsPerformingBatchUpdates()) 174 for (auto& observer : observers_)
106 storageLayer_->SavePersistentUnreadList(unread_); 175 observer.ReadingListWillAddReadEntry(this, *entry);
176 read_->insert(read_->begin(), std::move(*entry));
177 } else {
178 for (auto& observer : observers_)
179 observer.ReadingListWillAddUnreadEntry(this, *entry);
180 has_unseen_ = true;
181 SavePersistentHasUnseen(true);
182 unread_->insert(unread_->begin(), std::move(*entry));
183 }
107 for (auto& observer : observers_) 184 for (auto& observer : observers_)
108 observer.ReadingListDidApplyChanges(this); 185 observer.ReadingListDidApplyChanges(this);
109 return; 186 return;
110 } 187 }
111 188
112 result = std::find(read_.begin(), read_.end(), entry); 189 if (existing_entry->UpdateTime() > entry->UpdateTime()) {
113 if (result != read_.end()) { 190 // Existing entry is newer thatn sync one. Do not update it.
jif 2016/11/07 12:30:52 s/thatn/than the/
Olivier 2016/11/07 18:18:45 Done.
114 for (auto& observer : observers_) { 191 return;
115 observer.ReadingListWillRemoveReadEntry( 192 }
116 this, std::distance(read_.begin(), result)); 193
194 // Merge local data in new entry.
195 entry->MergeLocalStateFrom(*existing_entry);
196
197 if (is_existing_entry_read) {
198 auto result = std::find(read_->begin(), read_->end(), *existing_entry);
199 for (auto& observer : observers_)
200 observer.ReadingListWillMoveEntry(
201 this, std::distance(read_->begin(), result), true);
jif 2016/11/07 12:30:51 Optional nit: writing: observer.ReadingListWillMov
Olivier 2016/11/07 18:18:46 Done.
202 if (result != read_->end()) {
jif 2016/11/07 12:30:52 I don't think "result != read_->end()" is supposed
Olivier 2016/11/07 18:18:46 Done.
203 read_->erase(result);
117 } 204 }
118 read_.erase(result); 205 } else {
119 if (storageLayer_ && !IsPerformingBatchUpdates()) 206 auto result = std::find(unread_->begin(), unread_->end(), *existing_entry);
120 storageLayer_->SavePersistentReadList(read_); 207 for (auto& observer : observers_)
208 observer.ReadingListWillMoveEntry(
209 this, std::distance(unread_->begin(), result), false);
210 if (result != unread_->end()) {
jif 2016/11/07 12:30:52 Same here: replace with DCHECK?
Olivier 2016/11/07 18:18:46 Done.
211 unread_->erase(result);
212 }
213 }
214
215 if (storage_layer_) {
216 storage_layer_->SaveEntry(*entry, read, true);
217 }
218
219 if (read) {
220 read_->push_back(std::move(*entry));
221 } else {
222 unread_->push_back(std::move(*entry));
223 }
224 for (auto& observer : observers_)
225 observer.ReadingListDidApplyChanges(this);
226 }
227
228 void ReadingListModelImpl::SyncRemoveEntry(const GURL& gurl) {
229 RemoveEntryByURLImpl(gurl, true);
230 }
231
232 void ReadingListModelImpl::RemoveEntryByURL(const GURL& url) {
233 RemoveEntryByURLImpl(url, false);
234 }
235
236 void ReadingListModelImpl::RemoveEntryByURLImpl(const GURL& url,
237 bool from_sync) {
238 DCHECK(CalledOnValidThread());
239 DCHECK(loaded());
240 const ReadingListEntry entry(url, std::string());
241
242 auto result = std::find(unread_->begin(), unread_->end(), entry);
243 if (result != unread_->end()) {
244 for (auto& observer : observers_)
245 observer.ReadingListWillRemoveUnreadEntry(
246 this, std::distance(unread_->begin(), result));
247
248 if (storage_layer_) {
249 storage_layer_->RemoveEntry(*result, from_sync);
250 }
251 unread_->erase(result);
252
121 for (auto& observer : observers_) 253 for (auto& observer : observers_)
122 observer.ReadingListDidApplyChanges(this); 254 observer.ReadingListDidApplyChanges(this);
123 return; 255 return;
256 }
257
258 result = std::find(read_->begin(), read_->end(), entry);
259 if (result != read_->end()) {
260 for (auto& observer : observers_)
261 observer.ReadingListWillRemoveReadEntry(
262 this, std::distance(read_->begin(), result));
263 if (storage_layer_) {
264 storage_layer_->RemoveEntry(*result, from_sync);
265 }
266 read_->erase(result);
267
268 for (auto& observer : observers_)
269 observer.ReadingListDidApplyChanges(this);
270 return;
124 } 271 }
125 } 272 }
126 273
127 const ReadingListEntry& ReadingListModelImpl::AddEntry( 274 const ReadingListEntry& ReadingListModelImpl::AddEntry(
128 const GURL& url, 275 const GURL& url,
129 const std::string& title) { 276 const std::string& title) {
277 DCHECK(CalledOnValidThread());
130 DCHECK(loaded()); 278 DCHECK(loaded());
131 RemoveEntryByUrl(url); 279 RemoveEntryByURL(url);
132 ReadingListEntry entry(url, title); 280 ReadingListEntry entry(url, title);
133 for (auto& observer : observers_) 281 for (auto& observer : observers_)
134 observer.ReadingListWillAddUnreadEntry(this, entry); 282 observer.ReadingListWillAddUnreadEntry(this, entry);
135 unread_.insert(unread_.begin(), std::move(entry)); 283 has_unseen_ = true;
136 hasUnseen_ = true; 284 SavePersistentHasUnseen(true);
137 if (storageLayer_ && !IsPerformingBatchUpdates()) { 285 if (storage_layer_) {
138 storageLayer_->SavePersistentUnreadList(unread_); 286 storage_layer_->SaveEntry(entry, false, false);
139 storageLayer_->SavePersistentHasUnseen(true);
140 } 287 }
288 unread_->insert(unread_->begin(), std::move(entry));
jif 2016/11/07 12:30:52 Would push_back(std::move(entry)) work? If yes, ch
Olivier 2016/11/07 18:18:46 It would, but may be bad in term of performances a
289
141 for (auto& observer : observers_) 290 for (auto& observer : observers_)
142 observer.ReadingListDidApplyChanges(this); 291 observer.ReadingListDidApplyChanges(this);
143 return *unread_.begin(); 292 return *unread_->begin();
144 } 293 }
145 294
146 void ReadingListModelImpl::MarkUnreadByURL(const GURL& url) { 295 void ReadingListModelImpl::MarkUnreadByURL(const GURL& url) {
296 DCHECK(CalledOnValidThread());
147 DCHECK(loaded()); 297 DCHECK(loaded());
148 ReadingListEntry entry(url, std::string()); 298 ReadingListEntry entry(url, std::string());
149 auto result = std::find(read_.begin(), read_.end(), entry); 299 auto result = std::find(read_->begin(), read_->end(), entry);
150 if (result == read_.end()) 300 if (result == read_->end())
151 return; 301 return;
152 302
153 for (ReadingListModelObserver& observer : observers_) { 303 for (ReadingListModelObserver& observer : observers_) {
154 observer.ReadingListWillMoveEntry(this, 304 observer.ReadingListWillMoveEntry(
155 std::distance(read_.begin(), result)); 305 this, std::distance(read_->begin(), result), true);
156 } 306 }
157 307
158 unread_.insert(unread_.begin(), std::move(*result)); 308 result->MarkEntryUpdated();
159 read_.erase(result); 309 if (storage_layer_) {
310 storage_layer_->SaveEntry(*result, false, false);
311 }
160 312
161 if (storageLayer_ && !IsPerformingBatchUpdates()) { 313 unread_->insert(unread_->begin(), std::move(*result));
162 storageLayer_->SavePersistentUnreadList(read_); 314 read_->erase(result);
163 storageLayer_->SavePersistentReadList(unread_); 315
164 }
165 for (ReadingListModelObserver& observer : observers_) { 316 for (ReadingListModelObserver& observer : observers_) {
166 observer.ReadingListDidApplyChanges(this); 317 observer.ReadingListDidApplyChanges(this);
167 } 318 }
168 } 319 }
169 320
170 void ReadingListModelImpl::MarkReadByURL(const GURL& url) { 321 void ReadingListModelImpl::MarkReadByURL(const GURL& url) {
322 DCHECK(CalledOnValidThread());
171 DCHECK(loaded()); 323 DCHECK(loaded());
172 ReadingListEntry entry(url, std::string()); 324 ReadingListEntry entry(url, std::string());
173 auto result = std::find(unread_.begin(), unread_.end(), entry); 325 auto result = std::find(unread_->begin(), unread_->end(), entry);
174 if (result == unread_.end()) 326 if (result == unread_->end())
175 return; 327 return;
176 328
177 for (auto& observer : observers_) { 329 for (auto& observer : observers_)
178 observer.ReadingListWillMoveEntry(this, 330 observer.ReadingListWillMoveEntry(
179 std::distance(unread_.begin(), result)); 331 this, std::distance(unread_->begin(), result), false);
332
333 result->MarkEntryUpdated();
334 if (storage_layer_) {
335 storage_layer_->SaveEntry(*result, true, false);
180 } 336 }
181 337
182 read_.insert(read_.begin(), std::move(*result)); 338 read_->insert(read_->begin(), std::move(*result));
183 unread_.erase(result); 339 unread_->erase(result);
184 340
185 if (storageLayer_ && !IsPerformingBatchUpdates()) {
186 storageLayer_->SavePersistentUnreadList(unread_);
187 storageLayer_->SavePersistentReadList(read_);
188 }
189 for (auto& observer : observers_) 341 for (auto& observer : observers_)
190 observer.ReadingListDidApplyChanges(this); 342 observer.ReadingListDidApplyChanges(this);
191 } 343 }
192 344
193 void ReadingListModelImpl::SetEntryTitle(const GURL& url, 345 void ReadingListModelImpl::SetEntryTitle(const GURL& url,
194 const std::string& title) { 346 const std::string& title) {
347 DCHECK(CalledOnValidThread());
195 DCHECK(loaded()); 348 DCHECK(loaded());
196 const ReadingListEntry entry(url, std::string()); 349 const ReadingListEntry entry(url, std::string());
197 350
198 auto result = std::find(unread_.begin(), unread_.end(), entry); 351 auto result = std::find(unread_->begin(), unread_->end(), entry);
199 if (result != unread_.end()) { 352 if (result != unread_->end()) {
200 for (auto& observer : observers_) { 353 for (auto& observer : observers_)
201 observer.ReadingListWillUpdateUnreadEntry( 354 observer.ReadingListWillUpdateUnreadEntry(
202 this, std::distance(unread_.begin(), result)); 355 this, std::distance(unread_->begin(), result));
356 result->SetTitle(title);
357
358 if (storage_layer_) {
359 storage_layer_->SaveEntry(*result, false, false);
203 } 360 }
204 result->SetTitle(title);
205 if (storageLayer_ && !IsPerformingBatchUpdates())
206 storageLayer_->SavePersistentUnreadList(unread_);
207 for (auto& observer : observers_) 361 for (auto& observer : observers_)
208 observer.ReadingListDidApplyChanges(this); 362 observer.ReadingListDidApplyChanges(this);
209 return; 363 return;
210 } 364 }
211 365
212 result = std::find(read_.begin(), read_.end(), entry); 366 result = std::find(read_->begin(), read_->end(), entry);
213 if (result != read_.end()) { 367 if (result != read_->end()) {
214 for (auto& observer : observers_) { 368 for (auto& observer : observers_)
215 observer.ReadingListWillUpdateReadEntry( 369 observer.ReadingListWillUpdateReadEntry(
216 this, std::distance(read_.begin(), result)); 370 this, std::distance(read_->begin(), result));
371 result->SetTitle(title);
372 if (storage_layer_) {
373 storage_layer_->SaveEntry(*result, true, false);
217 } 374 }
218 result->SetTitle(title);
219 if (storageLayer_ && !IsPerformingBatchUpdates())
220 storageLayer_->SavePersistentReadList(read_);
221 for (auto& observer : observers_) 375 for (auto& observer : observers_)
222 observer.ReadingListDidApplyChanges(this); 376 observer.ReadingListDidApplyChanges(this);
223 return; 377 return;
224 } 378 }
225 } 379 }
226 380
227 void ReadingListModelImpl::SetEntryDistilledURL(const GURL& url, 381 void ReadingListModelImpl::SetEntryDistilledURL(const GURL& url,
228 const GURL& distilled_url) { 382 const GURL& distilled_url) {
383 DCHECK(CalledOnValidThread());
229 DCHECK(loaded()); 384 DCHECK(loaded());
230 const ReadingListEntry entry(url, std::string()); 385 const ReadingListEntry entry(url, std::string());
231 386
232 auto result = std::find(unread_.begin(), unread_.end(), entry); 387 auto result = std::find(unread_->begin(), unread_->end(), entry);
233 if (result != unread_.end()) { 388 if (result != unread_->end()) {
234 for (auto& observer : observers_) { 389 for (auto& observer : observers_)
235 observer.ReadingListWillUpdateUnreadEntry( 390 observer.ReadingListWillUpdateUnreadEntry(
236 this, std::distance(unread_.begin(), result)); 391 this, std::distance(unread_->begin(), result));
392 result->SetDistilledURL(distilled_url);
393 if (storage_layer_) {
394 storage_layer_->SaveEntry(*result, false, false);
237 } 395 }
238 result->SetDistilledURL(distilled_url);
239 if (storageLayer_ && !IsPerformingBatchUpdates())
240 storageLayer_->SavePersistentUnreadList(unread_);
241 for (auto& observer : observers_) 396 for (auto& observer : observers_)
242 observer.ReadingListDidApplyChanges(this); 397 observer.ReadingListDidApplyChanges(this);
243 return; 398 return;
244 } 399 }
245 400
246 result = std::find(read_.begin(), read_.end(), entry); 401 result = std::find(read_->begin(), read_->end(), entry);
247 if (result != read_.end()) { 402 if (result != read_->end()) {
248 for (auto& observer : observers_) { 403 for (auto& observer : observers_)
249 observer.ReadingListWillUpdateReadEntry( 404 observer.ReadingListWillUpdateReadEntry(
250 this, std::distance(read_.begin(), result)); 405 this, std::distance(read_->begin(), result));
406 result->SetDistilledURL(distilled_url);
407 if (storage_layer_) {
408 storage_layer_->SaveEntry(*result, true, false);
251 } 409 }
252 result->SetDistilledURL(distilled_url);
253 if (storageLayer_ && !IsPerformingBatchUpdates())
254 storageLayer_->SavePersistentReadList(read_);
255 for (auto& observer : observers_) 410 for (auto& observer : observers_)
256 observer.ReadingListDidApplyChanges(this); 411 observer.ReadingListDidApplyChanges(this);
257 return; 412 return;
258 } 413 }
259 } 414 }
260 415
261 void ReadingListModelImpl::SetEntryDistilledState( 416 void ReadingListModelImpl::SetEntryDistilledState(
262 const GURL& url, 417 const GURL& url,
263 ReadingListEntry::DistillationState state) { 418 ReadingListEntry::DistillationState state) {
419 DCHECK(CalledOnValidThread());
264 DCHECK(loaded()); 420 DCHECK(loaded());
265 const ReadingListEntry entry(url, std::string()); 421 const ReadingListEntry entry(url, std::string());
266 422
267 auto result = std::find(unread_.begin(), unread_.end(), entry); 423 auto result = std::find(unread_->begin(), unread_->end(), entry);
268 if (result != unread_.end()) { 424 if (result != unread_->end()) {
269 for (auto& observer : observers_) { 425 for (auto& observer : observers_)
270 observer.ReadingListWillUpdateUnreadEntry( 426 observer.ReadingListWillUpdateUnreadEntry(
271 this, std::distance(unread_.begin(), result)); 427 this, std::distance(unread_->begin(), result));
428 result->SetDistilledState(state);
429 if (storage_layer_) {
430 storage_layer_->SaveEntry(*result, false, false);
272 } 431 }
273 result->SetDistilledState(state);
274 if (storageLayer_ && !IsPerformingBatchUpdates())
275 storageLayer_->SavePersistentUnreadList(unread_);
276 for (auto& observer : observers_) 432 for (auto& observer : observers_)
277 observer.ReadingListDidApplyChanges(this); 433 observer.ReadingListDidApplyChanges(this);
278 return; 434 return;
279 } 435 }
280 436
281 result = std::find(read_.begin(), read_.end(), entry); 437 result = std::find(read_->begin(), read_->end(), entry);
282 if (result != read_.end()) { 438 if (result != read_->end()) {
283 for (auto& observer : observers_) { 439 for (auto& observer : observers_)
284 observer.ReadingListWillUpdateReadEntry( 440 observer.ReadingListWillUpdateReadEntry(
285 this, std::distance(read_.begin(), result)); 441 this, std::distance(read_->begin(), result));
442 result->SetDistilledState(state);
443 if (storage_layer_) {
444 storage_layer_->SaveEntry(*result, true, false);
286 } 445 }
287 result->SetDistilledState(state);
288 if (storageLayer_ && !IsPerformingBatchUpdates())
289 storageLayer_->SavePersistentReadList(read_);
290 for (auto& observer : observers_) 446 for (auto& observer : observers_)
291 observer.ReadingListDidApplyChanges(this); 447 observer.ReadingListDidApplyChanges(this);
292 return; 448 return;
293 } 449 }
294 }; 450 };
295 451
296 void ReadingListModelImpl::EndBatchUpdates() { 452 void ReadingListModelImpl::LeavingBatchUpdates() {
297 ReadingListModel::EndBatchUpdates(); 453 DCHECK(CalledOnValidThread());
298 if (IsPerformingBatchUpdates() || !storageLayer_) { 454 ReadingListModel::LeavingBatchUpdates();
jif 2016/11/07 12:30:52 Shouldn't |ReadingListModel::LeavingBatchUpdates()
Olivier 2016/11/07 18:18:46 Done.
455 if (storage_layer_) {
456 SavePersistentHasUnseen(has_unseen_);
457 storage_layer_->CommitTransaction();
458 SortEntries();
459 }
460 }
461
462 void ReadingListModelImpl::EnteringBatchUpdates() {
463 DCHECK(CalledOnValidThread());
464 if (storage_layer_) {
465 storage_layer_->BeginTransaction();
466 }
467 ReadingListModel::EnteringBatchUpdates();
jif 2016/11/07 12:30:52 And this, at the beginning of the method?
Olivier 2016/11/07 18:18:46 Done.
468 }
469
470 void ReadingListModelImpl::SavePersistentHasUnseen(bool has_unseen) {
471 DCHECK(CalledOnValidThread());
472 if (!pref_service_) {
299 return; 473 return;
300 } 474 }
301 storageLayer_->SavePersistentUnreadList(unread_); 475 pref_service_->SetBoolean(reading_list::prefs::kReadingListHasUnseenEntries,
302 storageLayer_->SavePersistentReadList(read_); 476 has_unseen);
303 storageLayer_->SavePersistentHasUnseen(hasUnseen_);
304 } 477 }
478
479 bool ReadingListModelImpl::LoadPersistentHasUnseen() {
jif 2016/11/07 12:30:52 Tiny nit: we aren't loading anything here. How abo
Olivier 2016/11/07 18:18:46 Done.
480 DCHECK(CalledOnValidThread());
481 if (!pref_service_) {
482 return false;
483 }
484 return pref_service_->GetBoolean(
485 reading_list::prefs::kReadingListHasUnseenEntries);
486 }
487
488 syncer::ModelTypeService* ReadingListModelImpl::GetModelTypeService() {
489 DCHECK(loaded());
490 return storage_layer_->GetModelTypeService();
jif 2016/11/07 12:30:51 Would this: if (!storage_layer_) return nullptr
Olivier 2016/11/07 18:18:46 Yes, thanks !
491 }
492
493 void ReadingListModelImpl::SortEntries() {
494 DCHECK(CalledOnValidThread());
495 DCHECK(loaded());
496 std::sort(read_->begin(), read_->end(),
497 ReadingListEntry::CompareEntryUpdateTime);
498 std::sort(unread_->begin(), unread_->end(),
499 ReadingListEntry::CompareEntryUpdateTime);
500 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698