Chromium Code Reviews| Index: components/payments/payment_request.cc |
| diff --git a/components/payments/payment_request.cc b/components/payments/payment_request.cc |
| index 282b3fa38afad3d8ef83f6db57f9f05c532ca0ad..96f67388ce1c82cc50d1daafc526356b239436e7 100644 |
| --- a/components/payments/payment_request.cc |
| +++ b/components/payments/payment_request.cc |
| @@ -22,7 +22,9 @@ PaymentRequest::PaymentRequest( |
| : web_contents_(web_contents), |
| delegate_(std::move(delegate)), |
| manager_(manager), |
| - binding_(this, std::move(request)) { |
| + binding_(this, std::move(request)), |
| + selected_shipping_profile_(nullptr), |
| + selected_contact_profile_(nullptr) { |
| binding_.set_connection_error_handler( |
| base::Bind(&PaymentRequest::OnError, base::Unretained(this))); |
| } |
| @@ -44,6 +46,8 @@ void PaymentRequest::Init( |
| } |
| client_ = std::move(client); |
| details_ = std::move(details); |
| + PopulateProfileCache(); |
| + SetDefaultProfileSelections(); |
| } |
| void PaymentRequest::Show() { |
| @@ -75,17 +79,14 @@ CurrencyFormatter* PaymentRequest::GetOrCreateCurrencyFormatter( |
| return currency_formatter_.get(); |
| } |
| -autofill::AutofillProfile* PaymentRequest::GetCurrentlySelectedProfile() { |
| - // TODO(tmartino): Implement more sophisticated algorithm for populating |
| - // this when it starts empty. |
| - if (!profile_) { |
| - autofill::PersonalDataManager* data_manager = |
| - delegate_->GetPersonalDataManager(); |
| - auto profiles = data_manager->GetProfiles(); |
| - if (!profiles.empty()) |
| - profile_ = base::MakeUnique<autofill::AutofillProfile>(*profiles[0]); |
| - } |
| - return profile_ ? profile_.get() : nullptr; |
| +const std::vector<autofill::AutofillProfile*>& |
| +PaymentRequest::shipping_profiles() { |
| + return shipping_profiles_; |
| +} |
| + |
| +const std::vector<autofill::AutofillProfile*>& |
| +PaymentRequest::contact_profiles() { |
| + return contact_profiles_; |
| } |
| autofill::CreditCard* PaymentRequest::GetCurrentlySelectedCreditCard() { |
| @@ -107,4 +108,32 @@ autofill::CreditCard* PaymentRequest::GetCurrentlySelectedCreditCard() { |
| return first_complete_card == cards.end() ? nullptr : *first_complete_card; |
| } |
| +void PaymentRequest::PopulateProfileCache() { |
| + autofill::PersonalDataManager* data_manager = |
| + delegate_->GetPersonalDataManager(); |
| + std::vector<autofill::AutofillProfile*> profiles = |
| + data_manager->GetProfilesToSuggest(); |
| + |
| + // PaymentRequest may outlive the Profiles returned by the Data Manager. |
| + // Thus, we store copies, and return a vector of pointers to these copies |
| + // whenever Profiles are requested. |
| + for (size_t i = 0; i < profiles.size(); i++) { |
| + profile_cache_.push_back( |
| + base::MakeUnique<autofill::AutofillProfile>(*profiles[0])); |
|
please use gerrit instead
2017/01/25 15:07:47
profiles[i]
tmartino
2017/01/25 18:48:10
Good catch, thanks.
|
| + |
| + // TODO(tmartino): Implement deduplication rules specific to shipping and |
| + // contact profiles. |
| + shipping_profiles_.push_back(profile_cache_[i].get()); |
| + contact_profiles_.push_back(profile_cache_[i].get()); |
| + } |
| +} |
| + |
| +void PaymentRequest::SetDefaultProfileSelections() { |
| + if (!shipping_profiles().empty()) |
| + set_selected_shipping_profile(shipping_profiles()[0]); |
| + |
| + if (!contact_profiles().empty()) |
| + set_selected_contact_profile(contact_profiles()[0]); |
| +} |
| + |
| } // namespace payments |