File: autofill_dialog_models.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (110 lines) | stat: -rw-r--r-- 3,427 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ui/autofill/payments/autofill_dialog_models.h"

#include <string>

#include "base/strings/string_number_conversions.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "base/time/time.h"
#include "chrome/grit/generated_resources.h"
#include "components/autofill/core/common/autofill_clock.h"
#include "ui/base/l10n/l10n_util.h"

namespace autofill {

namespace {

// Number of years to be shown in the year combobox, including the current year.
// YearComboboxModel has the option of passing an additional year if not
// contained within the initial range.
const int kNumberOfExpirationYears = 10;

// Returns the items that are in the expiration year dropdown. If
// |additional_year| is not 0 and not within the normal range, it will be added
// accordingly.
std::vector<ui::SimpleComboboxModel::Item> GetExpirationYearItems(
    int additional_year) {
  std::vector<ui::SimpleComboboxModel::Item> years;
  // Add the "Year" placeholder item.
  years.emplace_back(
      l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_PLACEHOLDER_EXPIRY_YEAR));

  base::Time::Exploded now_exploded;
  AutofillClock::Now().LocalExplode(&now_exploded);

  if (additional_year != 0 && additional_year < now_exploded.year) {
    years.emplace_back(base::NumberToString16(additional_year));
  }

  for (int i = 0; i < kNumberOfExpirationYears; i++) {
    years.emplace_back(base::NumberToString16(now_exploded.year + i));
  }

  if (additional_year != 0 &&
      additional_year >= now_exploded.year + kNumberOfExpirationYears) {
    years.emplace_back(base::NumberToString16(additional_year));
  }

  return years;
}

// Formats a month, zero-padded (e.g. "02").
std::u16string FormatMonth(int month) {
  return base::ASCIIToUTF16(base::StringPrintf("%.2d", month));
}

}  // namespace

// MonthComboboxModel ----------------------------------------------------------

MonthComboboxModel::MonthComboboxModel() = default;

MonthComboboxModel::~MonthComboboxModel() = default;

size_t MonthComboboxModel::GetItemCount() const {
  // 12 months plus the empty entry.
  return 13;
}

std::u16string MonthComboboxModel::GetItemAt(size_t index) const {
  return index == 0 ? l10n_util::GetStringUTF16(
                          IDS_AUTOFILL_DIALOG_PLACEHOLDER_EXPIRY_MONTH)
                    : FormatMonth(static_cast<int>(index));
}

void MonthComboboxModel::SetDefaultIndexByMonth(int month) {
  if (month >= 1 && month <= 12) {
    default_index_ = static_cast<size_t>(month);
  }
}

std::optional<size_t> MonthComboboxModel::GetDefaultIndex() const {
  return default_index_;
}

// YearComboboxModel -----------------------------------------------------------

YearComboboxModel::YearComboboxModel(int additional_year)
    : ui::SimpleComboboxModel(GetExpirationYearItems(additional_year)) {}

YearComboboxModel::~YearComboboxModel() = default;

void YearComboboxModel::SetDefaultIndexByYear(int year) {
  const std::u16string& year_value = base::NumberToString16(year);
  for (size_t i = 1; i < GetItemCount(); i++) {
    if (year_value == GetItemAt(i)) {
      default_index_ = i;
      return;
    }
  }
}

std::optional<size_t> YearComboboxModel::GetDefaultIndex() const {
  return default_index_;
}

}  // namespace autofill