Chromium Code Reviews| Index: chrome/browser/ui/webui/options/autofill_options_handler.cc |
| diff --git a/chrome/browser/ui/webui/options/autofill_options_handler.cc b/chrome/browser/ui/webui/options/autofill_options_handler.cc |
| index 60b5aebbc5e0d2a72c1ea522f960b999b6a5f5af..0f84d86894ee65d28a349a7b1b0e9eb7f8a3d06b 100644 |
| --- a/chrome/browser/ui/webui/options/autofill_options_handler.cc |
| +++ b/chrome/browser/ui/webui/options/autofill_options_handler.cc |
| @@ -12,6 +12,7 @@ |
| #include "base/logging.h" |
| #include "base/strings/string16.h" |
| #include "base/strings/string_number_conversions.h" |
| +#include "base/strings/string_split.h" |
| #include "base/strings/utf_string_conversions.h" |
| #include "base/values.h" |
| #include "chrome/browser/autofill/personal_data_manager_factory.h" |
| @@ -28,6 +29,9 @@ |
| #include "content/public/browser/web_ui.h" |
| #include "grit/component_strings.h" |
| #include "grit/generated_resources.h" |
| +#include "grit/libaddressinput_strings.h" |
| +#include "third_party/libaddressinput/chromium/cpp/include/libaddressinput/address_ui.h" |
| +#include "third_party/libaddressinput/chromium/cpp/include/libaddressinput/address_ui_component.h" |
| #include "ui/base/l10n/l10n_util.h" |
| #include "ui/base/webui/web_ui_util.h" |
| @@ -52,8 +56,6 @@ void SetCountryData(const PersonalDataManager& manager, |
| // An ordered list of options to show in the <select>. |
| scoped_ptr<base::ListValue> country_list(new base::ListValue()); |
| - // A dictionary of postal code and state info, keyed on country code. |
| - scoped_ptr<base::DictionaryValue> country_data(new base::DictionaryValue()); |
| for (size_t i = 0; i < countries.size(); ++i) { |
| scoped_ptr<base::DictionaryValue> option_details( |
| new base::DictionaryValue()); |
| @@ -62,18 +64,75 @@ void SetCountryData(const PersonalDataManager& manager, |
| "value", |
| countries[i] ? countries[i]->country_code() : "separator"); |
| country_list->Append(option_details.release()); |
| - |
| - if (!countries[i]) |
| - continue; |
| - |
| - scoped_ptr<base::DictionaryValue> details(new base::DictionaryValue()); |
| - details->SetString("postalCodeLabel", countries[i]->postal_code_label()); |
| - details->SetString("stateLabel", countries[i]->state_label()); |
| - country_data->Set(countries[i]->country_code(), details.release()); |
| - |
| } |
| localized_strings->Set("autofillCountrySelectList", country_list.release()); |
| - localized_strings->Set("autofillCountryData", country_data.release()); |
| +} |
| + |
| +// Fills |components| with the address UI components that should be used to |
| +// input an address for |country_code| when UI BCP 47 language code is |
| +// |ui_language_code|. If |components_language_code| is not NULL, then sets it |
| +// the BCP 47 language code that should be used to format the address for |
| +// display. |
| +void GetAddressComponents(const std::string& country_code, |
| + const std::string& ui_language_code, |
| + base::ListValue* address_components, |
| + std::string* components_language_code) { |
| + DCHECK(address_components); |
| + std::vector<i18n::addressinput::AddressUiComponent> components = |
| + i18n::addressinput::BuildComponents( |
| + country_code, ui_language_code, components_language_code); |
| + if (components.empty()) { |
| + static const char kDefaultCountryCode[] = "US"; |
| + components = i18n::addressinput::BuildComponents( |
| + kDefaultCountryCode, ui_language_code, components_language_code); |
| + } |
| + DCHECK(!components.empty()); |
| + static const char kField[] = "field"; |
| + static const char kLengthHint[] = "length"; |
| + for (std::vector<i18n::addressinput::AddressUiComponent>::const_iterator it = |
| + components.begin(); |
| + it != components.end(); ++it) { |
| + scoped_ptr<base::DictionaryValue> component(new base::DictionaryValue); |
| + switch (it->field) { |
| + case i18n::addressinput::COUNTRY: |
| + component->SetString(kField, "country"); |
| + break; |
| + case i18n::addressinput::ADMIN_AREA: |
| + component->SetString(kField, "admin-area"); |
| + break; |
| + case i18n::addressinput::LOCALITY: |
| + component->SetString(kField, "locality"); |
| + break; |
| + case i18n::addressinput::DEPENDENT_LOCALITY: |
| + component->SetString(kField, "dependent-locality"); |
| + break; |
| + case i18n::addressinput::SORTING_CODE: |
| + component->SetString(kField, "sorting-code"); |
| + break; |
| + case i18n::addressinput::POSTAL_CODE: |
| + component->SetString(kField, "postal-code"); |
| + break; |
| + case i18n::addressinput::STREET_ADDRESS: |
| + component->SetString(kField, "street-address"); |
| + break; |
| + case i18n::addressinput::ORGANIZATION: |
| + component->SetString(kField, "organization"); |
| + break; |
| + case i18n::addressinput::RECIPIENT: |
| + component->SetString(kField, "recipient"); |
| + break; |
| + } |
| + component->SetString("name", l10n_util::GetStringUTF16(it->name_id)); |
| + switch (it->length_hint) { |
| + case i18n::addressinput::AddressUiComponent::HINT_SHORT: |
| + component->SetString(kLengthHint, "short"); |
| + break; |
| + case i18n::addressinput::AddressUiComponent::HINT_LONG: |
| + component->SetString(kLengthHint, "long"); |
| + break; |
| + } |
| + address_components->Append(component.release()); |
| + } |
| } |
|
Evan Stade
2014/04/22 00:51:58
vertical whitespace in this function
please use gerrit instead
2014/04/24 20:10:59
Done.
|
| // Get the multi-valued element for |type| and return it in |ListValue| form. |
| @@ -306,6 +365,10 @@ void AutofillOptionsHandler::RegisterMessages() { |
| base::Bind(&AutofillOptionsHandler::LoadAddressEditor, |
| base::Unretained(this))); |
| web_ui()->RegisterMessageCallback( |
| + "loadAddressEditorComponents", |
| + base::Bind(&AutofillOptionsHandler::LoadAddressEditorComponents, |
| + base::Unretained(this))); |
| + web_ui()->RegisterMessageCallback( |
| "loadCreditCardEditor", |
| base::Bind(&AutofillOptionsHandler::LoadCreditCardEditor, |
| base::Unretained(this))); |
| @@ -338,16 +401,8 @@ void AutofillOptionsHandler::SetAddressOverlayStrings( |
| l10n_util::GetStringUTF16(IDS_AUTOFILL_FIELD_LABEL_MIDDLE_NAME)); |
| localized_strings->SetString("autofillLastNameLabel", |
| l10n_util::GetStringUTF16(IDS_AUTOFILL_FIELD_LABEL_LAST_NAME)); |
| - localized_strings->SetString("autofillCompanyNameLabel", |
| - l10n_util::GetStringUTF16(IDS_AUTOFILL_FIELD_LABEL_COMPANY_NAME)); |
| - localized_strings->SetString("autofillAddrLine1Label", |
| - l10n_util::GetStringUTF16(IDS_AUTOFILL_FIELD_LABEL_ADDRESS_LINE_1)); |
| - localized_strings->SetString("autofillAddrLine2Label", |
| - l10n_util::GetStringUTF16(IDS_AUTOFILL_FIELD_LABEL_ADDRESS_LINE_2)); |
| - localized_strings->SetString("autofillCityLabel", |
| - l10n_util::GetStringUTF16(IDS_AUTOFILL_FIELD_LABEL_CITY)); |
| localized_strings->SetString("autofillCountryLabel", |
| - l10n_util::GetStringUTF16(IDS_AUTOFILL_FIELD_LABEL_COUNTRY)); |
| + l10n_util::GetStringUTF16(IDS_LIBADDRESSINPUT_I18N_COUNTRY_LABEL)); |
| localized_strings->SetString("autofillPhoneLabel", |
| l10n_util::GetStringUTF16(IDS_AUTOFILL_FIELD_LABEL_PHONE)); |
| localized_strings->SetString("autofillEmailLabel", |
| @@ -362,6 +417,9 @@ void AutofillOptionsHandler::SetAddressOverlayStrings( |
| l10n_util::GetStringUTF16(IDS_AUTOFILL_FIELD_LABEL_ADD_PHONE)); |
| localized_strings->SetString("autofillAddEmailPlaceholder", |
| l10n_util::GetStringUTF16(IDS_AUTOFILL_FIELD_LABEL_ADD_EMAIL)); |
| + localized_strings->SetString("addStreetAddressLinePlaceholder", |
| + l10n_util::GetStringUTF16( |
| + IDS_AUTOFILL_FIELD_LABEL_ADD_STREET_ADDRESS_LINE)); |
| SetCountryData(*personal_data_, localized_strings); |
| } |
| @@ -452,14 +510,26 @@ void AutofillOptionsHandler::LoadAddressEditor(const base::ListValue* args) { |
| address.SetString("guid", profile->guid()); |
| scoped_ptr<base::ListValue> list; |
| GetNameList(*profile, &list); |
| - address.Set("fullName", list.release()); |
| - address.SetString("companyName", profile->GetRawInfo(autofill::COMPANY_NAME)); |
| - address.SetString("addrLine1", |
| - profile->GetRawInfo(autofill::ADDRESS_HOME_LINE1)); |
| - address.SetString("addrLine2", |
| - profile->GetRawInfo(autofill::ADDRESS_HOME_LINE2)); |
| - address.SetString("city", profile->GetRawInfo(autofill::ADDRESS_HOME_CITY)); |
| - address.SetString("state", profile->GetRawInfo(autofill::ADDRESS_HOME_STATE)); |
| + address.Set("recipient", list.release()); |
| + address.SetString( |
| + "organization", profile->GetRawInfo(autofill::COMPANY_NAME)); |
| + |
| + std::vector<base::string16> street_address; |
| + base::SplitString(profile->GetRawInfo(autofill::ADDRESS_HOME_STREET_ADDRESS), |
| + '\n', &street_address); |
| + scoped_ptr<base::ListValue> street_address_list(new base::ListValue); |
| + street_address_list->AppendStrings(street_address); |
| + address.Set("streetAddress", street_address_list.release()); |
| + |
| + address.SetString( |
| + "locality", profile->GetRawInfo(autofill::ADDRESS_HOME_CITY)); |
| + address.SetString( |
| + "adminArea", profile->GetRawInfo(autofill::ADDRESS_HOME_STATE)); |
| + address.SetString( |
| + "dependentLocality", |
| + profile->GetRawInfo(autofill::ADDRESS_HOME_DEPENDENT_LOCALITY)); |
| + address.SetString("sortingCode", |
| + profile->GetRawInfo(autofill::ADDRESS_HOME_SORTING_CODE)); |
| address.SetString("postalCode", |
| profile->GetRawInfo(autofill::ADDRESS_HOME_ZIP)); |
| address.SetString("country", |
| @@ -468,10 +538,37 @@ void AutofillOptionsHandler::LoadAddressEditor(const base::ListValue* args) { |
| address.Set("phone", list.release()); |
| GetValueList(*profile, autofill::EMAIL_ADDRESS, &list); |
| address.Set("email", list.release()); |
| + address.SetString("languageCode", profile->language_code()); |
| + |
| + scoped_ptr<base::ListValue> components(new base::ListValue); |
| + GetAddressComponents( |
| + UTF16ToUTF8(profile->GetRawInfo(autofill::ADDRESS_HOME_COUNTRY)), |
| + profile->language_code(), components.get(), NULL); |
| + address.Set("components", components.release()); |
| web_ui()->CallJavascriptFunction("AutofillOptions.editAddress", address); |
| } |
| +void AutofillOptionsHandler::LoadAddressEditorComponents( |
| + const base::ListValue* args) { |
| + std::string country_code; |
| + if (!args->GetString(0, &country_code)) { |
| + NOTREACHED(); |
| + return; |
| + } |
| + |
| + base::DictionaryValue input; |
| + scoped_ptr<base::ListValue> components(new base::ListValue); |
| + std::string language_code; |
| + GetAddressComponents(country_code, g_browser_process->GetApplicationLocale(), |
| + components.get(), &language_code); |
| + input.Set("components", components.release()); |
| + input.SetString("languageCode", language_code); |
| + |
| + web_ui()->CallJavascriptFunction( |
| + "AutofillEditAddressOverlay.loadAddressComponents", input); |
| +} |
| + |
| void AutofillOptionsHandler::LoadCreditCardEditor(const base::ListValue* args) { |
| DCHECK(IsPersonalDataLoaded()); |
| @@ -523,6 +620,7 @@ void AutofillOptionsHandler::SetAddress(const base::ListValue* args) { |
| AutofillProfile profile(guid, kSettingsOrigin); |
| std::string country_code; |
| + std::string language_code; |
| base::string16 value; |
| const base::ListValue* list_value; |
| if (args->GetList(1, &list_value)) |
| @@ -531,11 +629,18 @@ void AutofillOptionsHandler::SetAddress(const base::ListValue* args) { |
| if (args->GetString(2, &value)) |
| profile.SetRawInfo(autofill::COMPANY_NAME, value); |
| - if (args->GetString(3, &value)) |
| - profile.SetRawInfo(autofill::ADDRESS_HOME_LINE1, value); |
| + if (args->GetList(3, &list_value)) { |
| + std::vector<base::string16> street_address; |
| + for (size_t i = 0; i < list_value->GetSize(); ++i) { |
| + if (list_value->GetString(i, &value)) |
| + street_address.push_back(value); |
| + } |
| + profile.SetRawInfo(autofill::ADDRESS_HOME_STREET_ADDRESS, |
| + JoinString(street_address, '\n')); |
| + } |
| if (args->GetString(4, &value)) |
| - profile.SetRawInfo(autofill::ADDRESS_HOME_LINE2, value); |
| + profile.SetRawInfo(autofill::ADDRESS_HOME_DEPENDENT_LOCALITY, value); |
| if (args->GetString(5, &value)) |
| profile.SetRawInfo(autofill::ADDRESS_HOME_CITY, value); |
| @@ -546,16 +651,22 @@ void AutofillOptionsHandler::SetAddress(const base::ListValue* args) { |
| if (args->GetString(7, &value)) |
| profile.SetRawInfo(autofill::ADDRESS_HOME_ZIP, value); |
| - if (args->GetString(8, &country_code)) |
| + if (args->GetString(8, &value)) |
| + profile.SetRawInfo(autofill::ADDRESS_HOME_SORTING_CODE, value); |
| + |
| + if (args->GetString(9, &country_code)) |
| profile.SetRawInfo(autofill::ADDRESS_HOME_COUNTRY, |
| base::ASCIIToUTF16(country_code)); |
| - if (args->GetList(9, &list_value)) |
| + if (args->GetList(10, &list_value)) |
| SetValueList(list_value, autofill::PHONE_HOME_WHOLE_NUMBER, &profile); |
| - if (args->GetList(10, &list_value)) |
| + if (args->GetList(11, &list_value)) |
| SetValueList(list_value, autofill::EMAIL_ADDRESS, &profile); |
| + if (args->GetString(12, &language_code)) |
| + profile.set_language_code(language_code); |
| + |
| if (!base::IsValidGUID(profile.guid())) { |
| profile.set_guid(base::GenerateGUID()); |
| personal_data_->AddProfile(profile); |