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/ui/webui/options/autofill_options_handler.h" | 5 #include "chrome/browser/ui/webui/options/autofill_options_handler.h" |
6 | 6 |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
11 #include "base/command_line.h" | 11 #include "base/command_line.h" |
12 #include "base/guid.h" | 12 #include "base/guid.h" |
13 #include "base/logging.h" | 13 #include "base/logging.h" |
14 #include "base/strings/string16.h" | 14 #include "base/strings/string16.h" |
15 #include "base/strings/string_number_conversions.h" | 15 #include "base/strings/string_number_conversions.h" |
16 #include "base/strings/utf_string_conversions.h" | 16 #include "base/strings/utf_string_conversions.h" |
17 #include "base/values.h" | 17 #include "base/values.h" |
18 #include "chrome/browser/autofill/personal_data_manager_factory.h" | 18 #include "chrome/browser/autofill/personal_data_manager_factory.h" |
19 #include "chrome/browser/browser_process.h" | 19 #include "chrome/browser/browser_process.h" |
20 #include "chrome/browser/profiles/profile.h" | 20 #include "chrome/browser/profiles/profile.h" |
21 #include "chrome/browser/sync/profile_sync_service.h" | 21 #include "chrome/browser/sync/profile_sync_service.h" |
22 #include "chrome/browser/sync/profile_sync_service_factory.h" | 22 #include "chrome/browser/sync/profile_sync_service_factory.h" |
23 #include "chrome/browser/ui/autofill/country_combobox_model.h" | 23 #include "chrome/browser/ui/autofill/country_combobox_model.h" |
24 #include "chrome/common/url_constants.h" | 24 #include "chrome/common/url_constants.h" |
| 25 #include "chrome/grit/chromium_strings.h" |
25 #include "chrome/grit/generated_resources.h" | 26 #include "chrome/grit/generated_resources.h" |
26 #include "components/autofill/content/browser/wallet/wallet_service_url.h" | 27 #include "components/autofill/content/browser/wallet/wallet_service_url.h" |
27 #include "components/autofill/core/browser/autofill_country.h" | 28 #include "components/autofill/core/browser/autofill_country.h" |
28 #include "components/autofill/core/browser/autofill_profile.h" | 29 #include "components/autofill/core/browser/autofill_profile.h" |
29 #include "components/autofill/core/browser/credit_card.h" | 30 #include "components/autofill/core/browser/credit_card.h" |
30 #include "components/autofill/core/browser/personal_data_manager.h" | 31 #include "components/autofill/core/browser/personal_data_manager.h" |
31 #include "components/autofill/core/browser/phone_number_i18n.h" | 32 #include "components/autofill/core/browser/phone_number_i18n.h" |
32 #include "components/autofill/core/common/autofill_constants.h" | 33 #include "components/autofill/core/common/autofill_constants.h" |
33 #include "components/autofill/core/common/autofill_switches.h" | 34 #include "components/autofill/core/common/autofill_switches.h" |
34 #include "content/public/browser/web_ui.h" | 35 #include "content/public/browser/web_ui.h" |
(...skipping 22 matching lines...) Expand all Loading... |
57 static const char kDependentLocalityField[] = "dependentLocality"; | 58 static const char kDependentLocalityField[] = "dependentLocality"; |
58 static const char kCityField[] = "city"; | 59 static const char kCityField[] = "city"; |
59 static const char kStateField[] = "state"; | 60 static const char kStateField[] = "state"; |
60 static const char kPostalCodeField[] = "postalCode"; | 61 static const char kPostalCodeField[] = "postalCode"; |
61 static const char kSortingCodeField[] = "sortingCode"; | 62 static const char kSortingCodeField[] = "sortingCode"; |
62 static const char kCountryField[] = "country"; | 63 static const char kCountryField[] = "country"; |
63 | 64 |
64 static const char kComponents[] = "components"; | 65 static const char kComponents[] = "components"; |
65 static const char kLanguageCode[] = "languageCode"; | 66 static const char kLanguageCode[] = "languageCode"; |
66 | 67 |
| 68 scoped_ptr<base::DictionaryValue> CreditCardToDictionary( |
| 69 const CreditCard& card) { |
| 70 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue); |
| 71 value->SetString("guid", card.guid()); |
| 72 value->SetString("label", card.Label()); |
| 73 value->SetBoolean("isLocal", card.record_type() == CreditCard::LOCAL_CARD); |
| 74 value->SetBoolean("isCached", |
| 75 card.record_type() == CreditCard::FULL_SERVER_CARD); |
| 76 return value.Pass(); |
| 77 } |
| 78 |
67 // Fills |components| with the address UI components that should be used to | 79 // Fills |components| with the address UI components that should be used to |
68 // input an address for |country_code| when UI BCP 47 language code is | 80 // input an address for |country_code| when UI BCP 47 language code is |
69 // |ui_language_code|. If |components_language_code| is not NULL, then sets it | 81 // |ui_language_code|. If |components_language_code| is not NULL, then sets it |
70 // to the BCP 47 language code that should be used to format the address for | 82 // to the BCP 47 language code that should be used to format the address for |
71 // display. | 83 // display. |
72 void GetAddressComponents(const std::string& country_code, | 84 void GetAddressComponents(const std::string& country_code, |
73 const std::string& ui_language_code, | 85 const std::string& ui_language_code, |
74 base::ListValue* address_components, | 86 base::ListValue* address_components, |
75 std::string* components_language_code) { | 87 std::string* components_language_code) { |
76 DCHECK(address_components); | 88 DCHECK(address_components); |
(...skipping 235 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
312 void AutofillOptionsHandler::GetLocalizedValues( | 324 void AutofillOptionsHandler::GetLocalizedValues( |
313 base::DictionaryValue* localized_strings) { | 325 base::DictionaryValue* localized_strings) { |
314 DCHECK(localized_strings); | 326 DCHECK(localized_strings); |
315 | 327 |
316 static OptionsStringResource resources[] = { | 328 static OptionsStringResource resources[] = { |
317 { "autofillAddresses", IDS_AUTOFILL_ADDRESSES_GROUP_NAME }, | 329 { "autofillAddresses", IDS_AUTOFILL_ADDRESSES_GROUP_NAME }, |
318 { "autofillCreditCards", IDS_AUTOFILL_CREDITCARDS_GROUP_NAME }, | 330 { "autofillCreditCards", IDS_AUTOFILL_CREDITCARDS_GROUP_NAME }, |
319 { "autofillAddAddress", IDS_AUTOFILL_ADD_ADDRESS_BUTTON }, | 331 { "autofillAddAddress", IDS_AUTOFILL_ADD_ADDRESS_BUTTON }, |
320 { "autofillAddCreditCard", IDS_AUTOFILL_ADD_CREDITCARD_BUTTON }, | 332 { "autofillAddCreditCard", IDS_AUTOFILL_ADD_CREDITCARD_BUTTON }, |
321 { "autofillEditProfileButton", IDS_AUTOFILL_EDIT_PROFILE_BUTTON }, | 333 { "autofillEditProfileButton", IDS_AUTOFILL_EDIT_PROFILE_BUTTON }, |
| 334 { "autofillDescribeLocalCopy", IDS_AUTOFILL_DESCRIBE_LOCAL_COPY }, |
| 335 { "autofillClearLocalCopyButton", IDS_AUTOFILL_CLEAR_LOCAL_COPY_BUTTON }, |
322 { "helpButton", IDS_AUTOFILL_HELP_LABEL }, | 336 { "helpButton", IDS_AUTOFILL_HELP_LABEL }, |
323 { "addAddressTitle", IDS_AUTOFILL_ADD_ADDRESS_CAPTION }, | 337 { "addAddressTitle", IDS_AUTOFILL_ADD_ADDRESS_CAPTION }, |
324 { "editAddressTitle", IDS_AUTOFILL_EDIT_ADDRESS_CAPTION }, | 338 { "editAddressTitle", IDS_AUTOFILL_EDIT_ADDRESS_CAPTION }, |
325 { "addCreditCardTitle", IDS_AUTOFILL_ADD_CREDITCARD_CAPTION }, | 339 { "addCreditCardTitle", IDS_AUTOFILL_ADD_CREDITCARD_CAPTION }, |
326 { "editCreditCardTitle", IDS_AUTOFILL_EDIT_CREDITCARD_CAPTION }, | 340 { "editCreditCardTitle", IDS_AUTOFILL_EDIT_CREDITCARD_CAPTION }, |
327 #if defined(OS_MACOSX) | 341 #if defined(OS_MACOSX) |
328 { "auxiliaryProfilesEnabled", IDS_AUTOFILL_USE_MAC_ADDRESS_BOOK }, | 342 { "auxiliaryProfilesEnabled", IDS_AUTOFILL_USE_MAC_ADDRESS_BOOK }, |
329 #endif // defined(OS_MACOSX) | 343 #endif // defined(OS_MACOSX) |
330 }; | 344 }; |
331 | 345 |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
395 base::Bind(&AutofillOptionsHandler::SetAddress, base::Unretained(this))); | 409 base::Bind(&AutofillOptionsHandler::SetAddress, base::Unretained(this))); |
396 web_ui()->RegisterMessageCallback( | 410 web_ui()->RegisterMessageCallback( |
397 "setCreditCard", | 411 "setCreditCard", |
398 base::Bind(&AutofillOptionsHandler::SetCreditCard, | 412 base::Bind(&AutofillOptionsHandler::SetCreditCard, |
399 base::Unretained(this))); | 413 base::Unretained(this))); |
400 web_ui()->RegisterMessageCallback( | 414 web_ui()->RegisterMessageCallback( |
401 "validatePhoneNumbers", | 415 "validatePhoneNumbers", |
402 base::Bind(&AutofillOptionsHandler::ValidatePhoneNumbers, | 416 base::Bind(&AutofillOptionsHandler::ValidatePhoneNumbers, |
403 base::Unretained(this))); | 417 base::Unretained(this))); |
404 web_ui()->RegisterMessageCallback( | 418 web_ui()->RegisterMessageCallback( |
405 "remaskServerCards", | 419 "clearLocalCardCopy", |
406 base::Bind(&AutofillOptionsHandler::RemaskServerCards, | 420 base::Bind(&AutofillOptionsHandler::RemaskServerCard, |
407 base::Unretained(this))); | 421 base::Unretained(this))); |
408 } | 422 } |
409 | 423 |
410 ///////////////////////////////////////////////////////////////////////////// | 424 ///////////////////////////////////////////////////////////////////////////// |
411 // PersonalDataManagerObserver implementation: | 425 // PersonalDataManagerObserver implementation: |
412 void AutofillOptionsHandler::OnPersonalDataChanged() { | 426 void AutofillOptionsHandler::OnPersonalDataChanged() { |
413 LoadAutofillData(); | 427 LoadAutofillData(); |
414 } | 428 } |
415 | 429 |
416 void AutofillOptionsHandler::SetAddressOverlayStrings( | 430 void AutofillOptionsHandler::SetAddressOverlayStrings( |
(...skipping 22 matching lines...) Expand all Loading... |
439 localized_strings->SetString("creditCardNumberLabel", | 453 localized_strings->SetString("creditCardNumberLabel", |
440 l10n_util::GetStringUTF16(IDS_AUTOFILL_FIELD_LABEL_CREDIT_CARD_NUMBER)); | 454 l10n_util::GetStringUTF16(IDS_AUTOFILL_FIELD_LABEL_CREDIT_CARD_NUMBER)); |
441 localized_strings->SetString("creditCardExpirationDateLabel", | 455 localized_strings->SetString("creditCardExpirationDateLabel", |
442 l10n_util::GetStringUTF16(IDS_AUTOFILL_FIELD_LABEL_EXPIRATION_DATE)); | 456 l10n_util::GetStringUTF16(IDS_AUTOFILL_FIELD_LABEL_EXPIRATION_DATE)); |
443 } | 457 } |
444 | 458 |
445 void AutofillOptionsHandler::LoadAutofillData() { | 459 void AutofillOptionsHandler::LoadAutofillData() { |
446 if (!IsPersonalDataLoaded()) | 460 if (!IsPersonalDataLoaded()) |
447 return; | 461 return; |
448 | 462 |
449 const std::vector<AutofillProfile*>& profiles = | 463 const std::vector<AutofillProfile*>& profiles = personal_data_->GetProfiles(); |
450 personal_data_->web_profiles(); | |
451 std::vector<base::string16> labels; | 464 std::vector<base::string16> labels; |
452 AutofillProfile::CreateDifferentiatingLabels( | 465 AutofillProfile::CreateDifferentiatingLabels( |
453 profiles, | 466 profiles, |
454 g_browser_process->GetApplicationLocale(), | 467 g_browser_process->GetApplicationLocale(), |
455 &labels); | 468 &labels); |
456 DCHECK_EQ(labels.size(), profiles.size()); | 469 DCHECK_EQ(labels.size(), profiles.size()); |
457 | 470 |
458 base::ListValue addresses; | 471 base::ListValue addresses; |
459 for (size_t i = 0; i < profiles.size(); ++i) { | 472 for (size_t i = 0; i < profiles.size(); ++i) { |
460 base::ListValue* entry = new base::ListValue(); | 473 // Skip showing auxiliary profiles (e.g. Mac Contacts) for now. |
461 entry->Append(new base::StringValue(profiles[i]->guid())); | 474 if (profiles[i]->record_type() == AutofillProfile::AUXILIARY_PROFILE) |
462 entry->Append(new base::StringValue(labels[i])); | 475 continue; |
463 addresses.Append(entry); | 476 |
| 477 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue); |
| 478 value->SetString("guid", profiles[i]->guid()); |
| 479 value->SetString("label", labels[i]); |
| 480 value->SetBoolean("isLocal", profiles[i]->record_type() == |
| 481 AutofillProfile::LOCAL_PROFILE); |
| 482 addresses.Append(value.release()); |
464 } | 483 } |
465 | 484 |
466 web_ui()->CallJavascriptFunction("AutofillOptions.setAddressList", addresses); | 485 web_ui()->CallJavascriptFunction("AutofillOptions.setAddressList", addresses); |
467 | 486 |
468 base::ListValue credit_cards; | 487 base::ListValue credit_cards; |
469 const std::vector<CreditCard*>& cards = personal_data_->GetLocalCreditCards(); | 488 const std::vector<CreditCard*>& cards = personal_data_->GetCreditCards(); |
470 for (const CreditCard* card : cards) { | 489 for (const CreditCard* card : cards) { |
471 // TODO(estade): this should be a dictionary. | 490 credit_cards.Append(CreditCardToDictionary(*card).release()); |
472 base::ListValue* entry = new base::ListValue(); | |
473 entry->Append(new base::StringValue(card->guid())); | |
474 entry->Append(new base::StringValue(card->Label())); | |
475 entry->Append(new base::StringValue( | |
476 webui::GetBitmapDataUrlFromResource( | |
477 CreditCard::IconResourceId(card->type())))); | |
478 entry->Append(new base::StringValue(card->TypeForDisplay())); | |
479 credit_cards.Append(entry); | |
480 } | 491 } |
481 | 492 |
482 web_ui()->CallJavascriptFunction("AutofillOptions.setCreditCardList", | 493 web_ui()->CallJavascriptFunction("AutofillOptions.setCreditCardList", |
483 credit_cards); | 494 credit_cards); |
484 } | 495 } |
485 | 496 |
486 #if defined(OS_MACOSX) && !defined(OS_IOS) | 497 #if defined(OS_MACOSX) && !defined(OS_IOS) |
487 void AutofillOptionsHandler::AccessAddressBook(const base::ListValue* args) { | 498 void AutofillOptionsHandler::AccessAddressBook(const base::ListValue* args) { |
488 personal_data_->AccessAddressBook(); | 499 personal_data_->AccessAddressBook(); |
489 } | 500 } |
(...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
687 } | 698 } |
688 } | 699 } |
689 | 700 |
690 void AutofillOptionsHandler::ValidatePhoneNumbers(const base::ListValue* args) { | 701 void AutofillOptionsHandler::ValidatePhoneNumbers(const base::ListValue* args) { |
691 if (!IsPersonalDataLoaded()) | 702 if (!IsPersonalDataLoaded()) |
692 return; | 703 return; |
693 | 704 |
694 scoped_ptr<base::ListValue> list_value = ValidatePhoneArguments(args); | 705 scoped_ptr<base::ListValue> list_value = ValidatePhoneArguments(args); |
695 | 706 |
696 web_ui()->CallJavascriptFunction( | 707 web_ui()->CallJavascriptFunction( |
697 "AutofillEditAddressOverlay.setValidatedPhoneNumbers", *list_value); | 708 "AutofillEditAddressOverlay.setValidatedPhoneNumbers", *list_value); |
698 } | 709 } |
699 | 710 |
700 void AutofillOptionsHandler::RemaskServerCards(const base::ListValue* args) { | 711 void AutofillOptionsHandler::RemaskServerCard(const base::ListValue* args) { |
701 personal_data_->ResetFullServerCards(); | 712 std::string guid; |
| 713 if (!args->GetString(0, &guid)) { |
| 714 NOTREACHED(); |
| 715 return; |
| 716 } |
| 717 |
| 718 personal_data_->ResetFullServerCard(guid); |
702 } | 719 } |
703 | 720 |
704 bool AutofillOptionsHandler::IsPersonalDataLoaded() const { | 721 bool AutofillOptionsHandler::IsPersonalDataLoaded() const { |
705 return personal_data_ && personal_data_->IsDataLoaded(); | 722 return personal_data_ && personal_data_->IsDataLoaded(); |
706 } | 723 } |
707 | 724 |
708 // static | 725 // static |
709 void AutofillOptionsHandler::AutofillProfileToDictionary( | 726 void AutofillOptionsHandler::AutofillProfileToDictionary( |
710 const autofill::AutofillProfile& profile, | 727 const autofill::AutofillProfile& profile, |
711 base::DictionaryValue* address) { | 728 base::DictionaryValue* address) { |
(...skipping 27 matching lines...) Expand all Loading... |
739 scoped_ptr<base::ListValue> components(new base::ListValue); | 756 scoped_ptr<base::ListValue> components(new base::ListValue); |
740 GetAddressComponents( | 757 GetAddressComponents( |
741 base::UTF16ToUTF8(profile.GetRawInfo(autofill::ADDRESS_HOME_COUNTRY)), | 758 base::UTF16ToUTF8(profile.GetRawInfo(autofill::ADDRESS_HOME_COUNTRY)), |
742 profile.language_code(), | 759 profile.language_code(), |
743 components.get(), | 760 components.get(), |
744 NULL); | 761 NULL); |
745 address->Set(kComponents, components.release()); | 762 address->Set(kComponents, components.release()); |
746 } | 763 } |
747 | 764 |
748 } // namespace options | 765 } // namespace options |
OLD | NEW |