OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/common/associated_interface_provider_impl.h" | 5 #include "content/common/associated_interface_provider_impl.h" |
| 6 #include "mojo/public/cpp/bindings/associated_binding.h" |
| 7 #include "mojo/public/cpp/bindings/binding.h" |
6 | 8 |
7 namespace content { | 9 namespace content { |
8 | 10 |
| 11 namespace { |
| 12 |
| 13 class LocalProvider : public mojom::RouteProvider, |
| 14 mojom::AssociatedInterfaceProvider { |
| 15 public: |
| 16 explicit LocalProvider(mojom::RouteProviderRequest request) |
| 17 : route_provider_binding_(this, std::move(request)), |
| 18 associated_interface_provider_binding_(this) {} |
| 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 void ClearBinders() { binders_.clear(); } |
| 28 |
| 29 private: |
| 30 // mojom::RouteProvider: |
| 31 void GetRoute( |
| 32 int32_t routing_id, |
| 33 mojom::AssociatedInterfaceProviderAssociatedRequest request) override { |
| 34 DCHECK(request.is_pending()); |
| 35 associated_interface_provider_binding_.Bind(std::move(request)); |
| 36 } |
| 37 |
| 38 // mojom::AssociatedInterfaceProvider: |
| 39 void GetAssociatedInterface( |
| 40 const std::string& name, |
| 41 mojom::AssociatedInterfaceAssociatedRequest request) override { |
| 42 // Local binders can be registered via TestApi. |
| 43 auto it = binders_.find(name); |
| 44 if (it != binders_.end()) |
| 45 it->second.Run(request.PassHandle()); |
| 46 } |
| 47 |
| 48 using BinderMap = |
| 49 std::map<std::string, |
| 50 base::Callback<void(mojo::ScopedInterfaceEndpointHandle)>>; |
| 51 BinderMap binders_; |
| 52 |
| 53 mojo::Binding<mojom::RouteProvider> route_provider_binding_; |
| 54 |
| 55 mojo::AssociatedBinding<mojom::AssociatedInterfaceProvider> |
| 56 associated_interface_provider_binding_; |
| 57 }; |
| 58 |
| 59 } // namespace |
| 60 |
9 AssociatedInterfaceProviderImpl::AssociatedInterfaceProviderImpl( | 61 AssociatedInterfaceProviderImpl::AssociatedInterfaceProviderImpl( |
10 mojom::AssociatedInterfaceProviderAssociatedPtr proxy) | 62 mojom::AssociatedInterfaceProviderAssociatedPtr proxy) |
11 : proxy_(std::move(proxy)) { | 63 : proxy_(std::move(proxy)) { |
| 64 DCHECK(proxy_.is_bound()); |
| 65 } |
| 66 |
| 67 AssociatedInterfaceProviderImpl::AssociatedInterfaceProviderImpl() { |
| 68 mojom::RouteProviderPtr route_provider_ptr; |
| 69 local_provider_.reset(new LocalProvider(mojo::GetProxy(&route_provider_ptr))); |
| 70 route_provider_ptr->GetRoute( |
| 71 0, mojo::GetProxy(&proxy_, route_provider_ptr.associated_group())); |
12 } | 72 } |
13 | 73 |
14 AssociatedInterfaceProviderImpl::~AssociatedInterfaceProviderImpl() {} | 74 AssociatedInterfaceProviderImpl::~AssociatedInterfaceProviderImpl() {} |
15 | 75 |
16 void AssociatedInterfaceProviderImpl::GetInterface( | 76 void AssociatedInterfaceProviderImpl::GetInterface( |
17 const std::string& name, | 77 const std::string& name, |
18 mojo::ScopedInterfaceEndpointHandle handle) { | 78 mojo::ScopedInterfaceEndpointHandle handle) { |
19 mojom::AssociatedInterfaceAssociatedRequest request; | 79 mojom::AssociatedInterfaceAssociatedRequest request; |
20 request.Bind(std::move(handle)); | 80 request.Bind(std::move(handle)); |
21 return proxy_->GetAssociatedInterface(name, std::move(request)); | 81 return proxy_->GetAssociatedInterface(name, std::move(request)); |
22 } | 82 } |
23 | 83 |
24 mojo::AssociatedGroup* AssociatedInterfaceProviderImpl::GetAssociatedGroup() { | 84 mojo::AssociatedGroup* AssociatedInterfaceProviderImpl::GetAssociatedGroup() { |
25 return proxy_.associated_group(); | 85 return proxy_.associated_group(); |
26 } | 86 } |
27 | 87 |
| 88 void AssociatedInterfaceProviderImpl::SetBinderForName( |
| 89 const std::string& name, |
| 90 const base::Callback<void(mojo::ScopedInterfaceEndpointHandle)>& binder) { |
| 91 DCHECK(local_provider_); |
| 92 local_provider_->SetBinderForName(name, binder); |
| 93 } |
| 94 |
| 95 void AssociatedInterfaceProviderImpl::ClearBinders() { |
| 96 DCHECK(local_provider_); |
| 97 local_provider_->ClearBinders(); |
| 98 } |
| 99 |
28 } // namespace content | 100 } // namespace content |
OLD | NEW |