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