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 3 // found in the LICENSE file. 4 5 #ifndef MOJO_PUBLIC_CPP_APPLICATION_LIB_SERVICE_CONNECTOR_H_ 6 #define MOJO_PUBLIC_CPP_APPLICATION_LIB_SERVICE_CONNECTOR_H_ 7 8 #include "mojo/public/cpp/application/interface_factory.h" 9 #include "mojo/public/cpp/bindings/interface_request.h" 10 11 namespace mojo { 12 class ApplicationConnection; 13 14 namespace internal { 15 16 class ServiceConnectorBase { 17 public: 18 ServiceConnectorBase(const std::string& name); 19 virtual ~ServiceConnectorBase(); 20 virtual void ConnectToService(const std::string& name, 21 ScopedMessagePipeHandle client_handle) = 0; name()22 std::string name() const { return name_; } set_application_connection(ApplicationConnection * connection)23 void set_application_connection(ApplicationConnection* connection) { 24 application_connection_ = connection; } 25 26 protected: 27 std::string name_; 28 ApplicationConnection* application_connection_; 29 30 MOJO_DISALLOW_COPY_AND_ASSIGN(ServiceConnectorBase); 31 }; 32 33 template <typename Interface> 34 class InterfaceFactoryConnector : public ServiceConnectorBase { 35 public: InterfaceFactoryConnector(InterfaceFactory<Interface> * factory)36 explicit InterfaceFactoryConnector(InterfaceFactory<Interface>* factory) 37 : ServiceConnectorBase(Interface::Name_), factory_(factory) {} ~InterfaceFactoryConnector()38 virtual ~InterfaceFactoryConnector() {} 39 ConnectToService(const std::string & name,ScopedMessagePipeHandle client_handle)40 virtual void ConnectToService(const std::string& name, 41 ScopedMessagePipeHandle client_handle) { 42 factory_->Create(application_connection_, 43 MakeRequest<Interface>(client_handle.Pass())); 44 } 45 46 private: 47 InterfaceFactory<Interface>* factory_; 48 MOJO_DISALLOW_COPY_AND_ASSIGN(InterfaceFactoryConnector); 49 }; 50 51 } // namespace internal 52 } // namespace mojo 53 54 #endif // MOJO_PUBLIC_CPP_APPLICATION_LIB_SERVICE_CONNECTOR_H_ 55