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