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 | |
pavely
2016/11/16 01:54:53
I reshuffled implementation of MergeSyncData and A
| |
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 // Do a first pass to update local metadata. | |
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 auto entity_data = base::MakeUnique<syncer::EntityData>(); | |
223 *(entity_data->specifics.mutable_reading_list()) = specifics; | |
224 entity_data->non_unique_name = kv.first; | |
225 // Update metadata of the entry. | |
226 change_processor()->Put(kv.first, std::move(entity_data), | |
227 metadata_change_list.get()); | |
228 } | |
229 | |
230 int unread_count = model_->unread_size(); | |
231 int read_count = model_->read_size(); | |
232 for (int index = 0; index < unread_count + read_count; index++) { | |
233 bool read = index >= unread_count; | |
234 const ReadingListEntry& entry = | |
235 read ? model_->GetReadEntryAtIndex(index - unread_count) | |
236 : model_->GetUnreadEntryAtIndex(index); | |
237 if (last_update.count(entry.URL().spec()) && | |
238 last_update[entry.URL().spec()] >= entry.UpdateTime()) { | |
239 // The synced entry is up to date. | |
240 continue; | |
241 } | |
242 std::unique_ptr<sync_pb::ReadingListSpecifics> entry_pb = | |
243 entry.AsReadingListSpecifics(read); | |
244 | |
245 auto entity_data = base::MakeUnique<syncer::EntityData>(); | |
246 *(entity_data->specifics.mutable_reading_list()) = *entry_pb; | |
247 entity_data->non_unique_name = entry_pb->entry_id(); | |
248 | |
249 change_processor()->Put(entry_pb->entry_id(), std::move(entity_data), | |
250 metadata_change_list.get()); | |
251 } | |
252 batch_->TransferMetadataChanges(std::move(metadata_change_list)); | |
253 | |
254 // Merge sync to local data. | |
255 for (const auto& kv : entity_data_map) { | |
256 const sync_pb::ReadingListSpecifics& specifics = | |
257 kv.second.value().specifics.reading_list(); | |
258 std::unique_ptr<ReadingListEntry> entry( | |
259 ReadingListEntry::FromReadingListSpecifics(specifics)); | |
260 DCHECK(entry->URL().spec() == kv.first); | |
261 DCHECK(specifics.entry_id() == kv.first); | |
262 | |
263 delegate_->SyncAddEntry( | |
264 std::move(entry), | |
265 specifics.status() == sync_pb::ReadingListSpecifics::READ); | |
266 } | |
267 | |
268 return syncer::SyncError(); | |
269 } | |
270 | |
271 // Apply changes from the sync server locally. | |
272 // Please note that |entity_changes| might have fewer entries than | |
273 // |metadata_change_list| in case when some of the data changes are filtered | |
274 // out, or even be empty in case when a commit confirmation is processed and | |
275 // only the metadata needs to persisted. | |
276 syncer::SyncError ReadingListStore::ApplySyncChanges( | |
277 std::unique_ptr<syncer::MetadataChangeList> metadata_change_list, | |
278 syncer::EntityChangeList entity_changes) { | |
279 DCHECK(CalledOnValidThread()); | |
280 std::unique_ptr<ReadingListModel::ScopedReadingListBatchUpdate> batch = | |
281 model_->BeginBatchUpdates(); | |
282 auto token = EnsureBatchCreated(); | |
283 | |
284 // Do a first pass to update metadata. | |
285 for (syncer::EntityChange& change : entity_changes) { | |
286 if (change.type() == syncer::EntityChange::ACTION_DELETE) { | |
287 change_processor()->Delete(change.storage_key(), | |
288 metadata_change_list.get()); | |
289 } else { | |
290 const sync_pb::ReadingListSpecifics& specifics = | |
291 change.data().specifics.reading_list(); | |
292 auto entity_data = base::MakeUnique<syncer::EntityData>(); | |
293 *(entity_data->specifics.mutable_reading_list()) = specifics; | |
294 entity_data->non_unique_name = change.storage_key(); | |
295 // Update metadata of the entry. | |
296 change_processor()->Put(change.storage_key(), std::move(entity_data), | |
297 metadata_change_list.get()); | |
298 } | |
299 } | |
300 | |
301 batch_->TransferMetadataChanges(std::move(metadata_change_list)); | |
302 | |
303 // Do a second pass to update model. | |
304 for (syncer::EntityChange& change : entity_changes) { | |
305 if (change.type() == syncer::EntityChange::ACTION_DELETE) { | |
306 delegate_->SyncRemoveEntry(GURL(change.storage_key())); | |
307 continue; | |
308 } | |
309 const sync_pb::ReadingListSpecifics& specifics = | |
310 change.data().specifics.reading_list(); | |
311 std::unique_ptr<ReadingListEntry> entry( | |
312 ReadingListEntry::FromReadingListSpecifics(specifics)); | |
313 delegate_->SyncAddEntry( | |
314 std::move(entry), | |
315 specifics.status() == sync_pb::ReadingListSpecifics::READ); | |
316 } | |
317 | |
318 return syncer::SyncError(); | |
319 } | |
320 | |
321 void ReadingListStore::GetData(StorageKeyList storage_keys, | |
322 DataCallback callback) { | |
323 DCHECK(CalledOnValidThread()); | |
324 auto batch = base::MakeUnique<syncer::MutableDataBatch>(); | |
325 for (std::string url_string : storage_keys) { | |
326 bool read; | |
327 const ReadingListEntry* entry = | |
328 model_->GetEntryFromURL(GURL(url_string), &read); | |
329 if (entry) { | |
330 AddEntryToBatch(batch.get(), *entry, read); | |
331 } | |
332 } | |
333 | |
334 callback.Run(syncer::SyncError(), std::move(batch)); | |
335 } | |
336 | |
337 void ReadingListStore::GetAllData(DataCallback callback) { | |
338 DCHECK(CalledOnValidThread()); | |
339 auto batch = base::MakeUnique<syncer::MutableDataBatch>(); | |
340 int unread_count = model_->unread_size(); | |
341 int read_count = model_->read_size(); | |
342 for (int index = 0; index < unread_count + read_count; index++) { | |
343 bool read = index >= unread_count; | |
344 const ReadingListEntry& entry = | |
345 read ? model_->GetReadEntryAtIndex(index - unread_count) | |
346 : model_->GetUnreadEntryAtIndex(index); | |
347 AddEntryToBatch(batch.get(), entry, read); | |
348 } | |
349 | |
350 callback.Run(syncer::SyncError(), std::move(batch)); | |
351 } | |
352 | |
353 void ReadingListStore::AddEntryToBatch(syncer::MutableDataBatch* batch, | |
354 const ReadingListEntry& entry, | |
355 bool read) { | |
356 DCHECK(CalledOnValidThread()); | |
357 std::unique_ptr<sync_pb::ReadingListSpecifics> entry_pb = | |
358 entry.AsReadingListSpecifics(read); | |
359 | |
360 std::unique_ptr<syncer::EntityData> entity_data(new syncer::EntityData()); | |
361 *(entity_data->specifics.mutable_reading_list()) = *entry_pb; | |
362 entity_data->non_unique_name = entry_pb->entry_id(); | |
363 | |
364 batch->Put(entry_pb->entry_id(), std::move(entity_data)); | |
365 } | |
366 | |
367 // Get or generate a client tag for |entity_data|. This must be the same tag | |
368 // that was/would have been generated in the SyncableService/Directory world | |
369 // for backward compatibility with pre-USS clients. The only time this | |
370 // theoretically needs to be called is on the creation of local data, however | |
371 // it is also used to verify the hash of remote data. If a data type was never | |
372 // launched pre-USS, then method does not need to be different from | |
373 // GetStorageKey(). | |
374 std::string ReadingListStore::GetClientTag( | |
375 const syncer::EntityData& entity_data) { | |
376 DCHECK(CalledOnValidThread()); | |
377 return GetStorageKey(entity_data); | |
378 } | |
379 | |
380 // Get or generate a storage key for |entity_data|. This will only ever be | |
381 // called once when first encountering a remote entity. Local changes will | |
382 // provide their storage keys directly to Put instead of using this method. | |
383 // Theoretically this function doesn't need to be stable across multiple calls | |
384 // on the same or different clients, but to keep things simple, it probably | |
385 // should be. | |
386 std::string ReadingListStore::GetStorageKey( | |
387 const syncer::EntityData& entity_data) { | |
388 DCHECK(CalledOnValidThread()); | |
389 return entity_data.specifics.reading_list().entry_id(); | |
390 } | |
OLD | NEW |