Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(308)

Side by Side Diff: components/autofill/core/browser/country_combobox_model.cc

Issue 2705773002: [Autofill] Move country combo box model from chrome to component. (Closed)
Patch Set: Rebase Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/ui/autofill/country_combobox_model.h" 5 #include "components/autofill/core/browser/country_combobox_model.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <iterator> 8 #include <iterator>
9 #include <utility>
9 10
10 #include "base/logging.h" 11 #include "base/logging.h"
11 #include "base/memory/ptr_util.h" 12 #include "base/memory/ptr_util.h"
12 #include "base/strings/utf_string_conversions.h" 13 #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" 14 #include "components/autofill/core/browser/autofill_country.h"
16 #include "components/autofill/core/browser/country_data.h" 15 #include "components/autofill/core/browser/country_data.h"
17 #include "components/autofill/core/browser/personal_data_manager.h" 16 #include "components/autofill/core/browser/personal_data_manager.h"
18 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_ui .h" 17 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_ui .h"
19 #include "ui/base/l10n/l10n_util_collator.h" 18 #include "ui/base/l10n/l10n_util_collator.h"
20 #include "ui/base/models/combobox_model_observer.h" 19 #include "ui/base/models/combobox_model_observer.h"
21 20
22 namespace autofill { 21 namespace autofill {
23 22
24 CountryComboboxModel::CountryComboboxModel() {} 23 CountryComboboxModel::CountryComboboxModel() {}
25 24
26 CountryComboboxModel::~CountryComboboxModel() {} 25 CountryComboboxModel::~CountryComboboxModel() {}
27 26
28 void CountryComboboxModel::SetCountries( 27 void CountryComboboxModel::SetCountries(
29 const PersonalDataManager& manager, 28 const PersonalDataManager& manager,
30 const base::Callback<bool(const std::string&)>& filter) { 29 const base::Callback<bool(const std::string&)>& filter,
30 const std::string& app_locale) {
31 countries_.clear(); 31 countries_.clear();
32 32
33 // Insert the default country at the top as well as in the ordered list. 33 // Insert the default country at the top as well as in the ordered list.
34 std::string default_country_code = 34 std::string default_country_code =
35 manager.GetDefaultCountryCodeForNewAddress(); 35 manager.GetDefaultCountryCodeForNewAddress();
36 DCHECK(!default_country_code.empty()); 36 DCHECK(!default_country_code.empty());
37 37
38 const std::string& app_locale = g_browser_process->GetApplicationLocale();
39 if (filter.is_null() || filter.Run(default_country_code)) { 38 if (filter.is_null() || filter.Run(default_country_code)) {
40 countries_.push_back( 39 countries_.push_back(
41 base::MakeUnique<AutofillCountry>(default_country_code, app_locale)); 40 base::MakeUnique<AutofillCountry>(default_country_code, app_locale));
42 #if !defined(OS_ANDROID) 41 #if !defined(OS_ANDROID)
43 // The separator item. On Android, there are separators after all items, so 42 // The separator item. On Android, there are separators after all items, so
44 // this is unnecessary. 43 // this is unnecessary.
45 countries_.push_back(nullptr); 44 countries_.push_back(nullptr);
46 #endif 45 #endif
47 } 46 }
48 47
(...skipping 13 matching lines...) Expand all
62 std::back_inserter(filtered_countries)); 61 std::back_inserter(filtered_countries));
63 available_countries = &filtered_countries; 62 available_countries = &filtered_countries;
64 63
65 CountryVector sorted_countries; 64 CountryVector sorted_countries;
66 for (const auto& country_code : *available_countries) { 65 for (const auto& country_code : *available_countries) {
67 if (filter.is_null() || filter.Run(country_code)) 66 if (filter.is_null() || filter.Run(country_code))
68 sorted_countries.push_back( 67 sorted_countries.push_back(
69 base::MakeUnique<AutofillCountry>(country_code, app_locale)); 68 base::MakeUnique<AutofillCountry>(country_code, app_locale));
70 } 69 }
71 70
72 l10n_util::SortStringsUsingMethod(app_locale, 71 l10n_util::SortStringsUsingMethod(app_locale, &sorted_countries,
73 &sorted_countries,
74 &AutofillCountry::name); 72 &AutofillCountry::name);
75 std::move(sorted_countries.begin(), sorted_countries.end(), 73 std::move(sorted_countries.begin(), sorted_countries.end(),
76 std::back_inserter(countries_)); 74 std::back_inserter(countries_));
77 } 75 }
78 76
79 int CountryComboboxModel::GetItemCount() const { 77 int CountryComboboxModel::GetItemCount() const {
80 return countries_.size(); 78 return countries_.size();
81 } 79 }
82 80
83 base::string16 CountryComboboxModel::GetItemAt(int index) { 81 base::string16 CountryComboboxModel::GetItemAt(int index) {
84 AutofillCountry* country = countries_[index].get(); 82 AutofillCountry* country = countries_[index].get();
85 if (country) 83 if (country)
86 return countries_[index]->name(); 84 return countries_[index]->name();
87 85
88 // The separator item. Implemented for platforms that don't yet support 86 // The separator item. Implemented for platforms that don't yet support
89 // IsItemSeparatorAt(). 87 // IsItemSeparatorAt().
90 return base::ASCIIToUTF16("---"); 88 return base::ASCIIToUTF16("---");
91 } 89 }
92 90
93 bool CountryComboboxModel::IsItemSeparatorAt(int index) { 91 bool CountryComboboxModel::IsItemSeparatorAt(int index) {
94 return !countries_[index].get(); 92 return !countries_[index].get();
95 } 93 }
96 94
97 std::string CountryComboboxModel::GetDefaultCountryCode() const { 95 std::string CountryComboboxModel::GetDefaultCountryCode() const {
98 return countries_[GetDefaultIndex()]->country_code(); 96 return countries_[GetDefaultIndex()]->country_code();
99 } 97 }
100 98
101 } // namespace autofill 99 } // namespace autofill
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698