Infobar material design refresh: layout
[chromium-blink-merge.git] / chrome / browser / ui / autofill / data_model_wrapper.cc
blob76b364112738fc55a9c9f7cef95403dd9f4c4e41
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
3 // found in the LICENSE file.
5 #include "chrome/browser/ui/autofill/data_model_wrapper.h"
7 #include "base/bind.h"
8 #include "base/callback.h"
9 #include "base/strings/string_util.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "chrome/browser/browser_process.h"
12 #include "chrome/browser/ui/autofill/autofill_dialog_i18n_input.h"
13 #include "chrome/browser/ui/autofill/autofill_dialog_models.h"
14 #include "components/autofill/core/browser/address_i18n.h"
15 #include "components/autofill/core/browser/autofill_country.h"
16 #include "components/autofill/core/browser/autofill_data_model.h"
17 #include "components/autofill/core/browser/autofill_field.h"
18 #include "components/autofill/core/browser/autofill_profile.h"
19 #include "components/autofill/core/browser/autofill_type.h"
20 #include "components/autofill/core/browser/credit_card.h"
21 #include "components/autofill/core/browser/form_structure.h"
22 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_data.h"
23 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_field.h"
24 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_formatter.h"
25 #include "ui/base/resource/resource_bundle.h"
26 #include "ui/gfx/image/image.h"
28 namespace autofill {
30 DataModelWrapper::~DataModelWrapper() {}
32 void DataModelWrapper::FillInputs(DetailInputs* inputs) {
33 for (size_t i = 0; i < inputs->size(); ++i) {
34 (*inputs)[i].initial_value =
35 GetInfoForDisplay(AutofillType((*inputs)[i].type));
39 base::string16 DataModelWrapper::GetInfoForDisplay(const AutofillType& type)
40 const {
41 return GetInfo(type);
44 gfx::Image DataModelWrapper::GetIcon() {
45 return gfx::Image();
48 bool DataModelWrapper::GetDisplayText(
49 base::string16* vertically_compact,
50 base::string16* horizontally_compact) {
51 base::string16 phone =
52 GetInfoForDisplay(AutofillType(PHONE_HOME_WHOLE_NUMBER));
53 if (phone.empty())
54 return false;
56 // Format the address.
57 scoped_ptr< ::i18n::addressinput::AddressData> address_data =
58 i18n::CreateAddressData(
59 base::Bind(&DataModelWrapper::GetInfo, base::Unretained(this)));
60 address_data->language_code = GetLanguageCode();
61 // Interactive autofill dialog does not display organization field.
62 address_data->organization.clear();
63 std::vector<std::string> lines;
64 ::i18n::addressinput::GetFormattedNationalAddress(*address_data, &lines);
66 std::string single_line;
67 ::i18n::addressinput::GetFormattedNationalAddressLine(
68 *address_data, &single_line);
70 // Email and phone number aren't part of address formatting.
71 base::string16 non_address_info;
72 base::string16 email = GetInfoForDisplay(AutofillType(EMAIL_ADDRESS));
73 if (!email.empty())
74 non_address_info += base::ASCIIToUTF16("\n") + email;
76 non_address_info += base::ASCIIToUTF16("\n") + phone;
78 *vertically_compact = base::UTF8ToUTF16(single_line) + non_address_info;
79 *horizontally_compact = base::UTF8ToUTF16(base::JoinString(lines, "\n")) +
80 non_address_info;
82 return true;
85 bool DataModelWrapper::FillFormStructure(
86 const std::vector<ServerFieldType>& types,
87 const FormStructure::InputFieldComparator& compare,
88 FormStructure* form_structure) const {
89 return form_structure->FillFields(
90 types,
91 compare,
92 base::Bind(&DataModelWrapper::GetInfo, base::Unretained(this)),
93 GetLanguageCode(),
94 g_browser_process->GetApplicationLocale());
97 DataModelWrapper::DataModelWrapper() {}
99 // AutofillProfileWrapper
101 AutofillProfileWrapper::AutofillProfileWrapper(const AutofillProfile* profile)
102 : profile_(profile) {}
104 AutofillProfileWrapper::~AutofillProfileWrapper() {}
106 base::string16 AutofillProfileWrapper::GetInfo(const AutofillType& type) const {
107 // Requests for the user's credit card are filled from the billing address,
108 // but the AutofillProfile class doesn't know how to fill credit card
109 // fields. So, request for the corresponding profile type instead.
110 AutofillType effective_type = type;
111 if (type.GetStorableType() == CREDIT_CARD_NAME)
112 effective_type = AutofillType(NAME_BILLING_FULL);
114 const std::string& app_locale = g_browser_process->GetApplicationLocale();
115 return profile_->GetInfo(effective_type, app_locale);
118 base::string16 AutofillProfileWrapper::GetInfoForDisplay(
119 const AutofillType& type) const {
120 // We display the "raw" phone number which contains user-defined formatting.
121 if (type.GetStorableType() == PHONE_HOME_WHOLE_NUMBER) {
122 base::string16 phone_number = profile_->GetRawInfo(type.GetStorableType());
124 // If there is no user-defined formatting at all, add some standard
125 // formatting.
126 if (base::ContainsOnlyChars(phone_number,
127 base::ASCIIToUTF16("+0123456789"))) {
128 std::string region = base::UTF16ToASCII(
129 GetInfo(AutofillType(HTML_TYPE_COUNTRY_CODE, HTML_MODE_NONE)));
130 i18n::PhoneObject phone(phone_number, region);
131 base::string16 formatted_number = phone.GetFormattedNumber();
132 // Formatting may fail.
133 if (!formatted_number.empty())
134 return formatted_number;
137 return phone_number;
140 return DataModelWrapper::GetInfoForDisplay(type);
143 const std::string& AutofillProfileWrapper::GetLanguageCode() const {
144 return profile_->language_code();
147 // AutofillShippingAddressWrapper
149 AutofillShippingAddressWrapper::AutofillShippingAddressWrapper(
150 const AutofillProfile* profile)
151 : AutofillProfileWrapper(profile) {}
153 AutofillShippingAddressWrapper::~AutofillShippingAddressWrapper() {}
155 base::string16 AutofillShippingAddressWrapper::GetInfo(
156 const AutofillType& type) const {
157 // Shipping addresses don't have email addresses associated with them.
158 if (type.GetStorableType() == EMAIL_ADDRESS)
159 return base::string16();
161 return AutofillProfileWrapper::GetInfo(type);
164 // AutofillCreditCardWrapper
166 AutofillCreditCardWrapper::AutofillCreditCardWrapper(const CreditCard* card)
167 : card_(card) {}
169 AutofillCreditCardWrapper::~AutofillCreditCardWrapper() {}
171 base::string16 AutofillCreditCardWrapper::GetInfo(const AutofillType& type)
172 const {
173 if (type.group() != CREDIT_CARD)
174 return base::string16();
176 if (type.GetStorableType() == CREDIT_CARD_EXP_MONTH)
177 return MonthComboboxModel::FormatMonth(card_->expiration_month());
179 return card_->GetInfo(type, g_browser_process->GetApplicationLocale());
182 gfx::Image AutofillCreditCardWrapper::GetIcon() {
183 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
184 return rb.GetImageNamed(CreditCard::IconResourceId(card_->type()));
187 bool AutofillCreditCardWrapper::GetDisplayText(
188 base::string16* vertically_compact,
189 base::string16* horizontally_compact) {
190 if (!card_->IsValid())
191 return false;
193 *vertically_compact = *horizontally_compact = card_->TypeAndLastFourDigits();
194 return true;
197 const std::string& AutofillCreditCardWrapper::GetLanguageCode() const {
198 // Formatting a credit card for display does not depend on language code.
199 return base::EmptyString();
202 // I18nAddressDataWrapper
204 I18nAddressDataWrapper::I18nAddressDataWrapper(
205 const ::i18n::addressinput::AddressData* address)
206 : address_(address) {}
208 I18nAddressDataWrapper::~I18nAddressDataWrapper() {}
210 base::string16 I18nAddressDataWrapper::GetInfo(const AutofillType& type) const {
211 ::i18n::addressinput::AddressField field;
212 if (!i18n::FieldForType(type.GetStorableType(), &field))
213 return base::string16();
215 if (field == ::i18n::addressinput::STREET_ADDRESS)
216 return base::string16();
218 if (field == ::i18n::addressinput::COUNTRY) {
219 return AutofillCountry(address_->region_code,
220 g_browser_process->GetApplicationLocale()).name();
223 return base::UTF8ToUTF16(address_->GetFieldValue(field));
226 const std::string& I18nAddressDataWrapper::GetLanguageCode() const {
227 return address_->language_code;
230 } // namespace autofill