Commit 7e14c9e6 authored by estade@chromium.org's avatar estade@chromium.org

Parse full name set in chrome://settings/autofill

BUG=394273

Review URL: https://codereview.chromium.org/399003002

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@284635 0039d316-1c4b-4281-b951-d872f2087c98
parent e2184de5
...@@ -36,6 +36,7 @@ ...@@ -36,6 +36,7 @@
#include "ui/base/webui/web_ui_util.h" #include "ui/base/webui/web_ui_util.h"
using autofill::AutofillCountry; using autofill::AutofillCountry;
using autofill::AutofillType;
using autofill::ServerFieldType; using autofill::ServerFieldType;
using autofill::AutofillProfile; using autofill::AutofillProfile;
using autofill::CreditCard; using autofill::CreditCard;
...@@ -200,17 +201,15 @@ void GetValueList(const AutofillProfile& profile, ...@@ -200,17 +201,15 @@ void GetValueList(const AutofillProfile& profile,
} }
} }
// Set the multi-valued element for |type| from input |list| values. // Converts a ListValue of StringValues to a vector of string16s.
void SetValueList(const base::ListValue* list, void ListValueToStringVector(const base::ListValue& list,
ServerFieldType type, std::vector<base::string16>* output) {
AutofillProfile* profile) { output->resize(list.GetSize());
std::vector<base::string16> values(list->GetSize()); for (size_t i = 0; i < list.GetSize(); ++i) {
for (size_t i = 0; i < list->GetSize(); ++i) {
base::string16 value; base::string16 value;
if (list->GetString(i, &value)) if (list.GetString(i, &value))
values[i] = value; (*output)[i].swap(value);
} }
profile->SetRawMultiInfo(type, values);
} }
// Pulls the phone number |index|, |phone_number_list|, and |country_code| from // Pulls the phone number |index|, |phone_number_list|, and |country_code| from
...@@ -601,8 +600,13 @@ void AutofillOptionsHandler::SetAddress(const base::ListValue* args) { ...@@ -601,8 +600,13 @@ void AutofillOptionsHandler::SetAddress(const base::ListValue* args) {
base::string16 value; base::string16 value;
const base::ListValue* list_value; const base::ListValue* list_value;
if (args->GetList(arg_counter++, &list_value)) if (args->GetList(arg_counter++, &list_value)) {
SetValueList(list_value, autofill::NAME_FULL, &profile); std::vector<base::string16> values;
ListValueToStringVector(*list_value, &values);
profile.SetMultiInfo(AutofillType(autofill::NAME_FULL),
values,
g_browser_process->GetApplicationLocale());
}
if (args->GetString(arg_counter++, &value)) if (args->GetString(arg_counter++, &value))
profile.SetRawInfo(autofill::COMPANY_NAME, value); profile.SetRawInfo(autofill::COMPANY_NAME, value);
...@@ -628,11 +632,17 @@ void AutofillOptionsHandler::SetAddress(const base::ListValue* args) { ...@@ -628,11 +632,17 @@ void AutofillOptionsHandler::SetAddress(const base::ListValue* args) {
if (args->GetString(arg_counter++, &value)) if (args->GetString(arg_counter++, &value))
profile.SetRawInfo(autofill::ADDRESS_HOME_COUNTRY, value); profile.SetRawInfo(autofill::ADDRESS_HOME_COUNTRY, value);
if (args->GetList(arg_counter++, &list_value)) if (args->GetList(arg_counter++, &list_value)) {
SetValueList(list_value, autofill::PHONE_HOME_WHOLE_NUMBER, &profile); std::vector<base::string16> values;
ListValueToStringVector(*list_value, &values);
profile.SetRawMultiInfo(autofill::PHONE_HOME_WHOLE_NUMBER, values);
}
if (args->GetList(arg_counter++, &list_value)) if (args->GetList(arg_counter++, &list_value)) {
SetValueList(list_value, autofill::EMAIL_ADDRESS, &profile); std::vector<base::string16> values;
ListValueToStringVector(*list_value, &values);
profile.SetRawMultiInfo(autofill::EMAIL_ADDRESS, values);
}
if (args->GetString(arg_counter++, &value)) if (args->GetString(arg_counter++, &value))
profile.set_language_code(base::UTF16ToUTF8(value)); profile.set_language_code(base::UTF16ToUTF8(value));
......
...@@ -136,17 +136,30 @@ base::string16 GetFormGroupInfo(const FormGroup& form_group, ...@@ -136,17 +136,30 @@ base::string16 GetFormGroupInfo(const FormGroup& form_group,
} }
template <class T> template <class T>
void CopyValuesToItems(ServerFieldType type, void CopyRawValuesToItems(ServerFieldType type,
const std::vector<base::string16>& values, const std::vector<base::string16>& values,
std::vector<T>* form_group_items, const T& prototype,
const T& prototype) { std::vector<T>* form_group_items) {
form_group_items->resize(values.size(), prototype); form_group_items->resize(values.size(), prototype);
for (size_t i = 0; i < form_group_items->size(); ++i) { for (size_t i = 0; i < form_group_items->size(); ++i) {
(*form_group_items)[i].SetRawInfo(type, values[i]); (*form_group_items)[i].SetRawInfo(type, values[i]);
} }
// Must have at least one (possibly empty) element. // Must have at least one (possibly empty) element.
if (form_group_items->empty()) form_group_items->resize(std::max<size_t>(1UL, values.size()), prototype);
form_group_items->resize(1, prototype); }
template <class T>
void CopyValuesToItems(AutofillType type,
const std::vector<base::string16>& values,
const T& prototype,
const std::string& app_locale,
std::vector<T>* form_group_items) {
form_group_items->resize(values.size(), prototype);
for (size_t i = 0; i < form_group_items->size(); ++i) {
(*form_group_items)[i].SetInfo(type, values[i], app_locale);
}
// Must have at least one (possibly empty) element.
form_group_items->resize(std::max<size_t>(1UL, values.size()), prototype);
} }
template <class T> template <class T>
...@@ -349,22 +362,22 @@ void AutofillProfile::SetRawMultiInfo( ...@@ -349,22 +362,22 @@ void AutofillProfile::SetRawMultiInfo(
switch (AutofillType(type).group()) { switch (AutofillType(type).group()) {
case NAME: case NAME:
case NAME_BILLING: case NAME_BILLING:
CopyValuesToItems(type, values, &name_, NameInfo()); CopyRawValuesToItems(type, values, NameInfo(), &name_);
break; break;
case EMAIL: case EMAIL:
CopyValuesToItems(type, values, &email_, EmailInfo()); CopyRawValuesToItems(type, values, EmailInfo(), &email_);
break; break;
case PHONE_HOME: case PHONE_HOME:
case PHONE_BILLING: case PHONE_BILLING:
CopyValuesToItems(type, CopyRawValuesToItems(type, values, PhoneNumber(this), &phone_number_);
values,
&phone_number_,
PhoneNumber(this));
break; break;
default: default:
if (values.size() == 1) { if (values.size() == 1U) {
SetRawInfo(type, values[0]); SetRawInfo(type, values[0]);
} else if (values.size() == 0) { } else if (values.empty()) {
SetRawInfo(type, base::string16()); SetRawInfo(type, base::string16());
} else { } else {
// Shouldn't attempt to set multiple values on single-valued field. // Shouldn't attempt to set multiple values on single-valued field.
...@@ -380,6 +393,38 @@ void AutofillProfile::GetRawMultiInfo( ...@@ -380,6 +393,38 @@ void AutofillProfile::GetRawMultiInfo(
GetMultiInfoImpl(AutofillType(type), std::string(), values); GetMultiInfoImpl(AutofillType(type), std::string(), values);
} }
void AutofillProfile::SetMultiInfo(const AutofillType& type,
const std::vector<base::string16>& values,
const std::string& app_locale) {
switch (AutofillType(type).group()) {
case NAME:
case NAME_BILLING:
CopyValuesToItems(type, values, NameInfo(), app_locale, &name_);
break;
case EMAIL:
CopyValuesToItems(type, values, EmailInfo(), app_locale, &email_);
break;
case PHONE_HOME:
case PHONE_BILLING:
CopyValuesToItems(
type, values, PhoneNumber(this), app_locale, &phone_number_);
break;
default:
if (values.size() == 1U) {
SetInfo(type, values[0], app_locale);
} else if (values.empty()) {
SetInfo(type, base::string16(), app_locale);
} else {
// Shouldn't attempt to set multiple values on single-valued field.
NOTREACHED();
}
break;
}
}
void AutofillProfile::GetMultiInfo(const AutofillType& type, void AutofillProfile::GetMultiInfo(const AutofillType& type,
const std::string& app_locale, const std::string& app_locale,
std::vector<base::string16>* values) const { std::vector<base::string16>* values) const {
......
...@@ -64,6 +64,9 @@ class AutofillProfile : public AutofillDataModel { ...@@ -64,6 +64,9 @@ class AutofillProfile : public AutofillDataModel {
const std::vector<base::string16>& values); const std::vector<base::string16>& values);
void GetRawMultiInfo(ServerFieldType type, void GetRawMultiInfo(ServerFieldType type,
std::vector<base::string16>* values) const; std::vector<base::string16>* values) const;
void SetMultiInfo(const AutofillType& type,
const std::vector<base::string16>& values,
const std::string& app_locale);
void GetMultiInfo(const AutofillType& type, void GetMultiInfo(const AutofillType& type,
const std::string& app_locale, const std::string& app_locale,
std::vector<base::string16>* values) const; std::vector<base::string16>* values) const;
......
...@@ -672,6 +672,26 @@ TEST(AutofillProfileTest, AssignmentOperator) { ...@@ -672,6 +672,26 @@ TEST(AutofillProfileTest, AssignmentOperator) {
EXPECT_TRUE(a == b); EXPECT_TRUE(a == b);
} }
TEST(AutofillProfileTest, SetMultiInfo) {
std::vector<base::string16> full_names;
full_names.push_back(ASCIIToUTF16("John Davis"));
full_names.push_back(ASCIIToUTF16("Elouise Davis"));
AutofillProfile p;
p.SetMultiInfo(AutofillType(NAME_FULL), full_names, "en-US");
std::vector<base::string16> first_names;
p.GetMultiInfo(AutofillType(NAME_FIRST), "en-US", &first_names);
ASSERT_EQ(2U, first_names.size());
EXPECT_EQ(ASCIIToUTF16("John"), first_names[0]);
EXPECT_EQ(ASCIIToUTF16("Elouise"), first_names[1]);
std::vector<base::string16> last_names;
p.GetMultiInfo(AutofillType(NAME_LAST), "en-US", &last_names);
ASSERT_EQ(2U, last_names.size());
EXPECT_EQ(ASCIIToUTF16("Davis"), last_names[0]);
EXPECT_EQ(ASCIIToUTF16("Davis"), last_names[1]);
}
TEST(AutofillProfileTest, Copy) { TEST(AutofillProfileTest, Copy) {
AutofillProfile a(base::GenerateGUID(), "https://www.example.com/"); AutofillProfile a(base::GenerateGUID(), "https://www.example.com/");
test::SetProfileInfo(&a, "Marion", "Mitchell", "Morrison", test::SetProfileInfo(&a, "Marion", "Mitchell", "Morrison",
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment