Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(815)

Side by Side Diff: components/payments/content/payment_request.cc

Issue 2713033004: Layered component for web payments (Closed)
Patch Set: Rebase Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/content/payment_request.h"
6
7 #include <unordered_map>
8 #include <utility>
6 9
7 #include "base/memory/ptr_util.h" 10 #include "base/memory/ptr_util.h"
8 #include "components/payments/payment_details_validation.h" 11 #include "components/autofill/core/browser/personal_data_manager.h"
9 #include "components/payments/payment_request_web_contents_manager.h" 12 #include "components/payments/content/payment_details_validation.h"
13 #include "components/payments/content/payment_request_web_contents_manager.h"
14 #include "components/payments/core/currency_formatter.h"
10 #include "content/public/browser/browser_thread.h" 15 #include "content/public/browser/browser_thread.h"
11 #include "content/public/browser/web_contents.h" 16 #include "content/public/browser/web_contents.h"
12 17
13 using payments::mojom::BasicCardNetwork;
14
15 namespace payments { 18 namespace payments {
16 19
17 namespace {
18
19 // Identifier for the basic card payment method in the PaymentMethodData.
20 const char* const kBasicCardMethodName = "basic-card";
21
22 } // namespace
23
24 PaymentRequest::PaymentRequest( 20 PaymentRequest::PaymentRequest(
25 content::WebContents* web_contents, 21 content::WebContents* web_contents,
26 std::unique_ptr<PaymentRequestDelegate> delegate, 22 std::unique_ptr<PaymentRequestDelegate> delegate,
27 PaymentRequestWebContentsManager* manager, 23 PaymentRequestWebContentsManager* manager,
28 mojo::InterfaceRequest<payments::mojom::PaymentRequest> request) 24 mojo::InterfaceRequest<payments::mojom::PaymentRequest> request)
29 : web_contents_(web_contents), 25 : web_contents_(web_contents),
30 delegate_(std::move(delegate)), 26 delegate_(std::move(delegate)),
31 manager_(manager), 27 manager_(manager),
32 binding_(this, std::move(request)), 28 binding_(this, std::move(request)),
33 selected_shipping_profile_(nullptr), 29 selected_shipping_profile_(nullptr),
34 selected_contact_profile_(nullptr), 30 selected_contact_profile_(nullptr),
35 selected_credit_card_(nullptr) { 31 selected_credit_card_(nullptr) {
36 // OnConnectionTerminated will be called when the Mojo pipe is closed. This 32 // OnConnectionTerminated will be called when the Mojo pipe is closed. This
37 // will happen as a result of many renderer-side events (both successful and 33 // will happen as a result of many renderer-side events (both successful and
38 // erroneous in nature). 34 // erroneous in nature).
39 // TODO(crbug.com/683636): Investigate using 35 // TODO(crbug.com/683636): Investigate using
40 // set_connection_error_with_reason_handler with Binding::CloseWithReason. 36 // set_connection_error_with_reason_handler with Binding::CloseWithReason.
41 binding_.set_connection_error_handler(base::Bind( 37 binding_.set_connection_error_handler(base::Bind(
42 &PaymentRequest::OnConnectionTerminated, base::Unretained(this))); 38 &PaymentRequest::OnConnectionTerminated, base::Unretained(this)));
43
44 } 39 }
45 40
46 PaymentRequest::~PaymentRequest() {} 41 PaymentRequest::~PaymentRequest() {}
47 42
48 void PaymentRequest::Init( 43 void PaymentRequest::Init(
49 payments::mojom::PaymentRequestClientPtr client, 44 payments::mojom::PaymentRequestClientPtr client,
50 std::vector<payments::mojom::PaymentMethodDataPtr> method_data, 45 std::vector<payments::mojom::PaymentMethodDataPtr> method_data,
51 payments::mojom::PaymentDetailsPtr details, 46 payments::mojom::PaymentDetailsPtr details,
52 payments::mojom::PaymentOptionsPtr options) { 47 payments::mojom::PaymentOptionsPtr options) {
53 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 48 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
113 const std::string& currency_system, 108 const std::string& currency_system,
114 const std::string& locale_name) { 109 const std::string& locale_name) {
115 if (!currency_formatter_) { 110 if (!currency_formatter_) {
116 currency_formatter_.reset( 111 currency_formatter_.reset(
117 new CurrencyFormatter(currency_code, currency_system, locale_name)); 112 new CurrencyFormatter(currency_code, currency_system, locale_name));
118 } 113 }
119 return currency_formatter_.get(); 114 return currency_formatter_.get();
120 } 115 }
121 116
122 const std::vector<autofill::AutofillProfile*>& 117 const std::vector<autofill::AutofillProfile*>&
123 PaymentRequest::shipping_profiles() { 118 PaymentRequest::shipping_profiles() {
124 return shipping_profiles_; 119 return shipping_profiles_;
125 } 120 }
126 121
127 const std::vector<autofill::AutofillProfile*>& 122 const std::vector<autofill::AutofillProfile*>&
128 PaymentRequest::contact_profiles() { 123 PaymentRequest::contact_profiles() {
129 return contact_profiles_; 124 return contact_profiles_;
130 } 125 }
131 126
132 void PaymentRequest::PopulateProfileCache() { 127 void PaymentRequest::PopulateProfileCache() {
133 std::vector<autofill::AutofillProfile*> profiles = 128 std::vector<autofill::AutofillProfile*> profiles =
134 personal_data_manager()->GetProfilesToSuggest(); 129 personal_data_manager()->GetProfilesToSuggest();
135 130
136 // PaymentRequest may outlive the Profiles returned by the Data Manager. 131 // PaymentRequest may outlive the Profiles returned by the Data Manager.
137 // Thus, we store copies, and return a vector of pointers to these copies 132 // Thus, we store copies, and return a vector of pointers to these copies
138 // whenever Profiles are requested. The same is true for credit cards. 133 // whenever Profiles are requested. The same is true for credit cards.
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
173 } 168 }
174 169
175 void PaymentRequest::PopulateValidatedMethodData( 170 void PaymentRequest::PopulateValidatedMethodData(
176 const std::vector<payments::mojom::PaymentMethodDataPtr>& method_data) { 171 const std::vector<payments::mojom::PaymentMethodDataPtr>& method_data) {
177 if (method_data.empty()) { 172 if (method_data.empty()) {
178 LOG(ERROR) << "Invalid payment methods or data"; 173 LOG(ERROR) << "Invalid payment methods or data";
179 OnConnectionTerminated(); 174 OnConnectionTerminated();
180 return; 175 return;
181 } 176 }
182 177
178 // Identifier for the basic card payment method in the PaymentMethodData.
179 const char* const kBasicCardMethodName = "basic-card";
183 std::set<std::string> card_networks{"amex", "diners", "discover", 180 std::set<std::string> card_networks{"amex", "diners", "discover",
184 "jcb", "mastercard", "mir", 181 "jcb", "mastercard", "mir",
185 "unionpay", "visa"}; 182 "unionpay", "visa"};
186 for (const payments::mojom::PaymentMethodDataPtr& method_data_entry : 183 for (const payments::mojom::PaymentMethodDataPtr& method_data_entry :
187 method_data) { 184 method_data) {
188 std::vector<std::string> supported_methods = 185 std::vector<std::string> supported_methods =
189 method_data_entry->supported_methods; 186 method_data_entry->supported_methods;
190 if (supported_methods.empty()) { 187 if (supported_methods.empty()) {
191 LOG(ERROR) << "Invalid payment methods or data"; 188 LOG(ERROR) << "Invalid payment methods or data";
192 OnConnectionTerminated(); 189 OnConnectionTerminated();
(...skipping 18 matching lines...) Expand all
211 // Empty |supported_networks| means all networks are supported. 208 // Empty |supported_networks| means all networks are supported.
212 supported_card_networks_.insert(supported_card_networks_.end(), 209 supported_card_networks_.insert(supported_card_networks_.end(),
213 card_networks.begin(), 210 card_networks.begin(),
214 card_networks.end()); 211 card_networks.end());
215 // Clear the set so that no further networks are added to 212 // Clear the set so that no further networks are added to
216 // |supported_card_networks_|. 213 // |supported_card_networks_|.
217 card_networks.clear(); 214 card_networks.clear();
218 } else { 215 } else {
219 // The merchant has specified a few basic card supported networks. Use 216 // The merchant has specified a few basic card supported networks. Use
220 // the mapping to transform to known basic-card types. 217 // the mapping to transform to known basic-card types.
218 using ::payments::mojom::BasicCardNetwork;
221 std::unordered_map<BasicCardNetwork, std::string> networks = { 219 std::unordered_map<BasicCardNetwork, std::string> networks = {
222 {BasicCardNetwork::AMEX, "amex"}, 220 {BasicCardNetwork::AMEX, "amex"},
223 {BasicCardNetwork::DINERS, "diners"}, 221 {BasicCardNetwork::DINERS, "diners"},
224 {BasicCardNetwork::DISCOVER, "discover"}, 222 {BasicCardNetwork::DISCOVER, "discover"},
225 {BasicCardNetwork::JCB, "jcb"}, 223 {BasicCardNetwork::JCB, "jcb"},
226 {BasicCardNetwork::MASTERCARD, "mastercard"}, 224 {BasicCardNetwork::MASTERCARD, "mastercard"},
227 {BasicCardNetwork::MIR, "mir"}, 225 {BasicCardNetwork::MIR, "mir"},
228 {BasicCardNetwork::UNIONPAY, "unionpay"}, 226 {BasicCardNetwork::UNIONPAY, "unionpay"},
229 {BasicCardNetwork::VISA, "visa"}}; 227 {BasicCardNetwork::VISA, "visa"}};
230 for (const BasicCardNetwork& supported_network : 228 for (const BasicCardNetwork& supported_network :
231 method_data_entry->supported_networks) { 229 method_data_entry->supported_networks) {
232 // Make sure that the network was not already added to 230 // Make sure that the network was not already added to
233 // |supported_card_networks_|. 231 // |supported_card_networks_|.
234 auto card_it = card_networks.find(networks[supported_network]); 232 auto card_it = card_networks.find(networks[supported_network]);
235 if (card_it != card_networks.end()) { 233 if (card_it != card_networks.end()) {
236 supported_card_networks_.push_back(networks[supported_network]); 234 supported_card_networks_.push_back(networks[supported_network]);
237 card_networks.erase(card_it); 235 card_networks.erase(card_it);
238 } 236 }
239 } 237 }
240 } 238 }
241 } 239 }
242 } 240 }
243 } 241 }
244 } 242 }
245 243
246 } // namespace payments 244 } // namespace payments
OLDNEW
« no previous file with comments | « components/payments/content/payment_request.h ('k') | components/payments/content/payment_request.mojom » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698