autofill_dialog_common.cc revision 010d83a9304c5a91596085d917d248abff47903a
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.
4
5#include "chrome/browser/ui/autofill/autofill_dialog_common.h"
6
7#include "base/strings/utf_string_conversions.h"
8#include "chrome/browser/browser_process.h"
9#include "components/autofill/core/browser/autofill_country.h"
10#include "components/autofill/core/browser/autofill_field.h"
11#include "components/autofill/core/browser/autofill_type.h"
12
13namespace autofill {
14namespace common {
15
16bool ServerTypeEncompassesFieldType(ServerFieldType type,
17                                    const AutofillType& field_type) {
18  // If any credit card expiration info is asked for, show both month and year
19  // inputs.
20  ServerFieldType server_type = field_type.GetStorableType();
21  if (server_type == CREDIT_CARD_EXP_4_DIGIT_YEAR ||
22      server_type == CREDIT_CARD_EXP_2_DIGIT_YEAR ||
23      server_type == CREDIT_CARD_EXP_DATE_4_DIGIT_YEAR ||
24      server_type == CREDIT_CARD_EXP_DATE_2_DIGIT_YEAR ||
25      server_type == CREDIT_CARD_EXP_MONTH) {
26    return type == CREDIT_CARD_EXP_4_DIGIT_YEAR ||
27           type == CREDIT_CARD_EXP_MONTH;
28  }
29
30  if (server_type == CREDIT_CARD_TYPE)
31    return type == CREDIT_CARD_NUMBER;
32
33  // Check the groups to distinguish billing types from shipping ones.
34  AutofillType autofill_type = AutofillType(type);
35  if (autofill_type.group() != field_type.group())
36    return false;
37
38  // The page may ask for individual address lines; this roughly matches the
39  // street address blob.
40  if (server_type == ADDRESS_HOME_LINE1 || server_type == ADDRESS_HOME_LINE2)
41    return autofill_type.GetStorableType() == ADDRESS_HOME_STREET_ADDRESS;
42
43  return autofill_type.GetStorableType() == server_type;
44}
45
46bool ServerTypeMatchesField(DialogSection section,
47                            ServerFieldType type,
48                            const AutofillField& field) {
49  AutofillType field_type = field.Type();
50
51  // The credit card name is filled from the billing section's data.
52  if (field_type.GetStorableType() == CREDIT_CARD_NAME &&
53      (section == SECTION_BILLING || section == SECTION_CC_BILLING)) {
54    return type == NAME_BILLING_FULL;
55  }
56
57  return ServerTypeEncompassesFieldType(type, field_type);
58}
59
60bool IsCreditCardType(ServerFieldType type) {
61  return AutofillType(type).group() == CREDIT_CARD;
62}
63
64void BuildInputs(const DetailInput* input_template,
65                 size_t template_size,
66                 DetailInputs* inputs) {
67  for (size_t i = 0; i < template_size; ++i) {
68    const DetailInput* input = &input_template[i];
69    inputs->push_back(*input);
70  }
71}
72
73AutofillMetrics::DialogUiEvent DialogSectionToUiItemAddedEvent(
74    DialogSection section) {
75  switch (section) {
76    case SECTION_BILLING:
77      return AutofillMetrics::DIALOG_UI_BILLING_ITEM_ADDED;
78
79    case SECTION_CC_BILLING:
80      return AutofillMetrics::DIALOG_UI_CC_BILLING_ITEM_ADDED;
81
82    case SECTION_SHIPPING:
83      return AutofillMetrics::DIALOG_UI_SHIPPING_ITEM_ADDED;
84
85    case SECTION_CC:
86      return AutofillMetrics::DIALOG_UI_CC_ITEM_ADDED;
87  }
88
89  NOTREACHED();
90  return AutofillMetrics::NUM_DIALOG_UI_EVENTS;
91}
92
93AutofillMetrics::DialogUiEvent DialogSectionToUiSelectionChangedEvent(
94    DialogSection section) {
95  switch (section) {
96    case SECTION_BILLING:
97      return AutofillMetrics::DIALOG_UI_BILLING_SELECTED_SUGGESTION_CHANGED;
98
99    case SECTION_CC_BILLING:
100      return AutofillMetrics::DIALOG_UI_CC_BILLING_SELECTED_SUGGESTION_CHANGED;
101
102    case SECTION_SHIPPING:
103      return AutofillMetrics::DIALOG_UI_SHIPPING_SELECTED_SUGGESTION_CHANGED;
104
105    case SECTION_CC:
106      return AutofillMetrics::DIALOG_UI_CC_SELECTED_SUGGESTION_CHANGED;
107  }
108
109  NOTREACHED();
110  return AutofillMetrics::NUM_DIALOG_UI_EVENTS;
111}
112
113std::vector<ServerFieldType> TypesFromInputs(const DetailInputs& inputs) {
114  std::vector<ServerFieldType> types;
115  for (size_t i = 0; i < inputs.size(); ++i) {
116    types.push_back(inputs[i].type);
117  }
118  return types;
119}
120
121}  // namespace common
122}  // namespace autofill
123