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/utf_string_conversions.h" | 10 #include "base/utf_string_conversions.h" |
| 11 #include "components/autofill/browser/autofill_country.h" |
11 #include "components/autofill/browser/autofill_profile.h" | 12 #include "components/autofill/browser/autofill_profile.h" |
12 #include "components/autofill/browser/form_group.h" | 13 #include "components/autofill/browser/form_group.h" |
13 #include "components/webdata/autofill/autofill_table.h" | 14 #include "components/webdata/autofill/autofill_table.h" |
14 #include "components/webdata/autofill/autofill_webdata_service.h" | 15 #include "components/webdata/autofill/autofill_webdata_service.h" |
15 #include "components/webdata/common/web_database.h" | 16 #include "components/webdata/common/web_database.h" |
16 #include "content/public/browser/browser_thread.h" | 17 #include "content/public/browser/browser_thread.h" |
17 #include "sync/api/sync_error.h" | 18 #include "sync/api/sync_error.h" |
18 #include "sync/api/sync_error_factory.h" | 19 #include "sync/api/sync_error_factory.h" |
19 #include "sync/protocol/sync.pb.h" | 20 #include "sync/protocol/sync.pb.h" |
20 | 21 |
(...skipping 13 matching lines...) Expand all Loading... |
34 // with something else. | 35 // with something else. |
35 static int user_data_key = 0; | 36 static int user_data_key = 0; |
36 return reinterpret_cast<void*>(&user_data_key); | 37 return reinterpret_cast<void*>(&user_data_key); |
37 } | 38 } |
38 | 39 |
39 } // namespace | 40 } // namespace |
40 | 41 |
41 const char kAutofillProfileTag[] = "google_chrome_autofill_profiles"; | 42 const char kAutofillProfileTag[] = "google_chrome_autofill_profiles"; |
42 | 43 |
43 AutofillProfileSyncableService::AutofillProfileSyncableService( | 44 AutofillProfileSyncableService::AutofillProfileSyncableService( |
44 AutofillWebDataService* web_data_service) | 45 AutofillWebDataService* web_data_service, |
| 46 const std::string& app_locale) |
45 : web_data_service_(web_data_service), | 47 : web_data_service_(web_data_service), |
| 48 app_locale_(app_locale), |
46 ALLOW_THIS_IN_INITIALIZER_LIST(scoped_observer_(this)) { | 49 ALLOW_THIS_IN_INITIALIZER_LIST(scoped_observer_(this)) { |
47 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); | 50 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); |
48 DCHECK(web_data_service_); | 51 DCHECK(web_data_service_); |
49 | 52 |
50 scoped_observer_.Add(web_data_service_); | 53 scoped_observer_.Add(web_data_service_); |
51 } | 54 } |
52 | 55 |
53 AutofillProfileSyncableService::~AutofillProfileSyncableService() { | 56 AutofillProfileSyncableService::~AutofillProfileSyncableService() { |
54 DCHECK(CalledOnValidThread()); | 57 DCHECK(CalledOnValidThread()); |
55 } | 58 } |
56 | 59 |
57 // static | 60 // static |
58 void AutofillProfileSyncableService::CreateForWebDataService( | 61 void AutofillProfileSyncableService::CreateForWebDataService( |
59 AutofillWebDataService* web_data_service) { | 62 AutofillWebDataService* web_data_service, |
| 63 const std::string& app_locale) { |
60 web_data_service->GetDBUserData()->SetUserData( | 64 web_data_service->GetDBUserData()->SetUserData( |
61 UserDataKey(), new AutofillProfileSyncableService(web_data_service)); | 65 UserDataKey(), |
| 66 new AutofillProfileSyncableService(web_data_service, app_locale)); |
62 } | 67 } |
63 | 68 |
64 // static | 69 // static |
65 AutofillProfileSyncableService* | 70 AutofillProfileSyncableService* |
66 AutofillProfileSyncableService::FromWebDataService( | 71 AutofillProfileSyncableService::FromWebDataService( |
67 AutofillWebDataService* web_data_service) { | 72 AutofillWebDataService* web_data_service) { |
68 return static_cast<AutofillProfileSyncableService*>( | 73 return static_cast<AutofillProfileSyncableService*>( |
69 web_data_service->GetDBUserData()->GetUserData(UserDataKey())); | 74 web_data_service->GetDBUserData()->GetUserData(UserDataKey())); |
70 } | 75 } |
71 | 76 |
(...skipping 220 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
292 if (!GetAutofillTable()->UpdateAutofillProfileMulti( | 297 if (!GetAutofillTable()->UpdateAutofillProfileMulti( |
293 *bundle.profiles_to_update[i])) | 298 *bundle.profiles_to_update[i])) |
294 success = false; | 299 success = false; |
295 } | 300 } |
296 return success; | 301 return success; |
297 } | 302 } |
298 | 303 |
299 // static | 304 // static |
300 bool AutofillProfileSyncableService::OverwriteProfileWithServerData( | 305 bool AutofillProfileSyncableService::OverwriteProfileWithServerData( |
301 const sync_pb::AutofillProfileSpecifics& specifics, | 306 const sync_pb::AutofillProfileSpecifics& specifics, |
302 AutofillProfile* profile) { | 307 AutofillProfile* profile, |
| 308 const std::string& app_locale) { |
303 bool diff = false; | 309 bool diff = false; |
304 diff = UpdateMultivaluedField(NAME_FIRST, | 310 diff = UpdateMultivaluedField(NAME_FIRST, |
305 specifics.name_first(), profile) || diff; | 311 specifics.name_first(), profile) || diff; |
306 diff = UpdateMultivaluedField(NAME_MIDDLE, | 312 diff = UpdateMultivaluedField(NAME_MIDDLE, |
307 specifics.name_middle(), profile) || diff; | 313 specifics.name_middle(), profile) || diff; |
308 diff = UpdateMultivaluedField(NAME_LAST, | 314 diff = UpdateMultivaluedField(NAME_LAST, |
309 specifics.name_last(), profile) || diff; | 315 specifics.name_last(), profile) || diff; |
310 diff = UpdateField(ADDRESS_HOME_LINE1, | 316 diff = UpdateField(ADDRESS_HOME_LINE1, |
311 specifics.address_home_line1(), profile) || diff; | 317 specifics.address_home_line1(), profile) || diff; |
312 diff = UpdateField(ADDRESS_HOME_LINE2, | 318 diff = UpdateField(ADDRESS_HOME_LINE2, |
313 specifics.address_home_line2(), profile) || diff; | 319 specifics.address_home_line2(), profile) || diff; |
314 diff = UpdateField(ADDRESS_HOME_CITY, | 320 diff = UpdateField(ADDRESS_HOME_CITY, |
315 specifics.address_home_city(), profile) || diff; | 321 specifics.address_home_city(), profile) || diff; |
316 diff = UpdateField(ADDRESS_HOME_STATE, | 322 diff = UpdateField(ADDRESS_HOME_STATE, |
317 specifics.address_home_state(), profile) || diff; | 323 specifics.address_home_state(), profile) || diff; |
318 diff = UpdateField(ADDRESS_HOME_COUNTRY, | 324 string16 country_name_or_code = |
319 specifics.address_home_country(), profile) || diff; | 325 ASCIIToUTF16(specifics.address_home_country()); |
| 326 std::string country_code = AutofillCountry::GetCountryCode( |
| 327 country_name_or_code, app_locale); |
| 328 diff = UpdateField(ADDRESS_HOME_COUNTRY, country_code, profile) || diff; |
320 diff = UpdateField(ADDRESS_HOME_ZIP, | 329 diff = UpdateField(ADDRESS_HOME_ZIP, |
321 specifics.address_home_zip(), profile) || diff; | 330 specifics.address_home_zip(), profile) || diff; |
322 diff = UpdateMultivaluedField(EMAIL_ADDRESS, | 331 diff = UpdateMultivaluedField(EMAIL_ADDRESS, |
323 specifics.email_address(), profile) || diff; | 332 specifics.email_address(), profile) || diff; |
324 diff = UpdateField(COMPANY_NAME, specifics.company_name(), profile) || diff; | 333 diff = UpdateField(COMPANY_NAME, specifics.company_name(), profile) || diff; |
325 diff = UpdateMultivaluedField(PHONE_HOME_WHOLE_NUMBER, | 334 diff = UpdateMultivaluedField(PHONE_HOME_WHOLE_NUMBER, |
326 specifics.phone_home_whole_number(), | 335 specifics.phone_home_whole_number(), |
327 profile) || diff; | 336 profile) || diff; |
328 return diff; | 337 return diff; |
329 } | 338 } |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
408 DCHECK_EQ(syncer::AUTOFILL_PROFILE, data.GetDataType()); | 417 DCHECK_EQ(syncer::AUTOFILL_PROFILE, data.GetDataType()); |
409 | 418 |
410 const sync_pb::EntitySpecifics& specifics = data.GetSpecifics(); | 419 const sync_pb::EntitySpecifics& specifics = data.GetSpecifics(); |
411 const sync_pb::AutofillProfileSpecifics& autofill_specifics( | 420 const sync_pb::AutofillProfileSpecifics& autofill_specifics( |
412 specifics.autofill_profile()); | 421 specifics.autofill_profile()); |
413 | 422 |
414 GUIDToProfileMap::iterator it = profile_map->find( | 423 GUIDToProfileMap::iterator it = profile_map->find( |
415 autofill_specifics.guid()); | 424 autofill_specifics.guid()); |
416 if (it != profile_map->end()) { | 425 if (it != profile_map->end()) { |
417 // Some profile that already present is synced. | 426 // Some profile that already present is synced. |
418 if (OverwriteProfileWithServerData(autofill_specifics, it->second)) | 427 if (OverwriteProfileWithServerData( |
| 428 autofill_specifics, it->second, app_locale_)) { |
419 bundle->profiles_to_update.push_back(it->second); | 429 bundle->profiles_to_update.push_back(it->second); |
| 430 } |
420 } else { | 431 } else { |
421 // New profile synced. | 432 // New profile synced. |
422 AutofillProfile* new_profile( | 433 AutofillProfile* new_profile( |
423 new AutofillProfile(autofill_specifics.guid())); | 434 new AutofillProfile(autofill_specifics.guid())); |
424 OverwriteProfileWithServerData(autofill_specifics, new_profile); | 435 OverwriteProfileWithServerData( |
| 436 autofill_specifics, new_profile, app_locale_); |
425 | 437 |
426 // Check if profile appears under a different guid. | 438 // Check if profile appears under a different guid. |
427 for (GUIDToProfileMap::iterator i = profile_map->begin(); | 439 for (GUIDToProfileMap::iterator i = profile_map->begin(); |
428 i != profile_map->end(); ++i) { | 440 i != profile_map->end(); ++i) { |
429 if (i->second->Compare(*new_profile) == 0) { | 441 if (i->second->Compare(*new_profile) == 0) { |
430 bundle->profiles_to_delete.push_back(i->second->guid()); | 442 bundle->profiles_to_delete.push_back(i->second->guid()); |
431 DVLOG(2) << "[AUTOFILL SYNC]" | 443 DVLOG(2) << "[AUTOFILL SYNC]" |
432 << "Found in sync db but with a different guid: " | 444 << "Found in sync db but with a different guid: " |
433 << UTF16ToUTF8(i->second->GetRawInfo(NAME_FIRST)) | 445 << UTF16ToUTF8(i->second->GetRawInfo(NAME_FIRST)) |
434 << UTF16ToUTF8(i->second->GetRawInfo(NAME_LAST)) | 446 << UTF16ToUTF8(i->second->GetRawInfo(NAME_LAST)) |
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
556 return (merge_into->Compare(merge_from) != 0); | 568 return (merge_into->Compare(merge_from) != 0); |
557 } | 569 } |
558 | 570 |
559 AutofillTable* AutofillProfileSyncableService::GetAutofillTable() const { | 571 AutofillTable* AutofillProfileSyncableService::GetAutofillTable() const { |
560 return AutofillTable::FromWebDatabase(web_data_service_->GetDatabase()); | 572 return AutofillTable::FromWebDatabase(web_data_service_->GetDatabase()); |
561 } | 573 } |
562 | 574 |
563 AutofillProfileSyncableService::DataBundle::DataBundle() {} | 575 AutofillProfileSyncableService::DataBundle::DataBundle() {} |
564 | 576 |
565 AutofillProfileSyncableService::DataBundle::~DataBundle() {} | 577 AutofillProfileSyncableService::DataBundle::~DataBundle() {} |
OLD | NEW |