OLD | NEW |
| (Empty) |
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 #ifndef SERVICES_SHELL_PUBLIC_CPP_REMOTE_INTERFACE_REGISTRY_H_ | |
6 #define SERVICES_SHELL_PUBLIC_CPP_REMOTE_INTERFACE_REGISTRY_H_ | |
7 | |
8 #include "services/shell/public/interfaces/interface_provider.mojom.h" | |
9 | |
10 namespace shell { | |
11 | |
12 // Encapsulates a mojom::InterfaceProviderPtr implemented in a remote | |
13 // application. Provides two main features: | |
14 // - a typesafe GetInterface() method for binding InterfacePtrs. | |
15 // - a testing API that allows local callbacks to be registered that bind | |
16 // requests for remote interfaces. | |
17 // An instance of this class is used by the GetInterface() methods on | |
18 // Connection. | |
19 class RemoteInterfaceRegistry { | |
20 public: | |
21 class TestApi { | |
22 public: | |
23 explicit TestApi(RemoteInterfaceRegistry* registry) : registry_(registry) {} | |
24 ~TestApi() {} | |
25 | |
26 template <typename Interface> | |
27 void SetBinderForName( | |
28 const std::string& name, | |
29 const base::Callback<void(mojo::InterfaceRequest<Interface>)>& binder) { | |
30 registry_->SetBinderForName(name, binder); | |
31 } | |
32 | |
33 void ClearBinders() { | |
34 registry_->ClearBinders(); | |
35 } | |
36 | |
37 private: | |
38 RemoteInterfaceRegistry* registry_; | |
39 DISALLOW_COPY_AND_ASSIGN(TestApi); | |
40 }; | |
41 | |
42 explicit RemoteInterfaceRegistry( | |
43 mojom::InterfaceProviderPtr remote_interfaces); | |
44 ~RemoteInterfaceRegistry(); | |
45 | |
46 // Returns a raw pointer to the remote InterfaceProvider. | |
47 mojom::InterfaceProvider* GetInterfaceProvider(); | |
48 | |
49 // Sets a closure to be run when the remote InterfaceProvider pipe is closed. | |
50 void SetConnectionLostClosure(const base::Closure& connection_lost_closure); | |
51 | |
52 // Binds |ptr| to an implementation of Interface in the remote application. | |
53 // |ptr| can immediately be used to start sending requests to the remote | |
54 // interface. | |
55 template <typename Interface> | |
56 void GetInterface(mojo::InterfacePtr<Interface>* ptr) { | |
57 mojo::MessagePipe pipe; | |
58 ptr->Bind(mojo::InterfacePtrInfo<Interface>(std::move(pipe.handle0), 0u)); | |
59 | |
60 // Local binders can be registered via TestApi. | |
61 auto it = binders_.find(Interface::Name_); | |
62 if (it != binders_.end()) { | |
63 it->second.Run(std::move(pipe.handle1)); | |
64 return; | |
65 } | |
66 remote_interfaces_->GetInterface(Interface::Name_, std::move(pipe.handle1)); | |
67 } | |
68 template <typename Interface> | |
69 void GetInterface(mojo::InterfaceRequest<Interface> request) { | |
70 GetInterface(Interface::Name_, std::move(request.PassMessagePipe())); | |
71 } | |
72 void GetInterface(const std::string& name, | |
73 mojo::ScopedMessagePipeHandle request_handle); | |
74 | |
75 private: | |
76 template <typename Interface> | |
77 void SetBinderForName( | |
78 const std::string& name, | |
79 const base::Callback<void(mojo::InterfaceRequest<Interface>)>& binder) { | |
80 binders_[name] = binder; | |
81 } | |
82 void ClearBinders(); | |
83 | |
84 using BinderMap = std::map< | |
85 std::string, base::Callback<void(mojo::ScopedMessagePipeHandle)>>; | |
86 BinderMap binders_; | |
87 | |
88 mojom::InterfaceProviderPtr remote_interfaces_; | |
89 | |
90 DISALLOW_COPY_AND_ASSIGN(RemoteInterfaceRegistry); | |
91 }; | |
92 | |
93 } // namespace shell | |
94 | |
95 #endif // SERVICES_SHELL_PUBLIC_CPP_REMOTE_INTERFACE_REGISTRY_H_ | |
OLD | NEW |