Chromium Code Reviews| 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 int kIndexOffset = 2; | |
| 20 } // namespace | |
| 21 | |
| 22 AddressComboboxModel::AddressComboboxModel( | |
| 23 const PersonalDataManager& personal_data_manager, | |
| 24 const std::string& app_locale) | |
| 25 : app_locale_(app_locale) { | |
| 26 for (const auto* profile : personal_data_manager.GetProfilesToSuggest()) { | |
| 27 profiles_cache_.push_back(base::MakeUnique<AutofillProfile>(*profile)); | |
| 28 } | |
| 29 UpdateAddresses(); | |
| 30 } | |
| 31 | |
| 32 AddressComboboxModel::~AddressComboboxModel() {} | |
| 33 | |
| 34 int AddressComboboxModel::GetItemCount() const { | |
| 35 // When there are not addresses, a special entry is shown to prompt the user | |
| 36 // to add addresses. | |
| 37 if (addresses_.size() == 0) | |
| 38 return 1; | |
| 39 // Add extra items for the "Select" entry and a separator. | |
|
sebsg
2017/05/02 22:58:55
Do we also have the add address button if some add
MAD
2017/05/03 16:15:19
I'm not sure I understand. You mean the credit car
sebsg
2017/05/03 19:19:29
Confusion! :) Yes I also think the option to add a
MAD
2017/05/03 23:50:47
Ha! You were talking about the Add Address entry w
sebsg
2017/05/04 16:50:54
Ahh I didn't realize the "Add address" was a butto
| |
| 40 return addresses_.size() + kIndexOffset; | |
| 41 } | |
| 42 | |
| 43 base::string16 AddressComboboxModel::GetItemAt(int index) { | |
| 44 DCHECK_GE(index, 0); | |
| 45 // A special entry is always added at index 0 and a separator at index 1. | |
| 46 DCHECK_LT(static_cast<size_t>(index), addresses_.size() + kIndexOffset); | |
| 47 | |
| 48 // Special entry when no profiles have been created yet. | |
| 49 if (addresses_.empty()) | |
| 50 return l10n_util::GetStringUTF16(IDS_AUTOFILL_ADD_BILLING_ADDRESS); | |
| 51 | |
| 52 // Always show the "Select" entry at the top, default selection position. | |
| 53 if (index == 0) | |
| 54 return l10n_util::GetStringUTF16(IDS_AUTOFILL_SELECT); | |
| 55 | |
| 56 // Always show the "Select" entry at the top, default selection position. | |
| 57 if (index == 1) | |
| 58 return base::ASCIIToUTF16("---"); | |
| 59 | |
| 60 return addresses_[index - kIndexOffset].second; | |
| 61 } | |
| 62 | |
| 63 bool AddressComboboxModel::IsItemSeparatorAt(int index) { | |
| 64 // The only separator is between the "Select" entry at 0 and the first address | |
| 65 // at index 2. So there must be at least one address for a separator to be | |
| 66 // shown. | |
| 67 DCHECK(index <= kIndexOffset || !addresses_.empty()); | |
| 68 return index == 1; | |
| 69 } | |
| 70 | |
| 71 void AddressComboboxModel::AddObserver(ui::ComboboxModelObserver* observer) { | |
| 72 observers_.AddObserver(observer); | |
| 73 } | |
| 74 | |
| 75 void AddressComboboxModel::RemoveObserver(ui::ComboboxModelObserver* observer) { | |
| 76 observers_.RemoveObserver(observer); | |
| 77 } | |
| 78 | |
| 79 int AddressComboboxModel::AddNewProfile(const AutofillProfile& profile) { | |
| 80 profiles_cache_.push_back(base::MakeUnique<AutofillProfile>(profile)); | |
|
sebsg
2017/05/02 22:58:55
One thing we did on Android is put the new address
MAD
2017/05/03 16:15:19
OK, do you also put the selected one at the top ev
sebsg
2017/05/03 19:19:29
No, we put in in all caps, which we may or may not
MAD
2017/05/03 23:50:47
What do we put all in caps?
The usual behavior i
sebsg
2017/05/04 16:50:54
I think it's good as it is and we can ask Bruno fo
| |
| 81 UpdateAddresses(); | |
| 82 DCHECK_GT(addresses_.size(), 0UL); | |
| 83 return addresses_.size() + kIndexOffset - 1; | |
| 84 } | |
| 85 | |
| 86 std::string AddressComboboxModel::GetItemIdentifierAt(int index) { | |
| 87 // The first two indices are special entries, with no addresses. | |
| 88 if (index < kIndexOffset) | |
| 89 return std::string(); | |
| 90 DCHECK_LT(index, addresses_.size() + kIndexOffset); | |
| 91 return addresses_[index - kIndexOffset].first; | |
| 92 } | |
| 93 | |
| 94 int AddressComboboxModel::GetIndexOfIdentifier(const std::string& identifier) { | |
| 95 for (size_t i = 0; i < addresses_.size(); ++i) { | |
| 96 if (addresses_[i].first == identifier) | |
| 97 return i + kIndexOffset; | |
| 98 } | |
| 99 return -1; | |
| 100 } | |
| 101 | |
| 102 void AddressComboboxModel::UpdateAddresses() { | |
| 103 addresses_.clear(); | |
| 104 std::vector<base::string16> labels; | |
| 105 // CreateDifferentiatingLabels is expecting a pointer vector and we keep | |
| 106 // profiles as unique_ptr. | |
| 107 std::vector<AutofillProfile*> profiles; | |
| 108 for (const auto& profile : profiles_cache_) { | |
| 109 profiles.push_back(profile.get()); | |
| 110 } | |
| 111 AutofillProfile::CreateDifferentiatingLabels(profiles, app_locale_, &labels); | |
| 112 DCHECK_EQ(labels.size(), profiles_cache_.size()); | |
| 113 | |
| 114 for (size_t i = 0; i < profiles_cache_.size(); ++i) { | |
| 115 // Skip showing auxiliary profiles (e.g. Mac Contacts). | |
| 116 if (profiles_cache_[i]->record_type() == AutofillProfile::AUXILIARY_PROFILE) | |
| 117 continue; | |
| 118 | |
| 119 base::string16 label(labels[i]); | |
| 120 if (label.size() > 20) { | |
| 121 // gfx::StringSlicer keeps a references to the string16. | |
| 122 base::string16 ellipsis(base::ASCIIToUTF16("...")); | |
| 123 gfx::StringSlicer slicer(label, | |
| 124 /*ellipsis=*/ellipsis, | |
| 125 /*elide_in_middle=*/false, | |
| 126 /*elide_at_beginning=*/false); | |
| 127 label = slicer.CutString(20, /*insert_ellipsis=*/true); | |
| 128 } | |
| 129 addresses_.push_back(std::make_pair(profiles_cache_[i]->guid(), label)); | |
| 130 } | |
| 131 | |
| 132 for (auto& observer : observers_) { | |
| 133 observer.OnComboboxModelChanged(this); | |
| 134 } | |
| 135 } | |
| 136 } // namespace autofill | |
| OLD | NEW |