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/core/reading_list_store.h" | 5 #include "components/reading_list/core/reading_list_store.h" |
6 | 6 |
7 #include <set> | 7 #include <set> |
8 #include <utility> | 8 #include <utility> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
(...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
188 // Creates an object used to communicate changes in the sync metadata to the | 188 // Creates an object used to communicate changes in the sync metadata to the |
189 // model type store. | 189 // model type store. |
190 std::unique_ptr<syncer::MetadataChangeList> | 190 std::unique_ptr<syncer::MetadataChangeList> |
191 ReadingListStore::CreateMetadataChangeList() { | 191 ReadingListStore::CreateMetadataChangeList() { |
192 return syncer::ModelTypeStore::WriteBatch::CreateMetadataChangeList(); | 192 return syncer::ModelTypeStore::WriteBatch::CreateMetadataChangeList(); |
193 } | 193 } |
194 | 194 |
195 // Perform the initial merge between local and sync data. This should only be | 195 // Perform the initial merge between local and sync data. This should only be |
196 // called when a data type is first enabled to start syncing, and there is no | 196 // called when a data type is first enabled to start syncing, and there is no |
197 // sync metadata. Best effort should be made to match local and sync data. The | 197 // sync metadata. Best effort should be made to match local and sync data. The |
198 // keys in the |entity_data_map| will have been created via GetClientTag(...), | 198 // storage keys in the |entity_data| are populated with GetStorageKey(...), |
199 // and if a local and sync data should match/merge but disagree on tags, the | 199 // local and sync copies of the same entity should resolve to the same storage |
200 // service should use the sync data's tag. Any local pieces of data that are | 200 // key. Any local pieces of data that are not present in sync should immediately |
201 // not present in sync should immediately be Put(...) to the processor before | 201 // be Put(...) to the processor before returning. The same MetadataChangeList |
202 // returning. The same MetadataChangeList that was passed into this function | 202 // that was passed into this function can be passed to Put(...) calls. |
203 // can be passed to Put(...) calls. Delete(...) can also be called but should | 203 // Delete(...) can also be called but should not be needed for most model types. |
204 // not be needed for most model types. Durable storage writes, if not able to | 204 // Durable storage writes, if not able to combine all change atomically, should |
205 // combine all change atomically, should save the metadata after the data | 205 // save the metadata after the data changes, so that this merge will be re- |
206 // changes, so that this merge will be re-driven by sync if is not completely | 206 // driven by sync if is not completely saved during the current run. |
207 // saved during the current run. | |
208 base::Optional<syncer::ModelError> ReadingListStore::MergeSyncData( | 207 base::Optional<syncer::ModelError> ReadingListStore::MergeSyncData( |
209 std::unique_ptr<syncer::MetadataChangeList> metadata_change_list, | 208 std::unique_ptr<syncer::MetadataChangeList> metadata_change_list, |
210 syncer::EntityDataMap entity_data_map) { | 209 syncer::EntityChangeList entity_data) { |
211 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); | 210 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
212 auto token = EnsureBatchCreated(); | 211 auto token = EnsureBatchCreated(); |
213 // Keep track of the last update of each item. | 212 // Keep track of the last update of each item. |
214 std::set<std::string> synced_entries; | 213 std::set<std::string> synced_entries; |
215 std::unique_ptr<ReadingListModel::ScopedReadingListBatchUpdate> | 214 std::unique_ptr<ReadingListModel::ScopedReadingListBatchUpdate> |
216 model_batch_updates = model_->BeginBatchUpdates(); | 215 model_batch_updates = model_->BeginBatchUpdates(); |
217 | 216 |
218 // Merge sync to local data. | 217 // Merge sync to local data. |
219 for (const auto& kv : entity_data_map) { | 218 for (const auto& change : entity_data) { |
220 synced_entries.insert(kv.first); | 219 synced_entries.insert(change.storage_key()); |
221 const sync_pb::ReadingListSpecifics& specifics = | 220 const sync_pb::ReadingListSpecifics& specifics = |
222 kv.second.value().specifics.reading_list(); | 221 change.data().specifics.reading_list(); |
223 // Deserialize entry. | 222 // Deserialize entry. |
224 std::unique_ptr<ReadingListEntry> entry( | 223 std::unique_ptr<ReadingListEntry> entry( |
225 ReadingListEntry::FromReadingListSpecifics(specifics, clock_->Now())); | 224 ReadingListEntry::FromReadingListSpecifics(specifics, clock_->Now())); |
226 | 225 |
227 const ReadingListEntry* existing_entry = | 226 const ReadingListEntry* existing_entry = |
228 model_->GetEntryByURL(entry->URL()); | 227 model_->GetEntryByURL(entry->URL()); |
229 | 228 |
230 if (!existing_entry) { | 229 if (!existing_entry) { |
231 // This entry is new. Add it to the store and model. | 230 // This entry is new. Add it to the store and model. |
232 // Convert to local store format and write to store. | 231 // Convert to local store format and write to store. |
(...skipping 223 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
456 if (rhs.first_read_time_us() == 0 && lhs.first_read_time_us() != 0) { | 455 if (rhs.first_read_time_us() == 0 && lhs.first_read_time_us() != 0) { |
457 return false; | 456 return false; |
458 } | 457 } |
459 if (rhs.first_read_time_us() > lhs.first_read_time_us() && | 458 if (rhs.first_read_time_us() > lhs.first_read_time_us() && |
460 lhs.first_read_time_us() != 0) { | 459 lhs.first_read_time_us() != 0) { |
461 return false; | 460 return false; |
462 } | 461 } |
463 } | 462 } |
464 return true; | 463 return true; |
465 } | 464 } |
OLD | NEW |