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

Side by Side Diff: chrome/browser/ui/autofill/country_combobox_model.cc

Issue 124533003: Add country combobox to change country and rebuild address inputs. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: mac Created 6 years, 11 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/ui/autofill/country_combobox_model.h" 5 #include "chrome/browser/ui/autofill/country_combobox_model.h"
6 6
7 #include "base/logging.h"
7 #include "base/strings/utf_string_conversions.h" 8 #include "base/strings/utf_string_conversions.h"
8 #include "chrome/browser/browser_process.h" 9 #include "chrome/browser/browser_process.h"
9 #include "components/autofill/core/browser/autofill_country.h" 10 #include "components/autofill/core/browser/autofill_country.h"
10 #include "components/autofill/core/browser/personal_data_manager.h" 11 #include "components/autofill/core/browser/personal_data_manager.h"
11 #include "ui/base/l10n/l10n_util_collator.h" 12 #include "ui/base/l10n/l10n_util_collator.h"
12 13
13 namespace autofill { 14 namespace autofill {
14 15
15 CountryComboboxModel::CountryComboboxModel(const PersonalDataManager& manager) { 16 CountryComboboxModel::CountryComboboxModel(const PersonalDataManager& manager)
17 : selected_index_(0) {
16 // Insert the default country at the top as well as in the ordered list. 18 // Insert the default country at the top as well as in the ordered list.
17 std::string app_locale = g_browser_process->GetApplicationLocale(); 19 const std::string& app_locale = g_browser_process->GetApplicationLocale();
18 std::string default_country_code = 20 std::string default_country_code =
19 manager.GetDefaultCountryCodeForNewAddress(); 21 manager.GetDefaultCountryCodeForNewAddress();
20 DCHECK(!default_country_code.empty()); 22 DCHECK(!default_country_code.empty());
21 23
22 countries_.push_back(new AutofillCountry(default_country_code, app_locale)); 24 countries_.push_back(new AutofillCountry(default_country_code, app_locale));
23 // The separator item. 25 // The separator item.
24 countries_.push_back(NULL); 26 countries_.push_back(NULL);
25 27
26 // The sorted list of countries. 28 // The sorted list of countries.
27 std::vector<std::string> country_codes; 29 std::vector<std::string> country_codes;
(...skipping 26 matching lines...) Expand all
54 56
55 // The separator item. Implemented for platforms that don't yet support 57 // The separator item. Implemented for platforms that don't yet support
56 // IsItemSeparatorAt(). 58 // IsItemSeparatorAt().
57 return base::ASCIIToUTF16("---"); 59 return base::ASCIIToUTF16("---");
58 } 60 }
59 61
60 bool CountryComboboxModel::IsItemSeparatorAt(int index) { 62 bool CountryComboboxModel::IsItemSeparatorAt(int index) {
61 return !countries_[index]; 63 return !countries_[index];
62 } 64 }
63 65
66 void CountryComboboxModel::SelectDefaultIndex() {
67 SelectIndex(GetDefaultIndex());
68 }
69
70 void CountryComboboxModel::SelectCountry(const std::string& country_code) {
71 DCHECK_EQ(2U, country_code.length());
72
73 if (country_code == GetSelectedCountryCode())
Evan Stade 2014/01/14 17:19:38 redundant imo
74 return;
75
76 for (size_t i = 0; i < countries_.size(); ++i) {
77 if (countries_[i] && countries_[i]->country_code() == country_code) {
78 SelectIndex(i);
79 return;
80 }
81 }
82
83 NOTREACHED();
84 }
85
86 std::string CountryComboboxModel::GetSelectedCountryCode() const {
87 return countries_[selected_index_]->country_code();
88 }
89
90 bool CountryComboboxModel::IsDefaultIndexSelected() const {
91 return selected_index_ == GetDefaultIndex();
92 }
93
94 void CountryComboboxModel::AddCountryComboboxObserver(
95 CountryComboboxModelObserver* observer) {
96 observers_.AddObserver(observer);
97 }
98
99 void CountryComboboxModel::RemoveCountryComboboxObserver(
100 CountryComboboxModelObserver* observer) {
101 observers_.RemoveObserver(observer);
102 }
103
104 void CountryComboboxModel::SelectIndex(int index) {
105 if (selected_index_ == index)
106 return;
107
108 DCHECK_GE(index, 0);
Evan Stade 2014/01/14 17:19:38 why do these dchecks come after the identity check
Dan Beam 2014/01/15 03:10:48 selected_index_ should always be valid, so index s
109 DCHECK_LT(index, static_cast<int>(countries_.size()));
110 DCHECK(!IsItemSeparatorAt(index));
111
112 selected_index_ = index;
113 FOR_EACH_OBSERVER(CountryComboboxModelObserver, observers_,
114 OnCountryComboboxModelChanged(this));
115 }
116
64 } // namespace autofill 117 } // namespace autofill
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698