OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "chrome/browser/webdata/autofill_profile_syncable_service.h" | 5 #include "chrome/browser/webdata/autofill_profile_syncable_service.h" |
6 | 6 |
7 #include "base/guid.h" | 7 #include "base/guid.h" |
8 #include "base/location.h" | 8 #include "base/location.h" |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
(...skipping 301 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
312 } | 312 } |
313 return success; | 313 return success; |
314 } | 314 } |
315 | 315 |
316 // static | 316 // static |
317 bool AutofillProfileSyncableService::OverwriteProfileWithServerData( | 317 bool AutofillProfileSyncableService::OverwriteProfileWithServerData( |
318 const sync_pb::AutofillProfileSpecifics& specifics, | 318 const sync_pb::AutofillProfileSpecifics& specifics, |
319 AutofillProfile* profile, | 319 AutofillProfile* profile, |
320 const std::string& app_locale) { | 320 const std::string& app_locale) { |
321 bool diff = false; | 321 bool diff = false; |
322 if (profile->origin() != specifics.origin()) { | |
323 bool was_verified = profile->IsVerified(); | |
324 profile->set_origin(specifics.origin()); | |
325 diff = true; | |
326 | |
327 // Verified profiles should never be overwritten by unverified ones. | |
328 DCHECK(!was_verified || profile->IsVerified()); | |
329 } | |
330 | |
322 diff = UpdateMultivaluedField(autofill::NAME_FIRST, | 331 diff = UpdateMultivaluedField(autofill::NAME_FIRST, |
323 specifics.name_first(), profile) || diff; | 332 specifics.name_first(), profile) || diff; |
324 diff = UpdateMultivaluedField(autofill::NAME_MIDDLE, | 333 diff = UpdateMultivaluedField(autofill::NAME_MIDDLE, |
325 specifics.name_middle(), profile) || diff; | 334 specifics.name_middle(), profile) || diff; |
326 diff = UpdateMultivaluedField(autofill::NAME_LAST, | 335 diff = UpdateMultivaluedField(autofill::NAME_LAST, |
327 specifics.name_last(), profile) || diff; | 336 specifics.name_last(), profile) || diff; |
328 diff = UpdateField(autofill::ADDRESS_HOME_LINE1, | 337 diff = UpdateField(autofill::ADDRESS_HOME_LINE1, |
329 specifics.address_home_line1(), profile) || diff; | 338 specifics.address_home_line1(), profile) || diff; |
330 diff = UpdateField(autofill::ADDRESS_HOME_LINE2, | 339 diff = UpdateField(autofill::ADDRESS_HOME_LINE2, |
331 specifics.address_home_line2(), profile) || diff; | 340 specifics.address_home_line2(), profile) || diff; |
(...skipping 29 matching lines...) Expand all Loading... | |
361 DCHECK(base::IsValidGUID(profile.guid())); | 370 DCHECK(base::IsValidGUID(profile.guid())); |
362 | 371 |
363 // Reset all multi-valued fields in the protobuf. | 372 // Reset all multi-valued fields in the protobuf. |
364 specifics->clear_name_first(); | 373 specifics->clear_name_first(); |
365 specifics->clear_name_middle(); | 374 specifics->clear_name_middle(); |
366 specifics->clear_name_last(); | 375 specifics->clear_name_last(); |
367 specifics->clear_email_address(); | 376 specifics->clear_email_address(); |
368 specifics->clear_phone_home_whole_number(); | 377 specifics->clear_phone_home_whole_number(); |
369 | 378 |
370 specifics->set_guid(profile.guid()); | 379 specifics->set_guid(profile.guid()); |
380 specifics->set_origin(profile.origin()); | |
381 | |
371 std::vector<string16> values; | 382 std::vector<string16> values; |
372 profile.GetRawMultiInfo(autofill::NAME_FIRST, &values); | 383 profile.GetRawMultiInfo(autofill::NAME_FIRST, &values); |
373 for (size_t i = 0; i < values.size(); ++i) { | 384 for (size_t i = 0; i < values.size(); ++i) { |
374 specifics->add_name_first(LimitData(UTF16ToUTF8(values[i]))); | 385 specifics->add_name_first(LimitData(UTF16ToUTF8(values[i]))); |
375 } | 386 } |
376 | 387 |
377 profile.GetRawMultiInfo(autofill::NAME_MIDDLE, &values); | 388 profile.GetRawMultiInfo(autofill::NAME_MIDDLE, &values); |
378 for (size_t i = 0; i < values.size(); ++i) { | 389 for (size_t i = 0; i < values.size(); ++i) { |
379 specifics->add_name_middle(LimitData(UTF16ToUTF8(values[i]))); | 390 specifics->add_name_middle(LimitData(UTF16ToUTF8(values[i]))); |
380 } | 391 } |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
438 GUIDToProfileMap::iterator it = profile_map->find( | 449 GUIDToProfileMap::iterator it = profile_map->find( |
439 autofill_specifics.guid()); | 450 autofill_specifics.guid()); |
440 if (it != profile_map->end()) { | 451 if (it != profile_map->end()) { |
441 // Some profile that already present is synced. | 452 // Some profile that already present is synced. |
442 if (OverwriteProfileWithServerData( | 453 if (OverwriteProfileWithServerData( |
443 autofill_specifics, it->second, app_locale_)) { | 454 autofill_specifics, it->second, app_locale_)) { |
444 bundle->profiles_to_update.push_back(it->second); | 455 bundle->profiles_to_update.push_back(it->second); |
445 } | 456 } |
446 } else { | 457 } else { |
447 // New profile synced. | 458 // New profile synced. |
448 // TODO(isherman): Read the origin from |autofill_specifics|. | 459 AutofillProfile* new_profile = new AutofillProfile( |
449 AutofillProfile* new_profile( | 460 autofill_specifics.guid(), autofill_specifics.origin()); |
450 new AutofillProfile(autofill_specifics.guid(), std::string())); | |
451 OverwriteProfileWithServerData( | 461 OverwriteProfileWithServerData( |
452 autofill_specifics, new_profile, app_locale_); | 462 autofill_specifics, new_profile, app_locale_); |
453 | 463 |
454 // Check if profile appears under a different guid. | 464 // Check if profile appears under a different guid. |
465 // Unverified profiles should never overwrite verified ones. | |
455 for (GUIDToProfileMap::iterator i = profile_map->begin(); | 466 for (GUIDToProfileMap::iterator i = profile_map->begin(); |
456 i != profile_map->end(); ++i) { | 467 i != profile_map->end(); ++i) { |
Evan Stade
2013/06/12 22:12:24
nit: more readable if you create a local variable
Ilya Sherman
2013/06/13 04:19:32
Done.
| |
457 if (i->second->Compare(*new_profile) == 0) { | 468 if (i->second->Compare(*new_profile) == 0) { |
469 // Ensure that a verified profile can never revert back to an unverified | |
470 // one. | |
471 if (i->second->IsVerified() && !new_profile->IsVerified()) { | |
472 new_profile->set_origin(i->second->origin()); | |
473 bundle->profiles_to_sync_back.push_back(new_profile); | |
474 } | |
475 | |
458 bundle->profiles_to_delete.push_back(i->second->guid()); | 476 bundle->profiles_to_delete.push_back(i->second->guid()); |
459 DVLOG(2) << "[AUTOFILL SYNC]" | 477 DVLOG(2) << "[AUTOFILL SYNC]" |
460 << "Found in sync db but with a different guid: " | 478 << "Found in sync db but with a different guid: " |
461 << UTF16ToUTF8(i->second->GetRawInfo(autofill::NAME_FIRST)) | 479 << UTF16ToUTF8(i->second->GetRawInfo(autofill::NAME_FIRST)) |
462 << UTF16ToUTF8(i->second->GetRawInfo(autofill::NAME_LAST)) | 480 << UTF16ToUTF8(i->second->GetRawInfo(autofill::NAME_LAST)) |
463 << "New guid " << new_profile->guid() | 481 << "New guid " << new_profile->guid() |
464 << ". Profile to be deleted " << i->second->guid(); | 482 << ". Profile to be deleted " << i->second->guid(); |
465 profile_map->erase(i); | 483 profile_map->erase(i); |
466 break; | 484 break; |
467 } else if (!i->second->PrimaryValue().empty() && | 485 } else if (!i->second->IsVerified() && |
486 !new_profile->IsVerified() && | |
487 !i->second->PrimaryValue().empty() && | |
468 i->second->PrimaryValue() == new_profile->PrimaryValue()) { | 488 i->second->PrimaryValue() == new_profile->PrimaryValue()) { |
469 // Add it to candidates for merge - if there is no profile with this | 489 // Add it to candidates for merge - if there is no profile with this |
470 // guid we will merge them. | 490 // guid we will merge them. |
471 bundle->candidates_to_merge.insert(std::make_pair(i->second->guid(), | 491 bundle->candidates_to_merge.insert(std::make_pair(i->second->guid(), |
472 new_profile)); | 492 new_profile)); |
473 } | 493 } |
474 } | 494 } |
475 profiles_.push_back(new_profile); | 495 profiles_.push_back(new_profile); |
476 it = profile_map->insert(std::make_pair(new_profile->guid(), | 496 it = profile_map->insert(std::make_pair(new_profile->guid(), |
477 new_profile)).first; | 497 new_profile)).first; |
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
574 } | 594 } |
575 if (changed) | 595 if (changed) |
576 autofill_profile->SetRawMultiInfo(field_type, values); | 596 autofill_profile->SetRawMultiInfo(field_type, values); |
577 return changed; | 597 return changed; |
578 } | 598 } |
579 | 599 |
580 bool AutofillProfileSyncableService::MergeProfile( | 600 bool AutofillProfileSyncableService::MergeProfile( |
581 const AutofillProfile& merge_from, | 601 const AutofillProfile& merge_from, |
582 AutofillProfile* merge_into, | 602 AutofillProfile* merge_into, |
583 const std::string& app_locale) { | 603 const std::string& app_locale) { |
604 DCHECK(!merge_into->IsVerified() || merge_from.IsVerified()); | |
605 merge_into->set_origin(merge_from.origin()); | |
584 merge_into->OverwriteWithOrAddTo(merge_from, app_locale); | 606 merge_into->OverwriteWithOrAddTo(merge_from, app_locale); |
585 return (merge_into->Compare(merge_from) != 0); | 607 return (merge_into->Compare(merge_from) != 0 || |
608 merge_into->origin() != merge_from.origin()); | |
586 } | 609 } |
587 | 610 |
588 AutofillTable* AutofillProfileSyncableService::GetAutofillTable() const { | 611 AutofillTable* AutofillProfileSyncableService::GetAutofillTable() const { |
589 return AutofillTable::FromWebDatabase(webdata_backend_->GetDatabase()); | 612 return AutofillTable::FromWebDatabase(webdata_backend_->GetDatabase()); |
590 } | 613 } |
591 | 614 |
592 void AutofillProfileSyncableService::InjectStartSyncFlare( | 615 void AutofillProfileSyncableService::InjectStartSyncFlare( |
593 const syncer::SyncableService::StartSyncFlare& flare) { | 616 const syncer::SyncableService::StartSyncFlare& flare) { |
594 flare_ = flare; | 617 flare_ = flare; |
595 } | 618 } |
596 | 619 |
597 AutofillProfileSyncableService::DataBundle::DataBundle() {} | 620 AutofillProfileSyncableService::DataBundle::DataBundle() {} |
598 | 621 |
599 AutofillProfileSyncableService::DataBundle::~DataBundle() {} | 622 AutofillProfileSyncableService::DataBundle::~DataBundle() {} |
OLD | NEW |