blob: 54472e4894e110ab288e2455e30bf1e686bee2df [file] [log] [blame]
rockotf62002a2016-09-15 00:08:591// 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"
csharrison95f01e922017-04-24 18:52:356#include "base/callback.h"
7#include "base/run_loop.h"
leon.han471b67d2016-10-11 02:46:108#include "mojo/public/cpp/bindings/associated_binding.h"
rockotf62002a2016-09-15 00:08:599
10namespace content {
11
leon.han471b67d2016-10-11 02:46:1012class AssociatedInterfaceProviderImpl::LocalProvider
leon.han4015f272016-12-28 03:12:4313 : public mojom::AssociatedInterfaceProvider {
leon.han471b67d2016-10-11 02:46:1014 public:
15 explicit LocalProvider(mojom::AssociatedInterfaceProviderAssociatedPtr* proxy)
leon.han4015f272016-12-28 03:12:4316 : associated_interface_provider_binding_(this) {
17 associated_interface_provider_binding_.Bind(
yzshen72bee952017-03-19 07:51:1918 mojo::MakeIsolatedRequest(proxy));
leon.han471b67d2016-10-11 02:46:1019 }
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.han471b67d2016-10-11 02:46:1030 // 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.han471b67d2016-10-11 02:46:1044 mojo::AssociatedBinding<mojom::AssociatedInterfaceProvider>
45 associated_interface_provider_binding_;
46};
47
rockotf62002a2016-09-15 00:08:5948AssociatedInterfaceProviderImpl::AssociatedInterfaceProviderImpl(
49 mojom::AssociatedInterfaceProviderAssociatedPtr proxy)
50 : proxy_(std::move(proxy)) {
leon.han471b67d2016-10-11 02:46:1051 DCHECK(proxy_.is_bound());
rockotf62002a2016-09-15 00:08:5952}
53
leon.han471b67d2016-10-11 02:46:1054AssociatedInterfaceProviderImpl::AssociatedInterfaceProviderImpl()
55 : local_provider_(new LocalProvider(&proxy_)) {}
56
rockotf62002a2016-09-15 00:08:5957AssociatedInterfaceProviderImpl::~AssociatedInterfaceProviderImpl() {}
58
59void AssociatedInterfaceProviderImpl::GetInterface(
60 const std::string& name,
61 mojo::ScopedInterfaceEndpointHandle handle) {
62 mojom::AssociatedInterfaceAssociatedRequest request;
63 request.Bind(std::move(handle));
csharrison95f01e922017-04-24 18:52:3564 proxy_->GetAssociatedInterface(name, std::move(request));
rockotf62002a2016-09-15 00:08:5965}
66
leon.han471b67d2016-10-11 02:46:1067void AssociatedInterfaceProviderImpl::OverrideBinderForTesting(
68 const std::string& name,
69 const base::Callback<void(mojo::ScopedInterfaceEndpointHandle)>& binder) {
70 DCHECK(local_provider_);
71 local_provider_->SetBinderForName(name, binder);
72}
73
rockotf62002a2016-09-15 00:08:5974} // namespace content