Chromium Code Reviews| 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 class RemoteInterfaceRegistry { | |
|
Ken Rockot(use gerrit already)
2016/06/16 23:48:58
nit: Documentation. It's worth calling out that th
| |
| 13 public: | |
| 14 class TestApi { | |
| 15 public: | |
| 16 explicit TestApi(RemoteInterfaceRegistry* registry) : registry_(registry) {} | |
| 17 ~TestApi() {} | |
| 18 | |
| 19 template <typename Interface> | |
| 20 void SetBinderForName( | |
| 21 const std::string& name, | |
| 22 const base::Callback<void(mojo::InterfaceRequest<Interface>)>& binder) { | |
| 23 registry_->SetBinderForName(name, binder); | |
| 24 } | |
| 25 | |
| 26 void ClearBinders() { | |
| 27 registry_->ClearBinders(); | |
| 28 } | |
| 29 | |
| 30 private: | |
| 31 RemoteInterfaceRegistry* registry_; | |
| 32 DISALLOW_COPY_AND_ASSIGN(TestApi); | |
| 33 }; | |
| 34 | |
| 35 explicit RemoteInterfaceRegistry( | |
| 36 mojom::InterfaceProviderPtr remote_interfaces); | |
| 37 ~RemoteInterfaceRegistry(); | |
| 38 | |
| 39 // Returns a raw pointer to the remote InterfaceProvider. | |
| 40 mojom::InterfaceProvider* GetInterfaceProvider(); | |
| 41 | |
| 42 // Sets a closure to be run when the remote InterfaceProvider pipe is closed. | |
| 43 void SetConnectionLostClosure(const base::Closure& connection_lost_closure); | |
| 44 | |
| 45 // Binds |ptr| to an implementation of Interface in the remote application. | |
| 46 // |ptr| can immediately be used to start sending requests to the remote | |
| 47 // interface. | |
| 48 template <typename Interface> | |
| 49 void GetInterface(mojo::InterfacePtr<Interface>* ptr) { | |
| 50 mojo::MessagePipe pipe; | |
| 51 ptr->Bind(mojo::InterfacePtrInfo<Interface>(std::move(pipe.handle0), 0u)); | |
| 52 | |
| 53 // Local binders can be registered via TestApi. | |
| 54 auto it = binders_.find(Interface::Name_); | |
| 55 if (it != binders_.end()) { | |
| 56 it->second.Run(std::move(pipe.handle1)); | |
| 57 return; | |
| 58 } | |
| 59 remote_interfaces_->GetInterface(Interface::Name_, std::move(pipe.handle1)); | |
| 60 } | |
| 61 template <typename Interface> | |
| 62 void GetInterface(mojo::InterfaceRequest<Interface> request) { | |
| 63 GetInterface(Interface::Name_, std::move(request.PassMessagePipe())); | |
| 64 } | |
| 65 void GetInterface(const std::string& name, | |
| 66 mojo::ScopedMessagePipeHandle request_handle); | |
| 67 | |
| 68 private: | |
| 69 template <typename Interface> | |
| 70 void SetBinderForName( | |
| 71 const std::string& name, | |
| 72 const base::Callback<void(mojo::InterfaceRequest<Interface>)>& binder) { | |
| 73 binders_[name] = binder; | |
| 74 } | |
| 75 void ClearBinders(); | |
| 76 | |
| 77 using BinderMap = std::map< | |
| 78 std::string, base::Callback<void(mojo::ScopedMessagePipeHandle)>>; | |
| 79 BinderMap binders_; | |
| 80 | |
| 81 mojom::InterfaceProviderPtr remote_interfaces_; | |
| 82 | |
| 83 DISALLOW_COPY_AND_ASSIGN(RemoteInterfaceRegistry); | |
| 84 }; | |
| 85 | |
| 86 } // namespace shell | |
| 87 | |
| 88 #endif // SERVICES_SHELL_PUBLIC_CPP_REMOTE_INTERFACE_REGISTRY_H_ | |
| OLD | NEW |