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