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/strings/utf_string_conversions.h"
      8 #include "chrome/browser/browser_process.h"
      9 #include "components/autofill/core/browser/autofill_country.h"
     10 #include "components/autofill/core/browser/personal_data_manager.h"
     11 #include "ui/base/l10n/l10n_util_collator.h"
     12 
     13 namespace autofill {
     14 
     15 CountryComboboxModel::CountryComboboxModel(const PersonalDataManager& manager) {
     16   // Insert the default country at the top as well as in the ordered list.
     17   std::string app_locale = g_browser_process->GetApplicationLocale();
     18   std::string default_country_code =
     19       manager.GetDefaultCountryCodeForNewAddress();
     20   DCHECK(!default_country_code.empty());
     21 
     22   countries_.push_back(new AutofillCountry(default_country_code, app_locale));
     23   // The separator item.
     24   countries_.push_back(NULL);
     25 
     26   // The sorted list of countries.
     27   std::vector<std::string> country_codes;
     28   AutofillCountry::GetAvailableCountries(&country_codes);
     29   std::vector<AutofillCountry*> sorted_countries;
     30 
     31   for (std::vector<std::string>::iterator iter = country_codes.begin();
     32        iter != country_codes.end(); ++iter) {
     33     sorted_countries.push_back(new AutofillCountry(*iter, app_locale));
     34   }
     35 
     36   l10n_util::SortStringsUsingMethod(app_locale,
     37                                     &sorted_countries,
     38                                     &AutofillCountry::name);
     39   countries_.insert(countries_.end(),
     40                     sorted_countries.begin(),
     41                     sorted_countries.end());
     42 }
     43 
     44 CountryComboboxModel::~CountryComboboxModel() {}
     45 
     46 int CountryComboboxModel::GetItemCount() const {
     47   return countries_.size();
     48 }
     49 
     50 base::string16 CountryComboboxModel::GetItemAt(int index) {
     51   AutofillCountry* country = countries_[index];
     52   if (country)
     53     return countries_[index]->name();
     54 
     55   // The separator item. Implemented for platforms that don't yet support
     56   // IsItemSeparatorAt().
     57   return ASCIIToUTF16("---");
     58 }
     59 
     60 bool CountryComboboxModel::IsItemSeparatorAt(int index) {
     61   return !countries_[index];
     62 }
     63 
     64 }  // namespace autofill
     65