country_combobox_model.cc revision 116680a4aac90f2aa7413d9095a592090648e557
136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines// Copyright (c) 2012 The Chromium Authors. All rights reserved.
236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines// Use of this source code is governed by a BSD-style license that can be
336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines// found in the LICENSE file.
436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines
536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines#include "chrome/browser/ui/autofill/country_combobox_model.h"
636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines
736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines#include <algorithm>
836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines#include <iterator>
936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines
1036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines#include "base/logging.h"
1136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines#include "base/strings/utf_string_conversions.h"
1236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines#include "chrome/browser/browser_process.h"
1336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines#include "components/autofill/core/browser/autofill_country.h"
1436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines#include "components/autofill/core/browser/personal_data_manager.h"
1536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines#include "ui/base/l10n/l10n_util_collator.h"
1636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines#include "ui/base/models/combobox_model_observer.h"
1736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines
1836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines// TODO(rouslan): Remove this check. http://crbug.com/337587
1936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines#if defined(ENABLE_AUTOFILL_DIALOG)
2036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines#include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_ui.h"
2136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines#endif
2236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines
2336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hinesnamespace autofill {
2436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines
2536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen HinesCountryComboboxModel::CountryComboboxModel() {}
2636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines
2736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen HinesCountryComboboxModel::~CountryComboboxModel() {}
2836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines
2936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hinesvoid CountryComboboxModel::SetCountries(
3036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    const PersonalDataManager& manager,
3136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    const base::Callback<bool(const std::string&)>& filter) {
3236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  countries_.clear();
33dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines
3436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  // Insert the default country at the top as well as in the ordered list.
3536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  std::string default_country_code =
3636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines      manager.GetDefaultCountryCodeForNewAddress();
3736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  DCHECK(!default_country_code.empty());
3836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines
3936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  const std::string& app_locale = g_browser_process->GetApplicationLocale();
4036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  if (filter.is_null() || filter.Run(default_country_code)) {
4136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    countries_.push_back(new AutofillCountry(default_country_code, app_locale));
4236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    // The separator item.
4336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    countries_.push_back(NULL);
4436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  }
4536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines
4636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  // The sorted list of countries.
47dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  std::vector<std::string> available_countries;
4836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  AutofillCountry::GetAvailableCountries(&available_countries);
4936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines
5036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines#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
64
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));
70  }
71
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());
78}
79
80int CountryComboboxModel::GetItemCount() const {
81  return countries_.size();
82}
83
84base::string16 CountryComboboxModel::GetItemAt(int index) {
85  AutofillCountry* country = countries_[index];
86  if (country)
87    return countries_[index]->name();
88
89  // The separator item. Implemented for platforms that don't yet support
90  // IsItemSeparatorAt().
91  return base::ASCIIToUTF16("---");
92}
93
94bool CountryComboboxModel::IsItemSeparatorAt(int index) {
95  return !countries_[index];
96}
97
98std::string CountryComboboxModel::GetDefaultCountryCode() const {
99  return countries_[GetDefaultIndex()]->country_code();
100}
101
102}  // namespace autofill
103