| OLD | NEW |
| (Empty) |
| 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/memory/ptr_util.h" | |
| 12 #include "base/strings/utf_string_conversions.h" | |
| 13 #include "build/build_config.h" | |
| 14 #include "chrome/browser/browser_process.h" | |
| 15 #include "components/autofill/core/browser/autofill_country.h" | |
| 16 #include "components/autofill/core/browser/country_data.h" | |
| 17 #include "components/autofill/core/browser/personal_data_manager.h" | |
| 18 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_ui
.h" | |
| 19 #include "ui/base/l10n/l10n_util_collator.h" | |
| 20 #include "ui/base/models/combobox_model_observer.h" | |
| 21 | |
| 22 namespace autofill { | |
| 23 | |
| 24 CountryComboboxModel::CountryComboboxModel() {} | |
| 25 | |
| 26 CountryComboboxModel::~CountryComboboxModel() {} | |
| 27 | |
| 28 void CountryComboboxModel::SetCountries( | |
| 29 const PersonalDataManager& manager, | |
| 30 const base::Callback<bool(const std::string&)>& filter) { | |
| 31 countries_.clear(); | |
| 32 | |
| 33 // Insert the default country at the top as well as in the ordered list. | |
| 34 std::string default_country_code = | |
| 35 manager.GetDefaultCountryCodeForNewAddress(); | |
| 36 DCHECK(!default_country_code.empty()); | |
| 37 | |
| 38 const std::string& app_locale = g_browser_process->GetApplicationLocale(); | |
| 39 if (filter.is_null() || filter.Run(default_country_code)) { | |
| 40 countries_.push_back( | |
| 41 base::MakeUnique<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(nullptr); | |
| 46 #endif | |
| 47 } | |
| 48 | |
| 49 // The sorted list of country codes. | |
| 50 const std::vector<std::string>* available_countries = | |
| 51 &CountryDataMap::GetInstance()->country_codes(); | |
| 52 | |
| 53 // Filter out the countries that do not have rules for address input and | |
| 54 // validation. | |
| 55 const std::vector<std::string>& addressinput_countries = | |
| 56 ::i18n::addressinput::GetRegionCodes(); | |
| 57 std::vector<std::string> filtered_countries; | |
| 58 filtered_countries.reserve(available_countries->size()); | |
| 59 std::set_intersection( | |
| 60 available_countries->begin(), available_countries->end(), | |
| 61 addressinput_countries.begin(), addressinput_countries.end(), | |
| 62 std::back_inserter(filtered_countries)); | |
| 63 available_countries = &filtered_countries; | |
| 64 | |
| 65 CountryVector sorted_countries; | |
| 66 for (const auto& country_code : *available_countries) { | |
| 67 if (filter.is_null() || filter.Run(country_code)) | |
| 68 sorted_countries.push_back( | |
| 69 base::MakeUnique<AutofillCountry>(country_code, app_locale)); | |
| 70 } | |
| 71 | |
| 72 l10n_util::SortStringsUsingMethod(app_locale, | |
| 73 &sorted_countries, | |
| 74 &AutofillCountry::name); | |
| 75 std::move(sorted_countries.begin(), sorted_countries.end(), | |
| 76 std::back_inserter(countries_)); | |
| 77 } | |
| 78 | |
| 79 int CountryComboboxModel::GetItemCount() const { | |
| 80 return countries_.size(); | |
| 81 } | |
| 82 | |
| 83 base::string16 CountryComboboxModel::GetItemAt(int index) { | |
| 84 AutofillCountry* country = countries_[index].get(); | |
| 85 if (country) | |
| 86 return countries_[index]->name(); | |
| 87 | |
| 88 // The separator item. Implemented for platforms that don't yet support | |
| 89 // IsItemSeparatorAt(). | |
| 90 return base::ASCIIToUTF16("---"); | |
| 91 } | |
| 92 | |
| 93 bool CountryComboboxModel::IsItemSeparatorAt(int index) { | |
| 94 return !countries_[index].get(); | |
| 95 } | |
| 96 | |
| 97 std::string CountryComboboxModel::GetDefaultCountryCode() const { | |
| 98 return countries_[GetDefaultIndex()]->country_code(); | |
| 99 } | |
| 100 | |
| 101 } // namespace autofill | |
| OLD | NEW |