OLD | NEW |
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 "chrome/browser/ui/autofill/country_combobox_model.h" |
6 | 6 |
7 #include <algorithm> | |
8 #include <iterator> | |
9 | |
10 #include "base/logging.h" | 7 #include "base/logging.h" |
11 #include "base/strings/utf_string_conversions.h" | 8 #include "base/strings/utf_string_conversions.h" |
12 #include "chrome/browser/browser_process.h" | 9 #include "chrome/browser/browser_process.h" |
13 #include "components/autofill/core/browser/autofill_country.h" | 10 #include "components/autofill/core/browser/autofill_country.h" |
14 #include "components/autofill/core/browser/personal_data_manager.h" | 11 #include "components/autofill/core/browser/personal_data_manager.h" |
15 #include "ui/base/l10n/l10n_util_collator.h" | 12 #include "ui/base/l10n/l10n_util_collator.h" |
16 #include "ui/base/models/combobox_model_observer.h" | 13 #include "ui/base/models/combobox_model_observer.h" |
17 | 14 |
18 // TODO(rouslan): Remove this check. http://crbug.com/337587 | 15 // TODO(rouslan): Remove this check. http://crbug.com/337587 |
19 #if defined(ENABLE_AUTOFILL_DIALOG) | 16 #if defined(ENABLE_AUTOFILL_DIALOG) |
20 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_ui
.h" | 17 #include "third_party/libaddressinput/chromium/cpp/include/libaddressinput/addre
ss_ui.h" |
21 #endif | 18 #endif |
22 | 19 |
23 namespace autofill { | 20 namespace autofill { |
24 | 21 |
25 CountryComboboxModel::CountryComboboxModel() {} | 22 CountryComboboxModel::CountryComboboxModel() {} |
26 | 23 |
27 CountryComboboxModel::~CountryComboboxModel() {} | 24 CountryComboboxModel::~CountryComboboxModel() {} |
28 | 25 |
29 void CountryComboboxModel::SetCountries( | 26 void CountryComboboxModel::SetCountries( |
30 const PersonalDataManager& manager, | 27 const PersonalDataManager& manager, |
31 const base::Callback<bool(const std::string&)>& filter) { | 28 const base::Callback<bool(const std::string&)>& filter) { |
32 countries_.clear(); | 29 countries_.clear(); |
33 | 30 |
34 // Insert the default country at the top as well as in the ordered list. | 31 // Insert the default country at the top as well as in the ordered list. |
35 std::string default_country_code = | 32 std::string default_country_code = |
36 manager.GetDefaultCountryCodeForNewAddress(); | 33 manager.GetDefaultCountryCodeForNewAddress(); |
37 DCHECK(!default_country_code.empty()); | 34 DCHECK(!default_country_code.empty()); |
38 | 35 |
39 const std::string& app_locale = g_browser_process->GetApplicationLocale(); | 36 const std::string& app_locale = g_browser_process->GetApplicationLocale(); |
40 if (filter.is_null() || filter.Run(default_country_code)) { | 37 if (filter.is_null() || filter.Run(default_country_code)) { |
41 countries_.push_back(new AutofillCountry(default_country_code, app_locale)); | 38 countries_.push_back(new AutofillCountry(default_country_code, app_locale)); |
42 // The separator item. | 39 // The separator item. |
43 countries_.push_back(NULL); | 40 countries_.push_back(NULL); |
44 } | 41 } |
45 | 42 |
46 // The sorted list of countries. | 43 // The sorted list of countries. |
| 44 #if defined(ENABLE_AUTOFILL_DIALOG) |
| 45 const std::vector<std::string>& available_countries = |
| 46 ::i18n::addressinput::GetRegionCodes(); |
| 47 #else |
47 std::vector<std::string> available_countries; | 48 std::vector<std::string> available_countries; |
48 AutofillCountry::GetAvailableCountries(&available_countries); | 49 AutofillCountry::GetAvailableCountries(&available_countries); |
49 | |
50 #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 | 50 #endif |
64 | 51 |
65 std::vector<AutofillCountry*> sorted_countries; | 52 std::vector<AutofillCountry*> sorted_countries; |
66 for (std::vector<std::string>::const_iterator it = | 53 for (std::vector<std::string>::const_iterator it = |
67 available_countries.begin(); it != available_countries.end(); ++it) { | 54 available_countries.begin(); it != available_countries.end(); ++it) { |
68 if (filter.is_null() || filter.Run(*it)) | 55 if (filter.is_null() || filter.Run(*it)) |
69 sorted_countries.push_back(new AutofillCountry(*it, app_locale)); | 56 sorted_countries.push_back(new AutofillCountry(*it, app_locale)); |
70 } | 57 } |
71 | 58 |
72 l10n_util::SortStringsUsingMethod(app_locale, | 59 l10n_util::SortStringsUsingMethod(app_locale, |
(...skipping 20 matching lines...) Expand all Loading... |
93 | 80 |
94 bool CountryComboboxModel::IsItemSeparatorAt(int index) { | 81 bool CountryComboboxModel::IsItemSeparatorAt(int index) { |
95 return !countries_[index]; | 82 return !countries_[index]; |
96 } | 83 } |
97 | 84 |
98 std::string CountryComboboxModel::GetDefaultCountryCode() const { | 85 std::string CountryComboboxModel::GetDefaultCountryCode() const { |
99 return countries_[GetDefaultIndex()]->country_code(); | 86 return countries_[GetDefaultIndex()]->country_code(); |
100 } | 87 } |
101 | 88 |
102 } // namespace autofill | 89 } // namespace autofill |
OLD | NEW |