Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(148)

Side by Side Diff: components/payments/content/payment_request_state.cc

Issue 2878763002: [Payments] Better default selections on iOS (Closed)
Patch Set: fix test, really Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | ios/chrome/browser/payments/payment_request.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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/payments/content/payment_request_state.h" 5 #include "components/payments/content/payment_request_state.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <set> 8 #include <set>
9 #include <utility> 9 #include <utility>
10 10
(...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after
192 autofill::RegionDataLoader* PaymentRequestState::GetRegionDataLoader() { 192 autofill::RegionDataLoader* PaymentRequestState::GetRegionDataLoader() {
193 return payment_request_delegate_->GetRegionDataLoader(); 193 return payment_request_delegate_->GetRegionDataLoader();
194 } 194 }
195 195
196 void PaymentRequestState::PopulateProfileCache() { 196 void PaymentRequestState::PopulateProfileCache() {
197 std::vector<autofill::AutofillProfile*> profiles = 197 std::vector<autofill::AutofillProfile*> profiles =
198 personal_data_manager_->GetProfilesToSuggest(); 198 personal_data_manager_->GetProfilesToSuggest();
199 199
200 // PaymentRequest may outlive the Profiles returned by the Data Manager. 200 // PaymentRequest may outlive the Profiles returned by the Data Manager.
201 // Thus, we store copies, and return a vector of pointers to these copies 201 // Thus, we store copies, and return a vector of pointers to these copies
202 // whenever Profiles are requested. The same is true for credit cards. 202 // whenever Profiles are requested.
203 for (size_t i = 0; i < profiles.size(); i++) { 203 for (size_t i = 0; i < profiles.size(); i++) {
204 profile_cache_.push_back( 204 profile_cache_.push_back(
205 base::MakeUnique<autofill::AutofillProfile>(*profiles[i])); 205 base::MakeUnique<autofill::AutofillProfile>(*profiles[i]));
206 206
207 // TODO(tmartino): Implement deduplication rules specific to shipping
208 // profiles.
209 shipping_profiles_.push_back(profile_cache_[i].get()); 207 shipping_profiles_.push_back(profile_cache_[i].get());
210 } 208 }
211 209
212 std::vector<autofill::AutofillProfile*> raw_profiles_for_filtering( 210 std::vector<autofill::AutofillProfile*> raw_profiles_for_filtering(
213 profile_cache_.size()); 211 profile_cache_.size());
214 std::transform(profile_cache_.begin(), profile_cache_.end(), 212 std::transform(profile_cache_.begin(), profile_cache_.end(),
215 raw_profiles_for_filtering.begin(), 213 raw_profiles_for_filtering.begin(),
216 [](const std::unique_ptr<autofill::AutofillProfile>& p) { 214 [](const std::unique_ptr<autofill::AutofillProfile>& p) {
217 return p.get(); 215 return p.get();
218 }); 216 });
219 217
220 contact_profiles_ = profile_comparator()->FilterProfilesForContact( 218 contact_profiles_ = profile_comparator()->FilterProfilesForContact(
221 raw_profiles_for_filtering); 219 raw_profiles_for_filtering);
222 220
223 // Create the list of available instruments. 221 // Create the list of available instruments. A copy of each card will be made
222 // by their respective AutofillPaymentInstrument.
224 const std::vector<autofill::CreditCard*>& cards = 223 const std::vector<autofill::CreditCard*>& cards =
225 personal_data_manager_->GetCreditCardsToSuggest(); 224 personal_data_manager_->GetCreditCardsToSuggest();
226 for (autofill::CreditCard* card : cards) 225 for (autofill::CreditCard* card : cards)
227 AddAutofillPaymentInstrument(/*selected=*/false, *card); 226 AddAutofillPaymentInstrument(/*selected=*/false, *card);
228 } 227 }
229 228
230 void PaymentRequestState::SetDefaultProfileSelections() { 229 void PaymentRequestState::SetDefaultProfileSelections() {
231 // Only pre-select an address if the merchant provided at least one selected 230 // Only pre-select an address if the merchant provided at least one selected
232 // shipping option. 231 // shipping option.
233 if (!shipping_profiles().empty() && spec_->selected_shipping_option()) 232 if (!shipping_profiles().empty() && spec_->selected_shipping_option()) {
234 selected_shipping_profile_ = shipping_profiles()[0]; 233 // Choose any complete shipping profile, or default to the most frecent
234 // address if no complete address could be found.
235 selected_shipping_profile_ = shipping_profiles_[0];
236 for (autofill::AutofillProfile* profile : shipping_profiles_) {
237 if (profile_comparator_.IsShippingComplete(profile)) {
238 selected_shipping_profile_ = profile;
239 break;
240 }
241 }
242 }
235 243
244 // Contact profiles were ordered by completeness in addition to frecency;
245 // the first one is the best default selection.
236 if (!contact_profiles().empty()) 246 if (!contact_profiles().empty())
237 selected_contact_profile_ = contact_profiles()[0]; 247 selected_contact_profile_ = contact_profiles()[0];
238 248
239 // TODO(crbug.com/702063): Change this code to prioritize instruments by use 249 // TODO(crbug.com/702063): Change this code to prioritize instruments by use
240 // count and other means, and implement a way to modify this function's return 250 // count and other means, and implement a way to modify this function's return
241 // value. 251 // value.
242 const std::vector<std::unique_ptr<PaymentInstrument>>& instruments = 252 const std::vector<std::unique_ptr<PaymentInstrument>>& instruments =
243 available_instruments(); 253 available_instruments();
244 auto first_complete_instrument = 254 auto first_complete_instrument =
245 std::find_if(instruments.begin(), instruments.end(), 255 std::find_if(instruments.begin(), instruments.end(),
(...skipping 30 matching lines...) Expand all
276 if (is_waiting_for_merchant_validation_) 286 if (is_waiting_for_merchant_validation_)
277 return false; 287 return false;
278 288
279 if (!profile_comparator()->IsShippingComplete(selected_shipping_profile_)) 289 if (!profile_comparator()->IsShippingComplete(selected_shipping_profile_))
280 return false; 290 return false;
281 291
282 return profile_comparator()->IsContactInfoComplete(selected_contact_profile_); 292 return profile_comparator()->IsContactInfoComplete(selected_contact_profile_);
283 } 293 }
284 294
285 } // namespace payments 295 } // namespace payments
OLDNEW
« no previous file with comments | « no previous file | ios/chrome/browser/payments/payment_request.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698