country_combobox_model.cc revision 5d1f7b1de12d16ceb2c938c56701a3e8bfa558f7
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 "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"
14
15// TODO(rouslan): Remove this check. http://crbug.com/337587
16#if defined(ENABLE_AUTOFILL_DIALOG)
17#include "third_party/libaddressinput/chromium/cpp/include/libaddressinput/address_ui.h"
18#endif
19
20namespace autofill {
21
22CountryComboboxModel::CountryComboboxModel(const PersonalDataManager& manager) {
23  // Insert the default country at the top as well as in the ordered list.
24  const std::string& app_locale = g_browser_process->GetApplicationLocale();
25  std::string default_country_code =
26      manager.GetDefaultCountryCodeForNewAddress();
27  DCHECK(!default_country_code.empty());
28
29  countries_.push_back(new AutofillCountry(default_country_code, app_locale));
30  // The separator item.
31  countries_.push_back(NULL);
32
33  // The sorted list of countries.
34#if defined(ENABLE_AUTOFILL_DIALOG)
35  const std::vector<std::string>& available_countries =
36      ::i18n::addressinput::GetRegionCodes();
37#else
38  std::vector<std::string> available_countries;
39  AutofillCountry::GetAvailableCountries(&available_countries);
40#endif
41
42  std::vector<AutofillCountry*> sorted_countries;
43  for (std::vector<std::string>::const_iterator it =
44           available_countries.begin(); it != available_countries.end(); ++it) {
45    sorted_countries.push_back(new AutofillCountry(*it, app_locale));
46  }
47
48  l10n_util::SortStringsUsingMethod(app_locale,
49                                    &sorted_countries,
50                                    &AutofillCountry::name);
51  countries_.insert(countries_.end(),
52                    sorted_countries.begin(),
53                    sorted_countries.end());
54}
55
56CountryComboboxModel::~CountryComboboxModel() {}
57
58int CountryComboboxModel::GetItemCount() const {
59  return countries_.size();
60}
61
62base::string16 CountryComboboxModel::GetItemAt(int index) {
63  AutofillCountry* country = countries_[index];
64  if (country)
65    return countries_[index]->name();
66
67  // The separator item. Implemented for platforms that don't yet support
68  // IsItemSeparatorAt().
69  return base::ASCIIToUTF16("---");
70}
71
72bool CountryComboboxModel::IsItemSeparatorAt(int index) {
73  return !countries_[index];
74}
75
76std::string CountryComboboxModel::GetDefaultCountryCode() const {
77  return countries_[GetDefaultIndex()]->country_code();
78}
79
80}  // namespace autofill
81