rockot | f62002a | 2016-09-15 00:08:59 | [diff] [blame] | 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 "content/common/associated_interface_provider_impl.h" |
csharrison | 95f01e92 | 2017-04-24 18:52:35 | [diff] [blame] | 6 | #include "base/callback.h" |
| 7 | #include "base/run_loop.h" |
leon.han | 471b67d | 2016-10-11 02:46:10 | [diff] [blame] | 8 | #include "mojo/public/cpp/bindings/associated_binding.h" |
rockot | f62002a | 2016-09-15 00:08:59 | [diff] [blame] | 9 | |
| 10 | namespace content { |
| 11 | |
leon.han | 471b67d | 2016-10-11 02:46:10 | [diff] [blame] | 12 | class AssociatedInterfaceProviderImpl::LocalProvider |
leon.han | 4015f27 | 2016-12-28 03:12:43 | [diff] [blame] | 13 | : public mojom::AssociatedInterfaceProvider { |
leon.han | 471b67d | 2016-10-11 02:46:10 | [diff] [blame] | 14 | public: |
| 15 | explicit LocalProvider(mojom::AssociatedInterfaceProviderAssociatedPtr* proxy) |
leon.han | 4015f27 | 2016-12-28 03:12:43 | [diff] [blame] | 16 | : associated_interface_provider_binding_(this) { |
| 17 | associated_interface_provider_binding_.Bind( |
Balazs Engedy | f0b497ed | 2017-11-04 00:50:21 | [diff] [blame] | 18 | mojo::MakeRequestAssociatedWithDedicatedPipe(proxy)); |
leon.han | 471b67d | 2016-10-11 02:46:10 | [diff] [blame] | 19 | } |
| 20 | |
| 21 | ~LocalProvider() override {} |
| 22 | |
| 23 | void SetBinderForName( |
| 24 | const std::string& name, |
| 25 | const base::Callback<void(mojo::ScopedInterfaceEndpointHandle)>& binder) { |
| 26 | binders_[name] = binder; |
| 27 | } |
| 28 | |
| 29 | private: |
leon.han | 471b67d | 2016-10-11 02:46:10 | [diff] [blame] | 30 | // mojom::AssociatedInterfaceProvider: |
| 31 | void GetAssociatedInterface( |
| 32 | const std::string& name, |
| 33 | mojom::AssociatedInterfaceAssociatedRequest request) override { |
| 34 | auto it = binders_.find(name); |
| 35 | if (it != binders_.end()) |
| 36 | it->second.Run(request.PassHandle()); |
| 37 | } |
| 38 | |
| 39 | using BinderMap = |
| 40 | std::map<std::string, |
| 41 | base::Callback<void(mojo::ScopedInterfaceEndpointHandle)>>; |
| 42 | BinderMap binders_; |
| 43 | |
leon.han | 471b67d | 2016-10-11 02:46:10 | [diff] [blame] | 44 | mojo::AssociatedBinding<mojom::AssociatedInterfaceProvider> |
| 45 | associated_interface_provider_binding_; |
| 46 | }; |
| 47 | |
rockot | f62002a | 2016-09-15 00:08:59 | [diff] [blame] | 48 | AssociatedInterfaceProviderImpl::AssociatedInterfaceProviderImpl( |
| 49 | mojom::AssociatedInterfaceProviderAssociatedPtr proxy) |
| 50 | : proxy_(std::move(proxy)) { |
leon.han | 471b67d | 2016-10-11 02:46:10 | [diff] [blame] | 51 | DCHECK(proxy_.is_bound()); |
rockot | f62002a | 2016-09-15 00:08:59 | [diff] [blame] | 52 | } |
| 53 | |
leon.han | 471b67d | 2016-10-11 02:46:10 | [diff] [blame] | 54 | AssociatedInterfaceProviderImpl::AssociatedInterfaceProviderImpl() |
Ke He | 31534c8 | 2018-01-30 08:11:00 | [diff] [blame] | 55 | : local_provider_(std::make_unique<LocalProvider>(&proxy_)) {} |
leon.han | 471b67d | 2016-10-11 02:46:10 | [diff] [blame] | 56 | |
rockot | f62002a | 2016-09-15 00:08:59 | [diff] [blame] | 57 | AssociatedInterfaceProviderImpl::~AssociatedInterfaceProviderImpl() {} |
| 58 | |
| 59 | void AssociatedInterfaceProviderImpl::GetInterface( |
| 60 | const std::string& name, |
| 61 | mojo::ScopedInterfaceEndpointHandle handle) { |
Ken Rockot | 96d1b7b5 | 2017-05-13 00:29:21 | [diff] [blame] | 62 | proxy_->GetAssociatedInterface( |
| 63 | name, mojom::AssociatedInterfaceAssociatedRequest(std::move(handle))); |
rockot | f62002a | 2016-09-15 00:08:59 | [diff] [blame] | 64 | } |
| 65 | |
leon.han | 471b67d | 2016-10-11 02:46:10 | [diff] [blame] | 66 | void AssociatedInterfaceProviderImpl::OverrideBinderForTesting( |
| 67 | const std::string& name, |
| 68 | const base::Callback<void(mojo::ScopedInterfaceEndpointHandle)>& binder) { |
Ke He | 31534c8 | 2018-01-30 08:11:00 | [diff] [blame] | 69 | if (!local_provider_) { |
| 70 | DCHECK(proxy_.is_bound()); |
| 71 | proxy_.reset(); |
| 72 | local_provider_ = std::make_unique<LocalProvider>(&proxy_); |
| 73 | } |
leon.han | 471b67d | 2016-10-11 02:46:10 | [diff] [blame] | 74 | local_provider_->SetBinderForName(name, binder); |
| 75 | } |
| 76 | |
rockot | f62002a | 2016-09-15 00:08:59 | [diff] [blame] | 77 | } // namespace content |