country_combobox_model.cc revision 6e8cce623b6e4fe0c9e4af605d675dd9d0338c38
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.
4
5#include "chrome/browser/ui/autofill/country_combobox_model.h"
6
7#include <algorithm>
8#include <iterator>
9
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"
17
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
22
23namespace autofill {
24
25CountryComboboxModel::CountryComboboxModel() {}
26
27CountryComboboxModel::~CountryComboboxModel() {}
28
29void CountryComboboxModel::SetCountries(
30    const PersonalDataManager& manager,
31    const base::Callback<bool(const std::string&)>& filter) {
32  countries_.clear();
33
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());
38
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
47  }
48
49  // The sorted list of countries.
50  std::vector<std::string> available_countries;
51  AutofillCountry::GetAvailableCountries(&available_countries);
52
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
67
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));
73  }
74
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());
81}
82
83int CountryComboboxModel::GetItemCount() const {
84  return countries_.size();
85}
86
87base::string16 CountryComboboxModel::GetItemAt(int index) {
88  AutofillCountry* country = countries_[index];
89  if (country)
90    return countries_[index]->name();
91
92  // The separator item. Implemented for platforms that don't yet support
93  // IsItemSeparatorAt().
94  return base::ASCIIToUTF16("---");
95}
96
97bool CountryComboboxModel::IsItemSeparatorAt(int index) {
98  return !countries_[index];
99}
100
101std::string CountryComboboxModel::GetDefaultCountryCode() const {
102  return countries_[GetDefaultIndex()]->country_code();
103}
104
105}  // namespace autofill
106