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 "base/strings/utf_string_conversions.h" | 7 #include "base/strings/utf_string_conversions.h" |
8 #include "chrome/browser/browser_process.h" | 8 #include "chrome/browser/browser_process.h" |
9 #include "components/autofill/core/browser/autofill_country.h" | 9 #include "components/autofill/core/browser/autofill_country.h" |
10 #include "components/autofill/core/browser/personal_data_manager.h" | 10 #include "components/autofill/core/browser/personal_data_manager.h" |
11 #include "ui/base/l10n/l10n_util_collator.h" | 11 #include "ui/base/l10n/l10n_util_collator.h" |
12 | 12 |
13 namespace autofill { | 13 namespace autofill { |
14 | 14 |
15 CountryComboboxModel::CountryComboboxModel(const PersonalDataManager& manager) { | 15 CountryComboboxModel::CountryComboboxModel(const PersonalDataManager& manager) |
| 16 : selected_index_(0) { |
16 // Insert the default country at the top as well as in the ordered list. | 17 // Insert the default country at the top as well as in the ordered list. |
17 std::string app_locale = g_browser_process->GetApplicationLocale(); | 18 const std::string& app_locale = g_browser_process->GetApplicationLocale(); |
18 std::string default_country_code = | 19 std::string default_country_code = |
19 manager.GetDefaultCountryCodeForNewAddress(); | 20 manager.GetDefaultCountryCodeForNewAddress(); |
20 DCHECK(!default_country_code.empty()); | 21 DCHECK(!default_country_code.empty()); |
21 | 22 |
22 countries_.push_back(new AutofillCountry(default_country_code, app_locale)); | 23 countries_.push_back(new AutofillCountry(default_country_code, app_locale)); |
23 // The separator item. | 24 // The separator item. |
24 countries_.push_back(NULL); | 25 countries_.push_back(NULL); |
25 | 26 |
26 // The sorted list of countries. | 27 // The sorted list of countries. |
27 std::vector<std::string> country_codes; | 28 std::vector<std::string> country_codes; |
(...skipping 26 matching lines...) Expand all Loading... |
54 | 55 |
55 // The separator item. Implemented for platforms that don't yet support | 56 // The separator item. Implemented for platforms that don't yet support |
56 // IsItemSeparatorAt(). | 57 // IsItemSeparatorAt(). |
57 return base::ASCIIToUTF16("---"); | 58 return base::ASCIIToUTF16("---"); |
58 } | 59 } |
59 | 60 |
60 bool CountryComboboxModel::IsItemSeparatorAt(int index) { | 61 bool CountryComboboxModel::IsItemSeparatorAt(int index) { |
61 return !countries_[index]; | 62 return !countries_[index]; |
62 } | 63 } |
63 | 64 |
| 65 void CountryComboboxModel::SelectIndex(int index) { |
| 66 DCHECK_GE(index, 0); |
| 67 DCHECK_LT(index, static_cast<int>(countries_.size())); |
| 68 DCHECK(!IsItemSeparatorAt(index)); |
| 69 selected_index_ = index; |
| 70 } |
| 71 |
| 72 void CountryComboboxModel::SelectDefaultIndex() { |
| 73 SelectIndex(GetDefaultIndex()); |
| 74 } |
| 75 |
| 76 void CountryComboboxModel::SelectCountry(const std::string& country_code) { |
| 77 DCHECK_EQ(2U, country_code.length()); |
| 78 for (size_t i = 0; i < countries_.size(); ++i) { |
| 79 if (countries_[i] && countries_[i]->country_code() == country_code) { |
| 80 SelectIndex(i); |
| 81 return; |
| 82 } |
| 83 } |
| 84 NOTREACHED(); |
| 85 } |
| 86 |
| 87 std::string CountryComboboxModel::GetSelectedCountryCode() const { |
| 88 return countries_[selected_index_]->country_code(); |
| 89 } |
| 90 |
| 91 bool CountryComboboxModel::IsDefaultIndexSelected() const { |
| 92 return selected_index_ == GetDefaultIndex(); |
| 93 } |
| 94 |
64 } // namespace autofill | 95 } // namespace autofill |
OLD | NEW |