Chromium Code Reviews| OLD | NEW |
|---|---|
| 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> |
|
Mathieu
2017/05/19 01:10:41
was that for std::transform?
tmartino
2017/05/19 18:11:27
std::find_if is still used
| |
| 8 #include <set> | 8 #include <set> |
| 9 #include <utility> | 9 #include <utility> |
| 10 | 10 |
| 11 #include "base/strings/utf_string_conversions.h" | 11 #include "base/strings/utf_string_conversions.h" |
| 12 #include "components/autofill/core/browser/autofill_country.h" | 12 #include "components/autofill/core/browser/autofill_country.h" |
| 13 #include "components/autofill/core/browser/autofill_data_util.h" | 13 #include "components/autofill/core/browser/autofill_data_util.h" |
| 14 #include "components/autofill/core/browser/autofill_profile.h" | 14 #include "components/autofill/core/browser/autofill_profile.h" |
| 15 #include "components/autofill/core/browser/credit_card.h" | 15 #include "components/autofill/core/browser/credit_card.h" |
| 16 #include "components/autofill/core/browser/personal_data_manager.h" | 16 #include "components/autofill/core/browser/personal_data_manager.h" |
| 17 #include "components/payments/content/payment_response_helper.h" | 17 #include "components/payments/content/payment_response_helper.h" |
| (...skipping 209 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 227 } | 227 } |
| 228 | 228 |
| 229 autofill::RegionDataLoader* PaymentRequestState::GetRegionDataLoader() { | 229 autofill::RegionDataLoader* PaymentRequestState::GetRegionDataLoader() { |
| 230 return payment_request_delegate_->GetRegionDataLoader(); | 230 return payment_request_delegate_->GetRegionDataLoader(); |
| 231 } | 231 } |
| 232 | 232 |
| 233 void PaymentRequestState::PopulateProfileCache() { | 233 void PaymentRequestState::PopulateProfileCache() { |
| 234 std::vector<autofill::AutofillProfile*> profiles = | 234 std::vector<autofill::AutofillProfile*> profiles = |
| 235 personal_data_manager_->GetProfilesToSuggest(); | 235 personal_data_manager_->GetProfilesToSuggest(); |
| 236 | 236 |
| 237 std::vector<autofill::AutofillProfile*> raw_profiles_for_filtering; | |
| 238 raw_profiles_for_filtering.reserve(profiles.size()); | |
| 239 | |
| 237 // PaymentRequest may outlive the Profiles returned by the Data Manager. | 240 // PaymentRequest may outlive the Profiles returned by the Data Manager. |
| 238 // Thus, we store copies, and return a vector of pointers to these copies | 241 // Thus, we store copies, and return a vector of pointers to these copies |
| 239 // whenever Profiles are requested. | 242 // whenever Profiles are requested. |
| 240 for (size_t i = 0; i < profiles.size(); i++) { | 243 for (size_t i = 0; i < profiles.size(); i++) { |
| 241 profile_cache_.push_back( | 244 profile_cache_.push_back( |
| 242 base::MakeUnique<autofill::AutofillProfile>(*profiles[i])); | 245 base::MakeUnique<autofill::AutofillProfile>(*profiles[i])); |
| 243 | 246 raw_profiles_for_filtering.push_back(profile_cache_.back().get()); |
| 244 shipping_profiles_.push_back(profile_cache_[i].get()); | |
| 245 } | 247 } |
| 246 | 248 |
| 247 std::vector<autofill::AutofillProfile*> raw_profiles_for_filtering( | |
| 248 profile_cache_.size()); | |
| 249 std::transform(profile_cache_.begin(), profile_cache_.end(), | |
| 250 raw_profiles_for_filtering.begin(), | |
| 251 [](const std::unique_ptr<autofill::AutofillProfile>& p) { | |
| 252 return p.get(); | |
| 253 }); | |
| 254 | |
| 255 contact_profiles_ = profile_comparator()->FilterProfilesForContact( | 249 contact_profiles_ = profile_comparator()->FilterProfilesForContact( |
| 256 raw_profiles_for_filtering); | 250 raw_profiles_for_filtering); |
| 251 shipping_profiles_ = profile_comparator()->FilterProfilesForShipping( | |
| 252 raw_profiles_for_filtering); | |
| 257 | 253 |
| 258 // Create the list of available instruments. A copy of each card will be made | 254 // Create the list of available instruments. A copy of each card will be made |
| 259 // by their respective AutofillPaymentInstrument. | 255 // by their respective AutofillPaymentInstrument. |
| 260 const std::vector<autofill::CreditCard*>& cards = | 256 const std::vector<autofill::CreditCard*>& cards = |
| 261 personal_data_manager_->GetCreditCardsToSuggest(); | 257 personal_data_manager_->GetCreditCardsToSuggest(); |
| 262 for (autofill::CreditCard* card : cards) | 258 for (autofill::CreditCard* card : cards) |
| 263 AddAutofillPaymentInstrument(/*selected=*/false, *card); | 259 AddAutofillPaymentInstrument(/*selected=*/false, *card); |
| 264 } | 260 } |
| 265 | 261 |
| 266 void PaymentRequestState::SetDefaultProfileSelections() { | 262 void PaymentRequestState::SetDefaultProfileSelections() { |
| 267 // Only pre-select an address if the merchant provided at least one selected | 263 // Only pre-select an address if the merchant provided at least one selected |
| 268 // shipping option. | 264 // shipping option, and the top profile is complete. Assumes that profiles |
|
Mathieu
2017/05/19 01:10:41
Did we check with Zach/UX? Perhaps we should selec
tmartino
2017/05/19 18:11:27
Just checked. This is preferred, so that the "prev
| |
| 269 if (!shipping_profiles().empty() && spec_->selected_shipping_option()) { | 265 // have already been sorted for completeness and frecency. |
| 270 // Choose any complete shipping profile, or default to the most frecent | 266 if (!shipping_profiles().empty() && spec_->selected_shipping_option() && |
| 271 // address if no complete address could be found. | 267 profile_comparator()->IsShippingComplete(shipping_profiles_[0])) { |
| 272 selected_shipping_profile_ = shipping_profiles_[0]; | 268 selected_shipping_profile_ = shipping_profiles()[0]; |
| 273 for (autofill::AutofillProfile* profile : shipping_profiles_) { | |
| 274 if (profile_comparator_.IsShippingComplete(profile)) { | |
| 275 selected_shipping_profile_ = profile; | |
| 276 break; | |
| 277 } | |
| 278 } | |
| 279 } | 269 } |
| 280 | 270 |
| 281 // Contact profiles were ordered by completeness in addition to frecency; | 271 // Contact profiles were ordered by completeness in addition to frecency; |
| 282 // the first one is the best default selection. | 272 // the first one is the best default selection. |
| 283 if (!contact_profiles().empty()) | 273 if (!contact_profiles().empty() && |
| 274 profile_comparator()->IsContactInfoComplete(contact_profiles_[0])) | |
|
Mathieu
2017/05/19 01:10:41
same question here
tmartino
2017/05/19 18:11:27
ack
| |
| 284 selected_contact_profile_ = contact_profiles()[0]; | 275 selected_contact_profile_ = contact_profiles()[0]; |
| 285 | 276 |
| 286 // TODO(crbug.com/702063): Change this code to prioritize instruments by use | 277 // TODO(crbug.com/702063): Change this code to prioritize instruments by use |
| 287 // count and other means, and implement a way to modify this function's return | 278 // count and other means, and implement a way to modify this function's return |
| 288 // value. | 279 // value. |
| 289 const std::vector<std::unique_ptr<PaymentInstrument>>& instruments = | 280 const std::vector<std::unique_ptr<PaymentInstrument>>& instruments = |
| 290 available_instruments(); | 281 available_instruments(); |
| 291 auto first_complete_instrument = | 282 auto first_complete_instrument = |
| 292 std::find_if(instruments.begin(), instruments.end(), | 283 std::find_if(instruments.begin(), instruments.end(), |
| 293 [](const std::unique_ptr<PaymentInstrument>& instrument) { | 284 [](const std::unique_ptr<PaymentInstrument>& instrument) { |
| (...skipping 27 matching lines...) Expand all Loading... | |
| 321 if (is_waiting_for_merchant_validation_) | 312 if (is_waiting_for_merchant_validation_) |
| 322 return false; | 313 return false; |
| 323 | 314 |
| 324 if (!profile_comparator()->IsShippingComplete(selected_shipping_profile_)) | 315 if (!profile_comparator()->IsShippingComplete(selected_shipping_profile_)) |
| 325 return false; | 316 return false; |
| 326 | 317 |
| 327 return profile_comparator()->IsContactInfoComplete(selected_contact_profile_); | 318 return profile_comparator()->IsContactInfoComplete(selected_contact_profile_); |
| 328 } | 319 } |
| 329 | 320 |
| 330 } // namespace payments | 321 } // namespace payments |
| OLD | NEW |