Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / ui / autofill / country_combobox_model.cc
blob707b76fabe3a73d173aaacf1afbd2ea0eef4565d
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
3 // found in the LICENSE file.
5 #include "chrome/browser/ui/autofill/country_combobox_model.h"
7 #include "base/logging.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "chrome/browser/browser_process.h"
10 #include "components/autofill/core/browser/autofill_country.h"
11 #include "components/autofill/core/browser/personal_data_manager.h"
12 #include "ui/base/l10n/l10n_util_collator.h"
13 #include "ui/base/models/combobox_model_observer.h"
15 namespace autofill {
17 CountryComboboxModel::CountryComboboxModel(const PersonalDataManager& manager)
18 : default_index_(0) {
19 // Insert the default country at the top as well as in the ordered list.
20 const std::string& app_locale = g_browser_process->GetApplicationLocale();
21 std::string default_country_code =
22 manager.GetDefaultCountryCodeForNewAddress();
23 DCHECK(!default_country_code.empty());
25 countries_.push_back(new AutofillCountry(default_country_code, app_locale));
26 // The separator item.
27 countries_.push_back(NULL);
29 // The sorted list of countries.
30 std::vector<std::string> country_codes;
31 AutofillCountry::GetAvailableCountries(&country_codes);
32 std::vector<AutofillCountry*> sorted_countries;
34 for (std::vector<std::string>::iterator iter = country_codes.begin();
35 iter != country_codes.end(); ++iter) {
36 sorted_countries.push_back(new AutofillCountry(*iter, app_locale));
39 l10n_util::SortStringsUsingMethod(app_locale,
40 &sorted_countries,
41 &AutofillCountry::name);
42 countries_.insert(countries_.end(),
43 sorted_countries.begin(),
44 sorted_countries.end());
47 CountryComboboxModel::~CountryComboboxModel() {}
49 int CountryComboboxModel::GetItemCount() const {
50 return countries_.size();
53 base::string16 CountryComboboxModel::GetItemAt(int index) {
54 AutofillCountry* country = countries_[index];
55 if (country)
56 return countries_[index]->name();
58 // The separator item. Implemented for platforms that don't yet support
59 // IsItemSeparatorAt().
60 return base::ASCIIToUTF16("---");
63 bool CountryComboboxModel::IsItemSeparatorAt(int index) {
64 return !countries_[index];
67 int CountryComboboxModel::GetDefaultIndex() const {
68 return default_index_;
71 void CountryComboboxModel::AddObserver(ui::ComboboxModelObserver* observer) {
72 observers_.AddObserver(observer);
75 void CountryComboboxModel::RemoveObserver(ui::ComboboxModelObserver* observer) {
76 observers_.RemoveObserver(observer);
79 void CountryComboboxModel::ResetDefault() {
80 SetDefaultIndex(0);
83 void CountryComboboxModel::SetDefaultCountry(const std::string& country_code) {
84 DCHECK_EQ(2U, country_code.length());
86 for (size_t i = 0; i < countries_.size(); ++i) {
87 if (countries_[i] && countries_[i]->country_code() == country_code) {
88 SetDefaultIndex(i);
89 return;
93 NOTREACHED();
96 std::string CountryComboboxModel::GetDefaultCountryCode() const {
97 return countries_[default_index_]->country_code();
100 void CountryComboboxModel::SetDefaultIndex(int index) {
101 DCHECK_GE(index, 0);
102 DCHECK_LT(index, static_cast<int>(countries_.size()));
103 DCHECK(!IsItemSeparatorAt(index));
105 if (index == default_index_)
106 return;
108 default_index_ = index;
109 FOR_EACH_OBSERVER(ui::ComboboxModelObserver, observers_,
110 OnComboboxModelChanged(this));
113 } // namespace autofill