Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(62)

Side by Side Diff: components/reading_list/ios/reading_list_store.cc

Issue 2553143002: Create a strict order in ReadingListSpecifics (Closed)
Patch Set: order by field Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 "base/bind.h" 7 #include "base/bind.h"
8 #include "base/logging.h" 8 #include "base/logging.h"
9 #include "base/memory/ptr_util.h" 9 #include "base/memory/ptr_util.h"
10 #include "components/reading_list/ios/proto/reading_list.pb.h" 10 #include "components/reading_list/ios/proto/reading_list.pb.h"
(...skipping 213 matching lines...) Expand 10 before | Expand all | Expand 10 after
224 224
225 if (!existing_entry) { 225 if (!existing_entry) {
226 // This entry is new. Add it to the store and model. 226 // This entry is new. Add it to the store and model.
227 // Convert to local store format and write to store. 227 // Convert to local store format and write to store.
228 std::unique_ptr<reading_list::ReadingListLocal> entry_pb = 228 std::unique_ptr<reading_list::ReadingListLocal> entry_pb =
229 entry->AsReadingListLocal(); 229 entry->AsReadingListLocal();
230 batch_->WriteData(entry->URL().spec(), entry_pb->SerializeAsString()); 230 batch_->WriteData(entry->URL().spec(), entry_pb->SerializeAsString());
231 231
232 // Notify model about updated entry. 232 // Notify model about updated entry.
233 delegate_->SyncAddEntry(std::move(entry)); 233 delegate_->SyncAddEntry(std::move(entry));
234 } else if (existing_entry->UpdateTime() < entry->UpdateTime()) { 234 } else {
235 // The entry from sync is more recent. 235 // Merge the local data and the sync data and store the result.
236 // Merge the local data to it and store it.
237 ReadingListEntry* merged_entry = 236 ReadingListEntry* merged_entry =
238 delegate_->SyncMergeEntry(std::move(entry)); 237 delegate_->SyncMergeEntry(std::move(entry));
239 238
240 // Write to the store. 239 // Write to the store.
241 std::unique_ptr<reading_list::ReadingListLocal> entry_local_pb = 240 std::unique_ptr<reading_list::ReadingListLocal> entry_local_pb =
242 merged_entry->AsReadingListLocal(); 241 merged_entry->AsReadingListLocal();
243 batch_->WriteData(entry->URL().spec(), 242 batch_->WriteData(entry->URL().spec(),
244 entry_local_pb->SerializeAsString()); 243 entry_local_pb->SerializeAsString());
245 244
246 // Send to sync 245 // Send to sync
247 std::unique_ptr<sync_pb::ReadingListSpecifics> entry_sync_pb = 246 std::unique_ptr<sync_pb::ReadingListSpecifics> entry_sync_pb =
248 merged_entry->AsReadingListSpecifics(); 247 merged_entry->AsReadingListSpecifics();
248 DCHECK(CompareEntriesForSync(specifics, *entry_sync_pb));
249 auto entity_data = base::MakeUnique<syncer::EntityData>(); 249 auto entity_data = base::MakeUnique<syncer::EntityData>();
250 *(entity_data->specifics.mutable_reading_list()) = *entry_sync_pb; 250 *(entity_data->specifics.mutable_reading_list()) = *entry_sync_pb;
251 entity_data->non_unique_name = entry_sync_pb->entry_id(); 251 entity_data->non_unique_name = entry_sync_pb->entry_id();
252 252
253 // TODO(crbug.com/666232): Investigate if there is a risk of sync 253 // TODO(crbug.com/666232): Investigate if there is a risk of sync
254 // ping-pong. 254 // ping-pong.
255 change_processor()->Put(entry_sync_pb->entry_id(), std::move(entity_data), 255 change_processor()->Put(entry_sync_pb->entry_id(), std::move(entity_data),
256 metadata_change_list.get()); 256 metadata_change_list.get());
257 257
258 } else {
259 // The entry from sync is out of date.
260 // Send back the local more recent entry.
261 // No need to update
262 std::unique_ptr<sync_pb::ReadingListSpecifics> entry_pb =
263 existing_entry->AsReadingListSpecifics();
264 auto entity_data = base::MakeUnique<syncer::EntityData>();
265 *(entity_data->specifics.mutable_reading_list()) = *entry_pb;
266 entity_data->non_unique_name = entry_pb->entry_id();
267
268 change_processor()->Put(entry_pb->entry_id(), std::move(entity_data),
269 metadata_change_list.get());
270 } 258 }
271 } 259 }
272 260
273 // Commit local only entries to server. 261 // Commit local only entries to server.
274 for (const auto& url : model_->Keys()) { 262 for (const auto& url : model_->Keys()) {
275 const ReadingListEntry* entry = model_->GetEntryByURL(url); 263 const ReadingListEntry* entry = model_->GetEntryByURL(url);
276 if (synced_entries.count(url.spec())) { 264 if (synced_entries.count(url.spec())) {
277 // Entry already exists and has been merged above. 265 // Entry already exists and has been merged above.
278 continue; 266 continue;
279 } 267 }
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
324 312
325 if (!existing_entry) { 313 if (!existing_entry) {
326 // This entry is new. Add it to the store and model. 314 // This entry is new. Add it to the store and model.
327 // Convert to local store format and write to store. 315 // Convert to local store format and write to store.
328 std::unique_ptr<reading_list::ReadingListLocal> entry_pb = 316 std::unique_ptr<reading_list::ReadingListLocal> entry_pb =
329 entry->AsReadingListLocal(); 317 entry->AsReadingListLocal();
330 batch_->WriteData(entry->URL().spec(), entry_pb->SerializeAsString()); 318 batch_->WriteData(entry->URL().spec(), entry_pb->SerializeAsString());
331 319
332 // Notify model about updated entry. 320 // Notify model about updated entry.
333 delegate_->SyncAddEntry(std::move(entry)); 321 delegate_->SyncAddEntry(std::move(entry));
334 } else if (existing_entry->UpdateTime() < entry->UpdateTime()) { 322 } else {
335 // The entry from sync is more recent. 323 // Merge the local data and the sync data and store the result.
336 // Merge the local data to it and store it.
337 ReadingListEntry* merged_entry = 324 ReadingListEntry* merged_entry =
338 delegate_->SyncMergeEntry(std::move(entry)); 325 delegate_->SyncMergeEntry(std::move(entry));
339 326
340 // Write to the store. 327 // Write to the store.
341 std::unique_ptr<reading_list::ReadingListLocal> entry_local_pb = 328 std::unique_ptr<reading_list::ReadingListLocal> entry_local_pb =
342 merged_entry->AsReadingListLocal(); 329 merged_entry->AsReadingListLocal();
343 batch_->WriteData(merged_entry->URL().spec(), 330 batch_->WriteData(merged_entry->URL().spec(),
344 entry_local_pb->SerializeAsString()); 331 entry_local_pb->SerializeAsString());
345 332
346 // Send to sync 333 // Send to sync
347 std::unique_ptr<sync_pb::ReadingListSpecifics> entry_sync_pb = 334 std::unique_ptr<sync_pb::ReadingListSpecifics> entry_sync_pb =
348 merged_entry->AsReadingListSpecifics(); 335 merged_entry->AsReadingListSpecifics();
336 DCHECK(CompareEntriesForSync(specifics, *entry_sync_pb));
349 auto entity_data = base::MakeUnique<syncer::EntityData>(); 337 auto entity_data = base::MakeUnique<syncer::EntityData>();
350 *(entity_data->specifics.mutable_reading_list()) = *entry_sync_pb; 338 *(entity_data->specifics.mutable_reading_list()) = *entry_sync_pb;
351 entity_data->non_unique_name = entry_sync_pb->entry_id(); 339 entity_data->non_unique_name = entry_sync_pb->entry_id();
352 340
353 // 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
354 // ping-pong. 342 // ping-pong.
355 change_processor()->Put(entry_sync_pb->entry_id(), 343 change_processor()->Put(entry_sync_pb->entry_id(),
356 std::move(entity_data), 344 std::move(entity_data),
357 metadata_change_list.get()); 345 metadata_change_list.get());
358 346
359 } else {
360 // The entry from sync is out of date.
361 // Send back the local more recent entry.
362 // No need to update
363 std::unique_ptr<sync_pb::ReadingListSpecifics> entry_pb =
364 existing_entry->AsReadingListSpecifics();
365 auto entity_data = base::MakeUnique<syncer::EntityData>();
366 *(entity_data->specifics.mutable_reading_list()) = *entry_pb;
367 entity_data->non_unique_name = entry_pb->entry_id();
368
369 change_processor()->Put(entry_pb->entry_id(), std::move(entity_data),
370 metadata_change_list.get());
371 } 347 }
372 } 348 }
373 } 349 }
374 350
375 batch_->TransferMetadataChanges(std::move(metadata_change_list)); 351 batch_->TransferMetadataChanges(std::move(metadata_change_list));
376 return syncer::SyncError(); 352 return syncer::SyncError();
377 } 353 }
378 354
379 void ReadingListStore::GetData(StorageKeyList storage_keys, 355 void ReadingListStore::GetData(StorageKeyList storage_keys,
380 DataCallback callback) { 356 DataCallback callback) {
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
432 // called once when first encountering a remote entity. Local changes will 408 // called once when first encountering a remote entity. Local changes will
433 // provide their storage keys directly to Put instead of using this method. 409 // provide their storage keys directly to Put instead of using this method.
434 // Theoretically this function doesn't need to be stable across multiple calls 410 // Theoretically this function doesn't need to be stable across multiple calls
435 // on the same or different clients, but to keep things simple, it probably 411 // on the same or different clients, but to keep things simple, it probably
436 // should be. 412 // should be.
437 std::string ReadingListStore::GetStorageKey( 413 std::string ReadingListStore::GetStorageKey(
438 const syncer::EntityData& entity_data) { 414 const syncer::EntityData& entity_data) {
439 DCHECK(CalledOnValidThread()); 415 DCHECK(CalledOnValidThread());
440 return entity_data.specifics.reading_list().entry_id(); 416 return entity_data.specifics.reading_list().entry_id();
441 } 417 }
418
419 bool ReadingListStore::CompareEntriesForSync(
420 const sync_pb::ReadingListSpecifics& lhs,
421 const sync_pb::ReadingListSpecifics& rhs) {
422 DCHECK(lhs.entry_id() == rhs.entry_id());
423 DCHECK(lhs.has_update_time_us());
424 DCHECK(rhs.has_update_time_us());
425 DCHECK(lhs.has_creation_time_us());
426 DCHECK(rhs.has_creation_time_us());
427 DCHECK(lhs.has_url());
428 DCHECK(rhs.has_url());
429 DCHECK(lhs.has_title());
430 DCHECK(rhs.has_title());
431 DCHECK(lhs.has_status());
432 DCHECK(rhs.has_status());
433 if (rhs.url() != lhs.url() || rhs.title().compare(lhs.title()) < 0 ||
434 rhs.creation_time_us() < lhs.creation_time_us() ||
435 rhs.update_time_us() < lhs.update_time_us()) {
436 return false;
437 }
438 if (rhs.update_time_us() == lhs.update_time_us()) {
439 if ((rhs.status() == sync_pb::ReadingListSpecifics::UNSEEN &&
440 lhs.status() != sync_pb::ReadingListSpecifics::UNSEEN) ||
441 (rhs.status() == sync_pb::ReadingListSpecifics::UNREAD &&
442 lhs.status() == sync_pb::ReadingListSpecifics::READ))
443 return false;
gambard 2016/12/07 13:49:54 Why not returning the condition directly?
Olivier 2016/12/08 15:23:22 Because we may add other cases after
444 }
445 return true;
446 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698