| 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 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 143 loaded_entries->insert(std::make_pair(url, std::move(*entry))); | 143 loaded_entries->insert(std::make_pair(url, std::move(*entry))); |
| 144 } | 144 } |
| 145 | 145 |
| 146 delegate_->StoreLoaded(std::move(loaded_entries)); | 146 delegate_->StoreLoaded(std::move(loaded_entries)); |
| 147 | 147 |
| 148 store_->ReadAllMetadata( | 148 store_->ReadAllMetadata( |
| 149 base::Bind(&ReadingListStore::OnReadAllMetadata, base::AsWeakPtr(this))); | 149 base::Bind(&ReadingListStore::OnReadAllMetadata, base::AsWeakPtr(this))); |
| 150 } | 150 } |
| 151 | 151 |
| 152 void ReadingListStore::OnReadAllMetadata( | 152 void ReadingListStore::OnReadAllMetadata( |
| 153 syncer::ModelError error, | 153 base::Optional<syncer::ModelError> error, |
| 154 std::unique_ptr<syncer::MetadataBatch> metadata_batch) { | 154 std::unique_ptr<syncer::MetadataBatch> metadata_batch) { |
| 155 DCHECK(CalledOnValidThread()); | 155 DCHECK(CalledOnValidThread()); |
| 156 if (error.IsSet()) { | 156 if (error) { |
| 157 change_processor()->ReportError(FROM_HERE, "Failed to read metadata."); | 157 change_processor()->ReportError(FROM_HERE, "Failed to read metadata."); |
| 158 } else { | 158 } else { |
| 159 change_processor()->OnMetadataLoaded(std::move(metadata_batch)); | 159 change_processor()->OnMetadataLoaded(std::move(metadata_batch)); |
| 160 } | 160 } |
| 161 } | 161 } |
| 162 | 162 |
| 163 void ReadingListStore::OnDatabaseSave(syncer::ModelTypeStore::Result result) { | 163 void ReadingListStore::OnDatabaseSave(syncer::ModelTypeStore::Result result) { |
| 164 return; | 164 return; |
| 165 } | 165 } |
| 166 | 166 |
| (...skipping 24 matching lines...) Expand all Loading... |
| 191 // 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(...), |
| 192 // 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 |
| 193 // 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 |
| 194 // 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 |
| 195 // returning. The same MetadataChangeList that was passed into this function | 195 // returning. The same MetadataChangeList that was passed into this function |
| 196 // 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 |
| 197 // 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 |
| 198 // combine all change atomically, should save the metadata after the data | 198 // combine all change atomically, should save the metadata after the data |
| 199 // 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 |
| 200 // saved during the current run. | 200 // saved during the current run. |
| 201 syncer::ModelError ReadingListStore::MergeSyncData( | 201 base::Optional<syncer::ModelError> ReadingListStore::MergeSyncData( |
| 202 std::unique_ptr<syncer::MetadataChangeList> metadata_change_list, | 202 std::unique_ptr<syncer::MetadataChangeList> metadata_change_list, |
| 203 syncer::EntityDataMap entity_data_map) { | 203 syncer::EntityDataMap entity_data_map) { |
| 204 DCHECK(CalledOnValidThread()); | 204 DCHECK(CalledOnValidThread()); |
| 205 auto token = EnsureBatchCreated(); | 205 auto token = EnsureBatchCreated(); |
| 206 // Keep track of the last update of each item. | 206 // Keep track of the last update of each item. |
| 207 std::set<std::string> synced_entries; | 207 std::set<std::string> synced_entries; |
| 208 std::unique_ptr<ReadingListModel::ScopedReadingListBatchUpdate> | 208 std::unique_ptr<ReadingListModel::ScopedReadingListBatchUpdate> |
| 209 model_batch_updates = model_->BeginBatchUpdates(); | 209 model_batch_updates = model_->BeginBatchUpdates(); |
| 210 | 210 |
| 211 // Merge sync to local data. | 211 // Merge sync to local data. |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 270 | 270 |
| 271 auto entity_data = base::MakeUnique<syncer::EntityData>(); | 271 auto entity_data = base::MakeUnique<syncer::EntityData>(); |
| 272 *(entity_data->specifics.mutable_reading_list()) = *entry_pb; | 272 *(entity_data->specifics.mutable_reading_list()) = *entry_pb; |
| 273 entity_data->non_unique_name = entry_pb->entry_id(); | 273 entity_data->non_unique_name = entry_pb->entry_id(); |
| 274 | 274 |
| 275 change_processor()->Put(entry_pb->entry_id(), std::move(entity_data), | 275 change_processor()->Put(entry_pb->entry_id(), std::move(entity_data), |
| 276 metadata_change_list.get()); | 276 metadata_change_list.get()); |
| 277 } | 277 } |
| 278 batch_->TransferMetadataChanges(std::move(metadata_change_list)); | 278 batch_->TransferMetadataChanges(std::move(metadata_change_list)); |
| 279 | 279 |
| 280 return syncer::ModelError(); | 280 return {}; |
| 281 } | 281 } |
| 282 | 282 |
| 283 // Apply changes from the sync server locally. | 283 // Apply changes from the sync server locally. |
| 284 // Please note that |entity_changes| might have fewer entries than | 284 // Please note that |entity_changes| might have fewer entries than |
| 285 // |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 |
| 286 // 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 |
| 287 // only the metadata needs to persisted. | 287 // only the metadata needs to persisted. |
| 288 syncer::ModelError ReadingListStore::ApplySyncChanges( | 288 base::Optional<syncer::ModelError> ReadingListStore::ApplySyncChanges( |
| 289 std::unique_ptr<syncer::MetadataChangeList> metadata_change_list, | 289 std::unique_ptr<syncer::MetadataChangeList> metadata_change_list, |
| 290 syncer::EntityChangeList entity_changes) { | 290 syncer::EntityChangeList entity_changes) { |
| 291 DCHECK(CalledOnValidThread()); | 291 DCHECK(CalledOnValidThread()); |
| 292 std::unique_ptr<ReadingListModel::ScopedReadingListBatchUpdate> batch = | 292 std::unique_ptr<ReadingListModel::ScopedReadingListBatchUpdate> batch = |
| 293 model_->BeginBatchUpdates(); | 293 model_->BeginBatchUpdates(); |
| 294 auto token = EnsureBatchCreated(); | 294 auto token = EnsureBatchCreated(); |
| 295 | 295 |
| 296 for (syncer::EntityChange& change : entity_changes) { | 296 for (syncer::EntityChange& change : entity_changes) { |
| 297 if (change.type() == syncer::EntityChange::ACTION_DELETE) { | 297 if (change.type() == syncer::EntityChange::ACTION_DELETE) { |
| 298 batch_->DeleteData(change.storage_key()); | 298 batch_->DeleteData(change.storage_key()); |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 340 // ping-pong. | 340 // ping-pong. |
| 341 change_processor()->Put(entry_sync_pb->entry_id(), | 341 change_processor()->Put(entry_sync_pb->entry_id(), |
| 342 std::move(entity_data), | 342 std::move(entity_data), |
| 343 metadata_change_list.get()); | 343 metadata_change_list.get()); |
| 344 | 344 |
| 345 } | 345 } |
| 346 } | 346 } |
| 347 } | 347 } |
| 348 | 348 |
| 349 batch_->TransferMetadataChanges(std::move(metadata_change_list)); | 349 batch_->TransferMetadataChanges(std::move(metadata_change_list)); |
| 350 return syncer::ModelError(); | 350 return {}; |
| 351 } | 351 } |
| 352 | 352 |
| 353 void ReadingListStore::GetData(StorageKeyList storage_keys, | 353 void ReadingListStore::GetData(StorageKeyList storage_keys, |
| 354 DataCallback callback) { | 354 DataCallback callback) { |
| 355 DCHECK(CalledOnValidThread()); | 355 DCHECK(CalledOnValidThread()); |
| 356 auto batch = base::MakeUnique<syncer::MutableDataBatch>(); | 356 auto batch = base::MakeUnique<syncer::MutableDataBatch>(); |
| 357 for (const std::string& url_string : storage_keys) { | 357 for (const std::string& url_string : storage_keys) { |
| 358 const ReadingListEntry* entry = model_->GetEntryByURL(GURL(url_string)); | 358 const ReadingListEntry* entry = model_->GetEntryByURL(GURL(url_string)); |
| 359 if (entry) { | 359 if (entry) { |
| 360 AddEntryToBatch(batch.get(), *entry); | 360 AddEntryToBatch(batch.get(), *entry); |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 451 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) { |
| 452 return false; | 452 return false; |
| 453 } | 453 } |
| 454 if (rhs.first_read_time_us() > lhs.first_read_time_us() && | 454 if (rhs.first_read_time_us() > lhs.first_read_time_us() && |
| 455 lhs.first_read_time_us() != 0) { | 455 lhs.first_read_time_us() != 0) { |
| 456 return false; | 456 return false; |
| 457 } | 457 } |
| 458 } | 458 } |
| 459 return true; | 459 return true; |
| 460 } | 460 } |
| OLD | NEW |