Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "ios/chrome/browser/reading_list/reading_list_store.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/logging.h" | |
| 9 #include "base/memory/ptr_util.h" | |
| 10 #include "components/sync/model/entity_change.h" | |
| 11 #include "components/sync/model/metadata_batch.h" | |
| 12 #include "components/sync/model/metadata_change_list.h" | |
| 13 #include "components/sync/model/model_type_change_processor.h" | |
| 14 #include "components/sync/model/mutable_data_batch.h" | |
| 15 #include "components/sync/model_impl/accumulating_metadata_change_list.h" | |
| 16 #include "components/sync/protocol/model_type_state.pb.h" | |
| 17 #include "ios/chrome/browser/reading_list/proto/reading_list.pb.h" | |
| 18 #include "ios/chrome/browser/reading_list/reading_list_model_impl.h" | |
| 19 #include "ios/web/public/web_thread.h" | |
| 20 | |
| 21 ReadingListStore::ReadingListStore( | |
| 22 StoreFactoryFunction create_store_callback, | |
| 23 const ChangeProcessorFactory& change_processor_factory) | |
| 24 : ModelTypeSyncBridge(change_processor_factory, syncer::READING_LIST), | |
| 25 create_store_callback_(create_store_callback), | |
| 26 pending_transaction_count_(0) {} | |
| 27 | |
| 28 ReadingListStore::~ReadingListStore() { | |
| 29 DCHECK(pending_transaction_count_ == 0); | |
| 30 } | |
| 31 | |
| 32 void ReadingListStore::SetReadingListModel(ReadingListModel* model, | |
| 33 ReadingListStoreDelegate* delegate) { | |
| 34 DCHECK(CalledOnValidThread()); | |
| 35 model_ = model; | |
| 36 delegate_ = delegate; | |
| 37 create_store_callback_.Run( | |
| 38 base::Bind(&ReadingListStore::OnStoreCreated, base::AsWeakPtr(this))); | |
| 39 } | |
| 40 | |
| 41 std::unique_ptr<ReadingListModelStorage::ScopedBatchUpdate> | |
| 42 ReadingListStore::EnsureBatchCreated() { | |
| 43 return base::WrapUnique<ReadingListModelStorage::ScopedBatchUpdate>( | |
| 44 new ScopedBatchUpdate(this)); | |
| 45 } | |
| 46 | |
| 47 ReadingListStore::ScopedBatchUpdate::ScopedBatchUpdate(ReadingListStore* store) | |
| 48 : store_(store) { | |
| 49 store_->BeginTransaction(); | |
| 50 } | |
| 51 | |
| 52 ReadingListStore::ScopedBatchUpdate::~ScopedBatchUpdate() { | |
| 53 store_->CommitTransaction(); | |
| 54 } | |
| 55 | |
| 56 void ReadingListStore::BeginTransaction() { | |
| 57 DCHECK(CalledOnValidThread()); | |
| 58 pending_transaction_count_++; | |
| 59 if (pending_transaction_count_ == 1) { | |
| 60 batch_ = store_->CreateWriteBatch(); | |
| 61 } | |
| 62 } | |
| 63 | |
| 64 void ReadingListStore::CommitTransaction() { | |
| 65 DCHECK(CalledOnValidThread()); | |
| 66 pending_transaction_count_--; | |
| 67 if (pending_transaction_count_ == 0) { | |
| 68 store_->CommitWriteBatch( | |
| 69 std::move(batch_), | |
| 70 base::Bind(&ReadingListStore::OnDatabaseSave, base::AsWeakPtr(this))); | |
| 71 batch_.reset(); | |
| 72 } | |
| 73 } | |
| 74 | |
| 75 void ReadingListStore::SaveEntry(const ReadingListEntry& entry, bool read) { | |
| 76 DCHECK(CalledOnValidThread()); | |
| 77 auto token = EnsureBatchCreated(); | |
| 78 | |
| 79 std::unique_ptr<reading_list::ReadingListLocal> pb_entry = | |
| 80 entry.AsReadingListLocal(read); | |
| 81 | |
| 82 batch_->WriteData(entry.URL().spec(), pb_entry->SerializeAsString()); | |
| 83 | |
| 84 if (!change_processor()->IsTrackingMetadata()) { | |
| 85 return; | |
| 86 } | |
| 87 std::unique_ptr<sync_pb::ReadingListSpecifics> pb_entry_sync = | |
| 88 entry.AsReadingListSpecifics(read); | |
| 89 | |
| 90 std::unique_ptr<syncer::MetadataChangeList> metadata_change_list = | |
| 91 CreateMetadataChangeList(); | |
| 92 | |
| 93 std::unique_ptr<syncer::EntityData> entity_data(new syncer::EntityData()); | |
| 94 *entity_data->specifics.mutable_reading_list() = *pb_entry_sync; | |
| 95 entity_data->non_unique_name = pb_entry_sync->entry_id(); | |
| 96 | |
| 97 change_processor()->Put(entry.URL().spec(), std::move(entity_data), | |
| 98 metadata_change_list.get()); | |
| 99 batch_->TransferMetadataChanges(std::move(metadata_change_list)); | |
| 100 } | |
| 101 | |
| 102 void ReadingListStore::RemoveEntry(const ReadingListEntry& entry) { | |
| 103 DCHECK(CalledOnValidThread()); | |
| 104 auto token = EnsureBatchCreated(); | |
| 105 | |
| 106 batch_->DeleteData(entry.URL().spec()); | |
| 107 if (!change_processor()->IsTrackingMetadata()) { | |
| 108 return; | |
| 109 } | |
| 110 std::unique_ptr<syncer::MetadataChangeList> metadata_change_list = | |
| 111 CreateMetadataChangeList(); | |
| 112 | |
| 113 change_processor()->Delete(entry.URL().spec(), metadata_change_list.get()); | |
| 114 batch_->TransferMetadataChanges(std::move(metadata_change_list)); | |
| 115 } | |
| 116 | |
| 117 void ReadingListStore::OnDatabaseLoad( | |
| 118 syncer::ModelTypeStore::Result result, | |
| 119 std::unique_ptr<syncer::ModelTypeStore::RecordList> entries) { | |
| 120 DCHECK(CalledOnValidThread()); | |
| 121 if (result != syncer::ModelTypeStore::Result::SUCCESS) { | |
| 122 change_processor()->OnMetadataLoaded( | |
| 123 change_processor()->CreateAndUploadError( | |
| 124 FROM_HERE, "Cannot load Reading List Database."), | |
| 125 nullptr); | |
| 126 return; | |
| 127 } | |
| 128 auto read = base::MakeUnique<ReadingListEntries>(); | |
| 129 auto unread = base::MakeUnique<ReadingListEntries>(); | |
| 130 | |
| 131 for (const syncer::ModelTypeStore::Record& r : *entries.get()) { | |
| 132 // for (const reading_list::ReadingListLocal& pb_entry : *entries) { | |
| 133 reading_list::ReadingListLocal proto; | |
| 134 if (!proto.ParseFromString(r.value)) { | |
| 135 continue; | |
| 136 // TODO(skym, crbug.com/582460): Handle unrecoverable initialization | |
| 137 // failure. | |
| 138 } | |
| 139 | |
| 140 std::unique_ptr<ReadingListEntry> entry( | |
| 141 ReadingListEntry::FromReadingListLocal(proto)); | |
| 142 if (!entry) { | |
| 143 continue; | |
| 144 } | |
| 145 if (proto.status() == reading_list::ReadingListLocal::READ) { | |
| 146 read->push_back(std::move(*entry)); | |
| 147 } else { | |
| 148 unread->push_back(std::move(*entry)); | |
| 149 } | |
| 150 } | |
| 151 | |
| 152 delegate_->StoreLoaded(std::move(unread), std::move(read)); | |
| 153 | |
| 154 store_->ReadAllMetadata( | |
| 155 base::Bind(&ReadingListStore::OnReadAllMetadata, base::AsWeakPtr(this))); | |
| 156 } | |
| 157 | |
| 158 void ReadingListStore::OnReadAllMetadata( | |
| 159 syncer::SyncError sync_error, | |
| 160 std::unique_ptr<syncer::MetadataBatch> metadata_batch) { | |
| 161 DCHECK(CalledOnValidThread()); | |
| 162 change_processor()->OnMetadataLoaded(sync_error, std::move(metadata_batch)); | |
| 163 } | |
| 164 | |
| 165 void ReadingListStore::OnDatabaseSave(syncer::ModelTypeStore::Result result) { | |
| 166 return; | |
| 167 } | |
| 168 | |
| 169 void ReadingListStore::OnStoreCreated( | |
| 170 syncer::ModelTypeStore::Result result, | |
| 171 std::unique_ptr<syncer::ModelTypeStore> store) { | |
| 172 DCHECK(CalledOnValidThread()); | |
| 173 if (result != syncer::ModelTypeStore::Result::SUCCESS) { | |
| 174 // TODO(crbug.com/664926): handle store creation error. | |
| 175 return; | |
| 176 } | |
| 177 store_ = std::move(store); | |
| 178 store_->ReadAllData( | |
| 179 base::Bind(&ReadingListStore::OnDatabaseLoad, base::AsWeakPtr(this))); | |
| 180 return; | |
| 181 } | |
| 182 | |
| 183 syncer::ModelTypeSyncBridge* ReadingListStore::GetModelTypeSyncBridge() { | |
| 184 return this; | |
| 185 } | |
| 186 | |
| 187 // Creates an object used to communicate changes in the sync metadata to the | |
| 188 // model type store. | |
| 189 std::unique_ptr<syncer::MetadataChangeList> | |
| 190 ReadingListStore::CreateMetadataChangeList() { | |
| 191 return syncer::ModelTypeStore::WriteBatch::CreateMetadataChangeList(); | |
| 192 } | |
| 193 | |
| 194 // Perform the initial merge between local and sync data. This should only be | |
| 195 // called when a data type is first enabled to start syncing, and there is no | |
| 196 // sync metadata. Best effort should be made to match local and sync data. The | |
| 197 // keys in the |entity_data_map| will have been created via GetClientTag(...), | |
| 198 // and if a local and sync data should match/merge but disagree on tags, the | |
| 199 // service should use the sync data's tag. Any local pieces of data that are | |
| 200 // not present in sync should immediately be Put(...) to the processor before | |
| 201 // returning. The same MetadataChangeList that was passed into this function | |
| 202 // can be passed to Put(...) calls. Delete(...) can also be called but should | |
| 203 // not be needed for most model types. Durable storage writes, if not able to | |
| 204 // combine all change atomically, should save the metadata after the data | |
| 205 // changes, so that this merge will be re-driven by sync if is not completely | |
| 206 // saved during the current run. | |
| 207 syncer::SyncError ReadingListStore::MergeSyncData( | |
| 208 std::unique_ptr<syncer::MetadataChangeList> metadata_change_list, | |
| 209 syncer::EntityDataMap entity_data_map) { | |
| 210 DCHECK(CalledOnValidThread()); | |
| 211 auto token = EnsureBatchCreated(); | |
| 212 // Keep track of the last update of each item. | |
| 213 std::map<std::string, int64_t> last_update; | |
| 214 std::unique_ptr<ReadingListModel::ScopedReadingListBatchUpdate> | |
| 215 model_batch_updates = model_->BeginBatchUpdates(); | |
| 216 | |
| 217 // Collect update timestamps from server entries. | |
| 218 for (const auto& kv : entity_data_map) { | |
| 219 const sync_pb::ReadingListSpecifics& specifics = | |
| 220 kv.second.value().specifics.reading_list(); | |
| 221 last_update[kv.first] = specifics.update_time_us(); | |
| 222 } | |
| 223 | |
| 224 // Merge sync to local data. | |
| 225 for (const auto& kv : entity_data_map) { | |
| 226 const sync_pb::ReadingListSpecifics& specifics = | |
| 227 kv.second.value().specifics.reading_list(); | |
| 228 // Deserialize entry. | |
| 229 bool read = specifics.status() == sync_pb::ReadingListSpecifics::READ; | |
| 230 std::unique_ptr<ReadingListEntry> entry( | |
| 231 ReadingListEntry::FromReadingListSpecifics(specifics)); | |
| 232 | |
| 233 // Convert to local store format and write to store. | |
| 234 std::unique_ptr<reading_list::ReadingListLocal> pb_entry = | |
| 235 entry->AsReadingListLocal(read); | |
| 236 batch_->WriteData(entry->URL().spec(), pb_entry->SerializeAsString()); | |
|
Olivier
2016/11/16 12:27:52
pb_entry does not contain the local info
| |
| 237 | |
| 238 // Notify model about updated entry. | |
| 239 delegate_->SyncAddEntry(std::move(entry), read); | |
| 240 } | |
| 241 | |
| 242 // Commit local only entries to server. | |
| 243 int unread_count = model_->unread_size(); | |
| 244 int read_count = model_->read_size(); | |
| 245 for (int index = 0; index < unread_count + read_count; index++) { | |
| 246 bool read = index >= unread_count; | |
| 247 const ReadingListEntry& entry = | |
| 248 read ? model_->GetReadEntryAtIndex(index - unread_count) | |
| 249 : model_->GetUnreadEntryAtIndex(index); | |
| 250 if (last_update.count(entry.URL().spec()) && | |
| 251 last_update[entry.URL().spec()] >= entry.UpdateTime()) { | |
| 252 // The synced entry is up to date. | |
| 253 continue; | |
| 254 } | |
| 255 | |
| 256 // Local entry has later timestamp. It should be committed to server. | |
| 257 std::unique_ptr<sync_pb::ReadingListSpecifics> entry_pb = | |
| 258 entry.AsReadingListSpecifics(read); | |
| 259 | |
| 260 auto entity_data = base::MakeUnique<syncer::EntityData>(); | |
| 261 *(entity_data->specifics.mutable_reading_list()) = *entry_pb; | |
| 262 entity_data->non_unique_name = entry_pb->entry_id(); | |
| 263 | |
| 264 change_processor()->Put(entry_pb->entry_id(), std::move(entity_data), | |
| 265 metadata_change_list.get()); | |
| 266 } | |
| 267 batch_->TransferMetadataChanges(std::move(metadata_change_list)); | |
| 268 | |
| 269 return syncer::SyncError(); | |
| 270 } | |
| 271 | |
| 272 // Apply changes from the sync server locally. | |
| 273 // Please note that |entity_changes| might have fewer entries than | |
| 274 // |metadata_change_list| in case when some of the data changes are filtered | |
| 275 // out, or even be empty in case when a commit confirmation is processed and | |
| 276 // only the metadata needs to persisted. | |
| 277 syncer::SyncError ReadingListStore::ApplySyncChanges( | |
| 278 std::unique_ptr<syncer::MetadataChangeList> metadata_change_list, | |
| 279 syncer::EntityChangeList entity_changes) { | |
| 280 DCHECK(CalledOnValidThread()); | |
| 281 std::unique_ptr<ReadingListModel::ScopedReadingListBatchUpdate> batch = | |
| 282 model_->BeginBatchUpdates(); | |
| 283 auto token = EnsureBatchCreated(); | |
| 284 | |
| 285 for (syncer::EntityChange& change : entity_changes) { | |
| 286 if (change.type() == syncer::EntityChange::ACTION_DELETE) { | |
| 287 batch_->DeleteData(change.storage_key()); | |
| 288 // Need to notify model that entry is deleted. | |
| 289 delegate_->SyncRemoveEntry(GURL(change.storage_key())); | |
|
Olivier
2016/11/16 12:27:52
Adding this compared to the gpaste.
| |
| 290 } else { | |
| 291 // Deserialize entry. | |
| 292 const sync_pb::ReadingListSpecifics& specifics = | |
| 293 change.data().specifics.reading_list(); | |
| 294 bool read = specifics.status() == sync_pb::ReadingListSpecifics::READ; | |
| 295 std::unique_ptr<ReadingListEntry> entry( | |
| 296 ReadingListEntry::FromReadingListSpecifics(specifics)); | |
| 297 | |
| 298 // Convert to local store format and write to store. | |
| 299 std::unique_ptr<reading_list::ReadingListLocal> pb_entry = | |
| 300 entry->AsReadingListLocal(read); | |
|
Olivier
2016/11/16 12:27:52
This is suspicious as pb_entry does not contain di
| |
| 301 batch_->WriteData(entry->URL().spec(), pb_entry->SerializeAsString()); | |
| 302 | |
| 303 // Notify model about updated entry. | |
| 304 delegate_->SyncAddEntry(std::move(entry), read); | |
| 305 } | |
| 306 } | |
| 307 | |
| 308 batch_->TransferMetadataChanges(std::move(metadata_change_list)); | |
| 309 return syncer::SyncError(); | |
| 310 } | |
| 311 | |
| 312 void ReadingListStore::GetData(StorageKeyList storage_keys, | |
| 313 DataCallback callback) { | |
| 314 DCHECK(CalledOnValidThread()); | |
| 315 auto batch = base::MakeUnique<syncer::MutableDataBatch>(); | |
| 316 for (std::string url_string : storage_keys) { | |
| 317 bool read; | |
| 318 const ReadingListEntry* entry = | |
| 319 model_->GetEntryFromURL(GURL(url_string), &read); | |
| 320 if (entry) { | |
| 321 AddEntryToBatch(batch.get(), *entry, read); | |
| 322 } | |
| 323 } | |
| 324 | |
| 325 callback.Run(syncer::SyncError(), std::move(batch)); | |
| 326 } | |
| 327 | |
| 328 void ReadingListStore::GetAllData(DataCallback callback) { | |
| 329 DCHECK(CalledOnValidThread()); | |
| 330 auto batch = base::MakeUnique<syncer::MutableDataBatch>(); | |
| 331 int unread_count = model_->unread_size(); | |
| 332 int read_count = model_->read_size(); | |
| 333 for (int index = 0; index < unread_count + read_count; index++) { | |
| 334 bool read = index >= unread_count; | |
| 335 const ReadingListEntry& entry = | |
| 336 read ? model_->GetReadEntryAtIndex(index - unread_count) | |
| 337 : model_->GetUnreadEntryAtIndex(index); | |
| 338 AddEntryToBatch(batch.get(), entry, read); | |
| 339 } | |
| 340 | |
| 341 callback.Run(syncer::SyncError(), std::move(batch)); | |
| 342 } | |
| 343 | |
| 344 void ReadingListStore::AddEntryToBatch(syncer::MutableDataBatch* batch, | |
| 345 const ReadingListEntry& entry, | |
| 346 bool read) { | |
| 347 DCHECK(CalledOnValidThread()); | |
| 348 std::unique_ptr<sync_pb::ReadingListSpecifics> entry_pb = | |
| 349 entry.AsReadingListSpecifics(read); | |
| 350 | |
| 351 std::unique_ptr<syncer::EntityData> entity_data(new syncer::EntityData()); | |
| 352 *(entity_data->specifics.mutable_reading_list()) = *entry_pb; | |
| 353 entity_data->non_unique_name = entry_pb->entry_id(); | |
| 354 | |
| 355 batch->Put(entry_pb->entry_id(), std::move(entity_data)); | |
| 356 } | |
| 357 | |
| 358 // Get or generate a client tag for |entity_data|. This must be the same tag | |
| 359 // that was/would have been generated in the SyncableService/Directory world | |
| 360 // for backward compatibility with pre-USS clients. The only time this | |
| 361 // theoretically needs to be called is on the creation of local data, however | |
| 362 // it is also used to verify the hash of remote data. If a data type was never | |
| 363 // launched pre-USS, then method does not need to be different from | |
| 364 // GetStorageKey(). | |
| 365 std::string ReadingListStore::GetClientTag( | |
| 366 const syncer::EntityData& entity_data) { | |
| 367 DCHECK(CalledOnValidThread()); | |
| 368 return GetStorageKey(entity_data); | |
| 369 } | |
| 370 | |
| 371 // Get or generate a storage key for |entity_data|. This will only ever be | |
| 372 // called once when first encountering a remote entity. Local changes will | |
| 373 // provide their storage keys directly to Put instead of using this method. | |
| 374 // Theoretically this function doesn't need to be stable across multiple calls | |
| 375 // on the same or different clients, but to keep things simple, it probably | |
| 376 // should be. | |
| 377 std::string ReadingListStore::GetStorageKey( | |
| 378 const syncer::EntityData& entity_data) { | |
| 379 DCHECK(CalledOnValidThread()); | |
| 380 return entity_data.specifics.reading_list().entry_id(); | |
| 381 } | |
| OLD | NEW |