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 <set> | |
| 8 | |
| 9 #include "base/lazy_instance.h" | |
| 10 #include "base/synchronization/lock.h" | |
| 11 #include "chrome/browser/payments/ui/payment_request_dialog.h" | |
| 12 #include "components/web_modal/web_contents_modal_dialog_host.h" | |
| 13 #include "components/web_modal/web_contents_modal_dialog_manager.h" | |
| 14 #include "components/web_modal/web_contents_modal_dialog_manager_delegate.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 mu_.Acquire(); | |
| 25 if (set_.find(web_contents) == set_.end()) { | |
|
Lei Zhang
2016/11/15 19:20:47
You can use base::ContainsKey() if that feels more
Kevin Bailey
2016/11/15 20:33:55
Not for me, but happy to follow Rouslan's preferen
please use gerrit instead
2016/11/15 20:38:07
I prefer what you have written.
| |
| 26 std::unique_ptr<payments::PaymentRequestImpl> p( | |
| 27 new payments::PaymentRequestImpl(web_contents, std::move(request))); | |
|
Lei Zhang
2016/11/15 19:20:47
base::MakeUnique?
Kevin Bailey
2016/11/15 20:33:55
Sorry, obsolete.
| |
| 28 set_.insert(web_contents); | |
| 29 mu_.Release(); | |
| 30 return true; | |
| 31 } else { | |
|
Lei Zhang
2016/11/15 19:20:47
No need for else after a return.
Kevin Bailey
2016/11/15 20:33:55
No harm in making the "either or" behavior more ob
| |
| 32 mu_.Release(); | |
| 33 return false; | |
| 34 } | |
| 35 } | |
| 36 | |
| 37 void UnassignPaymentRequest(content::WebContents* web_contents) { | |
| 38 mu_.Acquire(); | |
| 39 set_.erase(web_contents); | |
| 40 mu_.Release(); | |
| 41 } | |
| 42 | |
| 43 private: | |
| 44 base::Lock mu_; | |
|
Lei Zhang
2016/11/15 19:20:47
This is going to protect against thread-safe acces
Kevin Bailey
2016/11/15 19:26:35
This comes directly from our conversation about mu
Lei Zhang
2016/11/15 19:30:57
When multiple frames try to communicate with the b
Kevin Bailey
2016/11/15 20:33:55
I got rid of the lock, but if you're saying that t
please use gerrit instead
2016/11/15 20:41:04
All WebContents objects live in the single browser
| |
| 45 std::set<content::WebContents*> set_; | |
| 46 }; | |
| 47 | |
| 48 base::LazyInstance<PaymentRequestFactory> payment_request_factory; | |
| 49 | |
| 50 } // namespace | |
| 51 | |
| 52 namespace payments { | |
| 53 | |
| 54 PaymentRequestImpl::PaymentRequestImpl( | |
| 55 content::WebContents* web_contents, | |
| 56 mojo::InterfaceRequest<payments::mojom::PaymentRequest> request) | |
| 57 : web_contents_(web_contents), binding_(this, std::move(request)) {} | |
| 58 | |
| 59 PaymentRequestImpl::~PaymentRequestImpl() { | |
| 60 payment_request_factory.Get().UnassignPaymentRequest(web_contents_); | |
| 61 } | |
| 62 | |
| 63 void PaymentRequestImpl::Init( | |
| 64 payments::mojom::PaymentRequestClientPtr client, | |
| 65 std::vector<payments::mojom::PaymentMethodDataPtr> methodData, | |
| 66 payments::mojom::PaymentDetailsPtr details, | |
| 67 payments::mojom::PaymentOptionsPtr options) { | |
| 68 views::DialogDelegate::CreateDialogWidget( | |
| 69 new PaymentRequestDialog(std::move(client)), nullptr, | |
| 70 web_modal::WebContentsModalDialogManager::FromWebContents(web_contents_) | |
| 71 ->delegate() | |
| 72 ->GetWebContentsModalDialogHost() | |
| 73 ->GetHostView()) | |
| 74 ->Show(); | |
| 75 } | |
| 76 | |
| 77 } // namespace payments | |
| 78 | |
| 79 void CreatePaymentRequestHandler( | |
| 80 content::WebContents* web_contents, | |
| 81 mojo::InterfaceRequest<payments::mojom::PaymentRequest> request) { | |
| 82 payment_request_factory.Get().AssignPaymentRequest(web_contents, | |
| 83 std::move(request)); | |
| 84 } | |
| OLD | NEW |