| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "components/autofill/core/browser/address_combobox_model.h" |
| 6 |
| 7 #include "base/memory/ptr_util.h" |
| 8 #include "base/strings/utf_string_conversions.h" |
| 9 #include "components/autofill/core/browser/autofill_profile.h" |
| 10 #include "components/autofill/core/browser/personal_data_manager.h" |
| 11 #include "components/strings/grit/components_strings.h" |
| 12 #include "ui/base/l10n/l10n_util.h" |
| 13 #include "ui/base/models/combobox_model_observer.h" |
| 14 #include "ui/gfx/text_elider.h" |
| 15 |
| 16 namespace autofill { |
| 17 |
| 18 namespace { |
| 19 // There's one header entry to prompt the user to select an address, and a |
| 20 // separator. |
| 21 int kNbHeaderEntries = 2; |
| 22 } // namespace |
| 23 |
| 24 AddressComboboxModel::AddressComboboxModel( |
| 25 const PersonalDataManager& personal_data_manager, |
| 26 const std::string& app_locale) |
| 27 : app_locale_(app_locale) { |
| 28 for (const auto* profile : personal_data_manager.GetProfilesToSuggest()) { |
| 29 profiles_cache_.push_back(base::MakeUnique<AutofillProfile>(*profile)); |
| 30 } |
| 31 UpdateAddresses(); |
| 32 } |
| 33 |
| 34 AddressComboboxModel::~AddressComboboxModel() {} |
| 35 |
| 36 int AddressComboboxModel::GetItemCount() const { |
| 37 // When there are not addresses, a special entry is shown to prompt the user |
| 38 // to add addresses, but nothing else is shown, since there are no address to |
| 39 // select from, and no need for a separator. |
| 40 if (addresses_.size() == 0) |
| 41 return 1; |
| 42 // If there are addresses to choose from, but none is selected, add extra |
| 43 // items for the "Select" entry and a separator. |
| 44 return addresses_.size() + kNbHeaderEntries; |
| 45 } |
| 46 |
| 47 base::string16 AddressComboboxModel::GetItemAt(int index) { |
| 48 DCHECK_GE(index, 0); |
| 49 // A special entry is always added at index 0 and a separator at index 1. |
| 50 DCHECK_LT(static_cast<size_t>(index), addresses_.size() + kNbHeaderEntries); |
| 51 |
| 52 // Special entry when no profiles have been created yet. |
| 53 if (addresses_.empty()) |
| 54 return l10n_util::GetStringUTF16(IDS_AUTOFILL_ADD_BILLING_ADDRESS); |
| 55 |
| 56 // Always show the "Select" entry at the top, default selection position. |
| 57 if (index == 0) |
| 58 return l10n_util::GetStringUTF16(IDS_AUTOFILL_SELECT); |
| 59 |
| 60 // Always show the "Select" entry at the top, default selection position. |
| 61 if (index == 1) |
| 62 return base::ASCIIToUTF16("---"); |
| 63 |
| 64 return addresses_[index - kNbHeaderEntries].second; |
| 65 } |
| 66 |
| 67 bool AddressComboboxModel::IsItemSeparatorAt(int index) { |
| 68 // The only separator is between the "Select" entry at 0 and the first address |
| 69 // at index 2. So there must be at least one address for a separator to be |
| 70 // shown. |
| 71 DCHECK(index <= kNbHeaderEntries || !addresses_.empty()); |
| 72 return index == 1; |
| 73 } |
| 74 |
| 75 void AddressComboboxModel::AddObserver(ui::ComboboxModelObserver* observer) { |
| 76 observers_.AddObserver(observer); |
| 77 } |
| 78 |
| 79 void AddressComboboxModel::RemoveObserver(ui::ComboboxModelObserver* observer) { |
| 80 observers_.RemoveObserver(observer); |
| 81 } |
| 82 |
| 83 int AddressComboboxModel::AddNewProfile(const AutofillProfile& profile) { |
| 84 profiles_cache_.push_back(base::MakeUnique<AutofillProfile>(profile)); |
| 85 UpdateAddresses(); |
| 86 DCHECK_GT(addresses_.size(), 0UL); |
| 87 return addresses_.size() + kNbHeaderEntries - 1; |
| 88 } |
| 89 |
| 90 std::string AddressComboboxModel::GetItemIdentifierAt(int index) { |
| 91 // The first two indices are special entries, with no addresses. |
| 92 if (index < kNbHeaderEntries) |
| 93 return std::string(); |
| 94 DCHECK_LT(static_cast<size_t>(index), addresses_.size() + kNbHeaderEntries); |
| 95 return addresses_[index - kNbHeaderEntries].first; |
| 96 } |
| 97 |
| 98 int AddressComboboxModel::GetIndexOfIdentifier(const std::string& identifier) { |
| 99 for (size_t i = 0; i < addresses_.size(); ++i) { |
| 100 if (addresses_[i].first == identifier) |
| 101 return i + kNbHeaderEntries; |
| 102 } |
| 103 return -1; |
| 104 } |
| 105 |
| 106 void AddressComboboxModel::UpdateAddresses() { |
| 107 addresses_.clear(); |
| 108 std::vector<base::string16> labels; |
| 109 // CreateDifferentiatingLabels is expecting a pointer vector and we keep |
| 110 // profiles as unique_ptr. |
| 111 std::vector<AutofillProfile*> profiles; |
| 112 for (const auto& profile : profiles_cache_) { |
| 113 profiles.push_back(profile.get()); |
| 114 } |
| 115 AutofillProfile::CreateDifferentiatingLabels(profiles, app_locale_, &labels); |
| 116 DCHECK_EQ(labels.size(), profiles_cache_.size()); |
| 117 |
| 118 for (size_t i = 0; i < profiles_cache_.size(); ++i) { |
| 119 // Skip showing auxiliary profiles (e.g. Mac Contacts). |
| 120 if (profiles_cache_[i]->record_type() == AutofillProfile::AUXILIARY_PROFILE) |
| 121 continue; |
| 122 |
| 123 addresses_.push_back(std::make_pair(profiles_cache_[i]->guid(), labels[i])); |
| 124 } |
| 125 |
| 126 for (auto& observer : observers_) { |
| 127 observer.OnComboboxModelChanged(this); |
| 128 } |
| 129 } |
| 130 } // namespace autofill |
| OLD | NEW |