1 // Copyright 2013 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 #import "ios/chrome/browser/autofill/autofill_agent_utils.h"
8 #include "base/macros.h"
9 #include "components/autofill/core/browser/autofill_manager.h"
10 #include "components/autofill/core/browser/detail_input.h"
11 #include "components/autofill/core/browser/dialog_section.h"
12 #include "components/autofill/core/browser/server_field_types_util.h"
13 #include "grit/components_strings.h"
14 #include "ios/chrome/browser/application_context.h"
15 #include "ui/base/l10n/l10n_util.h"
17 // TODO (sgrant): Switch to componentized version of this code when
18 // http://crbug/328070 is fixed.
19 // This code was largely copied from autofill_dialog_controller_android.cc
23 // Returns true if |input_type| in |section| is needed for |form_structure|.
24 bool IsSectionInputUsedInFormStructure(
25 autofill::DialogSection section,
26 autofill::ServerFieldType input_type,
27 const autofill::FormStructure& form_structure) {
28 autofill::DetailInput input;
29 input.length = autofill::DetailInput::SHORT;
30 input.type = input_type;
31 input.placeholder_text = base::string16();
32 input.expand_weight = 0;
34 for (size_t i = 0; i < form_structure.field_count(); ++i) {
35 const autofill::AutofillField* field = form_structure.field(i);
36 if (field && autofill::ServerTypeMatchesField(section, input.type, *field))
44 namespace autofill_agent_util {
46 // Determines if the |structure| has any fields that are of type
47 // autofill::CREDIT_CARD and thus asking for credit card info.
48 bool RequestingCreditCardInfo(const autofill::FormStructure* structure) {
51 size_t field_count = structure->field_count();
52 for (size_t i = 0; i < field_count; ++i) {
53 autofill::AutofillType type(structure->field(i)->Type().GetStorableType());
54 if (type.group() == autofill::CREDIT_CARD)
61 // Returns true if one of the nodes in |structure| request information related
62 // to a billing address.
63 bool RequestFullBillingAddress(autofill::FormStructure* structure) {
64 const autofill::ServerFieldType fieldsToCheckFor[] = {
65 autofill::ADDRESS_BILLING_LINE1,
66 autofill::ADDRESS_BILLING_LINE2,
67 autofill::ADDRESS_BILLING_CITY,
68 autofill::ADDRESS_BILLING_STATE,
69 autofill::PHONE_BILLING_WHOLE_NUMBER};
71 for (size_t i = 0; i < arraysize(fieldsToCheckFor); ++i) {
72 if (IsSectionInputUsedInFormStructure(autofill::SECTION_BILLING,
73 fieldsToCheckFor[i], *structure)) {
81 // Return empty info string for fill fields method.
82 base::string16 ReturnEmptyInfo(const autofill::AutofillType& type) {
83 return base::string16();
86 // Returns true if one of the nodes in |structure| request information related
87 // to a shipping address. To determine this actually attempt to fill the form
88 // using an empty data model that tracks which fields are requested.
89 bool RequestShippingAddress(autofill::FormStructure* structure) {
90 // Country code is unused for iOS and Android, so it
91 // doesn't matter what's passed.
92 std::string country_code;
93 autofill::DetailInputs inputs;
94 // TODO(eugenebut): Clean up kShippingInputs definition, unify with
95 // android codebase. crbug.com/371074
96 const autofill::DetailInput kShippingInputs[] = {
97 {autofill::DetailInput::LONG, autofill::NAME_FULL},
98 {autofill::DetailInput::LONG, autofill::ADDRESS_HOME_LINE1},
99 {autofill::DetailInput::LONG, autofill::ADDRESS_HOME_LINE2},
100 {autofill::DetailInput::LONG, autofill::ADDRESS_HOME_CITY},
101 {autofill::DetailInput::SHORT,
102 autofill::ADDRESS_HOME_STATE,
103 l10n_util::GetStringUTF16(IDS_AUTOFILL_FIELD_LABEL_STATE)},
104 {autofill::DetailInput::SHORT_EOL, autofill::ADDRESS_HOME_ZIP},
105 {autofill::DetailInput::NONE, autofill::ADDRESS_HOME_COUNTRY},
107 autofill::BuildInputs(kShippingInputs, arraysize(kShippingInputs), &inputs);
109 // TODO(ios): [Merge r284576]: The 4th argument to FillFields()
110 // is the language code based on either the billing or shipping address.
111 // See implementation in upstream's autofill_dialog_controller_impl.cc
112 // AutofillDialogControllerImpl::MutableAddressLanguageCodeForSection()
113 // Temporarily using std::string here to complete merge.
114 // See http://crbug/363063.
115 return structure->FillFields(
116 autofill::TypesFromInputs(inputs),
117 base::Bind(autofill::ServerTypeMatchesField, autofill::SECTION_SHIPPING),
118 base::Bind(&ReturnEmptyInfo), std::string(),
119 GetApplicationContext()->GetApplicationLocale());
122 // Returns true if one of the nodes in |structure| request information related
123 // to a phone number.
124 bool RequestPhoneNumber(autofill::FormStructure* structure) {
125 if (IsSectionInputUsedInFormStructure(autofill::SECTION_BILLING,
126 autofill::PHONE_BILLING_WHOLE_NUMBER,
131 if (IsSectionInputUsedInFormStructure(autofill::SECTION_SHIPPING,
132 autofill::PHONE_HOME_WHOLE_NUMBER,