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