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

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

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

Powered by Google App Engine
This is Rietveld 408576698