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::set<std::string> synced_entries; | |
214 std::unique_ptr<ReadingListModel::ScopedReadingListBatchUpdate> | |
215 model_batch_updates = model_->BeginBatchUpdates(); | |
216 | |
217 // Merge sync to local data. | |
218 for (const auto& kv : entity_data_map) { | |
219 synced_entries.insert(kv.first); | |
220 const sync_pb::ReadingListSpecifics& specifics = | |
221 kv.second.value().specifics.reading_list(); | |
222 // Deserialize entry. | |
223 bool read = specifics.status() == sync_pb::ReadingListSpecifics::READ; | |
224 std::unique_ptr<ReadingListEntry> entry( | |
225 ReadingListEntry::FromReadingListSpecifics(specifics)); | |
226 | |
227 bool was_read; | |
228 const ReadingListEntry* existing_entry = | |
229 model_->GetEntryFromURL(entry->URL(), &was_read); | |
230 | |
231 if (!existing_entry) { | |
232 // This entry is new. Add it to the store and model. | |
233 // Convert to local store format and write to store. | |
234 std::unique_ptr<reading_list::ReadingListLocal> entry_pb = | |
235 entry->AsReadingListLocal(read); | |
236 batch_->WriteData(entry->URL().spec(), entry_pb->SerializeAsString()); | |
237 | |
238 // Notify model about updated entry. | |
239 delegate_->SyncAddEntry(std::move(entry), read); | |
240 } else if (existing_entry->UpdateTime() < entry->UpdateTime()) { | |
241 // The entry from sync is more recent. | |
242 // Merge the local data to it and store it. | |
243 ReadingListEntry* merged_entry = | |
244 delegate_->SyncMergeEntry(std::move(entry), read); | |
245 | |
246 // Write to the store. | |
247 std::unique_ptr<reading_list::ReadingListLocal> entry_local_pb = | |
248 merged_entry->AsReadingListLocal(read); | |
249 batch_->WriteData(entry->URL().spec(), | |
250 entry_local_pb->SerializeAsString()); | |
251 | |
252 // Send to sync | |
253 std::unique_ptr<sync_pb::ReadingListSpecifics> entry_sync_pb = | |
254 merged_entry->AsReadingListSpecifics(read); | |
255 auto entity_data = base::MakeUnique<syncer::EntityData>(); | |
256 *(entity_data->specifics.mutable_reading_list()) = *entry_sync_pb; | |
257 entity_data->non_unique_name = entry_sync_pb->entry_id(); | |
258 | |
259 change_processor()->Put(entry_sync_pb->entry_id(), std::move(entity_data), | |
260 metadata_change_list.get()); | |
261 | |
262 } else { | |
263 // The entry from sync is out of date. | |
264 // Send back the local more recent entry. | |
265 // No need to update | |
266 std::unique_ptr<sync_pb::ReadingListSpecifics> entry_pb = | |
267 existing_entry->AsReadingListSpecifics(was_read); | |
268 auto entity_data = base::MakeUnique<syncer::EntityData>(); | |
269 *(entity_data->specifics.mutable_reading_list()) = *entry_pb; | |
270 entity_data->non_unique_name = entry_pb->entry_id(); | |
271 | |
272 change_processor()->Put(entry_pb->entry_id(), std::move(entity_data), | |
273 metadata_change_list.get()); | |
274 } | |
275 } | |
276 | |
277 // Commit local only entries to server. | |
278 int unread_count = model_->unread_size(); | |
279 int read_count = model_->read_size(); | |
280 for (int index = 0; index < unread_count + read_count; index++) { | |
281 bool read = index >= unread_count; | |
282 const ReadingListEntry& entry = | |
283 read ? model_->GetReadEntryAtIndex(index - unread_count) | |
284 : model_->GetUnreadEntryAtIndex(index); | |
285 if (synced_entries.count(entry.URL().spec())) { | |
286 // Entry already exists and has been merged above. | |
287 continue; | |
288 } | |
289 | |
290 // Local entry has later timestamp. It should be committed to server. | |
291 std::unique_ptr<sync_pb::ReadingListSpecifics> entry_pb = | |
292 entry.AsReadingListSpecifics(read); | |
293 | |
294 auto entity_data = base::MakeUnique<syncer::EntityData>(); | |
295 *(entity_data->specifics.mutable_reading_list()) = *entry_pb; | |
296 entity_data->non_unique_name = entry_pb->entry_id(); | |
297 | |
298 change_processor()->Put(entry_pb->entry_id(), std::move(entity_data), | |
299 metadata_change_list.get()); | |
300 } | |
301 batch_->TransferMetadataChanges(std::move(metadata_change_list)); | |
302 | |
303 return syncer::SyncError(); | |
304 } | |
305 | |
306 // Apply changes from the sync server locally. | |
307 // Please note that |entity_changes| might have fewer entries than | |
308 // |metadata_change_list| in case when some of the data changes are filtered | |
309 // out, or even be empty in case when a commit confirmation is processed and | |
310 // only the metadata needs to persisted. | |
311 syncer::SyncError ReadingListStore::ApplySyncChanges( | |
312 std::unique_ptr<syncer::MetadataChangeList> metadata_change_list, | |
313 syncer::EntityChangeList entity_changes) { | |
314 DCHECK(CalledOnValidThread()); | |
315 std::unique_ptr<ReadingListModel::ScopedReadingListBatchUpdate> batch = | |
316 model_->BeginBatchUpdates(); | |
317 auto token = EnsureBatchCreated(); | |
318 | |
319 for (syncer::EntityChange& change : entity_changes) { | |
320 if (change.type() == syncer::EntityChange::ACTION_DELETE) { | |
321 batch_->DeleteData(change.storage_key()); | |
322 // Need to notify model that entry is deleted. | |
323 delegate_->SyncRemoveEntry(GURL(change.storage_key())); | |
324 } else { | |
325 // Deserialize entry. | |
326 const sync_pb::ReadingListSpecifics& specifics = | |
327 change.data().specifics.reading_list(); | |
328 bool read = specifics.status() == sync_pb::ReadingListSpecifics::READ; | |
329 std::unique_ptr<ReadingListEntry> entry( | |
330 ReadingListEntry::FromReadingListSpecifics(specifics)); | |
331 | |
332 bool was_read; | |
333 const ReadingListEntry* existing_entry = | |
334 model_->GetEntryFromURL(entry->URL(), &was_read); | |
335 | |
336 if (!existing_entry) { | |
337 // This entry is new. Add it to the store and model. | |
338 // Convert to local store format and write to store. | |
339 std::unique_ptr<reading_list::ReadingListLocal> entry_pb = | |
340 entry->AsReadingListLocal(read); | |
341 batch_->WriteData(entry->URL().spec(), entry_pb->SerializeAsString()); | |
342 | |
343 // Notify model about updated entry. | |
344 delegate_->SyncAddEntry(std::move(entry), read); | |
345 } else if (existing_entry->UpdateTime() < entry->UpdateTime()) { | |
346 // The entry from sync is more recent. | |
347 // Merge the local data to it and store it. | |
348 ReadingListEntry* merged_entry = | |
349 delegate_->SyncMergeEntry(std::move(entry), read); | |
350 | |
351 // Write to the store. | |
352 std::unique_ptr<reading_list::ReadingListLocal> entry_local_pb = | |
353 merged_entry->AsReadingListLocal(read); | |
354 batch_->WriteData(merged_entry->URL().spec(), | |
355 entry_local_pb->SerializeAsString()); | |
356 | |
357 // Send to sync | |
358 std::unique_ptr<sync_pb::ReadingListSpecifics> entry_sync_pb = | |
359 merged_entry->AsReadingListSpecifics(read); | |
360 auto entity_data = base::MakeUnique<syncer::EntityData>(); | |
361 *(entity_data->specifics.mutable_reading_list()) = *entry_sync_pb; | |
362 entity_data->non_unique_name = entry_sync_pb->entry_id(); | |
363 | |
364 change_processor()->Put(entry_sync_pb->entry_id(), | |
pavely
2016/11/17 06:39:23
Important consideration about passing accepted syn
Olivier
2016/11/17 08:54:34
Added a todo
| |
365 std::move(entity_data), | |
366 metadata_change_list.get()); | |
367 | |
368 } else { | |
369 // The entry from sync is out of date. | |
370 // Send back the local more recent entry. | |
371 // No need to update | |
372 std::unique_ptr<sync_pb::ReadingListSpecifics> entry_pb = | |
373 existing_entry->AsReadingListSpecifics(was_read); | |
374 auto entity_data = base::MakeUnique<syncer::EntityData>(); | |
375 *(entity_data->specifics.mutable_reading_list()) = *entry_pb; | |
376 entity_data->non_unique_name = entry_pb->entry_id(); | |
377 | |
378 change_processor()->Put(entry_pb->entry_id(), std::move(entity_data), | |
379 metadata_change_list.get()); | |
380 } | |
381 } | |
382 } | |
383 | |
384 batch_->TransferMetadataChanges(std::move(metadata_change_list)); | |
385 return syncer::SyncError(); | |
386 } | |
387 | |
388 void ReadingListStore::GetData(StorageKeyList storage_keys, | |
389 DataCallback callback) { | |
390 DCHECK(CalledOnValidThread()); | |
391 auto batch = base::MakeUnique<syncer::MutableDataBatch>(); | |
392 for (std::string url_string : storage_keys) { | |
393 bool read; | |
394 const ReadingListEntry* entry = | |
395 model_->GetEntryFromURL(GURL(url_string), &read); | |
396 if (entry) { | |
397 AddEntryToBatch(batch.get(), *entry, read); | |
398 } | |
399 } | |
400 | |
401 callback.Run(syncer::SyncError(), std::move(batch)); | |
402 } | |
403 | |
404 void ReadingListStore::GetAllData(DataCallback callback) { | |
405 DCHECK(CalledOnValidThread()); | |
406 auto batch = base::MakeUnique<syncer::MutableDataBatch>(); | |
407 int unread_count = model_->unread_size(); | |
408 int read_count = model_->read_size(); | |
409 for (int index = 0; index < unread_count + read_count; index++) { | |
410 bool read = index >= unread_count; | |
411 const ReadingListEntry& entry = | |
412 read ? model_->GetReadEntryAtIndex(index - unread_count) | |
413 : model_->GetUnreadEntryAtIndex(index); | |
414 AddEntryToBatch(batch.get(), entry, read); | |
415 } | |
416 | |
417 callback.Run(syncer::SyncError(), std::move(batch)); | |
418 } | |
419 | |
420 void ReadingListStore::AddEntryToBatch(syncer::MutableDataBatch* batch, | |
421 const ReadingListEntry& entry, | |
422 bool read) { | |
423 DCHECK(CalledOnValidThread()); | |
424 std::unique_ptr<sync_pb::ReadingListSpecifics> entry_pb = | |
425 entry.AsReadingListSpecifics(read); | |
426 | |
427 std::unique_ptr<syncer::EntityData> entity_data(new syncer::EntityData()); | |
428 *(entity_data->specifics.mutable_reading_list()) = *entry_pb; | |
429 entity_data->non_unique_name = entry_pb->entry_id(); | |
430 | |
431 batch->Put(entry_pb->entry_id(), std::move(entity_data)); | |
432 } | |
433 | |
434 // Get or generate a client tag for |entity_data|. This must be the same tag | |
435 // that was/would have been generated in the SyncableService/Directory world | |
436 // for backward compatibility with pre-USS clients. The only time this | |
437 // theoretically needs to be called is on the creation of local data, however | |
438 // it is also used to verify the hash of remote data. If a data type was never | |
439 // launched pre-USS, then method does not need to be different from | |
440 // GetStorageKey(). | |
441 std::string ReadingListStore::GetClientTag( | |
442 const syncer::EntityData& entity_data) { | |
443 DCHECK(CalledOnValidThread()); | |
444 return GetStorageKey(entity_data); | |
445 } | |
446 | |
447 // Get or generate a storage key for |entity_data|. This will only ever be | |
448 // called once when first encountering a remote entity. Local changes will | |
449 // provide their storage keys directly to Put instead of using this method. | |
450 // Theoretically this function doesn't need to be stable across multiple calls | |
451 // on the same or different clients, but to keep things simple, it probably | |
452 // should be. | |
453 std::string ReadingListStore::GetStorageKey( | |
454 const syncer::EntityData& entity_data) { | |
455 DCHECK(CalledOnValidThread()); | |
456 return entity_data.specifics.reading_list().entry_id(); | |
457 } | |
OLD | NEW |