Chromium Code Reviews| 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 binding_.set_connection_error_handler( | 26 // OnConnectionTerminated will be called when the Mojo pipe is closed. This |
| 27 base::Bind(&PaymentRequest::OnError, base::Unretained(this))); | 27 // will happen as a result of many renderer-side events (both successful and |
| 28 // erroneous in nature). | |
| 29 // TODO(crbug.com/683636): Investigate using | |
| 30 // set_connection_error_with_reason_handler with Binding::CloseWithReason. | |
| 31 binding_.set_connection_error_handler(base::Bind( | |
| 32 &PaymentRequest::OnConnectionTerminated, base::Unretained(this))); | |
| 28 } | 33 } |
| 29 | 34 |
| 30 PaymentRequest::~PaymentRequest() {} | 35 PaymentRequest::~PaymentRequest() {} |
| 31 | 36 |
| 32 void PaymentRequest::Init( | 37 void PaymentRequest::Init( |
| 33 payments::mojom::PaymentRequestClientPtr client, | 38 payments::mojom::PaymentRequestClientPtr client, |
| 34 std::vector<payments::mojom::PaymentMethodDataPtr> methodData, | 39 std::vector<payments::mojom::PaymentMethodDataPtr> methodData, |
| 35 payments::mojom::PaymentDetailsPtr details, | 40 payments::mojom::PaymentDetailsPtr details, |
| 36 payments::mojom::PaymentOptionsPtr options) { | 41 payments::mojom::PaymentOptionsPtr options) { |
| 37 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 42 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 38 std::string error; | 43 std::string error; |
| 39 if (!payments::validatePaymentDetails(details, &error)) { | 44 if (!payments::validatePaymentDetails(details, &error)) { |
| 40 LOG(ERROR) << error; | 45 LOG(ERROR) << error; |
| 41 OnError(); | 46 OnConnectionTerminated(); |
| 42 client_.reset(); | |
| 43 return; | 47 return; |
| 44 } | 48 } |
| 45 client_ = std::move(client); | 49 client_ = std::move(client); |
| 46 details_ = std::move(details); | 50 details_ = std::move(details); |
| 47 } | 51 } |
| 48 | 52 |
| 49 void PaymentRequest::Show() { | 53 void PaymentRequest::Show() { |
| 50 if (!client_.is_bound() || !binding_.is_bound()) { | 54 if (!client_.is_bound() || !binding_.is_bound()) { |
|
please use gerrit instead
2017/01/23 21:18:50
Nit for the future: If the dialog is already showi
Mathieu
2017/01/23 21:53:57
Acknowledged.
| |
| 51 OnError(); | 55 LOG(ERROR) << "Attempted Show(), but binding(s) missing."; |
| 56 OnConnectionTerminated(); | |
| 52 return; | 57 return; |
| 53 } | 58 } |
| 54 delegate_->ShowPaymentRequestDialog(this); | 59 delegate_->ShowDialog(this); |
| 55 } | 60 } |
| 56 | 61 |
| 57 void PaymentRequest::Cancel() { | 62 void PaymentRequest::Abort() { |
| 58 client_->OnError(payments::mojom::PaymentErrorReason::USER_CANCEL); | 63 // The API user has decided to abort. We return a successful abort message to |
| 64 // the renderer, which closes the Mojo message pipe, which triggers | |
| 65 // PaymentRequest::OnConnectionTerminated, which destroys this object. | |
| 66 if (client_.is_bound()) | |
| 67 client_->OnAbort(true /* aborted_successfully */); | |
| 59 } | 68 } |
| 60 | 69 |
| 61 void PaymentRequest::OnError() { | 70 void PaymentRequest::UserCancelled() { |
| 71 // If |client_| is not bound, then the object is already being destroyed as | |
| 72 // a result of a renderer event. | |
| 73 if (!client_.is_bound()) | |
| 74 return; | |
| 75 | |
| 76 // This sends an error to the renderer, which informs the API user. | |
| 77 client_->OnError(payments::mojom::PaymentErrorReason::USER_CANCEL); | |
| 78 | |
| 79 // We close all bindings and ask to be destroyed. | |
| 80 client_.reset(); | |
| 62 binding_.Close(); | 81 binding_.Close(); |
| 63 manager_->DestroyRequest(this); | 82 manager_->DestroyRequest(this); |
|
please use gerrit instead
2017/01/23 21:18:50
This line should be called unconditionally, even i
Mathieu
2017/01/23 21:53:57
Not really. If we are calling this method while th
| |
| 64 } | 83 } |
| 65 | 84 |
| 85 void PaymentRequest::OnConnectionTerminated() { | |
| 86 // We are here because of a browser-side error, or likely as a result of the | |
| 87 // connection_error_handler on |binding_|, which can mean that the renderer | |
| 88 // has decided to close the pipe for various reasons (see all uses of | |
| 89 // PaymentRequest::clearResolversAndCloseMojoConnection() in Blink). We close | |
| 90 // the binding and the dialog, and ask to be deleted. | |
| 91 client_.reset(); | |
| 92 binding_.Close(); | |
| 93 delegate_->CloseDialog(); | |
| 94 manager_->DestroyRequest(this); | |
| 95 } | |
| 96 | |
| 66 CurrencyFormatter* PaymentRequest::GetOrCreateCurrencyFormatter( | 97 CurrencyFormatter* PaymentRequest::GetOrCreateCurrencyFormatter( |
| 67 const std::string& currency_code, | 98 const std::string& currency_code, |
| 68 const base::Optional<std::string> currency_system, | 99 const base::Optional<std::string> currency_system, |
| 69 const std::string& locale_name) { | 100 const std::string& locale_name) { |
| 70 if (!currency_formatter_) { | 101 if (!currency_formatter_) { |
| 71 currency_formatter_.reset( | 102 currency_formatter_.reset( |
| 72 new CurrencyFormatter(currency_code, currency_system, locale_name)); | 103 new CurrencyFormatter(currency_code, currency_system, locale_name)); |
| 73 } | 104 } |
| 74 | |
| 75 return currency_formatter_.get(); | 105 return currency_formatter_.get(); |
| 76 } | 106 } |
| 77 | 107 |
| 78 autofill::AutofillProfile* PaymentRequest::GetCurrentlySelectedProfile() { | 108 autofill::AutofillProfile* PaymentRequest::GetCurrentlySelectedProfile() { |
| 79 // TODO(tmartino): Implement more sophisticated algorithm for populating | 109 // TODO(tmartino): Implement more sophisticated algorithm for populating |
| 80 // this when it starts empty. | 110 // this when it starts empty. |
| 81 if (!profile_) { | 111 if (!profile_) { |
| 82 autofill::PersonalDataManager* data_manager = | 112 autofill::PersonalDataManager* data_manager = |
| 83 delegate_->GetPersonalDataManager(); | 113 delegate_->GetPersonalDataManager(); |
| 84 auto profiles = data_manager->GetProfiles(); | 114 auto profiles = data_manager->GetProfiles(); |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 101 cards.begin(), | 131 cards.begin(), |
| 102 cards.end(), | 132 cards.end(), |
| 103 [] (autofill::CreditCard* card) { | 133 [] (autofill::CreditCard* card) { |
| 104 return card->IsValid(); | 134 return card->IsValid(); |
| 105 }); | 135 }); |
| 106 | 136 |
| 107 return first_complete_card == cards.end() ? nullptr : *first_complete_card; | 137 return first_complete_card == cards.end() ? nullptr : *first_complete_card; |
| 108 } | 138 } |
| 109 | 139 |
| 110 } // namespace payments | 140 } // namespace payments |
| OLD | NEW |