1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/extensions/api/autofill_private/autofill_util.h"
7 #include "base/prefs/pref_service.h"
8 #include "base/strings/string_split.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "chrome/browser/browser_process.h"
11 #include "chrome/browser/extensions/api/settings_private/prefs_util.h"
12 #include "chrome/browser/profiles/profile.h"
13 #include "chrome/common/extensions/api/autofill_private.h"
14 #include "chrome/common/pref_names.h"
15 #include "components/autofill/core/browser/autofill_profile.h"
16 #include "components/autofill/core/browser/autofill_type.h"
17 #include "components/autofill/core/browser/credit_card.h"
18 #include "components/autofill/core/browser/field_types.h"
19 #include "grit/components_strings.h"
20 #include "ui/base/l10n/l10n_util.h"
22 namespace autofill_private
= extensions::api::autofill_private
;
26 // Get the multi-valued element for |type| and return it as a |vector|.
27 // TODO(khorimoto): remove this function since multi-valued types are
29 scoped_ptr
<std::vector
<std::string
>> GetValueList(
30 const autofill::AutofillProfile
& profile
, autofill::ServerFieldType type
) {
31 scoped_ptr
<std::vector
<std::string
>> list(new std::vector
<std::string
>);
33 std::vector
<base::string16
> values
;
34 if (autofill::AutofillType(type
).group() == autofill::NAME
) {
36 profile
.GetInfo(autofill::AutofillType(type
),
37 g_browser_process
->GetApplicationLocale()));
39 values
.push_back(profile
.GetRawInfo(type
));
42 // |Get[Raw]MultiInfo()| always returns at least one, potentially empty, item.
43 // If this is the case, there is no info to return, so return an empty vector.
44 if (values
.size() == 1 && values
.front().empty())
47 for (const base::string16
& value16
: values
)
48 list
->push_back(base::UTF16ToUTF8(value16
));
53 // Gets the string corresponding to |type| from |profile|.
54 scoped_ptr
<std::string
> GetStringFromProfile(
55 const autofill::AutofillProfile
& profile
,
56 const autofill::ServerFieldType
& type
) {
57 return make_scoped_ptr(
58 new std::string(base::UTF16ToUTF8(profile
.GetRawInfo(type
))));
61 scoped_ptr
<autofill_private::AddressEntry
> ProfileToAddressEntry(
62 const autofill::AutofillProfile
& profile
,
63 const base::string16
& label
) {
64 scoped_ptr
<autofill_private::AddressEntry
>
65 address(new autofill_private::AddressEntry
);
67 // Add all address fields to the entry.
68 address
->guid
.reset(new std::string(profile
.guid()));
69 address
->full_names
= GetValueList(profile
, autofill::NAME_FULL
);
70 address
->company_name
.reset(GetStringFromProfile(
71 profile
, autofill::COMPANY_NAME
).release());
72 address
->address_lines
.reset(GetStringFromProfile(
73 profile
, autofill::ADDRESS_HOME_STREET_ADDRESS
).release());
74 address
->address_level1
.reset(GetStringFromProfile(
75 profile
, autofill::ADDRESS_HOME_STATE
).release());
76 address
->address_level2
.reset(GetStringFromProfile(
77 profile
, autofill::ADDRESS_HOME_CITY
).release());
78 address
->address_level3
.reset(GetStringFromProfile(
79 profile
, autofill::ADDRESS_HOME_DEPENDENT_LOCALITY
).release());
80 address
->postal_code
.reset(GetStringFromProfile(
81 profile
, autofill::ADDRESS_HOME_ZIP
).release());
82 address
->sorting_code
.reset(GetStringFromProfile(
83 profile
, autofill::ADDRESS_HOME_SORTING_CODE
).release());
84 address
->country_code
.reset(GetStringFromProfile(
85 profile
, autofill::ADDRESS_HOME_COUNTRY
).release());
86 address
->phone_numbers
=
87 GetValueList(profile
, autofill::PHONE_HOME_WHOLE_NUMBER
);
88 address
->email_addresses
=
89 GetValueList(profile
, autofill::EMAIL_ADDRESS
);
90 address
->language_code
.reset(new std::string(profile
.language_code()));
92 // Parse |label| so that it can be used to create address metadata.
93 base::string16 separator
=
94 l10n_util::GetStringUTF16(IDS_AUTOFILL_ADDRESS_SUMMARY_SEPARATOR
);
95 std::vector
<base::string16
> label_pieces
;
96 base::SplitStringUsingSubstr(label
, separator
, &label_pieces
);
98 // Create address metadata and add it to |address|.
99 scoped_ptr
<autofill_private::AutofillMetadata
>
100 metadata(new autofill_private::AutofillMetadata
);
101 metadata
->summary_label
= base::UTF16ToUTF8(label_pieces
[0]);
102 metadata
->summary_sublabel
.reset(new std::string(base::UTF16ToUTF8(
103 label
.substr(label_pieces
[0].size()))));
104 metadata
->is_local
.reset(new bool(
105 profile
.record_type() == autofill::AutofillProfile::LOCAL_PROFILE
));
106 address
->metadata
= metadata
.Pass();
108 return address
.Pass();
111 scoped_ptr
<autofill_private::CreditCardEntry
> CreditCardToCreditCardEntry(
112 const autofill::CreditCard
& credit_card
) {
113 scoped_ptr
<autofill_private::CreditCardEntry
>
114 card(new autofill_private::CreditCardEntry
);
116 // Add all credit card fields to the entry.
117 card
->guid
.reset(new std::string(credit_card
.guid()));
118 card
->name
.reset(new std::string(base::UTF16ToUTF8(
119 credit_card
.GetRawInfo(autofill::CREDIT_CARD_NAME
))));
120 card
->card_number
.reset(new std::string(base::UTF16ToUTF8(
121 credit_card
.GetRawInfo(autofill::CREDIT_CARD_NUMBER
))));
122 card
->expiration_month
.reset(new std::string(base::UTF16ToUTF8(
123 credit_card
.GetRawInfo(autofill::CREDIT_CARD_EXP_MONTH
))));
124 card
->expiration_year
.reset(new std::string(base::UTF16ToUTF8(
125 credit_card
.GetRawInfo(autofill::CREDIT_CARD_EXP_4_DIGIT_YEAR
))));
127 // Create address metadata and add it to |address|.
128 scoped_ptr
<autofill_private::AutofillMetadata
>
129 metadata(new autofill_private::AutofillMetadata
);
130 std::pair
<base::string16
, base::string16
> label_pieces
=
131 credit_card
.LabelPieces();
132 metadata
->summary_label
= base::UTF16ToUTF8(label_pieces
.first
);
133 metadata
->summary_sublabel
.reset(new std::string(base::UTF16ToUTF8(
134 label_pieces
.second
)));
135 metadata
->is_local
.reset(new bool(
136 credit_card
.record_type() == autofill::CreditCard::LOCAL_CARD
));
137 metadata
->is_cached
.reset(new bool(
138 credit_card
.record_type() == autofill::CreditCard::FULL_SERVER_CARD
));
139 card
->metadata
= metadata
.Pass();
146 namespace extensions
{
148 namespace autofill_util
{
150 scoped_ptr
<AddressEntryList
> GenerateAddressList(
151 const autofill::PersonalDataManager
& personal_data
) {
152 const std::vector
<autofill::AutofillProfile
*>& profiles
=
153 personal_data
.GetProfiles();
154 std::vector
<base::string16
> labels
;
155 autofill::AutofillProfile::CreateDifferentiatingLabels(
157 g_browser_process
->GetApplicationLocale(),
159 DCHECK_EQ(labels
.size(), profiles
.size());
161 scoped_ptr
<AddressEntryList
> list(new AddressEntryList
);
162 for (size_t i
= 0; i
< profiles
.size(); ++i
) {
163 autofill_private::AddressEntry
* entry
=
164 ProfileToAddressEntry(*profiles
[i
], labels
[i
]).release();
165 list
->push_back(linked_ptr
<autofill_private::AddressEntry
>(entry
));
171 scoped_ptr
<CreditCardEntryList
> GenerateCreditCardList(
172 const autofill::PersonalDataManager
& personal_data
) {
173 const std::vector
<autofill::CreditCard
*>& cards
=
174 personal_data
.GetCreditCards();
176 scoped_ptr
<CreditCardEntryList
> list(new CreditCardEntryList
);
177 for (const autofill::CreditCard
* card
: cards
) {
178 autofill_private::CreditCardEntry
* entry
=
179 CreditCardToCreditCardEntry(*card
).release();
180 list
->push_back(linked_ptr
<autofill_private::CreditCardEntry
>(entry
));
186 } // namespace autofill_util
188 } // namespace extensions