autofill_dialog_common.cc revision cedac228d2dd51db4b79ea1e72c7f249408ee061
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          field_type.html_type() == HTML_TYPE_FULL_ADDRESS) {
42    return autofill_type.GetStorableType() == ADDRESS_HOME_STREET_ADDRESS;
43  }
44
45  return autofill_type.GetStorableType() == server_type;
46}
47
48bool ServerTypeMatchesField(DialogSection section,
49                            ServerFieldType type,
50                            const AutofillField& field) {
51  AutofillType field_type = field.Type();
52
53  // The credit card name is filled from the billing section's data.
54  if (field_type.GetStorableType() == CREDIT_CARD_NAME &&
55      (section == SECTION_BILLING || section == SECTION_CC_BILLING)) {
56    return type == NAME_BILLING_FULL;
57  }
58
59  return ServerTypeEncompassesFieldType(type, field_type);
60}
61
62bool IsCreditCardType(ServerFieldType type) {
63  return AutofillType(type).group() == CREDIT_CARD;
64}
65
66void BuildInputs(const DetailInput* input_template,
67                 size_t template_size,
68                 DetailInputs* inputs) {
69  for (size_t i = 0; i < template_size; ++i) {
70    const DetailInput* input = &input_template[i];
71    inputs->push_back(*input);
72  }
73}
74
75AutofillMetrics::DialogUiEvent DialogSectionToUiItemAddedEvent(
76    DialogSection section) {
77  switch (section) {
78    case SECTION_BILLING:
79      return AutofillMetrics::DIALOG_UI_BILLING_ITEM_ADDED;
80
81    case SECTION_CC_BILLING:
82      return AutofillMetrics::DIALOG_UI_CC_BILLING_ITEM_ADDED;
83
84    case SECTION_SHIPPING:
85      return AutofillMetrics::DIALOG_UI_SHIPPING_ITEM_ADDED;
86
87    case SECTION_CC:
88      return AutofillMetrics::DIALOG_UI_CC_ITEM_ADDED;
89  }
90
91  NOTREACHED();
92  return AutofillMetrics::NUM_DIALOG_UI_EVENTS;
93}
94
95AutofillMetrics::DialogUiEvent DialogSectionToUiSelectionChangedEvent(
96    DialogSection section) {
97  switch (section) {
98    case SECTION_BILLING:
99      return AutofillMetrics::DIALOG_UI_BILLING_SELECTED_SUGGESTION_CHANGED;
100
101    case SECTION_CC_BILLING:
102      return AutofillMetrics::DIALOG_UI_CC_BILLING_SELECTED_SUGGESTION_CHANGED;
103
104    case SECTION_SHIPPING:
105      return AutofillMetrics::DIALOG_UI_SHIPPING_SELECTED_SUGGESTION_CHANGED;
106
107    case SECTION_CC:
108      return AutofillMetrics::DIALOG_UI_CC_SELECTED_SUGGESTION_CHANGED;
109  }
110
111  NOTREACHED();
112  return AutofillMetrics::NUM_DIALOG_UI_EVENTS;
113}
114
115std::vector<ServerFieldType> TypesFromInputs(const DetailInputs& inputs) {
116  std::vector<ServerFieldType> types;
117  for (size_t i = 0; i < inputs.size(); ++i) {
118    types.push_back(inputs[i].type);
119  }
120  return types;
121}
122
123}  // namespace common
124}  // namespace autofill
125