blob: 647ba6a161407b5e698863420df35cb043c4d096 [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"
leon.han471b67d2016-10-11 02:46:106#include "mojo/public/cpp/bindings/associated_binding.h"
rockotf62002a2016-09-15 00:08:597
8namespace content {
9
leon.han471b67d2016-10-11 02:46:1010class AssociatedInterfaceProviderImpl::LocalProvider
leon.han4015f272016-12-28 03:12:4311 : public mojom::AssociatedInterfaceProvider {
leon.han471b67d2016-10-11 02:46:1012 public:
13 explicit LocalProvider(mojom::AssociatedInterfaceProviderAssociatedPtr* proxy)
leon.han4015f272016-12-28 03:12:4314 : associated_interface_provider_binding_(this) {
15 associated_interface_provider_binding_.Bind(
yzshen72bee952017-03-19 07:51:1916 mojo::MakeIsolatedRequest(proxy));
leon.han471b67d2016-10-11 02:46:1017 }
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.han471b67d2016-10-11 02:46:1028 // 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.han471b67d2016-10-11 02:46:1042 mojo::AssociatedBinding<mojom::AssociatedInterfaceProvider>
43 associated_interface_provider_binding_;
44};
45
rockotf62002a2016-09-15 00:08:5946AssociatedInterfaceProviderImpl::AssociatedInterfaceProviderImpl(
47 mojom::AssociatedInterfaceProviderAssociatedPtr proxy)
48 : proxy_(std::move(proxy)) {
leon.han471b67d2016-10-11 02:46:1049 DCHECK(proxy_.is_bound());
rockotf62002a2016-09-15 00:08:5950}
51
leon.han471b67d2016-10-11 02:46:1052AssociatedInterfaceProviderImpl::AssociatedInterfaceProviderImpl()
53 : local_provider_(new LocalProvider(&proxy_)) {}
54
rockotf62002a2016-09-15 00:08:5955AssociatedInterfaceProviderImpl::~AssociatedInterfaceProviderImpl() {}
56
57void 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.han471b67d2016-10-11 02:46:1065void 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
rockotf62002a2016-09-15 00:08:5972} // namespace content