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/payments/payment_details_validation.h" | 8 #include "components/payments/payment_details_validation.h" |
9 #include "components/payments/payment_request_web_contents_manager.h" | 9 #include "components/payments/payment_request_web_contents_manager.h" |
10 #include "content/public/browser/browser_thread.h" | 10 #include "content/public/browser/browser_thread.h" |
(...skipping 13 matching lines...) Expand all Loading... | |
24 PaymentRequest::PaymentRequest( | 24 PaymentRequest::PaymentRequest( |
25 content::WebContents* web_contents, | 25 content::WebContents* web_contents, |
26 std::unique_ptr<PaymentRequestDelegate> delegate, | 26 std::unique_ptr<PaymentRequestDelegate> delegate, |
27 PaymentRequestWebContentsManager* manager, | 27 PaymentRequestWebContentsManager* manager, |
28 mojo::InterfaceRequest<payments::mojom::PaymentRequest> request) | 28 mojo::InterfaceRequest<payments::mojom::PaymentRequest> request) |
29 : web_contents_(web_contents), | 29 : web_contents_(web_contents), |
30 delegate_(std::move(delegate)), | 30 delegate_(std::move(delegate)), |
31 manager_(manager), | 31 manager_(manager), |
32 binding_(this, std::move(request)), | 32 binding_(this, std::move(request)), |
33 selected_shipping_profile_(nullptr), | 33 selected_shipping_profile_(nullptr), |
34 selected_contact_profile_(nullptr) { | 34 selected_contact_profile_(nullptr), |
35 selected_credit_card_(nullptr) { | |
35 // OnConnectionTerminated will be called when the Mojo pipe is closed. This | 36 // OnConnectionTerminated will be called when the Mojo pipe is closed. This |
36 // will happen as a result of many renderer-side events (both successful and | 37 // will happen as a result of many renderer-side events (both successful and |
37 // erroneous in nature). | 38 // erroneous in nature). |
38 // TODO(crbug.com/683636): Investigate using | 39 // TODO(crbug.com/683636): Investigate using |
39 // set_connection_error_with_reason_handler with Binding::CloseWithReason. | 40 // set_connection_error_with_reason_handler with Binding::CloseWithReason. |
40 binding_.set_connection_error_handler(base::Bind( | 41 binding_.set_connection_error_handler(base::Bind( |
41 &PaymentRequest::OnConnectionTerminated, base::Unretained(this))); | 42 &PaymentRequest::OnConnectionTerminated, base::Unretained(this))); |
42 | 43 |
43 } | 44 } |
44 | 45 |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
121 const std::vector<autofill::AutofillProfile*>& | 122 const std::vector<autofill::AutofillProfile*>& |
122 PaymentRequest::shipping_profiles() { | 123 PaymentRequest::shipping_profiles() { |
123 return shipping_profiles_; | 124 return shipping_profiles_; |
124 } | 125 } |
125 | 126 |
126 const std::vector<autofill::AutofillProfile*>& | 127 const std::vector<autofill::AutofillProfile*>& |
127 PaymentRequest::contact_profiles() { | 128 PaymentRequest::contact_profiles() { |
128 return contact_profiles_; | 129 return contact_profiles_; |
129 } | 130 } |
130 | 131 |
131 autofill::CreditCard* PaymentRequest::GetCurrentlySelectedCreditCard() { | 132 const std::vector<autofill::CreditCard*>& PaymentRequest::credit_cards() { |
Mathieu
2017/02/23 00:39:07
this can go in the .h?
| |
132 // TODO(anthonyvd): Change this code to prioritize server cards and implement | 133 return credit_cards_; |
133 // a way to modify this function's return value. | |
134 const std::vector<autofill::CreditCard*> cards = | |
135 personal_data_manager()->GetCreditCardsToSuggest(); | |
136 | |
137 auto first_complete_card = std::find_if( | |
138 cards.begin(), | |
139 cards.end(), | |
140 [] (autofill::CreditCard* card) { | |
141 return card->IsValid(); | |
142 }); | |
143 | |
144 return first_complete_card == cards.end() ? nullptr : *first_complete_card; | |
145 } | 134 } |
146 | 135 |
147 void PaymentRequest::PopulateProfileCache() { | 136 void PaymentRequest::PopulateProfileCache() { |
148 std::vector<autofill::AutofillProfile*> profiles = | 137 std::vector<autofill::AutofillProfile*> profiles = |
149 personal_data_manager()->GetProfilesToSuggest(); | 138 personal_data_manager()->GetProfilesToSuggest(); |
150 | 139 |
151 // PaymentRequest may outlive the Profiles returned by the Data Manager. | 140 // PaymentRequest may outlive the Profiles returned by the Data Manager. |
152 // Thus, we store copies, and return a vector of pointers to these copies | 141 // Thus, we store copies, and return a vector of pointers to these copies |
153 // whenever Profiles are requested. | 142 // whenever Profiles are requested. The same is true for credit cards. |
154 for (size_t i = 0; i < profiles.size(); i++) { | 143 for (size_t i = 0; i < profiles.size(); i++) { |
155 profile_cache_.push_back( | 144 profile_cache_.push_back( |
156 base::MakeUnique<autofill::AutofillProfile>(*profiles[i])); | 145 base::MakeUnique<autofill::AutofillProfile>(*profiles[i])); |
157 | 146 |
158 // TODO(tmartino): Implement deduplication rules specific to shipping and | 147 // TODO(tmartino): Implement deduplication rules specific to shipping and |
159 // contact profiles. | 148 // contact profiles. |
160 shipping_profiles_.push_back(profile_cache_[i].get()); | 149 shipping_profiles_.push_back(profile_cache_[i].get()); |
161 contact_profiles_.push_back(profile_cache_[i].get()); | 150 contact_profiles_.push_back(profile_cache_[i].get()); |
162 } | 151 } |
152 | |
153 const std::vector<autofill::CreditCard*>& cards = | |
154 personal_data_manager()->GetCreditCardsToSuggest(); | |
155 for (autofill::CreditCard* card : cards) { | |
156 card_cache_.push_back(base::MakeUnique<autofill::CreditCard>(*card)); | |
157 credit_cards_.push_back(card_cache_.back().get()); | |
158 } | |
163 } | 159 } |
164 | 160 |
165 void PaymentRequest::SetDefaultProfileSelections() { | 161 void PaymentRequest::SetDefaultProfileSelections() { |
166 if (!shipping_profiles().empty()) | 162 if (!shipping_profiles().empty()) |
167 set_selected_shipping_profile(shipping_profiles()[0]); | 163 set_selected_shipping_profile(shipping_profiles()[0]); |
168 | 164 |
169 if (!contact_profiles().empty()) | 165 if (!contact_profiles().empty()) |
170 set_selected_contact_profile(contact_profiles()[0]); | 166 set_selected_contact_profile(contact_profiles()[0]); |
167 | |
168 // TODO(anthonyvd): Change this code to prioritize server cards and implement | |
169 // a way to modify this function's return value. | |
170 const std::vector<autofill::CreditCard*> cards = credit_cards(); | |
171 auto first_complete_card = | |
172 std::find_if(cards.begin(), cards.end(), | |
173 [](autofill::CreditCard* card) { return card->IsValid(); }); | |
174 | |
175 selected_credit_card_ = | |
176 first_complete_card == cards.end() ? nullptr : *first_complete_card; | |
171 } | 177 } |
172 | 178 |
173 void PaymentRequest::PopulateValidatedMethodData( | 179 void PaymentRequest::PopulateValidatedMethodData( |
174 const std::vector<payments::mojom::PaymentMethodDataPtr>& method_data) { | 180 const std::vector<payments::mojom::PaymentMethodDataPtr>& method_data) { |
175 if (method_data.empty()) { | 181 if (method_data.empty()) { |
176 LOG(ERROR) << "Invalid payment methods or data"; | 182 LOG(ERROR) << "Invalid payment methods or data"; |
177 OnConnectionTerminated(); | 183 OnConnectionTerminated(); |
178 return; | 184 return; |
179 } | 185 } |
180 | 186 |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
235 card_networks.erase(card_it); | 241 card_networks.erase(card_it); |
236 } | 242 } |
237 } | 243 } |
238 } | 244 } |
239 } | 245 } |
240 } | 246 } |
241 } | 247 } |
242 } | 248 } |
243 | 249 |
244 } // namespace payments | 250 } // namespace payments |
OLD | NEW |