OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 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 | 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 "components/autofill/core/browser/address_combobox_model.h" | 5 #include "components/autofill/core/browser/address_combobox_model.h" |
6 | 6 |
7 #include "base/memory/ptr_util.h" | 7 #include "base/memory/ptr_util.h" |
8 #include "base/strings/utf_string_conversions.h" | 8 #include "base/strings/utf_string_conversions.h" |
9 #include "components/autofill/core/browser/autofill_profile.h" | 9 #include "components/autofill/core/browser/autofill_profile.h" |
10 #include "components/autofill/core/browser/personal_data_manager.h" | 10 #include "components/autofill/core/browser/personal_data_manager.h" |
11 #include "components/strings/grit/components_strings.h" | 11 #include "components/strings/grit/components_strings.h" |
12 #include "ui/base/l10n/l10n_util.h" | 12 #include "ui/base/l10n/l10n_util.h" |
13 #include "ui/base/models/combobox_model_observer.h" | 13 #include "ui/base/models/combobox_model_observer.h" |
14 #include "ui/gfx/text_elider.h" | 14 #include "ui/gfx/text_elider.h" |
15 | 15 |
16 namespace autofill { | 16 namespace autofill { |
17 | 17 |
18 namespace { | 18 namespace { |
19 // There's one header entry to prompt the user to select an address, and a | 19 // There's one header entry to prompt the user to select an address, and a |
20 // separator. | 20 // separator. |
21 int kNbHeaderEntries = 2; | 21 int kNbHeaderEntries = 2; |
22 } // namespace | 22 } // namespace |
23 | 23 |
24 AddressComboboxModel::AddressComboboxModel( | 24 AddressComboboxModel::AddressComboboxModel( |
25 const PersonalDataManager& personal_data_manager, | 25 const PersonalDataManager& personal_data_manager, |
26 const std::string& app_locale) | 26 const std::string& app_locale, |
27 : app_locale_(app_locale) { | 27 const std::string& default_selected_guid) |
| 28 : app_locale_(app_locale), default_selected_guid_(default_selected_guid) { |
28 for (const auto* profile : personal_data_manager.GetProfilesToSuggest()) { | 29 for (const auto* profile : personal_data_manager.GetProfilesToSuggest()) { |
29 profiles_cache_.push_back(base::MakeUnique<AutofillProfile>(*profile)); | 30 profiles_cache_.push_back(base::MakeUnique<AutofillProfile>(*profile)); |
30 } | 31 } |
31 UpdateAddresses(); | 32 UpdateAddresses(); |
32 } | 33 } |
33 | 34 |
34 AddressComboboxModel::~AddressComboboxModel() {} | 35 AddressComboboxModel::~AddressComboboxModel() {} |
35 | 36 |
36 int AddressComboboxModel::GetItemCount() const { | 37 int AddressComboboxModel::GetItemCount() const { |
37 // When there are not addresses, a special entry is shown to prompt the user | 38 // When there are not addresses, a special entry is shown to prompt the user |
(...skipping 27 matching lines...) Expand all Loading... |
65 } | 66 } |
66 | 67 |
67 bool AddressComboboxModel::IsItemSeparatorAt(int index) { | 68 bool AddressComboboxModel::IsItemSeparatorAt(int index) { |
68 // The only separator is between the "Select" entry at 0 and the first address | 69 // 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 // at index 2. So there must be at least one address for a separator to be |
70 // shown. | 71 // shown. |
71 DCHECK(index <= kNbHeaderEntries || !addresses_.empty()); | 72 DCHECK(index <= kNbHeaderEntries || !addresses_.empty()); |
72 return index == 1; | 73 return index == 1; |
73 } | 74 } |
74 | 75 |
| 76 int AddressComboboxModel::GetDefaultIndex() const { |
| 77 if (!default_selected_guid_.empty()) { |
| 78 int address_index = GetIndexOfIdentifier(default_selected_guid_); |
| 79 if (address_index != -1) |
| 80 return address_index; |
| 81 } |
| 82 return ui::ComboboxModel::GetDefaultIndex(); |
| 83 } |
| 84 |
75 void AddressComboboxModel::AddObserver(ui::ComboboxModelObserver* observer) { | 85 void AddressComboboxModel::AddObserver(ui::ComboboxModelObserver* observer) { |
76 observers_.AddObserver(observer); | 86 observers_.AddObserver(observer); |
77 } | 87 } |
78 | 88 |
79 void AddressComboboxModel::RemoveObserver(ui::ComboboxModelObserver* observer) { | 89 void AddressComboboxModel::RemoveObserver(ui::ComboboxModelObserver* observer) { |
80 observers_.RemoveObserver(observer); | 90 observers_.RemoveObserver(observer); |
81 } | 91 } |
82 | 92 |
83 int AddressComboboxModel::AddNewProfile(const AutofillProfile& profile) { | 93 int AddressComboboxModel::AddNewProfile(const AutofillProfile& profile) { |
84 profiles_cache_.push_back(base::MakeUnique<AutofillProfile>(profile)); | 94 profiles_cache_.push_back(base::MakeUnique<AutofillProfile>(profile)); |
85 UpdateAddresses(); | 95 UpdateAddresses(); |
86 DCHECK_GT(addresses_.size(), 0UL); | 96 DCHECK_GT(addresses_.size(), 0UL); |
87 return addresses_.size() + kNbHeaderEntries - 1; | 97 return addresses_.size() + kNbHeaderEntries - 1; |
88 } | 98 } |
89 | 99 |
90 std::string AddressComboboxModel::GetItemIdentifierAt(int index) { | 100 std::string AddressComboboxModel::GetItemIdentifierAt(int index) { |
91 // The first two indices are special entries, with no addresses. | 101 // The first two indices are special entries, with no addresses. |
92 if (index < kNbHeaderEntries) | 102 if (index < kNbHeaderEntries) |
93 return std::string(); | 103 return std::string(); |
94 DCHECK_LT(static_cast<size_t>(index), addresses_.size() + kNbHeaderEntries); | 104 DCHECK_LT(static_cast<size_t>(index), addresses_.size() + kNbHeaderEntries); |
95 return addresses_[index - kNbHeaderEntries].first; | 105 return addresses_[index - kNbHeaderEntries].first; |
96 } | 106 } |
97 | 107 |
98 int AddressComboboxModel::GetIndexOfIdentifier(const std::string& identifier) { | 108 int AddressComboboxModel::GetIndexOfIdentifier( |
| 109 const std::string& identifier) const { |
99 for (size_t i = 0; i < addresses_.size(); ++i) { | 110 for (size_t i = 0; i < addresses_.size(); ++i) { |
100 if (addresses_[i].first == identifier) | 111 if (addresses_[i].first == identifier) |
101 return i + kNbHeaderEntries; | 112 return i + kNbHeaderEntries; |
102 } | 113 } |
103 return -1; | 114 return -1; |
104 } | 115 } |
105 | 116 |
106 void AddressComboboxModel::UpdateAddresses() { | 117 void AddressComboboxModel::UpdateAddresses() { |
107 addresses_.clear(); | 118 addresses_.clear(); |
108 std::vector<base::string16> labels; | 119 std::vector<base::string16> labels; |
(...skipping 12 matching lines...) Expand all Loading... |
121 continue; | 132 continue; |
122 | 133 |
123 addresses_.push_back(std::make_pair(profiles_cache_[i]->guid(), labels[i])); | 134 addresses_.push_back(std::make_pair(profiles_cache_[i]->guid(), labels[i])); |
124 } | 135 } |
125 | 136 |
126 for (auto& observer : observers_) { | 137 for (auto& observer : observers_) { |
127 observer.OnComboboxModelChanged(this); | 138 observer.OnComboboxModelChanged(this); |
128 } | 139 } |
129 } | 140 } |
130 } // namespace autofill | 141 } // namespace autofill |
OLD | NEW |