Chromium Code Reviews| 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/payments/ui/payment_request_dialog.h" | |
| 11 #include "components/web_modal/web_contents_modal_dialog_host.h" | |
| 12 #include "components/web_modal/web_contents_modal_dialog_manager.h" | |
| 13 #include "components/web_modal/web_contents_modal_dialog_manager_delegate.h" | |
| 14 #include "content/public/browser/browser_thread.h" | |
| 15 #include "content/public/browser/web_contents.h" | |
| 16 | |
| 17 namespace { | |
| 18 | |
| 19 class PaymentRequestFactory { | |
| 20 public: | |
| 21 bool AssignPaymentRequest( | |
| 22 content::WebContents* web_contents, | |
| 23 mojo::InterfaceRequest<payments::mojom::PaymentRequest> request) { | |
| 24 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 25 if (map_.find(web_contents) == map_.end()) { | |
| 26 map_[web_contents] = scoped_refptr<payments::PaymentRequestImpl>( | |
|
Lei Zhang
2016/11/16 23:02:26
You may be able to assign directly without explici
Kevin Bailey
2016/11/17 15:05:42
It's worse than that. It's incorrect to get rid of
| |
| 27 new payments::PaymentRequestImpl(web_contents, std::move(request))); | |
| 28 return true; | |
| 29 } | |
| 30 return false; | |
| 31 } | |
| 32 | |
| 33 void UnassignPaymentRequest(content::WebContents* web_contents) { | |
| 34 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 35 map_.erase(web_contents); | |
| 36 } | |
| 37 | |
| 38 private: | |
| 39 std::map<content::WebContents*, scoped_refptr<payments::PaymentRequestImpl>> | |
| 40 map_; | |
| 41 }; | |
| 42 | |
| 43 base::LazyInstance<PaymentRequestFactory> payment_request_factory; | |
| 44 | |
| 45 } // namespace | |
| 46 | |
| 47 namespace payments { | |
| 48 | |
| 49 PaymentRequestImpl::PaymentRequestImpl( | |
| 50 content::WebContents* web_contents, | |
| 51 mojo::InterfaceRequest<payments::mojom::PaymentRequest> request) | |
| 52 : web_contents_(web_contents), | |
| 53 binding_(this, std::move(request)), | |
| 54 dialog_(nullptr) { | |
| 55 binding_.set_connection_error_handler( | |
| 56 base::Bind(&PaymentRequestImpl::OnError, this)); | |
| 57 } | |
| 58 | |
| 59 PaymentRequestImpl::~PaymentRequestImpl() {} | |
| 60 | |
| 61 void PaymentRequestImpl::Init( | |
| 62 payments::mojom::PaymentRequestClientPtr client, | |
| 63 std::vector<payments::mojom::PaymentMethodDataPtr> methodData, | |
| 64 payments::mojom::PaymentDetailsPtr details, | |
| 65 payments::mojom::PaymentOptionsPtr options) { | |
| 66 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 67 dialog_ = new PaymentRequestDialog(std::move(client)); | |
| 68 views::DialogDelegate::CreateDialogWidget( | |
| 69 dialog_, nullptr, | |
| 70 web_modal::WebContentsModalDialogManager::FromWebContents(web_contents_) | |
| 71 ->delegate() | |
| 72 ->GetWebContentsModalDialogHost() | |
| 73 ->GetHostView()) | |
| 74 ->Show(); | |
| 75 } | |
| 76 | |
| 77 void PaymentRequestImpl::OnError() { | |
| 78 binding_.Close(); | |
| 79 // TODO(krb): Call dialog_->Close() here, but avoid double-free | |
| 80 payment_request_factory.Get().UnassignPaymentRequest(web_contents_); | |
| 81 } | |
| 82 | |
| 83 } // namespace payments | |
| 84 | |
| 85 void CreatePaymentRequestHandler( | |
| 86 content::WebContents* web_contents, | |
| 87 mojo::InterfaceRequest<payments::mojom::PaymentRequest> request) { | |
| 88 payment_request_factory.Get().AssignPaymentRequest(web_contents, | |
| 89 std::move(request)); | |
| 90 } | |
| OLD | NEW |