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