| 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 <set> |
| 8 #include <utility> |
| 9 |
| 7 #include "base/bind.h" | 10 #include "base/bind.h" |
| 8 #include "base/logging.h" | 11 #include "base/logging.h" |
| 9 #include "base/memory/ptr_util.h" | 12 #include "base/memory/ptr_util.h" |
| 10 #include "components/reading_list/ios/proto/reading_list.pb.h" | 13 #include "components/reading_list/ios/proto/reading_list.pb.h" |
| 11 #include "components/reading_list/ios/reading_list_model_impl.h" | 14 #include "components/reading_list/ios/reading_list_model_impl.h" |
| 12 #include "components/sync/model/entity_change.h" | 15 #include "components/sync/model/entity_change.h" |
| 13 #include "components/sync/model/metadata_batch.h" | 16 #include "components/sync/model/metadata_batch.h" |
| 14 #include "components/sync/model/metadata_change_list.h" | 17 #include "components/sync/model/metadata_change_list.h" |
| 15 #include "components/sync/model/model_type_change_processor.h" | 18 #include "components/sync/model/model_type_change_processor.h" |
| 16 #include "components/sync/model/mutable_data_batch.h" | 19 #include "components/sync/model/mutable_data_batch.h" |
| 17 #include "components/sync/model_impl/accumulating_metadata_change_list.h" | 20 #include "components/sync/model_impl/accumulating_metadata_change_list.h" |
| 18 #include "components/sync/protocol/model_type_state.pb.h" | 21 #include "components/sync/protocol/model_type_state.pb.h" |
| 19 | 22 |
| 20 ReadingListStore::ReadingListStore( | 23 ReadingListStore::ReadingListStore( |
| 21 StoreFactoryFunction create_store_callback, | 24 StoreFactoryFunction create_store_callback, |
| 22 const ChangeProcessorFactory& change_processor_factory) | 25 const ChangeProcessorFactory& change_processor_factory) |
| 23 : ReadingListModelStorage(change_processor_factory, syncer::READING_LIST), | 26 : ReadingListModelStorage(change_processor_factory, syncer::READING_LIST), |
| 24 create_store_callback_(create_store_callback), | 27 create_store_callback_(create_store_callback), |
| 25 pending_transaction_count_(0) {} | 28 pending_transaction_count_(0) {} |
| 26 | 29 |
| 27 ReadingListStore::~ReadingListStore() { | 30 ReadingListStore::~ReadingListStore() { |
| 28 DCHECK(pending_transaction_count_ == 0); | 31 DCHECK_EQ(0, pending_transaction_count_); |
| 29 } | 32 } |
| 30 | 33 |
| 31 void ReadingListStore::SetReadingListModel(ReadingListModel* model, | 34 void ReadingListStore::SetReadingListModel(ReadingListModel* model, |
| 32 ReadingListStoreDelegate* delegate) { | 35 ReadingListStoreDelegate* delegate) { |
| 33 DCHECK(CalledOnValidThread()); | 36 DCHECK(CalledOnValidThread()); |
| 34 model_ = model; | 37 model_ = model; |
| 35 delegate_ = delegate; | 38 delegate_ = delegate; |
| 36 create_store_callback_.Run( | 39 create_store_callback_.Run( |
| 37 base::Bind(&ReadingListStore::OnStoreCreated, base::AsWeakPtr(this))); | 40 base::Bind(&ReadingListStore::OnStoreCreated, base::AsWeakPtr(this))); |
| 38 } | 41 } |
| (...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 149 base::Bind(&ReadingListStore::OnReadAllMetadata, base::AsWeakPtr(this))); | 152 base::Bind(&ReadingListStore::OnReadAllMetadata, base::AsWeakPtr(this))); |
| 150 } | 153 } |
| 151 | 154 |
| 152 void ReadingListStore::OnReadAllMetadata( | 155 void ReadingListStore::OnReadAllMetadata( |
| 153 base::Optional<syncer::ModelError> error, | 156 base::Optional<syncer::ModelError> error, |
| 154 std::unique_ptr<syncer::MetadataBatch> metadata_batch) { | 157 std::unique_ptr<syncer::MetadataBatch> metadata_batch) { |
| 155 DCHECK(CalledOnValidThread()); | 158 DCHECK(CalledOnValidThread()); |
| 156 if (error) { | 159 if (error) { |
| 157 change_processor()->ReportError(FROM_HERE, "Failed to read metadata."); | 160 change_processor()->ReportError(FROM_HERE, "Failed to read metadata."); |
| 158 } else { | 161 } else { |
| 159 change_processor()->OnMetadataLoaded(std::move(metadata_batch)); | 162 change_processor()->ModelReadyToSync(std::move(metadata_batch)); |
| 160 } | 163 } |
| 161 } | 164 } |
| 162 | 165 |
| 163 void ReadingListStore::OnDatabaseSave(syncer::ModelTypeStore::Result result) { | 166 void ReadingListStore::OnDatabaseSave(syncer::ModelTypeStore::Result result) { |
| 164 return; | 167 return; |
| 165 } | 168 } |
| 166 | 169 |
| 167 void ReadingListStore::OnStoreCreated( | 170 void ReadingListStore::OnStoreCreated( |
| 168 syncer::ModelTypeStore::Result result, | 171 syncer::ModelTypeStore::Result result, |
| 169 std::unique_ptr<syncer::ModelTypeStore> store) { | 172 std::unique_ptr<syncer::ModelTypeStore> store) { |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 245 merged_entry->AsReadingListSpecifics(); | 248 merged_entry->AsReadingListSpecifics(); |
| 246 DCHECK(CompareEntriesForSync(specifics, *entry_sync_pb)); | 249 DCHECK(CompareEntriesForSync(specifics, *entry_sync_pb)); |
| 247 auto entity_data = base::MakeUnique<syncer::EntityData>(); | 250 auto entity_data = base::MakeUnique<syncer::EntityData>(); |
| 248 *(entity_data->specifics.mutable_reading_list()) = *entry_sync_pb; | 251 *(entity_data->specifics.mutable_reading_list()) = *entry_sync_pb; |
| 249 entity_data->non_unique_name = entry_sync_pb->entry_id(); | 252 entity_data->non_unique_name = entry_sync_pb->entry_id(); |
| 250 | 253 |
| 251 // 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 |
| 252 // ping-pong. | 255 // ping-pong. |
| 253 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), |
| 254 metadata_change_list.get()); | 257 metadata_change_list.get()); |
| 255 | |
| 256 } | 258 } |
| 257 } | 259 } |
| 258 | 260 |
| 259 // Commit local only entries to server. | 261 // Commit local only entries to server. |
| 260 for (const auto& url : model_->Keys()) { | 262 for (const auto& url : model_->Keys()) { |
| 261 const ReadingListEntry* entry = model_->GetEntryByURL(url); | 263 const ReadingListEntry* entry = model_->GetEntryByURL(url); |
| 262 if (synced_entries.count(url.spec())) { | 264 if (synced_entries.count(url.spec())) { |
| 263 // Entry already exists and has been merged above. | 265 // Entry already exists and has been merged above. |
| 264 continue; | 266 continue; |
| 265 } | 267 } |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 334 DCHECK(CompareEntriesForSync(specifics, *entry_sync_pb)); | 336 DCHECK(CompareEntriesForSync(specifics, *entry_sync_pb)); |
| 335 auto entity_data = base::MakeUnique<syncer::EntityData>(); | 337 auto entity_data = base::MakeUnique<syncer::EntityData>(); |
| 336 *(entity_data->specifics.mutable_reading_list()) = *entry_sync_pb; | 338 *(entity_data->specifics.mutable_reading_list()) = *entry_sync_pb; |
| 337 entity_data->non_unique_name = entry_sync_pb->entry_id(); | 339 entity_data->non_unique_name = entry_sync_pb->entry_id(); |
| 338 | 340 |
| 339 // TODO(crbug.com/666232): Investigate if there is a risk of sync | 341 // TODO(crbug.com/666232): Investigate if there is a risk of sync |
| 340 // ping-pong. | 342 // ping-pong. |
| 341 change_processor()->Put(entry_sync_pb->entry_id(), | 343 change_processor()->Put(entry_sync_pb->entry_id(), |
| 342 std::move(entity_data), | 344 std::move(entity_data), |
| 343 metadata_change_list.get()); | 345 metadata_change_list.get()); |
| 344 | |
| 345 } | 346 } |
| 346 } | 347 } |
| 347 } | 348 } |
| 348 | 349 |
| 349 batch_->TransferMetadataChanges(std::move(metadata_change_list)); | 350 batch_->TransferMetadataChanges(std::move(metadata_change_list)); |
| 350 return {}; | 351 return {}; |
| 351 } | 352 } |
| 352 | 353 |
| 353 void ReadingListStore::GetData(StorageKeyList storage_keys, | 354 void ReadingListStore::GetData(StorageKeyList storage_keys, |
| 354 DataCallback callback) { | 355 DataCallback callback) { |
| (...skipping 96 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) { | 452 if (rhs.first_read_time_us() == 0 && lhs.first_read_time_us() != 0) { |
| 452 return false; | 453 return false; |
| 453 } | 454 } |
| 454 if (rhs.first_read_time_us() > lhs.first_read_time_us() && | 455 if (rhs.first_read_time_us() > lhs.first_read_time_us() && |
| 455 lhs.first_read_time_us() != 0) { | 456 lhs.first_read_time_us() != 0) { |
| 456 return false; | 457 return false; |
| 457 } | 458 } |
| 458 } | 459 } |
| 459 return true; | 460 return true; |
| 460 } | 461 } |
| OLD | NEW |