| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/payments/payment_request_impl.h" | |
| 6 | |
| 7 #include <map> | |
| 8 | |
| 9 #include "base/lazy_instance.h" | |
| 10 #include "chrome/browser/ui/browser_dialogs.h" | |
| 11 #include "components/payments/payment_details_validation.h" | |
| 12 #include "content/public/browser/browser_thread.h" | |
| 13 #include "content/public/browser/web_contents.h" | |
| 14 | |
| 15 namespace { | |
| 16 | |
| 17 class PaymentRequestFactory { | |
| 18 public: | |
| 19 bool AssignPaymentRequest( | |
| 20 content::WebContents* web_contents, | |
| 21 mojo::InterfaceRequest<payments::mojom::PaymentRequest> request) { | |
| 22 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 23 if (map_.find(web_contents) == map_.end()) { | |
| 24 scoped_refptr<payments::PaymentRequestImpl> p( | |
| 25 new payments::PaymentRequestImpl(web_contents, std::move(request))); | |
| 26 map_[web_contents] = p; | |
| 27 return true; | |
| 28 } | |
| 29 return false; | |
| 30 } | |
| 31 | |
| 32 void UnassignPaymentRequest(content::WebContents* web_contents) { | |
| 33 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 34 map_.erase(web_contents); | |
| 35 } | |
| 36 | |
| 37 private: | |
| 38 std::map<content::WebContents*, scoped_refptr<payments::PaymentRequestImpl>> | |
| 39 map_; | |
| 40 }; | |
| 41 | |
| 42 base::LazyInstance<PaymentRequestFactory> payment_request_factory; | |
| 43 | |
| 44 } // namespace | |
| 45 | |
| 46 namespace payments { | |
| 47 | |
| 48 PaymentRequestImpl::PaymentRequestImpl( | |
| 49 content::WebContents* web_contents, | |
| 50 mojo::InterfaceRequest<payments::mojom::PaymentRequest> request) | |
| 51 : web_contents_(web_contents), | |
| 52 binding_(this, std::move(request)) { | |
| 53 binding_.set_connection_error_handler( | |
| 54 base::Bind(&PaymentRequestImpl::OnError, this)); | |
| 55 } | |
| 56 | |
| 57 PaymentRequestImpl::~PaymentRequestImpl() {} | |
| 58 | |
| 59 void PaymentRequestImpl::Init( | |
| 60 payments::mojom::PaymentRequestClientPtr client, | |
| 61 std::vector<payments::mojom::PaymentMethodDataPtr> methodData, | |
| 62 payments::mojom::PaymentDetailsPtr details, | |
| 63 payments::mojom::PaymentOptionsPtr options) { | |
| 64 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 65 std::string error; | |
| 66 if (!payments::validatePaymentDetails(details, &error)) { | |
| 67 LOG(ERROR) << error; | |
| 68 OnError(); | |
| 69 return; | |
| 70 } | |
| 71 client_ = std::move(client); | |
| 72 details_ = std::move(details); | |
| 73 } | |
| 74 | |
| 75 void PaymentRequestImpl::Show() { | |
| 76 chrome::ShowPaymentRequestDialog(this); | |
| 77 } | |
| 78 | |
| 79 void PaymentRequestImpl::Cancel() { | |
| 80 client_->OnError(payments::mojom::PaymentErrorReason::USER_CANCEL); | |
| 81 } | |
| 82 | |
| 83 void PaymentRequestImpl::OnError() { | |
| 84 binding_.Close(); | |
| 85 // TODO(krb): Close the dialog here, but avoid double-free | |
| 86 payment_request_factory.Get().UnassignPaymentRequest(web_contents_); | |
| 87 } | |
| 88 | |
| 89 } // namespace payments | |
| 90 | |
| 91 void CreatePaymentRequestHandler( | |
| 92 content::WebContents* web_contents, | |
| 93 mojo::InterfaceRequest<payments::mojom::PaymentRequest> request) { | |
| 94 payment_request_factory.Get().AssignPaymentRequest(web_contents, | |
| 95 std::move(request)); | |
| 96 } | |
| OLD | NEW |