| 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 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 111 | 111 |
| 112 change_processor()->Delete(entry.URL().spec(), metadata_change_list.get()); | 112 change_processor()->Delete(entry.URL().spec(), metadata_change_list.get()); |
| 113 batch_->TransferMetadataChanges(std::move(metadata_change_list)); | 113 batch_->TransferMetadataChanges(std::move(metadata_change_list)); |
| 114 } | 114 } |
| 115 | 115 |
| 116 void ReadingListStore::OnDatabaseLoad( | 116 void ReadingListStore::OnDatabaseLoad( |
| 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()->ReportError(FROM_HERE, |
| 122 change_processor()->CreateAndUploadError( | 122 "Cannot load Reading List Database."); |
| 123 FROM_HERE, "Cannot load Reading List Database."), | |
| 124 nullptr); | |
| 125 return; | 123 return; |
| 126 } | 124 } |
| 127 auto loaded_entries = | 125 auto loaded_entries = |
| 128 base::MakeUnique<ReadingListStoreDelegate::ReadingListEntries>(); | 126 base::MakeUnique<ReadingListStoreDelegate::ReadingListEntries>(); |
| 129 | 127 |
| 130 for (const syncer::ModelTypeStore::Record& r : *entries.get()) { | 128 for (const syncer::ModelTypeStore::Record& r : *entries.get()) { |
| 131 reading_list::ReadingListLocal proto; | 129 reading_list::ReadingListLocal proto; |
| 132 if (!proto.ParseFromString(r.value)) { | 130 if (!proto.ParseFromString(r.value)) { |
| 133 continue; | 131 continue; |
| 134 // TODO(skym, crbug.com/582460): Handle unrecoverable initialization | 132 // TODO(skym, crbug.com/582460): Handle unrecoverable initialization |
| (...skipping 10 matching lines...) Expand all Loading... |
| 145 loaded_entries->insert(std::make_pair(url, std::move(*entry))); | 143 loaded_entries->insert(std::make_pair(url, std::move(*entry))); |
| 146 } | 144 } |
| 147 | 145 |
| 148 delegate_->StoreLoaded(std::move(loaded_entries)); | 146 delegate_->StoreLoaded(std::move(loaded_entries)); |
| 149 | 147 |
| 150 store_->ReadAllMetadata( | 148 store_->ReadAllMetadata( |
| 151 base::Bind(&ReadingListStore::OnReadAllMetadata, base::AsWeakPtr(this))); | 149 base::Bind(&ReadingListStore::OnReadAllMetadata, base::AsWeakPtr(this))); |
| 152 } | 150 } |
| 153 | 151 |
| 154 void ReadingListStore::OnReadAllMetadata( | 152 void ReadingListStore::OnReadAllMetadata( |
| 155 syncer::SyncError sync_error, | 153 syncer::ModelError error, |
| 156 std::unique_ptr<syncer::MetadataBatch> metadata_batch) { | 154 std::unique_ptr<syncer::MetadataBatch> metadata_batch) { |
| 157 DCHECK(CalledOnValidThread()); | 155 DCHECK(CalledOnValidThread()); |
| 158 change_processor()->OnMetadataLoaded(sync_error, std::move(metadata_batch)); | 156 if (error.IsSet()) { |
| 157 change_processor()->ReportError(FROM_HERE, "Failed to read metadata."); |
| 158 } else { |
| 159 change_processor()->OnMetadataLoaded(std::move(metadata_batch)); |
| 160 } |
| 159 } | 161 } |
| 160 | 162 |
| 161 void ReadingListStore::OnDatabaseSave(syncer::ModelTypeStore::Result result) { | 163 void ReadingListStore::OnDatabaseSave(syncer::ModelTypeStore::Result result) { |
| 162 return; | 164 return; |
| 163 } | 165 } |
| 164 | 166 |
| 165 void ReadingListStore::OnStoreCreated( | 167 void ReadingListStore::OnStoreCreated( |
| 166 syncer::ModelTypeStore::Result result, | 168 syncer::ModelTypeStore::Result result, |
| 167 std::unique_ptr<syncer::ModelTypeStore> store) { | 169 std::unique_ptr<syncer::ModelTypeStore> store) { |
| 168 DCHECK(CalledOnValidThread()); | 170 DCHECK(CalledOnValidThread()); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 189 // keys in the |entity_data_map| will have been created via GetClientTag(...), | 191 // keys in the |entity_data_map| will have been created via GetClientTag(...), |
| 190 // and if a local and sync data should match/merge but disagree on tags, the | 192 // and if a local and sync data should match/merge but disagree on tags, the |
| 191 // service should use the sync data's tag. Any local pieces of data that are | 193 // service should use the sync data's tag. Any local pieces of data that are |
| 192 // not present in sync should immediately be Put(...) to the processor before | 194 // not present in sync should immediately be Put(...) to the processor before |
| 193 // returning. The same MetadataChangeList that was passed into this function | 195 // returning. The same MetadataChangeList that was passed into this function |
| 194 // can be passed to Put(...) calls. Delete(...) can also be called but should | 196 // can be passed to Put(...) calls. Delete(...) can also be called but should |
| 195 // not be needed for most model types. Durable storage writes, if not able to | 197 // not be needed for most model types. Durable storage writes, if not able to |
| 196 // combine all change atomically, should save the metadata after the data | 198 // combine all change atomically, should save the metadata after the data |
| 197 // changes, so that this merge will be re-driven by sync if is not completely | 199 // changes, so that this merge will be re-driven by sync if is not completely |
| 198 // saved during the current run. | 200 // saved during the current run. |
| 199 syncer::SyncError ReadingListStore::MergeSyncData( | 201 syncer::ModelError ReadingListStore::MergeSyncData( |
| 200 std::unique_ptr<syncer::MetadataChangeList> metadata_change_list, | 202 std::unique_ptr<syncer::MetadataChangeList> metadata_change_list, |
| 201 syncer::EntityDataMap entity_data_map) { | 203 syncer::EntityDataMap entity_data_map) { |
| 202 DCHECK(CalledOnValidThread()); | 204 DCHECK(CalledOnValidThread()); |
| 203 auto token = EnsureBatchCreated(); | 205 auto token = EnsureBatchCreated(); |
| 204 // Keep track of the last update of each item. | 206 // Keep track of the last update of each item. |
| 205 std::set<std::string> synced_entries; | 207 std::set<std::string> synced_entries; |
| 206 std::unique_ptr<ReadingListModel::ScopedReadingListBatchUpdate> | 208 std::unique_ptr<ReadingListModel::ScopedReadingListBatchUpdate> |
| 207 model_batch_updates = model_->BeginBatchUpdates(); | 209 model_batch_updates = model_->BeginBatchUpdates(); |
| 208 | 210 |
| 209 // Merge sync to local data. | 211 // Merge sync to local data. |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 268 | 270 |
| 269 auto entity_data = base::MakeUnique<syncer::EntityData>(); | 271 auto entity_data = base::MakeUnique<syncer::EntityData>(); |
| 270 *(entity_data->specifics.mutable_reading_list()) = *entry_pb; | 272 *(entity_data->specifics.mutable_reading_list()) = *entry_pb; |
| 271 entity_data->non_unique_name = entry_pb->entry_id(); | 273 entity_data->non_unique_name = entry_pb->entry_id(); |
| 272 | 274 |
| 273 change_processor()->Put(entry_pb->entry_id(), std::move(entity_data), | 275 change_processor()->Put(entry_pb->entry_id(), std::move(entity_data), |
| 274 metadata_change_list.get()); | 276 metadata_change_list.get()); |
| 275 } | 277 } |
| 276 batch_->TransferMetadataChanges(std::move(metadata_change_list)); | 278 batch_->TransferMetadataChanges(std::move(metadata_change_list)); |
| 277 | 279 |
| 278 return syncer::SyncError(); | 280 return syncer::ModelError(); |
| 279 } | 281 } |
| 280 | 282 |
| 281 // Apply changes from the sync server locally. | 283 // Apply changes from the sync server locally. |
| 282 // Please note that |entity_changes| might have fewer entries than | 284 // Please note that |entity_changes| might have fewer entries than |
| 283 // |metadata_change_list| in case when some of the data changes are filtered | 285 // |metadata_change_list| in case when some of the data changes are filtered |
| 284 // out, or even be empty in case when a commit confirmation is processed and | 286 // out, or even be empty in case when a commit confirmation is processed and |
| 285 // only the metadata needs to persisted. | 287 // only the metadata needs to persisted. |
| 286 syncer::SyncError ReadingListStore::ApplySyncChanges( | 288 syncer::ModelError ReadingListStore::ApplySyncChanges( |
| 287 std::unique_ptr<syncer::MetadataChangeList> metadata_change_list, | 289 std::unique_ptr<syncer::MetadataChangeList> metadata_change_list, |
| 288 syncer::EntityChangeList entity_changes) { | 290 syncer::EntityChangeList entity_changes) { |
| 289 DCHECK(CalledOnValidThread()); | 291 DCHECK(CalledOnValidThread()); |
| 290 std::unique_ptr<ReadingListModel::ScopedReadingListBatchUpdate> batch = | 292 std::unique_ptr<ReadingListModel::ScopedReadingListBatchUpdate> batch = |
| 291 model_->BeginBatchUpdates(); | 293 model_->BeginBatchUpdates(); |
| 292 auto token = EnsureBatchCreated(); | 294 auto token = EnsureBatchCreated(); |
| 293 | 295 |
| 294 for (syncer::EntityChange& change : entity_changes) { | 296 for (syncer::EntityChange& change : entity_changes) { |
| 295 if (change.type() == syncer::EntityChange::ACTION_DELETE) { | 297 if (change.type() == syncer::EntityChange::ACTION_DELETE) { |
| 296 batch_->DeleteData(change.storage_key()); | 298 batch_->DeleteData(change.storage_key()); |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 338 // ping-pong. | 340 // ping-pong. |
| 339 change_processor()->Put(entry_sync_pb->entry_id(), | 341 change_processor()->Put(entry_sync_pb->entry_id(), |
| 340 std::move(entity_data), | 342 std::move(entity_data), |
| 341 metadata_change_list.get()); | 343 metadata_change_list.get()); |
| 342 | 344 |
| 343 } | 345 } |
| 344 } | 346 } |
| 345 } | 347 } |
| 346 | 348 |
| 347 batch_->TransferMetadataChanges(std::move(metadata_change_list)); | 349 batch_->TransferMetadataChanges(std::move(metadata_change_list)); |
| 348 return syncer::SyncError(); | 350 return syncer::ModelError(); |
| 349 } | 351 } |
| 350 | 352 |
| 351 void ReadingListStore::GetData(StorageKeyList storage_keys, | 353 void ReadingListStore::GetData(StorageKeyList storage_keys, |
| 352 DataCallback callback) { | 354 DataCallback callback) { |
| 353 DCHECK(CalledOnValidThread()); | 355 DCHECK(CalledOnValidThread()); |
| 354 auto batch = base::MakeUnique<syncer::MutableDataBatch>(); | 356 auto batch = base::MakeUnique<syncer::MutableDataBatch>(); |
| 355 for (const std::string& url_string : storage_keys) { | 357 for (const std::string& url_string : storage_keys) { |
| 356 const ReadingListEntry* entry = model_->GetEntryByURL(GURL(url_string)); | 358 const ReadingListEntry* entry = model_->GetEntryByURL(GURL(url_string)); |
| 357 if (entry) { | 359 if (entry) { |
| 358 AddEntryToBatch(batch.get(), *entry); | 360 AddEntryToBatch(batch.get(), *entry); |
| 359 } | 361 } |
| 360 } | 362 } |
| 361 | 363 |
| 362 callback.Run(syncer::SyncError(), std::move(batch)); | 364 callback.Run(std::move(batch)); |
| 363 } | 365 } |
| 364 | 366 |
| 365 void ReadingListStore::GetAllData(DataCallback callback) { | 367 void ReadingListStore::GetAllData(DataCallback callback) { |
| 366 DCHECK(CalledOnValidThread()); | 368 DCHECK(CalledOnValidThread()); |
| 367 auto batch = base::MakeUnique<syncer::MutableDataBatch>(); | 369 auto batch = base::MakeUnique<syncer::MutableDataBatch>(); |
| 368 | 370 |
| 369 for (const auto& url : model_->Keys()) { | 371 for (const auto& url : model_->Keys()) { |
| 370 const ReadingListEntry* entry = model_->GetEntryByURL(GURL(url)); | 372 const ReadingListEntry* entry = model_->GetEntryByURL(GURL(url)); |
| 371 AddEntryToBatch(batch.get(), *entry); | 373 AddEntryToBatch(batch.get(), *entry); |
| 372 } | 374 } |
| 373 | 375 |
| 374 callback.Run(syncer::SyncError(), std::move(batch)); | 376 callback.Run(std::move(batch)); |
| 375 } | 377 } |
| 376 | 378 |
| 377 void ReadingListStore::AddEntryToBatch(syncer::MutableDataBatch* batch, | 379 void ReadingListStore::AddEntryToBatch(syncer::MutableDataBatch* batch, |
| 378 const ReadingListEntry& entry) { | 380 const ReadingListEntry& entry) { |
| 379 DCHECK(CalledOnValidThread()); | 381 DCHECK(CalledOnValidThread()); |
| 380 std::unique_ptr<sync_pb::ReadingListSpecifics> entry_pb = | 382 std::unique_ptr<sync_pb::ReadingListSpecifics> entry_pb = |
| 381 entry.AsReadingListSpecifics(); | 383 entry.AsReadingListSpecifics(); |
| 382 | 384 |
| 383 std::unique_ptr<syncer::EntityData> entity_data(new syncer::EntityData()); | 385 std::unique_ptr<syncer::EntityData> entity_data(new syncer::EntityData()); |
| 384 *(entity_data->specifics.mutable_reading_list()) = *entry_pb; | 386 *(entity_data->specifics.mutable_reading_list()) = *entry_pb; |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 449 if (rhs.first_read_time_us() == 0 && lhs.first_read_time_us() != 0) { | 451 if (rhs.first_read_time_us() == 0 && lhs.first_read_time_us() != 0) { |
| 450 return false; | 452 return false; |
| 451 } | 453 } |
| 452 if (rhs.first_read_time_us() > lhs.first_read_time_us() && | 454 if (rhs.first_read_time_us() > lhs.first_read_time_us() && |
| 453 lhs.first_read_time_us() != 0) { | 455 lhs.first_read_time_us() != 0) { |
| 454 return false; | 456 return false; |
| 455 } | 457 } |
| 456 } | 458 } |
| 457 return true; | 459 return true; |
| 458 } | 460 } |
| OLD | NEW |