| 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> |
| 8 #include <set> | 8 #include <set> |
| 9 #include <utility> | 9 #include <utility> |
| 10 | 10 |
| (...skipping 220 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 231 } | 231 } |
| 232 | 232 |
| 233 bool PaymentRequestState::IsPaymentAppInvoked() const { | 233 bool PaymentRequestState::IsPaymentAppInvoked() const { |
| 234 return !!response_helper_; | 234 return !!response_helper_; |
| 235 } | 235 } |
| 236 | 236 |
| 237 void PaymentRequestState::PopulateProfileCache() { | 237 void PaymentRequestState::PopulateProfileCache() { |
| 238 std::vector<autofill::AutofillProfile*> profiles = | 238 std::vector<autofill::AutofillProfile*> profiles = |
| 239 personal_data_manager_->GetProfilesToSuggest(); | 239 personal_data_manager_->GetProfilesToSuggest(); |
| 240 | 240 |
| 241 std::vector<autofill::AutofillProfile*> raw_profiles_for_filtering; |
| 242 raw_profiles_for_filtering.reserve(profiles.size()); |
| 243 |
| 241 // PaymentRequest may outlive the Profiles returned by the Data Manager. | 244 // PaymentRequest may outlive the Profiles returned by the Data Manager. |
| 242 // Thus, we store copies, and return a vector of pointers to these copies | 245 // Thus, we store copies, and return a vector of pointers to these copies |
| 243 // whenever Profiles are requested. | 246 // whenever Profiles are requested. |
| 244 for (size_t i = 0; i < profiles.size(); i++) { | 247 for (size_t i = 0; i < profiles.size(); i++) { |
| 245 profile_cache_.push_back( | 248 profile_cache_.push_back( |
| 246 base::MakeUnique<autofill::AutofillProfile>(*profiles[i])); | 249 base::MakeUnique<autofill::AutofillProfile>(*profiles[i])); |
| 247 | 250 raw_profiles_for_filtering.push_back(profile_cache_.back().get()); |
| 248 shipping_profiles_.push_back(profile_cache_[i].get()); | |
| 249 } | 251 } |
| 250 | 252 |
| 251 std::vector<autofill::AutofillProfile*> raw_profiles_for_filtering( | |
| 252 profile_cache_.size()); | |
| 253 std::transform(profile_cache_.begin(), profile_cache_.end(), | |
| 254 raw_profiles_for_filtering.begin(), | |
| 255 [](const std::unique_ptr<autofill::AutofillProfile>& p) { | |
| 256 return p.get(); | |
| 257 }); | |
| 258 | |
| 259 contact_profiles_ = profile_comparator()->FilterProfilesForContact( | 253 contact_profiles_ = profile_comparator()->FilterProfilesForContact( |
| 260 raw_profiles_for_filtering); | 254 raw_profiles_for_filtering); |
| 255 shipping_profiles_ = profile_comparator()->FilterProfilesForShipping( |
| 256 raw_profiles_for_filtering); |
| 261 | 257 |
| 262 // Create the list of available instruments. A copy of each card will be made | 258 // Create the list of available instruments. A copy of each card will be made |
| 263 // by their respective AutofillPaymentInstrument. | 259 // by their respective AutofillPaymentInstrument. |
| 264 const std::vector<autofill::CreditCard*>& cards = | 260 const std::vector<autofill::CreditCard*>& cards = |
| 265 personal_data_manager_->GetCreditCardsToSuggest(); | 261 personal_data_manager_->GetCreditCardsToSuggest(); |
| 266 for (autofill::CreditCard* card : cards) | 262 for (autofill::CreditCard* card : cards) |
| 267 AddAutofillPaymentInstrument(/*selected=*/false, *card); | 263 AddAutofillPaymentInstrument(/*selected=*/false, *card); |
| 268 } | 264 } |
| 269 | 265 |
| 270 void PaymentRequestState::SetDefaultProfileSelections() { | 266 void PaymentRequestState::SetDefaultProfileSelections() { |
| 271 // Only pre-select an address if the merchant provided at least one selected | 267 // Only pre-select an address if the merchant provided at least one selected |
| 272 // shipping option. | 268 // shipping option, and the top profile is complete. Assumes that profiles |
| 273 if (!shipping_profiles().empty() && spec_->selected_shipping_option()) { | 269 // have already been sorted for completeness and frecency. |
| 274 // Choose any complete shipping profile, or default to the most frecent | 270 if (!shipping_profiles().empty() && spec_->selected_shipping_option() && |
| 275 // address if no complete address could be found. | 271 profile_comparator()->IsShippingComplete(shipping_profiles_[0])) { |
| 276 selected_shipping_profile_ = shipping_profiles_[0]; | 272 selected_shipping_profile_ = shipping_profiles()[0]; |
| 277 for (autofill::AutofillProfile* profile : shipping_profiles_) { | |
| 278 if (profile_comparator_.IsShippingComplete(profile)) { | |
| 279 selected_shipping_profile_ = profile; | |
| 280 break; | |
| 281 } | |
| 282 } | |
| 283 } | 273 } |
| 284 | 274 |
| 285 // Contact profiles were ordered by completeness in addition to frecency; | 275 // Contact profiles were ordered by completeness in addition to frecency; |
| 286 // the first one is the best default selection. | 276 // the first one is the best default selection. |
| 287 if (!contact_profiles().empty()) | 277 if (!contact_profiles().empty() && |
| 278 profile_comparator()->IsContactInfoComplete(contact_profiles_[0])) |
| 288 selected_contact_profile_ = contact_profiles()[0]; | 279 selected_contact_profile_ = contact_profiles()[0]; |
| 289 | 280 |
| 290 // TODO(crbug.com/702063): Change this code to prioritize instruments by use | 281 // TODO(crbug.com/702063): Change this code to prioritize instruments by use |
| 291 // count and other means, and implement a way to modify this function's return | 282 // count and other means, and implement a way to modify this function's return |
| 292 // value. | 283 // value. |
| 293 const std::vector<std::unique_ptr<PaymentInstrument>>& instruments = | 284 const std::vector<std::unique_ptr<PaymentInstrument>>& instruments = |
| 294 available_instruments(); | 285 available_instruments(); |
| 295 auto first_complete_instrument = | 286 auto first_complete_instrument = |
| 296 std::find_if(instruments.begin(), instruments.end(), | 287 std::find_if(instruments.begin(), instruments.end(), |
| 297 [](const std::unique_ptr<PaymentInstrument>& instrument) { | 288 [](const std::unique_ptr<PaymentInstrument>& instrument) { |
| (...skipping 27 matching lines...) Expand all Loading... |
| 325 if (is_waiting_for_merchant_validation_) | 316 if (is_waiting_for_merchant_validation_) |
| 326 return false; | 317 return false; |
| 327 | 318 |
| 328 if (!profile_comparator()->IsShippingComplete(selected_shipping_profile_)) | 319 if (!profile_comparator()->IsShippingComplete(selected_shipping_profile_)) |
| 329 return false; | 320 return false; |
| 330 | 321 |
| 331 return profile_comparator()->IsContactInfoComplete(selected_contact_profile_); | 322 return profile_comparator()->IsContactInfoComplete(selected_contact_profile_); |
| 332 } | 323 } |
| 333 | 324 |
| 334 } // namespace payments | 325 } // namespace payments |
| OLD | NEW |