Add ability to gather metrics to BubbleManager.
[chromium-blink-merge.git] / chrome / browser / ui / autofill / country_combobox_model.cc
blob5cff94340daf927a3e801f8a5183731a133ba9c0
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 #if !defined(OS_ANDROID)
43 // The separator item. On Android, there are separators after all items, so
44 // this is unnecessary.
45 countries_.push_back(NULL);
46 #endif
49 // The sorted list of countries.
50 std::vector<std::string> available_countries;
51 AutofillCountry::GetAvailableCountries(&available_countries);
53 #if defined(ENABLE_AUTOFILL_DIALOG)
54 // Filter out the countries that do not have rules for address input and
55 // validation.
56 const std::vector<std::string>& addressinput_countries =
57 ::i18n::addressinput::GetRegionCodes();
58 std::vector<std::string> filtered_countries;
59 filtered_countries.reserve(available_countries.size());
60 std::set_intersection(available_countries.begin(),
61 available_countries.end(),
62 addressinput_countries.begin(),
63 addressinput_countries.end(),
64 std::back_inserter(filtered_countries));
65 available_countries.swap(filtered_countries);
66 #endif
68 std::vector<AutofillCountry*> sorted_countries;
69 for (std::vector<std::string>::const_iterator it =
70 available_countries.begin(); it != available_countries.end(); ++it) {
71 if (filter.is_null() || filter.Run(*it))
72 sorted_countries.push_back(new AutofillCountry(*it, app_locale));
75 l10n_util::SortStringsUsingMethod(app_locale,
76 &sorted_countries,
77 &AutofillCountry::name);
78 countries_.insert(countries_.end(),
79 sorted_countries.begin(),
80 sorted_countries.end());
83 int CountryComboboxModel::GetItemCount() const {
84 return countries_.size();
87 base::string16 CountryComboboxModel::GetItemAt(int index) {
88 AutofillCountry* country = countries_[index];
89 if (country)
90 return countries_[index]->name();
92 // The separator item. Implemented for platforms that don't yet support
93 // IsItemSeparatorAt().
94 return base::ASCIIToUTF16("---");
97 bool CountryComboboxModel::IsItemSeparatorAt(int index) {
98 return !countries_[index];
101 std::string CountryComboboxModel::GetDefaultCountryCode() const {
102 return countries_[GetDefaultIndex()]->country_code();
105 } // namespace autofill