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" | 8 #include "components/autofill/core/browser/personal_data_manager.h" |
9 #include "components/payments/payment_details_validation.h" | 9 #include "components/payments/payment_details_validation.h" |
10 #include "components/payments/payment_request_delegate.h" | 10 #include "components/payments/payment_request_delegate.h" |
11 #include "components/payments/payment_request_web_contents_manager.h" | 11 #include "components/payments/payment_request_web_contents_manager.h" |
12 #include "content/public/browser/browser_thread.h" | 12 #include "content/public/browser/browser_thread.h" |
13 #include "content/public/browser/web_contents.h" | 13 #include "content/public/browser/web_contents.h" |
14 | 14 |
15 namespace payments { | 15 namespace payments { |
16 | 16 |
17 PaymentRequest::PaymentRequest( | 17 PaymentRequest::PaymentRequest( |
18 content::WebContents* web_contents, | 18 content::WebContents* web_contents, |
19 std::unique_ptr<PaymentRequestDelegate> delegate, | 19 std::unique_ptr<PaymentRequestDelegate> delegate, |
20 PaymentRequestWebContentsManager* manager, | 20 PaymentRequestWebContentsManager* manager, |
21 mojo::InterfaceRequest<payments::mojom::PaymentRequest> request) | 21 mojo::InterfaceRequest<payments::mojom::PaymentRequest> request) |
22 : web_contents_(web_contents), | 22 : web_contents_(web_contents), |
23 delegate_(std::move(delegate)), | 23 delegate_(std::move(delegate)), |
24 manager_(manager), | 24 manager_(manager), |
25 binding_(this, std::move(request)), | 25 binding_(this, std::move(request)), |
26 selected_shipping_profile_(nullptr), | 26 selected_shipping_profile_(nullptr), |
27 selected_contact_profile_(nullptr) { | 27 selected_contact_profile_(nullptr) { |
please use gerrit instead
2017/02/14 00:29:14
Need to initialize selected_credit_card_ to nullpt
anthonyvd
2017/02/22 20:15:21
Done.
| |
28 // OnConnectionTerminated will be called when the Mojo pipe is closed. This | 28 // OnConnectionTerminated will be called when the Mojo pipe is closed. This |
29 // will happen as a result of many renderer-side events (both successful and | 29 // will happen as a result of many renderer-side events (both successful and |
30 // erroneous in nature). | 30 // erroneous in nature). |
31 // TODO(crbug.com/683636): Investigate using | 31 // TODO(crbug.com/683636): Investigate using |
32 // set_connection_error_with_reason_handler with Binding::CloseWithReason. | 32 // set_connection_error_with_reason_handler with Binding::CloseWithReason. |
33 binding_.set_connection_error_handler(base::Bind( | 33 binding_.set_connection_error_handler(base::Bind( |
34 &PaymentRequest::OnConnectionTerminated, base::Unretained(this))); | 34 &PaymentRequest::OnConnectionTerminated, base::Unretained(this))); |
35 | 35 |
36 } | 36 } |
37 | 37 |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
113 const std::vector<autofill::AutofillProfile*>& | 113 const std::vector<autofill::AutofillProfile*>& |
114 PaymentRequest::shipping_profiles() { | 114 PaymentRequest::shipping_profiles() { |
115 return shipping_profiles_; | 115 return shipping_profiles_; |
116 } | 116 } |
117 | 117 |
118 const std::vector<autofill::AutofillProfile*>& | 118 const std::vector<autofill::AutofillProfile*>& |
119 PaymentRequest::contact_profiles() { | 119 PaymentRequest::contact_profiles() { |
120 return contact_profiles_; | 120 return contact_profiles_; |
121 } | 121 } |
122 | 122 |
123 autofill::CreditCard* PaymentRequest::GetCurrentlySelectedCreditCard() { | 123 const std::vector<autofill::CreditCard*> PaymentRequest::credit_cards() { |
124 // TODO(anthonyvd): Change this code to prioritize server cards and implement | |
125 // a way to modify this function's return value. | |
126 autofill::PersonalDataManager* data_manager = | 124 autofill::PersonalDataManager* data_manager = |
127 delegate_->GetPersonalDataManager(); | 125 delegate_->GetPersonalDataManager(); |
128 | 126 |
129 const std::vector<autofill::CreditCard*> cards = | 127 return data_manager->GetCreditCardsToSuggest(); |
please use gerrit instead
2017/02/14 00:29:14
These cards are owned by data_manager and can chan
anthonyvd
2017/02/22 20:15:21
Ahh, didn't know that! Done. I imagine we'll need
| |
130 data_manager->GetCreditCardsToSuggest(); | |
131 | |
132 auto first_complete_card = std::find_if( | |
133 cards.begin(), | |
134 cards.end(), | |
135 [] (autofill::CreditCard* card) { | |
136 return card->IsValid(); | |
137 }); | |
138 | |
139 return first_complete_card == cards.end() ? nullptr : *first_complete_card; | |
140 } | 128 } |
141 | 129 |
142 void PaymentRequest::PopulateProfileCache() { | 130 void PaymentRequest::PopulateProfileCache() { |
143 autofill::PersonalDataManager* data_manager = | 131 autofill::PersonalDataManager* data_manager = |
144 delegate_->GetPersonalDataManager(); | 132 delegate_->GetPersonalDataManager(); |
145 std::vector<autofill::AutofillProfile*> profiles = | 133 std::vector<autofill::AutofillProfile*> profiles = |
146 data_manager->GetProfilesToSuggest(); | 134 data_manager->GetProfilesToSuggest(); |
147 | 135 |
148 // PaymentRequest may outlive the Profiles returned by the Data Manager. | 136 // PaymentRequest may outlive the Profiles returned by the Data Manager. |
149 // Thus, we store copies, and return a vector of pointers to these copies | 137 // Thus, we store copies, and return a vector of pointers to these copies |
150 // whenever Profiles are requested. | 138 // whenever Profiles are requested. |
151 for (size_t i = 0; i < profiles.size(); i++) { | 139 for (size_t i = 0; i < profiles.size(); i++) { |
152 profile_cache_.push_back( | 140 profile_cache_.push_back( |
153 base::MakeUnique<autofill::AutofillProfile>(*profiles[i])); | 141 base::MakeUnique<autofill::AutofillProfile>(*profiles[i])); |
154 | 142 |
155 // TODO(tmartino): Implement deduplication rules specific to shipping and | 143 // TODO(tmartino): Implement deduplication rules specific to shipping and |
156 // contact profiles. | 144 // contact profiles. |
157 shipping_profiles_.push_back(profile_cache_[i].get()); | 145 shipping_profiles_.push_back(profile_cache_[i].get()); |
158 contact_profiles_.push_back(profile_cache_[i].get()); | 146 contact_profiles_.push_back(profile_cache_[i].get()); |
159 } | 147 } |
160 } | 148 } |
161 | 149 |
162 void PaymentRequest::SetDefaultProfileSelections() { | 150 void PaymentRequest::SetDefaultProfileSelections() { |
163 if (!shipping_profiles().empty()) | 151 if (!shipping_profiles().empty()) |
164 set_selected_shipping_profile(shipping_profiles()[0]); | 152 set_selected_shipping_profile(shipping_profiles()[0]); |
165 | 153 |
166 if (!contact_profiles().empty()) | 154 if (!contact_profiles().empty()) |
167 set_selected_contact_profile(contact_profiles()[0]); | 155 set_selected_contact_profile(contact_profiles()[0]); |
156 | |
157 // TODO(anthonyvd): Change this code to prioritize server cards and implement | |
158 // a way to modify this function's return value. | |
159 const std::vector<autofill::CreditCard*> cards = credit_cards(); | |
160 auto first_complete_card = std::find_if( | |
161 cards.begin(), | |
162 cards.end(), | |
163 [] (autofill::CreditCard* card) { | |
164 return card->IsValid(); | |
165 }); | |
166 | |
167 selected_credit_card_ = | |
168 first_complete_card == cards.end() ?nullptr : *first_complete_card; | |
please use gerrit instead
2017/02/14 00:29:14
need space after "?"
anthonyvd
2017/02/22 20:15:21
Done.
| |
168 } | 169 } |
169 | 170 |
170 } // namespace payments | 171 } // namespace payments |
OLD | NEW |