| 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/payment_request.h" | 5 #include "components/payments/payment_request.h" |
| 6 | 6 |
| 7 #include "base/memory/ptr_util.h" | 7 #include "base/memory/ptr_util.h" |
| 8 #include "components/autofill/core/browser/personal_data_manager.h" | |
| 9 #include "components/payments/payment_details_validation.h" | 8 #include "components/payments/payment_details_validation.h" |
| 10 #include "components/payments/payment_request_delegate.h" | |
| 11 #include "components/payments/payment_request_web_contents_manager.h" | 9 #include "components/payments/payment_request_web_contents_manager.h" |
| 12 #include "content/public/browser/browser_thread.h" | 10 #include "content/public/browser/browser_thread.h" |
| 13 #include "content/public/browser/web_contents.h" | 11 #include "content/public/browser/web_contents.h" |
| 14 | 12 |
| 15 namespace payments { | 13 namespace payments { |
| 16 | 14 |
| 17 PaymentRequest::PaymentRequest( | 15 PaymentRequest::PaymentRequest( |
| 18 content::WebContents* web_contents, | 16 content::WebContents* web_contents, |
| 19 std::unique_ptr<PaymentRequestDelegate> delegate, | 17 std::unique_ptr<PaymentRequestDelegate> delegate, |
| 20 PaymentRequestWebContentsManager* manager, | 18 PaymentRequestWebContentsManager* manager, |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 116 } | 114 } |
| 117 | 115 |
| 118 const std::vector<autofill::AutofillProfile*>& | 116 const std::vector<autofill::AutofillProfile*>& |
| 119 PaymentRequest::contact_profiles() { | 117 PaymentRequest::contact_profiles() { |
| 120 return contact_profiles_; | 118 return contact_profiles_; |
| 121 } | 119 } |
| 122 | 120 |
| 123 autofill::CreditCard* PaymentRequest::GetCurrentlySelectedCreditCard() { | 121 autofill::CreditCard* PaymentRequest::GetCurrentlySelectedCreditCard() { |
| 124 // TODO(anthonyvd): Change this code to prioritize server cards and implement | 122 // TODO(anthonyvd): Change this code to prioritize server cards and implement |
| 125 // a way to modify this function's return value. | 123 // a way to modify this function's return value. |
| 126 autofill::PersonalDataManager* data_manager = | |
| 127 delegate_->GetPersonalDataManager(); | |
| 128 | |
| 129 const std::vector<autofill::CreditCard*> cards = | 124 const std::vector<autofill::CreditCard*> cards = |
| 130 data_manager->GetCreditCardsToSuggest(); | 125 personal_data_manager()->GetCreditCardsToSuggest(); |
| 131 | 126 |
| 132 auto first_complete_card = std::find_if( | 127 auto first_complete_card = std::find_if( |
| 133 cards.begin(), | 128 cards.begin(), |
| 134 cards.end(), | 129 cards.end(), |
| 135 [] (autofill::CreditCard* card) { | 130 [] (autofill::CreditCard* card) { |
| 136 return card->IsValid(); | 131 return card->IsValid(); |
| 137 }); | 132 }); |
| 138 | 133 |
| 139 return first_complete_card == cards.end() ? nullptr : *first_complete_card; | 134 return first_complete_card == cards.end() ? nullptr : *first_complete_card; |
| 140 } | 135 } |
| 141 | 136 |
| 142 void PaymentRequest::PopulateProfileCache() { | 137 void PaymentRequest::PopulateProfileCache() { |
| 143 autofill::PersonalDataManager* data_manager = | |
| 144 delegate_->GetPersonalDataManager(); | |
| 145 std::vector<autofill::AutofillProfile*> profiles = | 138 std::vector<autofill::AutofillProfile*> profiles = |
| 146 data_manager->GetProfilesToSuggest(); | 139 personal_data_manager()->GetProfilesToSuggest(); |
| 147 | 140 |
| 148 // PaymentRequest may outlive the Profiles returned by the Data Manager. | 141 // PaymentRequest may outlive the Profiles returned by the Data Manager. |
| 149 // Thus, we store copies, and return a vector of pointers to these copies | 142 // Thus, we store copies, and return a vector of pointers to these copies |
| 150 // whenever Profiles are requested. | 143 // whenever Profiles are requested. |
| 151 for (size_t i = 0; i < profiles.size(); i++) { | 144 for (size_t i = 0; i < profiles.size(); i++) { |
| 152 profile_cache_.push_back( | 145 profile_cache_.push_back( |
| 153 base::MakeUnique<autofill::AutofillProfile>(*profiles[i])); | 146 base::MakeUnique<autofill::AutofillProfile>(*profiles[i])); |
| 154 | 147 |
| 155 // TODO(tmartino): Implement deduplication rules specific to shipping and | 148 // TODO(tmartino): Implement deduplication rules specific to shipping and |
| 156 // contact profiles. | 149 // contact profiles. |
| 157 shipping_profiles_.push_back(profile_cache_[i].get()); | 150 shipping_profiles_.push_back(profile_cache_[i].get()); |
| 158 contact_profiles_.push_back(profile_cache_[i].get()); | 151 contact_profiles_.push_back(profile_cache_[i].get()); |
| 159 } | 152 } |
| 160 } | 153 } |
| 161 | 154 |
| 162 void PaymentRequest::SetDefaultProfileSelections() { | 155 void PaymentRequest::SetDefaultProfileSelections() { |
| 163 if (!shipping_profiles().empty()) | 156 if (!shipping_profiles().empty()) |
| 164 set_selected_shipping_profile(shipping_profiles()[0]); | 157 set_selected_shipping_profile(shipping_profiles()[0]); |
| 165 | 158 |
| 166 if (!contact_profiles().empty()) | 159 if (!contact_profiles().empty()) |
| 167 set_selected_contact_profile(contact_profiles()[0]); | 160 set_selected_contact_profile(contact_profiles()[0]); |
| 168 } | 161 } |
| 169 | 162 |
| 170 } // namespace payments | 163 } // namespace payments |
| OLD | NEW |