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