blob: d58763218d8a8424274cecf9fddacab198f04a17 [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(
Balazs Engedyf0b497ed2017-11-04 00:50:2118 mojo::MakeRequestAssociatedWithDedicatedPipe(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()
Ke He31534c82018-01-30 08:11:0055 : local_provider_(std::make_unique<LocalProvider>(&proxy_)) {}
leon.han471b67d2016-10-11 02:46:1056
rockotf62002a2016-09-15 00:08:5957AssociatedInterfaceProviderImpl::~AssociatedInterfaceProviderImpl() {}
58
59void AssociatedInterfaceProviderImpl::GetInterface(
60 const std::string& name,
61 mojo::ScopedInterfaceEndpointHandle handle) {
Ken Rockot96d1b7b52017-05-13 00:29:2162 proxy_->GetAssociatedInterface(
63 name, mojom::AssociatedInterfaceAssociatedRequest(std::move(handle)));
rockotf62002a2016-09-15 00:08:5964}
65
leon.han471b67d2016-10-11 02:46:1066void AssociatedInterfaceProviderImpl::OverrideBinderForTesting(
67 const std::string& name,
68 const base::Callback<void(mojo::ScopedInterfaceEndpointHandle)>& binder) {
Ke He31534c82018-01-30 08:11:0069 if (!local_provider_) {
70 DCHECK(proxy_.is_bound());
71 proxy_.reset();
72 local_provider_ = std::make_unique<LocalProvider>(&proxy_);
73 }
leon.han471b67d2016-10-11 02:46:1074 local_provider_->SetBinderForName(name, binder);
75}
76
rockotf62002a2016-09-15 00:08:5977} // namespace content