| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef MOJO_PUBLIC_APPLICATION_SERVICE_PROVIDER_IMPL_H_ | 5 #ifndef MOJO_PUBLIC_APPLICATION_SERVICE_PROVIDER_IMPL_H_ |
| 6 #define MOJO_PUBLIC_APPLICATION_SERVICE_PROVIDER_IMPL_H_ | 6 #define MOJO_PUBLIC_APPLICATION_SERVICE_PROVIDER_IMPL_H_ |
| 7 | 7 |
| 8 #include <functional> | 8 #include <functional> |
| 9 #include <string> | 9 #include <string> |
| 10 #include <utility> |
| 10 | 11 |
| 11 #include "mojo/public/cpp/application/application_connection.h" | |
| 12 #include "mojo/public/cpp/application/connection_context.h" | 12 #include "mojo/public/cpp/application/connection_context.h" |
| 13 #include "mojo/public/cpp/application/lib/interface_factory_connector.h" | |
| 14 #include "mojo/public/cpp/application/lib/service_connector_registry.h" | 13 #include "mojo/public/cpp/application/lib/service_connector_registry.h" |
| 15 #include "mojo/public/cpp/application/service_connector.h" | 14 #include "mojo/public/cpp/application/service_connector.h" |
| 16 #include "mojo/public/cpp/bindings/binding.h" | 15 #include "mojo/public/cpp/bindings/binding.h" |
| 17 #include "mojo/public/interfaces/application/service_provider.mojom.h" | 16 #include "mojo/public/interfaces/application/service_provider.mojom.h" |
| 18 | 17 |
| 19 namespace mojo { | 18 namespace mojo { |
| 20 | 19 |
| 21 // An implementation of |ServiceProvider|, which can be customized appropriately | 20 // An implementation of |ServiceProvider|, which can be customized appropriately |
| 22 // (to select what services it provides). | 21 // (to select what services it provides). |
| 23 class ServiceProviderImpl : public ServiceProvider { | 22 class ServiceProviderImpl : public ServiceProvider { |
| (...skipping 22 matching lines...) Expand all Loading... |
| 46 // Binds this service provider implementation to the given interface request. | 45 // Binds this service provider implementation to the given interface request. |
| 47 // This may only be called if this object is unbound. | 46 // This may only be called if this object is unbound. |
| 48 void Bind(const ConnectionContext& connection_context, | 47 void Bind(const ConnectionContext& connection_context, |
| 49 InterfaceRequest<ServiceProvider> service_provider_request); | 48 InterfaceRequest<ServiceProvider> service_provider_request); |
| 50 | 49 |
| 51 // Disconnect this service provider implementation and put it in a state where | 50 // Disconnect this service provider implementation and put it in a state where |
| 52 // it can be rebound to a new request (i.e., restores this object to an | 51 // it can be rebound to a new request (i.e., restores this object to an |
| 53 // unbound state). This may be called even if this object is already unbound. | 52 // unbound state). This may be called even if this object is already unbound. |
| 54 void Close(); | 53 void Close(); |
| 55 | 54 |
| 56 // TODO(vtl): Remove this. | 55 // Adds a supported service with the given |name|, using the given |
| 57 template <typename Interface> | 56 // |service_connector|. |
| 58 void AddService(InterfaceFactory<Interface>* factory, | 57 void AddServiceForName(std::unique_ptr<ServiceConnector> service_connector, |
| 59 const std::string& interface_name = Interface::Name_) { | 58 const std::string& interface_name) { |
| 60 service_connector_registry_.SetServiceConnectorForName( | 59 service_connector_registry_.SetServiceConnectorForName( |
| 61 std::unique_ptr<ServiceConnector>( | 60 std::move(service_connector), interface_name); |
| 62 new internal::InterfaceFactoryConnector<Interface>(factory)), | |
| 63 interface_name); | |
| 64 } | 61 } |
| 65 | 62 |
| 66 // Adds a supported service with the given |name|, using the given | 63 // Adds a supported service with the given |name|, using the given |
| 67 // |interface_request_handler| (see above for information about | 64 // |interface_request_handler| (see above for information about |
| 68 // |InterfaceRequestHandler<Interface>|). |interface_request_handler| should | 65 // |InterfaceRequestHandler<Interface>|). |interface_request_handler| should |
| 69 // remain valid for the lifetime of this object. | 66 // remain valid for the lifetime of this object. |
| 70 // | 67 // |
| 71 // A typical usage may be: | 68 // A typical usage may be: |
| 72 // | 69 // |
| 73 // service_provider_impl_->AddServiceNew<Foobar>( | 70 // service_provider_impl_->AddService<Foobar>( |
| 74 // [](const ConnectionContext& connection_context, | 71 // [](const ConnectionContext& connection_context, |
| 75 // InterfaceRequest<FooBar> foobar_request) { | 72 // InterfaceRequest<FooBar> foobar_request) { |
| 76 // // |FoobarImpl| owns itself. | 73 // // |FoobarImpl| owns itself. |
| 77 // new FoobarImpl(std::move(foobar_request)); | 74 // new FoobarImpl(std::move(foobar_request)); |
| 78 // }); | 75 // }); |
| 79 // TODO(vtl): Remove the AddService() above and rename this to AddService(). | |
| 80 template <typename Interface> | 76 template <typename Interface> |
| 81 void AddServiceNew( | 77 void AddService(InterfaceRequestHandler<Interface> interface_request_handler, |
| 82 InterfaceRequestHandler<Interface> interface_request_handler, | 78 const std::string& name = Interface::Name_) { |
| 83 const std::string& name = Interface::Name_) { | 79 AddServiceForName( |
| 84 service_connector_registry_.SetServiceConnectorForName( | |
| 85 std::unique_ptr<ServiceConnector>(new ServiceConnectorImpl<Interface>( | 80 std::unique_ptr<ServiceConnector>(new ServiceConnectorImpl<Interface>( |
| 86 std::move(interface_request_handler))), | 81 std::move(interface_request_handler))), |
| 87 name); | 82 name); |
| 88 } | 83 } |
| 89 | 84 |
| 85 // Removes support for the service with the given |name|. |
| 86 void RemoveServiceForName(const std::string& name) { |
| 87 service_connector_registry_.RemoveServiceConnectorForName(name); |
| 88 } |
| 89 |
| 90 // Like |RemoveServiceForName()| (above), but designed so that it can be used |
| 91 // like |RemoveService<Interface>()| or even |RemoveService<Interface>(name)| |
| 92 // (to parallel |AddService<Interface>()|). |
| 93 template <typename Interface> |
| 94 void RemoveService(const std::string& name = Interface::Name_) { |
| 95 RemoveServiceForName(name); |
| 96 } |
| 97 |
| 90 // This uses the provided |fallback_service_provider| for connection requests | 98 // This uses the provided |fallback_service_provider| for connection requests |
| 91 // for services that are not known (haven't been added). (Set it to null to | 99 // for services that are not known (haven't been added). (Set it to null to |
| 92 // not have any fallback.) A fallback must outlive this object (or until it is | 100 // not have any fallback.) A fallback must outlive this object (or until it is |
| 93 // "cleared" or replaced by a different fallback. | 101 // "cleared" or replaced by a different fallback. |
| 94 void set_fallback_service_provider( | 102 void set_fallback_service_provider( |
| 95 ServiceProvider* fallback_service_provider) { | 103 ServiceProvider* fallback_service_provider) { |
| 96 fallback_service_provider_ = fallback_service_provider; | 104 fallback_service_provider_ = fallback_service_provider; |
| 97 } | 105 } |
| 98 | 106 |
| 107 // Gets the context for the connection that this object is bound to (if not |
| 108 // bound, the context is just a default-initialized |ConnectionContext|). |
| 109 const ConnectionContext& connection_context() const { |
| 110 return connection_context_; |
| 111 } |
| 112 |
| 99 private: | 113 private: |
| 100 // Objects of this class are used to adapt a generic (untyped) connection | 114 // Objects of this class are used to adapt a generic (untyped) connection |
| 101 // request (i.e., |ServiceConnector::ConnectToService()|) to the type-safe | 115 // request (i.e., |ServiceConnector::ConnectToService()|) to the type-safe |
| 102 // |InterfaceRequestHandler<Interface>|. | 116 // |InterfaceRequestHandler<Interface>|. |
| 103 template <typename Interface> | 117 template <typename Interface> |
| 104 class ServiceConnectorImpl : public ServiceConnector { | 118 class ServiceConnectorImpl : public ServiceConnector { |
| 105 public: | 119 public: |
| 106 explicit ServiceConnectorImpl( | 120 explicit ServiceConnectorImpl( |
| 107 InterfaceRequestHandler<Interface> interface_request_handler) | 121 InterfaceRequestHandler<Interface> interface_request_handler) |
| 108 : interface_request_handler_(std::move(interface_request_handler)) {} | 122 : interface_request_handler_(std::move(interface_request_handler)) {} |
| (...skipping 22 matching lines...) Expand all Loading... |
| 131 | 145 |
| 132 internal::ServiceConnectorRegistry service_connector_registry_; | 146 internal::ServiceConnectorRegistry service_connector_registry_; |
| 133 ServiceProvider* fallback_service_provider_; | 147 ServiceProvider* fallback_service_provider_; |
| 134 | 148 |
| 135 MOJO_DISALLOW_COPY_AND_ASSIGN(ServiceProviderImpl); | 149 MOJO_DISALLOW_COPY_AND_ASSIGN(ServiceProviderImpl); |
| 136 }; | 150 }; |
| 137 | 151 |
| 138 } // namespace mojo | 152 } // namespace mojo |
| 139 | 153 |
| 140 #endif // MOJO_PUBLIC_APPLICATION_SERVICE_PROVIDER_IMPL_H_ | 154 #endif // MOJO_PUBLIC_APPLICATION_SERVICE_PROVIDER_IMPL_H_ |
| OLD | NEW |