Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(52)

Unified Diff: chrome/browser/ui/autofill/autofill_dialog_models.cc

Issue 2918133002: [Merge M60][Payments] Refactor the year and month combobox models and preselect. (Closed)
Patch Set: Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/browser/ui/autofill/autofill_dialog_models.cc
diff --git a/chrome/browser/ui/autofill/autofill_dialog_models.cc b/chrome/browser/ui/autofill/autofill_dialog_models.cc
index 71f02b10f9c29fe5b629a86eb7c5ff6cf5f0316e..c0ad8efd5868b73646b8efc82e49715fbc414ea8 100644
--- a/chrome/browser/ui/autofill/autofill_dialog_models.cc
+++ b/chrome/browser/ui/autofill/autofill_dialog_models.cc
@@ -1,21 +1,60 @@
-// Copyright (c) 2012 The Chromium Authors. All rights reserved.
+// Copyright 2012 The Chromium Authors. All rights reserved.
// 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/autofill_dialog_models.h"
-#include "base/bind.h"
+#include "base/strings/string16.h"
#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/browser/autofill_country.h"
-#include "components/prefs/pref_service.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<base::string16> GetExpirationYearItems(int additional_year) {
+ std::vector<base::string16> years;
+ // Add the "Year" placeholder item.
+ years.push_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.push_back(base::UTF8ToUTF16(std::to_string(additional_year)));
+
+ for (int i = 0; i < kNumberOfExpirationYears; i++)
+ years.push_back(base::UTF8ToUTF16(std::to_string(now_exploded.year + i)));
+
+ if (additional_year != 0 &&
+ additional_year >= now_exploded.year + kNumberOfExpirationYears) {
+ years.push_back(base::UTF8ToUTF16(std::to_string(additional_year)));
+ }
+
+ return years;
+}
+
+// Formats a month, zero-padded (e.g. "02").
+base::string16 FormatMonth(int month) {
+ return base::ASCIIToUTF16(base::StringPrintf("%.2d", month));
+}
+
+} // namespace
+
SuggestionsMenuModelDelegate::~SuggestionsMenuModelDelegate() {}
// SuggestionsMenuModel ----------------------------------------------------
@@ -135,11 +174,6 @@ int MonthComboboxModel::GetItemCount() const {
return 13;
}
-// static
-base::string16 MonthComboboxModel::FormatMonth(int index) {
- return base::ASCIIToUTF16(base::StringPrintf("%.2d", index));
-}
-
base::string16 MonthComboboxModel::GetItemAt(int index) {
return index == 0 ?
l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_PLACEHOLDER_EXPIRY_MONTH) :
@@ -148,27 +182,9 @@ base::string16 MonthComboboxModel::GetItemAt(int index) {
// YearComboboxModel -----------------------------------------------------------
-YearComboboxModel::YearComboboxModel() : this_year_(0) {
- base::Time time = base::Time::Now();
- base::Time::Exploded exploded;
- time.LocalExplode(&exploded);
- this_year_ = exploded.year;
-}
+YearComboboxModel::YearComboboxModel(int additional_year)
+ : ui::SimpleComboboxModel(GetExpirationYearItems(additional_year)) {}
YearComboboxModel::~YearComboboxModel() {}
-int YearComboboxModel::GetItemCount() const {
- // 10 years plus the empty entry.
- return 11;
-}
-
-base::string16 YearComboboxModel::GetItemAt(int index) {
- if (index == 0) {
- return l10n_util::GetStringUTF16(
- IDS_AUTOFILL_DIALOG_PLACEHOLDER_EXPIRY_YEAR);
- }
-
- return base::IntToString16(this_year_ + index - 1);
-}
-
} // namespace autofill
« no previous file with comments | « chrome/browser/ui/autofill/autofill_dialog_models.h ('k') | chrome/browser/ui/autofill/autofill_dialog_models_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698