| 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/content/payment_request.h" | 5 #include "components/payments/content/payment_request.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <set> |
| 8 #include <unordered_map> | 9 #include <unordered_map> |
| 9 #include <utility> | 10 #include <utility> |
| 10 | 11 |
| 11 #include "base/memory/ptr_util.h" | 12 #include "base/memory/ptr_util.h" |
| 12 #include "components/autofill/core/browser/autofill_data_util.h" | 13 #include "components/autofill/core/browser/autofill_data_util.h" |
| 13 #include "components/autofill/core/browser/field_types.h" | 14 #include "components/autofill/core/browser/field_types.h" |
| 14 #include "components/autofill/core/browser/personal_data_manager.h" | 15 #include "components/autofill/core/browser/personal_data_manager.h" |
| 15 #include "components/payments/content/payment_details_validation.h" | 16 #include "components/payments/content/payment_details_validation.h" |
| 16 #include "components/payments/content/payment_request_web_contents_manager.h" | 17 #include "components/payments/content/payment_request_web_contents_manager.h" |
| 17 #include "components/payments/core/autofill_payment_instrument.h" | 18 #include "components/payments/core/autofill_payment_instrument.h" |
| (...skipping 13 matching lines...) Expand all Loading... |
| 31 std::unique_ptr<PaymentRequestDelegate> delegate, | 32 std::unique_ptr<PaymentRequestDelegate> delegate, |
| 32 PaymentRequestWebContentsManager* manager, | 33 PaymentRequestWebContentsManager* manager, |
| 33 mojo::InterfaceRequest<payments::mojom::PaymentRequest> request) | 34 mojo::InterfaceRequest<payments::mojom::PaymentRequest> request) |
| 34 : web_contents_(web_contents), | 35 : web_contents_(web_contents), |
| 35 delegate_(std::move(delegate)), | 36 delegate_(std::move(delegate)), |
| 36 manager_(manager), | 37 manager_(manager), |
| 37 binding_(this, std::move(request)), | 38 binding_(this, std::move(request)), |
| 38 is_ready_to_pay_(false), | 39 is_ready_to_pay_(false), |
| 39 selected_shipping_profile_(nullptr), | 40 selected_shipping_profile_(nullptr), |
| 40 selected_contact_profile_(nullptr), | 41 selected_contact_profile_(nullptr), |
| 41 selected_credit_card_(nullptr) { | 42 selected_credit_card_(nullptr), |
| 43 selected_shipping_option_(nullptr) { |
| 42 // OnConnectionTerminated will be called when the Mojo pipe is closed. This | 44 // OnConnectionTerminated will be called when the Mojo pipe is closed. This |
| 43 // will happen as a result of many renderer-side events (both successful and | 45 // will happen as a result of many renderer-side events (both successful and |
| 44 // erroneous in nature). | 46 // erroneous in nature). |
| 45 // TODO(crbug.com/683636): Investigate using | 47 // TODO(crbug.com/683636): Investigate using |
| 46 // set_connection_error_with_reason_handler with Binding::CloseWithReason. | 48 // set_connection_error_with_reason_handler with Binding::CloseWithReason. |
| 47 binding_.set_connection_error_handler(base::Bind( | 49 binding_.set_connection_error_handler(base::Bind( |
| 48 &PaymentRequest::OnConnectionTerminated, base::Unretained(this))); | 50 &PaymentRequest::OnConnectionTerminated, base::Unretained(this))); |
| 49 } | 51 } |
| 50 | 52 |
| 51 PaymentRequest::~PaymentRequest() {} | 53 PaymentRequest::~PaymentRequest() {} |
| 52 | 54 |
| 53 void PaymentRequest::Init( | 55 void PaymentRequest::Init( |
| 54 payments::mojom::PaymentRequestClientPtr client, | 56 payments::mojom::PaymentRequestClientPtr client, |
| 55 std::vector<payments::mojom::PaymentMethodDataPtr> method_data, | 57 std::vector<payments::mojom::PaymentMethodDataPtr> method_data, |
| 56 payments::mojom::PaymentDetailsPtr details, | 58 payments::mojom::PaymentDetailsPtr details, |
| 57 payments::mojom::PaymentOptionsPtr options) { | 59 payments::mojom::PaymentOptionsPtr options) { |
| 58 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 60 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 59 std::string error; | 61 std::string error; |
| 60 if (!payments::validatePaymentDetails(details, &error)) { | 62 if (!payments::validatePaymentDetails(details, &error)) { |
| 61 LOG(ERROR) << error; | 63 LOG(ERROR) << error; |
| 62 OnConnectionTerminated(); | 64 OnConnectionTerminated(); |
| 63 return; | 65 return; |
| 64 } | 66 } |
| 65 client_ = std::move(client); | 67 client_ = std::move(client); |
| 66 details_ = std::move(details); | 68 details_ = std::move(details); |
| 67 options_ = std::move(options); | 69 options_ = std::move(options); |
| 68 PopulateValidatedMethodData(method_data); | 70 PopulateValidatedMethodData(method_data); |
| 69 PopulateProfileCache(); | 71 PopulateProfileCache(); |
| 70 SetDefaultProfileSelections(); | 72 SetDefaultProfileSelections(); |
| 73 UpdateSelectedShippingOptionFromDetails(); |
| 71 } | 74 } |
| 72 | 75 |
| 73 void PaymentRequest::Show() { | 76 void PaymentRequest::Show() { |
| 74 if (!client_.is_bound() || !binding_.is_bound()) { | 77 if (!client_.is_bound() || !binding_.is_bound()) { |
| 75 LOG(ERROR) << "Attempted Show(), but binding(s) missing."; | 78 LOG(ERROR) << "Attempted Show(), but binding(s) missing."; |
| 76 OnConnectionTerminated(); | 79 OnConnectionTerminated(); |
| 77 return; | 80 return; |
| 78 } | 81 } |
| 79 delegate_->ShowDialog(this); | 82 delegate_->ShowDialog(this); |
| 80 } | 83 } |
| (...skipping 299 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 380 selected_contact_profile_ | 383 selected_contact_profile_ |
| 381 ->GetInfo(autofill::AutofillType(autofill::PHONE_HOME_WHOLE_NUMBER), | 384 ->GetInfo(autofill::AutofillType(autofill::PHONE_HOME_WHOLE_NUMBER), |
| 382 app_locale) | 385 app_locale) |
| 383 .empty())) { | 386 .empty())) { |
| 384 return false; | 387 return false; |
| 385 } | 388 } |
| 386 | 389 |
| 387 return true; | 390 return true; |
| 388 } | 391 } |
| 389 | 392 |
| 393 void PaymentRequest::UpdateSelectedShippingOptionFromDetails() { |
| 394 selected_shipping_option_ = nullptr; |
| 395 |
| 396 // As per the spec, the selected shipping option should initially be the last |
| 397 // one in the array that has its selected field set to true. |
| 398 auto selected_shipping_option_it = std::find_if( |
| 399 details()->shipping_options.rbegin(), details()->shipping_options.rend(), |
| 400 [](const payments::mojom::PaymentShippingOptionPtr& element) { |
| 401 return element->selected; |
| 402 }); |
| 403 if (selected_shipping_option_it != details()->shipping_options.rend()) { |
| 404 selected_shipping_option_ = selected_shipping_option_it->get(); |
| 405 } |
| 406 } |
| 407 |
| 390 } // namespace payments | 408 } // namespace payments |
| OLD | NEW |