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

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

Issue 2525663002: Refactor Reading List Model to use URL as key. (Closed)
Patch Set: format Created 4 years 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_store.h" 5 #include "ios/chrome/browser/reading_list/reading_list_store.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/logging.h" 8 #include "base/logging.h"
9 #include "base/memory/ptr_util.h" 9 #include "base/memory/ptr_util.h"
10 #include "components/sync/model/entity_change.h" 10 #include "components/sync/model/entity_change.h"
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
65 DCHECK(CalledOnValidThread()); 65 DCHECK(CalledOnValidThread());
66 pending_transaction_count_--; 66 pending_transaction_count_--;
67 if (pending_transaction_count_ == 0) { 67 if (pending_transaction_count_ == 0) {
68 store_->CommitWriteBatch( 68 store_->CommitWriteBatch(
69 std::move(batch_), 69 std::move(batch_),
70 base::Bind(&ReadingListStore::OnDatabaseSave, base::AsWeakPtr(this))); 70 base::Bind(&ReadingListStore::OnDatabaseSave, base::AsWeakPtr(this)));
71 batch_.reset(); 71 batch_.reset();
72 } 72 }
73 } 73 }
74 74
75 void ReadingListStore::SaveEntry(const ReadingListEntry& entry, bool read) { 75 void ReadingListStore::SaveEntry(const ReadingListEntry* entry) {
76 DCHECK(CalledOnValidThread()); 76 DCHECK(CalledOnValidThread());
77 auto token = EnsureBatchCreated(); 77 auto token = EnsureBatchCreated();
78 78
79 std::unique_ptr<reading_list::ReadingListLocal> pb_entry = 79 std::unique_ptr<reading_list::ReadingListLocal> pb_entry =
80 entry.AsReadingListLocal(read); 80 entry->AsReadingListLocal();
81 81
82 batch_->WriteData(entry.URL().spec(), pb_entry->SerializeAsString()); 82 batch_->WriteData(entry->URL().spec(), pb_entry->SerializeAsString());
83 83
84 if (!change_processor()->IsTrackingMetadata()) { 84 if (!change_processor()->IsTrackingMetadata()) {
85 return; 85 return;
86 } 86 }
87 std::unique_ptr<sync_pb::ReadingListSpecifics> pb_entry_sync = 87 std::unique_ptr<sync_pb::ReadingListSpecifics> pb_entry_sync =
88 entry.AsReadingListSpecifics(read); 88 entry->AsReadingListSpecifics();
89 89
90 std::unique_ptr<syncer::MetadataChangeList> metadata_change_list = 90 std::unique_ptr<syncer::MetadataChangeList> metadata_change_list =
91 CreateMetadataChangeList(); 91 CreateMetadataChangeList();
92 92
93 std::unique_ptr<syncer::EntityData> entity_data(new syncer::EntityData()); 93 std::unique_ptr<syncer::EntityData> entity_data(new syncer::EntityData());
94 *entity_data->specifics.mutable_reading_list() = *pb_entry_sync; 94 *entity_data->specifics.mutable_reading_list() = *pb_entry_sync;
95 entity_data->non_unique_name = pb_entry_sync->entry_id(); 95 entity_data->non_unique_name = pb_entry_sync->entry_id();
96 96
97 change_processor()->Put(entry.URL().spec(), std::move(entity_data), 97 change_processor()->Put(entry->URL().spec(), std::move(entity_data),
98 metadata_change_list.get()); 98 metadata_change_list.get());
99 batch_->TransferMetadataChanges(std::move(metadata_change_list)); 99 batch_->TransferMetadataChanges(std::move(metadata_change_list));
100 } 100 }
101 101
102 void ReadingListStore::RemoveEntry(const ReadingListEntry& entry) { 102 void ReadingListStore::RemoveEntry(const ReadingListEntry* entry) {
103 DCHECK(CalledOnValidThread()); 103 DCHECK(CalledOnValidThread());
104 auto token = EnsureBatchCreated(); 104 auto token = EnsureBatchCreated();
105 105
106 batch_->DeleteData(entry.URL().spec()); 106 batch_->DeleteData(entry->URL().spec());
107 if (!change_processor()->IsTrackingMetadata()) { 107 if (!change_processor()->IsTrackingMetadata()) {
108 return; 108 return;
109 } 109 }
110 std::unique_ptr<syncer::MetadataChangeList> metadata_change_list = 110 std::unique_ptr<syncer::MetadataChangeList> metadata_change_list =
111 CreateMetadataChangeList(); 111 CreateMetadataChangeList();
112 112
113 change_processor()->Delete(entry.URL().spec(), metadata_change_list.get()); 113 change_processor()->Delete(entry->URL().spec(), metadata_change_list.get());
114 batch_->TransferMetadataChanges(std::move(metadata_change_list)); 114 batch_->TransferMetadataChanges(std::move(metadata_change_list));
115 } 115 }
116 116
117 void ReadingListStore::OnDatabaseLoad( 117 void ReadingListStore::OnDatabaseLoad(
118 syncer::ModelTypeStore::Result result, 118 syncer::ModelTypeStore::Result result,
119 std::unique_ptr<syncer::ModelTypeStore::RecordList> entries) { 119 std::unique_ptr<syncer::ModelTypeStore::RecordList> entries) {
120 DCHECK(CalledOnValidThread()); 120 DCHECK(CalledOnValidThread());
121 if (result != syncer::ModelTypeStore::Result::SUCCESS) { 121 if (result != syncer::ModelTypeStore::Result::SUCCESS) {
122 change_processor()->OnMetadataLoaded( 122 change_processor()->OnMetadataLoaded(
123 change_processor()->CreateAndUploadError( 123 change_processor()->CreateAndUploadError(
124 FROM_HERE, "Cannot load Reading List Database."), 124 FROM_HERE, "Cannot load Reading List Database."),
125 nullptr); 125 nullptr);
126 return; 126 return;
127 } 127 }
128 auto read = base::MakeUnique<ReadingListEntries>(); 128 auto loaded_entries = base::MakeUnique<ReadingListEntries>();
129 auto unread = base::MakeUnique<ReadingListEntries>();
130 129
131 for (const syncer::ModelTypeStore::Record& r : *entries.get()) { 130 for (const syncer::ModelTypeStore::Record& r : *entries.get()) {
132 // for (const reading_list::ReadingListLocal& pb_entry : *entries) { 131 // for (const reading_list::ReadingListLocal& pb_entry : *entries) {
133 reading_list::ReadingListLocal proto; 132 reading_list::ReadingListLocal proto;
134 if (!proto.ParseFromString(r.value)) { 133 if (!proto.ParseFromString(r.value)) {
135 continue; 134 continue;
136 // TODO(skym, crbug.com/582460): Handle unrecoverable initialization 135 // TODO(skym, crbug.com/582460): Handle unrecoverable initialization
137 // failure. 136 // failure.
138 } 137 }
139 138
140 std::unique_ptr<ReadingListEntry> entry( 139 std::unique_ptr<ReadingListEntry> entry(
141 ReadingListEntry::FromReadingListLocal(proto)); 140 ReadingListEntry::FromReadingListLocal(proto));
142 if (!entry) { 141 if (!entry) {
143 continue; 142 continue;
144 } 143 }
145 if (proto.status() == reading_list::ReadingListLocal::READ) { 144 GURL url = entry->URL();
146 read->push_back(std::move(*entry)); 145 DCHECK(!loaded_entries->count(url));
147 } else { 146 loaded_entries->insert(std::make_pair(url, std::move(*entry)));
148 unread->push_back(std::move(*entry));
149 }
150 } 147 }
151 148
152 delegate_->StoreLoaded(std::move(unread), std::move(read)); 149 delegate_->StoreLoaded(std::move(loaded_entries));
153 150
154 store_->ReadAllMetadata( 151 store_->ReadAllMetadata(
155 base::Bind(&ReadingListStore::OnReadAllMetadata, base::AsWeakPtr(this))); 152 base::Bind(&ReadingListStore::OnReadAllMetadata, base::AsWeakPtr(this)));
156 } 153 }
157 154
158 void ReadingListStore::OnReadAllMetadata( 155 void ReadingListStore::OnReadAllMetadata(
159 syncer::SyncError sync_error, 156 syncer::SyncError sync_error,
160 std::unique_ptr<syncer::MetadataBatch> metadata_batch) { 157 std::unique_ptr<syncer::MetadataBatch> metadata_batch) {
161 DCHECK(CalledOnValidThread()); 158 DCHECK(CalledOnValidThread());
162 change_processor()->OnMetadataLoaded(sync_error, std::move(metadata_batch)); 159 change_processor()->OnMetadataLoaded(sync_error, std::move(metadata_batch));
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
213 std::set<std::string> synced_entries; 210 std::set<std::string> synced_entries;
214 std::unique_ptr<ReadingListModel::ScopedReadingListBatchUpdate> 211 std::unique_ptr<ReadingListModel::ScopedReadingListBatchUpdate>
215 model_batch_updates = model_->BeginBatchUpdates(); 212 model_batch_updates = model_->BeginBatchUpdates();
216 213
217 // Merge sync to local data. 214 // Merge sync to local data.
218 for (const auto& kv : entity_data_map) { 215 for (const auto& kv : entity_data_map) {
219 synced_entries.insert(kv.first); 216 synced_entries.insert(kv.first);
220 const sync_pb::ReadingListSpecifics& specifics = 217 const sync_pb::ReadingListSpecifics& specifics =
221 kv.second.value().specifics.reading_list(); 218 kv.second.value().specifics.reading_list();
222 // Deserialize entry. 219 // Deserialize entry.
223 bool read = specifics.status() == sync_pb::ReadingListSpecifics::READ;
224 std::unique_ptr<ReadingListEntry> entry( 220 std::unique_ptr<ReadingListEntry> entry(
225 ReadingListEntry::FromReadingListSpecifics(specifics)); 221 ReadingListEntry::FromReadingListSpecifics(specifics));
226 222
227 bool was_read;
228 const ReadingListEntry* existing_entry = 223 const ReadingListEntry* existing_entry =
229 model_->GetEntryFromURL(entry->URL(), &was_read); 224 model_->GetEntryFromURL(entry->URL());
230 225
231 if (!existing_entry) { 226 if (!existing_entry) {
232 // This entry is new. Add it to the store and model. 227 // This entry is new. Add it to the store and model.
233 // Convert to local store format and write to store. 228 // Convert to local store format and write to store.
234 std::unique_ptr<reading_list::ReadingListLocal> entry_pb = 229 std::unique_ptr<reading_list::ReadingListLocal> entry_pb =
235 entry->AsReadingListLocal(read); 230 entry->AsReadingListLocal();
236 batch_->WriteData(entry->URL().spec(), entry_pb->SerializeAsString()); 231 batch_->WriteData(entry->URL().spec(), entry_pb->SerializeAsString());
237 232
238 // Notify model about updated entry. 233 // Notify model about updated entry.
239 delegate_->SyncAddEntry(std::move(entry), read); 234 delegate_->SyncAddEntry(std::move(entry));
240 } else if (existing_entry->UpdateTime() < entry->UpdateTime()) { 235 } else if (existing_entry->UpdateTime() < entry->UpdateTime()) {
241 // The entry from sync is more recent. 236 // The entry from sync is more recent.
242 // Merge the local data to it and store it. 237 // Merge the local data to it and store it.
243 ReadingListEntry* merged_entry = 238 ReadingListEntry* merged_entry =
244 delegate_->SyncMergeEntry(std::move(entry), read); 239 delegate_->SyncMergeEntry(std::move(entry));
245 240
246 // Write to the store. 241 // Write to the store.
247 std::unique_ptr<reading_list::ReadingListLocal> entry_local_pb = 242 std::unique_ptr<reading_list::ReadingListLocal> entry_local_pb =
248 merged_entry->AsReadingListLocal(read); 243 merged_entry->AsReadingListLocal();
249 batch_->WriteData(entry->URL().spec(), 244 batch_->WriteData(entry->URL().spec(),
250 entry_local_pb->SerializeAsString()); 245 entry_local_pb->SerializeAsString());
251 246
252 // Send to sync 247 // Send to sync
253 std::unique_ptr<sync_pb::ReadingListSpecifics> entry_sync_pb = 248 std::unique_ptr<sync_pb::ReadingListSpecifics> entry_sync_pb =
254 merged_entry->AsReadingListSpecifics(read); 249 merged_entry->AsReadingListSpecifics();
255 auto entity_data = base::MakeUnique<syncer::EntityData>(); 250 auto entity_data = base::MakeUnique<syncer::EntityData>();
256 *(entity_data->specifics.mutable_reading_list()) = *entry_sync_pb; 251 *(entity_data->specifics.mutable_reading_list()) = *entry_sync_pb;
257 entity_data->non_unique_name = entry_sync_pb->entry_id(); 252 entity_data->non_unique_name = entry_sync_pb->entry_id();
258 253
259 // TODO(crbug.com/666232): Investigate if there is a risk of sync 254 // TODO(crbug.com/666232): Investigate if there is a risk of sync
260 // ping-pong. 255 // ping-pong.
261 change_processor()->Put(entry_sync_pb->entry_id(), std::move(entity_data), 256 change_processor()->Put(entry_sync_pb->entry_id(), std::move(entity_data),
262 metadata_change_list.get()); 257 metadata_change_list.get());
263 258
264 } else { 259 } else {
265 // The entry from sync is out of date. 260 // The entry from sync is out of date.
266 // Send back the local more recent entry. 261 // Send back the local more recent entry.
267 // No need to update 262 // No need to update
268 std::unique_ptr<sync_pb::ReadingListSpecifics> entry_pb = 263 std::unique_ptr<sync_pb::ReadingListSpecifics> entry_pb =
269 existing_entry->AsReadingListSpecifics(was_read); 264 existing_entry->AsReadingListSpecifics();
270 auto entity_data = base::MakeUnique<syncer::EntityData>(); 265 auto entity_data = base::MakeUnique<syncer::EntityData>();
271 *(entity_data->specifics.mutable_reading_list()) = *entry_pb; 266 *(entity_data->specifics.mutable_reading_list()) = *entry_pb;
272 entity_data->non_unique_name = entry_pb->entry_id(); 267 entity_data->non_unique_name = entry_pb->entry_id();
273 268
274 change_processor()->Put(entry_pb->entry_id(), std::move(entity_data), 269 change_processor()->Put(entry_pb->entry_id(), std::move(entity_data),
275 metadata_change_list.get()); 270 metadata_change_list.get());
276 } 271 }
277 } 272 }
278 273
279 // Commit local only entries to server. 274 // Commit local only entries to server.
280 int unread_count = model_->unread_size(); 275 int count = model_->size();
281 int read_count = model_->read_size(); 276 for (int index = 0; index < count; index++) {
282 for (int index = 0; index < unread_count + read_count; index++) { 277 const ReadingListEntry* entry = model_->GetEntryAt(index);
283 bool read = index >= unread_count; 278 if (synced_entries.count(entry->URL().spec())) {
284 const ReadingListEntry& entry =
285 read ? model_->GetReadEntryAtIndex(index - unread_count)
286 : model_->GetUnreadEntryAtIndex(index);
287 if (synced_entries.count(entry.URL().spec())) {
288 // Entry already exists and has been merged above. 279 // Entry already exists and has been merged above.
289 continue; 280 continue;
290 } 281 }
291 282
292 // Local entry has later timestamp. It should be committed to server. 283 // Local entry has later timestamp. It should be committed to server.
293 std::unique_ptr<sync_pb::ReadingListSpecifics> entry_pb = 284 std::unique_ptr<sync_pb::ReadingListSpecifics> entry_pb =
294 entry.AsReadingListSpecifics(read); 285 entry->AsReadingListSpecifics();
295 286
296 auto entity_data = base::MakeUnique<syncer::EntityData>(); 287 auto entity_data = base::MakeUnique<syncer::EntityData>();
297 *(entity_data->specifics.mutable_reading_list()) = *entry_pb; 288 *(entity_data->specifics.mutable_reading_list()) = *entry_pb;
298 entity_data->non_unique_name = entry_pb->entry_id(); 289 entity_data->non_unique_name = entry_pb->entry_id();
299 290
300 change_processor()->Put(entry_pb->entry_id(), std::move(entity_data), 291 change_processor()->Put(entry_pb->entry_id(), std::move(entity_data),
301 metadata_change_list.get()); 292 metadata_change_list.get());
302 } 293 }
303 batch_->TransferMetadataChanges(std::move(metadata_change_list)); 294 batch_->TransferMetadataChanges(std::move(metadata_change_list));
304 295
(...skipping 15 matching lines...) Expand all
320 311
321 for (syncer::EntityChange& change : entity_changes) { 312 for (syncer::EntityChange& change : entity_changes) {
322 if (change.type() == syncer::EntityChange::ACTION_DELETE) { 313 if (change.type() == syncer::EntityChange::ACTION_DELETE) {
323 batch_->DeleteData(change.storage_key()); 314 batch_->DeleteData(change.storage_key());
324 // Need to notify model that entry is deleted. 315 // Need to notify model that entry is deleted.
325 delegate_->SyncRemoveEntry(GURL(change.storage_key())); 316 delegate_->SyncRemoveEntry(GURL(change.storage_key()));
326 } else { 317 } else {
327 // Deserialize entry. 318 // Deserialize entry.
328 const sync_pb::ReadingListSpecifics& specifics = 319 const sync_pb::ReadingListSpecifics& specifics =
329 change.data().specifics.reading_list(); 320 change.data().specifics.reading_list();
330 bool read = specifics.status() == sync_pb::ReadingListSpecifics::READ;
331 std::unique_ptr<ReadingListEntry> entry( 321 std::unique_ptr<ReadingListEntry> entry(
332 ReadingListEntry::FromReadingListSpecifics(specifics)); 322 ReadingListEntry::FromReadingListSpecifics(specifics));
333 323
334 bool was_read;
335 const ReadingListEntry* existing_entry = 324 const ReadingListEntry* existing_entry =
336 model_->GetEntryFromURL(entry->URL(), &was_read); 325 model_->GetEntryFromURL(entry->URL());
337 326
338 if (!existing_entry) { 327 if (!existing_entry) {
339 // This entry is new. Add it to the store and model. 328 // This entry is new. Add it to the store and model.
340 // Convert to local store format and write to store. 329 // Convert to local store format and write to store.
341 std::unique_ptr<reading_list::ReadingListLocal> entry_pb = 330 std::unique_ptr<reading_list::ReadingListLocal> entry_pb =
342 entry->AsReadingListLocal(read); 331 entry->AsReadingListLocal();
343 batch_->WriteData(entry->URL().spec(), entry_pb->SerializeAsString()); 332 batch_->WriteData(entry->URL().spec(), entry_pb->SerializeAsString());
344 333
345 // Notify model about updated entry. 334 // Notify model about updated entry.
346 delegate_->SyncAddEntry(std::move(entry), read); 335 delegate_->SyncAddEntry(std::move(entry));
347 } else if (existing_entry->UpdateTime() < entry->UpdateTime()) { 336 } else if (existing_entry->UpdateTime() < entry->UpdateTime()) {
348 // The entry from sync is more recent. 337 // The entry from sync is more recent.
349 // Merge the local data to it and store it. 338 // Merge the local data to it and store it.
350 ReadingListEntry* merged_entry = 339 ReadingListEntry* merged_entry =
351 delegate_->SyncMergeEntry(std::move(entry), read); 340 delegate_->SyncMergeEntry(std::move(entry));
352 341
353 // Write to the store. 342 // Write to the store.
354 std::unique_ptr<reading_list::ReadingListLocal> entry_local_pb = 343 std::unique_ptr<reading_list::ReadingListLocal> entry_local_pb =
355 merged_entry->AsReadingListLocal(read); 344 merged_entry->AsReadingListLocal();
356 batch_->WriteData(merged_entry->URL().spec(), 345 batch_->WriteData(merged_entry->URL().spec(),
357 entry_local_pb->SerializeAsString()); 346 entry_local_pb->SerializeAsString());
358 347
359 // Send to sync 348 // Send to sync
360 std::unique_ptr<sync_pb::ReadingListSpecifics> entry_sync_pb = 349 std::unique_ptr<sync_pb::ReadingListSpecifics> entry_sync_pb =
361 merged_entry->AsReadingListSpecifics(read); 350 merged_entry->AsReadingListSpecifics();
362 auto entity_data = base::MakeUnique<syncer::EntityData>(); 351 auto entity_data = base::MakeUnique<syncer::EntityData>();
363 *(entity_data->specifics.mutable_reading_list()) = *entry_sync_pb; 352 *(entity_data->specifics.mutable_reading_list()) = *entry_sync_pb;
364 entity_data->non_unique_name = entry_sync_pb->entry_id(); 353 entity_data->non_unique_name = entry_sync_pb->entry_id();
365 354
366 // TODO(crbug.com/666232): Investigate if there is a risk of sync 355 // TODO(crbug.com/666232): Investigate if there is a risk of sync
367 // ping-pong. 356 // ping-pong.
368 change_processor()->Put(entry_sync_pb->entry_id(), 357 change_processor()->Put(entry_sync_pb->entry_id(),
369 std::move(entity_data), 358 std::move(entity_data),
370 metadata_change_list.get()); 359 metadata_change_list.get());
371 360
372 } else { 361 } else {
373 // The entry from sync is out of date. 362 // The entry from sync is out of date.
374 // Send back the local more recent entry. 363 // Send back the local more recent entry.
375 // No need to update 364 // No need to update
376 std::unique_ptr<sync_pb::ReadingListSpecifics> entry_pb = 365 std::unique_ptr<sync_pb::ReadingListSpecifics> entry_pb =
377 existing_entry->AsReadingListSpecifics(was_read); 366 existing_entry->AsReadingListSpecifics();
378 auto entity_data = base::MakeUnique<syncer::EntityData>(); 367 auto entity_data = base::MakeUnique<syncer::EntityData>();
379 *(entity_data->specifics.mutable_reading_list()) = *entry_pb; 368 *(entity_data->specifics.mutable_reading_list()) = *entry_pb;
380 entity_data->non_unique_name = entry_pb->entry_id(); 369 entity_data->non_unique_name = entry_pb->entry_id();
381 370
382 change_processor()->Put(entry_pb->entry_id(), std::move(entity_data), 371 change_processor()->Put(entry_pb->entry_id(), std::move(entity_data),
383 metadata_change_list.get()); 372 metadata_change_list.get());
384 } 373 }
385 } 374 }
386 } 375 }
387 376
388 batch_->TransferMetadataChanges(std::move(metadata_change_list)); 377 batch_->TransferMetadataChanges(std::move(metadata_change_list));
389 return syncer::SyncError(); 378 return syncer::SyncError();
390 } 379 }
391 380
392 void ReadingListStore::GetData(StorageKeyList storage_keys, 381 void ReadingListStore::GetData(StorageKeyList storage_keys,
393 DataCallback callback) { 382 DataCallback callback) {
394 DCHECK(CalledOnValidThread()); 383 DCHECK(CalledOnValidThread());
395 auto batch = base::MakeUnique<syncer::MutableDataBatch>(); 384 auto batch = base::MakeUnique<syncer::MutableDataBatch>();
396 for (std::string url_string : storage_keys) { 385 for (std::string url_string : storage_keys) {
397 bool read; 386 const ReadingListEntry* entry = model_->GetEntryFromURL(GURL(url_string));
398 const ReadingListEntry* entry =
399 model_->GetEntryFromURL(GURL(url_string), &read);
400 if (entry) { 387 if (entry) {
401 AddEntryToBatch(batch.get(), *entry, read); 388 AddEntryToBatch(batch.get(), entry);
402 } 389 }
403 } 390 }
404 391
405 callback.Run(syncer::SyncError(), std::move(batch)); 392 callback.Run(syncer::SyncError(), std::move(batch));
406 } 393 }
407 394
408 void ReadingListStore::GetAllData(DataCallback callback) { 395 void ReadingListStore::GetAllData(DataCallback callback) {
409 DCHECK(CalledOnValidThread()); 396 DCHECK(CalledOnValidThread());
410 auto batch = base::MakeUnique<syncer::MutableDataBatch>(); 397 auto batch = base::MakeUnique<syncer::MutableDataBatch>();
411 int unread_count = model_->unread_size(); 398 int count = model_->size();
412 int read_count = model_->read_size(); 399 for (int index = 0; index < count; index++) {
413 for (int index = 0; index < unread_count + read_count; index++) { 400 const ReadingListEntry* entry = model_->GetEntryAt(index);
414 bool read = index >= unread_count; 401 AddEntryToBatch(batch.get(), entry);
415 const ReadingListEntry& entry =
416 read ? model_->GetReadEntryAtIndex(index - unread_count)
417 : model_->GetUnreadEntryAtIndex(index);
418 AddEntryToBatch(batch.get(), entry, read);
419 } 402 }
420 403
421 callback.Run(syncer::SyncError(), std::move(batch)); 404 callback.Run(syncer::SyncError(), std::move(batch));
422 } 405 }
423 406
424 void ReadingListStore::AddEntryToBatch(syncer::MutableDataBatch* batch, 407 void ReadingListStore::AddEntryToBatch(syncer::MutableDataBatch* batch,
425 const ReadingListEntry& entry, 408 const ReadingListEntry* entry) {
426 bool read) {
427 DCHECK(CalledOnValidThread()); 409 DCHECK(CalledOnValidThread());
428 std::unique_ptr<sync_pb::ReadingListSpecifics> entry_pb = 410 std::unique_ptr<sync_pb::ReadingListSpecifics> entry_pb =
429 entry.AsReadingListSpecifics(read); 411 entry->AsReadingListSpecifics();
430 412
431 std::unique_ptr<syncer::EntityData> entity_data(new syncer::EntityData()); 413 std::unique_ptr<syncer::EntityData> entity_data(new syncer::EntityData());
432 *(entity_data->specifics.mutable_reading_list()) = *entry_pb; 414 *(entity_data->specifics.mutable_reading_list()) = *entry_pb;
433 entity_data->non_unique_name = entry_pb->entry_id(); 415 entity_data->non_unique_name = entry_pb->entry_id();
434 416
435 batch->Put(entry_pb->entry_id(), std::move(entity_data)); 417 batch->Put(entry_pb->entry_id(), std::move(entity_data));
436 } 418 }
437 419
438 // Get or generate a client tag for |entity_data|. This must be the same tag 420 // Get or generate a client tag for |entity_data|. This must be the same tag
439 // that was/would have been generated in the SyncableService/Directory world 421 // that was/would have been generated in the SyncableService/Directory world
(...skipping 12 matching lines...) Expand all
452 // called once when first encountering a remote entity. Local changes will 434 // called once when first encountering a remote entity. Local changes will
453 // provide their storage keys directly to Put instead of using this method. 435 // provide their storage keys directly to Put instead of using this method.
454 // Theoretically this function doesn't need to be stable across multiple calls 436 // Theoretically this function doesn't need to be stable across multiple calls
455 // on the same or different clients, but to keep things simple, it probably 437 // on the same or different clients, but to keep things simple, it probably
456 // should be. 438 // should be.
457 std::string ReadingListStore::GetStorageKey( 439 std::string ReadingListStore::GetStorageKey(
458 const syncer::EntityData& entity_data) { 440 const syncer::EntityData& entity_data) {
459 DCHECK(CalledOnValidThread()); 441 DCHECK(CalledOnValidThread());
460 return entity_data.specifics.reading_list().entry_id(); 442 return entity_data.specifics.reading_list().entry_id();
461 } 443 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698