| 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" | |
| 11 #include "ui/base/l10n/l10n_util_collator.h" | 10 #include "ui/base/l10n/l10n_util_collator.h" |
| 11 #include "ui/base/models/combobox_model_observer.h" |
| 12 | 12 |
| 13 namespace autofill { | 13 namespace autofill { |
| 14 | 14 |
| 15 CountryComboboxModel::CountryComboboxModel(const PersonalDataManager& manager) { | 15 CountryComboboxModel::CountryComboboxModel( |
| 16 const std::string& default_country_code) |
| 17 : default_index_(0) { |
| 16 // Insert the default country at the top as well as in the ordered list. | 18 // Insert the default country at the top as well as in the ordered list. |
| 17 std::string app_locale = g_browser_process->GetApplicationLocale(); | 19 const std::string& app_locale = g_browser_process->GetApplicationLocale(); |
| 18 std::string default_country_code = | 20 countries_.push_back(new AutofillCountry(default_country_code, app_locale)); |
| 19 manager.GetDefaultCountryCodeForNewAddress(); | |
| 20 DCHECK(!default_country_code.empty()); | |
| 21 | 21 |
| 22 countries_.push_back(new AutofillCountry(default_country_code, app_locale)); | |
| 23 // The separator item. | 22 // The separator item. |
| 24 countries_.push_back(NULL); | 23 countries_.push_back(NULL); |
| 25 | 24 |
| 26 // The sorted list of countries. | 25 // The sorted list of countries. |
| 27 std::vector<std::string> country_codes; | 26 std::vector<std::string> country_codes; |
| 28 AutofillCountry::GetAvailableCountries(&country_codes); | 27 AutofillCountry::GetAvailableCountries(&country_codes); |
| 29 std::vector<AutofillCountry*> sorted_countries; | 28 std::vector<AutofillCountry*> sorted_countries; |
| 30 | 29 |
| 31 for (std::vector<std::string>::iterator iter = country_codes.begin(); | 30 for (std::vector<std::string>::iterator iter = country_codes.begin(); |
| 32 iter != country_codes.end(); ++iter) { | 31 iter != country_codes.end(); ++iter) { |
| (...skipping 21 matching lines...) Expand all Loading... |
| 54 | 53 |
| 55 // The separator item. Implemented for platforms that don't yet support | 54 // The separator item. Implemented for platforms that don't yet support |
| 56 // IsItemSeparatorAt(). | 55 // IsItemSeparatorAt(). |
| 57 return base::ASCIIToUTF16("---"); | 56 return base::ASCIIToUTF16("---"); |
| 58 } | 57 } |
| 59 | 58 |
| 60 bool CountryComboboxModel::IsItemSeparatorAt(int index) { | 59 bool CountryComboboxModel::IsItemSeparatorAt(int index) { |
| 61 return !countries_[index]; | 60 return !countries_[index]; |
| 62 } | 61 } |
| 63 | 62 |
| 63 int CountryComboboxModel::GetDefaultIndex() const { |
| 64 return default_index_; |
| 65 } |
| 66 |
| 67 void CountryComboboxModel::AddObserver(ui::ComboboxModelObserver* observer) { |
| 68 observers_.AddObserver(observer); |
| 69 } |
| 70 |
| 71 void CountryComboboxModel::RemoveObserver(ui::ComboboxModelObserver* observer) { |
| 72 observers_.RemoveObserver(observer); |
| 73 } |
| 74 |
| 75 void CountryComboboxModel::SetDefaultIndex(int default_index) { |
| 76 DCHECK_GE(default_index, 0); |
| 77 DCHECK_LT(default_index, static_cast<int>(countries_.size())); |
| 78 DCHECK(!IsItemSeparatorAt(default_index)); |
| 79 |
| 80 default_index_ = default_index; |
| 81 FOR_EACH_OBSERVER(ui::ComboboxModelObserver, observers_, |
| 82 OnComboboxModelChanged(this)); |
| 83 } |
| 84 |
| 85 std::string CountryComboboxModel::GetDefaultCountryCode() const { |
| 86 return countries()[GetDefaultIndex()]->country_code(); |
| 87 } |
| 88 |
| 64 } // namespace autofill | 89 } // namespace autofill |
| OLD | NEW |