Extend EnrollmentHandler to handle consumer management.
[chromium-blink-merge.git] / chrome / browser / ui / autofill / country_combobox_model.cc
blobfda64cb4407d06e7e507e3f2c62be4a13dfa2154
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 <algorithm>
8 #include <iterator>
10 #include "base/logging.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "chrome/browser/browser_process.h"
13 #include "components/autofill/core/browser/autofill_country.h"
14 #include "components/autofill/core/browser/personal_data_manager.h"
15 #include "ui/base/l10n/l10n_util_collator.h"
16 #include "ui/base/models/combobox_model_observer.h"
18 // TODO(rouslan): Remove this check. http://crbug.com/337587
19 #if defined(ENABLE_AUTOFILL_DIALOG)
20 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_ui.h"
21 #endif
23 namespace autofill {
25 CountryComboboxModel::CountryComboboxModel() {}
27 CountryComboboxModel::~CountryComboboxModel() {}
29 void CountryComboboxModel::SetCountries(
30 const PersonalDataManager& manager,
31 const base::Callback<bool(const std::string&)>& filter) {
32 countries_.clear();
34 // Insert the default country at the top as well as in the ordered list.
35 std::string default_country_code =
36 manager.GetDefaultCountryCodeForNewAddress();
37 DCHECK(!default_country_code.empty());
39 const std::string& app_locale = g_browser_process->GetApplicationLocale();
40 if (filter.is_null() || filter.Run(default_country_code)) {
41 countries_.push_back(new AutofillCountry(default_country_code, app_locale));
42 // The separator item.
43 countries_.push_back(NULL);
46 // The sorted list of countries.
47 std::vector<std::string> available_countries;
48 AutofillCountry::GetAvailableCountries(&available_countries);
50 #if defined(ENABLE_AUTOFILL_DIALOG)
51 // Filter out the countries that do not have rules for address input and
52 // validation.
53 const std::vector<std::string>& addressinput_countries =
54 ::i18n::addressinput::GetRegionCodes();
55 std::vector<std::string> filtered_countries;
56 filtered_countries.reserve(available_countries.size());
57 std::set_intersection(available_countries.begin(),
58 available_countries.end(),
59 addressinput_countries.begin(),
60 addressinput_countries.end(),
61 std::back_inserter(filtered_countries));
62 available_countries.swap(filtered_countries);
63 #endif
65 std::vector<AutofillCountry*> sorted_countries;
66 for (std::vector<std::string>::const_iterator it =
67 available_countries.begin(); it != available_countries.end(); ++it) {
68 if (filter.is_null() || filter.Run(*it))
69 sorted_countries.push_back(new AutofillCountry(*it, app_locale));
72 l10n_util::SortStringsUsingMethod(app_locale,
73 &sorted_countries,
74 &AutofillCountry::name);
75 countries_.insert(countries_.end(),
76 sorted_countries.begin(),
77 sorted_countries.end());
80 int CountryComboboxModel::GetItemCount() const {
81 return countries_.size();
84 base::string16 CountryComboboxModel::GetItemAt(int index) {
85 AutofillCountry* country = countries_[index];
86 if (country)
87 return countries_[index]->name();
89 // The separator item. Implemented for platforms that don't yet support
90 // IsItemSeparatorAt().
91 return base::ASCIIToUTF16("---");
94 bool CountryComboboxModel::IsItemSeparatorAt(int index) {
95 return !countries_[index];
98 std::string CountryComboboxModel::GetDefaultCountryCode() const {
99 return countries_[GetDefaultIndex()]->country_code();
102 } // namespace autofill