| 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 MOJO_SHELL_PUBLIC_CPP_INTERFACE_REGISTRY_H_ | |
| 6 #define MOJO_SHELL_PUBLIC_CPP_INTERFACE_REGISTRY_H_ | |
| 7 | |
| 8 #include "mojo/public/cpp/bindings/binding.h" | |
| 9 #include "mojo/shell/public/cpp/lib/interface_factory_binder.h" | |
| 10 #include "mojo/shell/public/interfaces/interface_provider.mojom.h" | |
| 11 | |
| 12 namespace mojo { | |
| 13 | |
| 14 class InterfaceBinder; | |
| 15 | |
| 16 // An implementation of shell::mojom::InterfaceProvider that allows the user to | |
| 17 // register services to be exposed to another application. | |
| 18 // | |
| 19 // To use, define a class that implements your specific interface. Then | |
| 20 // implement an InterfaceFactory<Foo> that binds instances of FooImpl to | |
| 21 // InterfaceRequest<Foo>s and register that on the registry like this: | |
| 22 // | |
| 23 // registry.AddInterface(&factory); | |
| 24 // | |
| 25 // Or, if you have multiple factories implemented by the same type, explicitly | |
| 26 // specify the interface to register the factory for: | |
| 27 // | |
| 28 // registry.AddInterface<Foo>(&my_foo_and_bar_factory_); | |
| 29 // registry.AddInterface<Bar>(&my_foo_and_bar_factory_); | |
| 30 // | |
| 31 // The InterfaceFactory must outlive the InterfaceRegistry. | |
| 32 // | |
| 33 // Additionally you may specify a default InterfaceBinder to handle requests for | |
| 34 // interfaces unhandled by any registered InterfaceFactory. Just as with | |
| 35 // InterfaceFactory, the default InterfaceBinder supplied must outlive | |
| 36 // InterfaceRegistry. | |
| 37 // | |
| 38 class InterfaceRegistry : public shell::mojom::InterfaceProvider { | |
| 39 public: | |
| 40 class TestApi { | |
| 41 public: | |
| 42 explicit TestApi(InterfaceRegistry* registry) : registry_(registry) {} | |
| 43 ~TestApi() {} | |
| 44 | |
| 45 void SetInterfaceBinderForName(InterfaceBinder* binder, | |
| 46 const std::string& interface_name) { | |
| 47 registry_->SetInterfaceBinderForName(binder, interface_name); | |
| 48 } | |
| 49 void RemoveInterfaceBinderForName(const std::string& interface_name) { | |
| 50 registry_->RemoveInterfaceBinderForName(interface_name); | |
| 51 } | |
| 52 | |
| 53 private: | |
| 54 InterfaceRegistry* registry_; | |
| 55 DISALLOW_COPY_AND_ASSIGN(TestApi); | |
| 56 }; | |
| 57 | |
| 58 // Construct with a Connection (which may be null), and create an | |
| 59 // InterfaceProvider pipe, the client end of which may be obtained by calling | |
| 60 // TakeClientHandle(). If |connection| is non-null, the Mojo Shell's | |
| 61 // rules filtering which interfaces are allowed to be exposed to clients are | |
| 62 // imposed on this registry. If null, they are not. | |
| 63 explicit InterfaceRegistry(Connection* connection); | |
| 64 // Construct with an InterfaceProviderRequest and a Connection (which may be | |
| 65 // null, see note above about filtering). | |
| 66 InterfaceRegistry(shell::mojom::InterfaceProviderRequest request, | |
| 67 Connection* connection); | |
| 68 ~InterfaceRegistry() override; | |
| 69 | |
| 70 // Takes the client end of the InterfaceProvider pipe created in the | |
| 71 // constructor. | |
| 72 shell::mojom::InterfaceProviderPtr TakeClientHandle(); | |
| 73 | |
| 74 template <typename Interface> | |
| 75 bool AddInterface(InterfaceFactory<Interface>* factory) { | |
| 76 return SetInterfaceBinderForName( | |
| 77 new internal::InterfaceFactoryBinder<Interface>(factory), | |
| 78 Interface::Name_); | |
| 79 } | |
| 80 | |
| 81 void set_default_binder(InterfaceBinder* binder) { default_binder_ = binder; } | |
| 82 | |
| 83 private: | |
| 84 using NameToInterfaceBinderMap = std::map<std::string, InterfaceBinder*>; | |
| 85 | |
| 86 // shell::mojom::InterfaceProvider: | |
| 87 void GetInterface(const String& interface_name, | |
| 88 ScopedMessagePipeHandle handle) override; | |
| 89 | |
| 90 // Returns true if the binder was set, false if it was not set (e.g. by | |
| 91 // some filtering policy preventing this interface from being exposed). | |
| 92 bool SetInterfaceBinderForName(InterfaceBinder* binder, | |
| 93 const std::string& name); | |
| 94 | |
| 95 void RemoveInterfaceBinderForName(const std::string& interface_name); | |
| 96 | |
| 97 shell::mojom::InterfaceProviderPtr client_handle_; | |
| 98 Binding<shell::mojom::InterfaceProvider> binding_; | |
| 99 Connection* connection_; | |
| 100 | |
| 101 InterfaceBinder* default_binder_; | |
| 102 NameToInterfaceBinderMap name_to_binder_; | |
| 103 | |
| 104 DISALLOW_COPY_AND_ASSIGN(InterfaceRegistry); | |
| 105 }; | |
| 106 | |
| 107 } // namespace mojo | |
| 108 | |
| 109 #endif // MOJO_SHELL_PUBLIC_CPP_INTERFACE_REGISTRY_H_ | |
| OLD | NEW |