Chromium Code Reviews| Index: chrome/browser/payments/payment_request_impl.cc |
| diff --git a/chrome/browser/payments/payment_request_impl.cc b/chrome/browser/payments/payment_request_impl.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..24c213b218a9ded80d3e336eaaf1c60baadb5421 |
| --- /dev/null |
| +++ b/chrome/browser/payments/payment_request_impl.cc |
| @@ -0,0 +1,84 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/browser/payments/payment_request_impl.h" |
| + |
| +#include <set> |
| + |
| +#include "base/lazy_instance.h" |
| +#include "base/synchronization/lock.h" |
| +#include "chrome/browser/payments/ui/payment_request_dialog.h" |
| +#include "components/web_modal/web_contents_modal_dialog_host.h" |
| +#include "components/web_modal/web_contents_modal_dialog_manager.h" |
| +#include "components/web_modal/web_contents_modal_dialog_manager_delegate.h" |
| +#include "content/public/browser/web_contents.h" |
| + |
| +namespace { |
| + |
| +class PaymentRequestFactory { |
| + public: |
| + bool AssignPaymentRequest( |
| + content::WebContents* web_contents, |
| + mojo::InterfaceRequest<payments::mojom::PaymentRequest> request) { |
| + mu_.Acquire(); |
| + 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.
|
| + std::unique_ptr<payments::PaymentRequestImpl> p( |
| + 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.
|
| + set_.insert(web_contents); |
| + mu_.Release(); |
| + return true; |
| + } 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
|
| + mu_.Release(); |
| + return false; |
| + } |
| + } |
| + |
| + void UnassignPaymentRequest(content::WebContents* web_contents) { |
| + mu_.Acquire(); |
| + set_.erase(web_contents); |
| + mu_.Release(); |
| + } |
| + |
| + private: |
| + 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
|
| + std::set<content::WebContents*> set_; |
| +}; |
| + |
| +base::LazyInstance<PaymentRequestFactory> payment_request_factory; |
| + |
| +} // namespace |
| + |
| +namespace payments { |
| + |
| +PaymentRequestImpl::PaymentRequestImpl( |
| + content::WebContents* web_contents, |
| + mojo::InterfaceRequest<payments::mojom::PaymentRequest> request) |
| + : web_contents_(web_contents), binding_(this, std::move(request)) {} |
| + |
| +PaymentRequestImpl::~PaymentRequestImpl() { |
| + payment_request_factory.Get().UnassignPaymentRequest(web_contents_); |
| +} |
| + |
| +void PaymentRequestImpl::Init( |
| + payments::mojom::PaymentRequestClientPtr client, |
| + std::vector<payments::mojom::PaymentMethodDataPtr> methodData, |
| + payments::mojom::PaymentDetailsPtr details, |
| + payments::mojom::PaymentOptionsPtr options) { |
| + views::DialogDelegate::CreateDialogWidget( |
| + new PaymentRequestDialog(std::move(client)), nullptr, |
| + web_modal::WebContentsModalDialogManager::FromWebContents(web_contents_) |
| + ->delegate() |
| + ->GetWebContentsModalDialogHost() |
| + ->GetHostView()) |
| + ->Show(); |
| +} |
| + |
| +} // namespace payments |
| + |
| +void CreatePaymentRequestHandler( |
| + content::WebContents* web_contents, |
| + mojo::InterfaceRequest<payments::mojom::PaymentRequest> request) { |
| + payment_request_factory.Get().AssignPaymentRequest(web_contents, |
| + std::move(request)); |
| +} |