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