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 CONTENT_PUBLIC_COMMON_SERVICE_REGISTRY_H_ 6#define CONTENT_PUBLIC_COMMON_SERVICE_REGISTRY_H_ 7 8#include <string> 9 10#include "base/bind.h" 11#include "base/callback.h" 12#include "base/strings/string_piece.h" 13#include "content/common/content_export.h" 14#include "mojo/public/cpp/bindings/interface_ptr.h" 15#include "mojo/public/cpp/bindings/interface_request.h" 16#include "mojo/public/cpp/system/core.h" 17 18namespace content { 19 20// A ServiceRegistry exposes local services that have been added using 21// AddService to a paired remote ServiceRegistry and provides local access to 22// services exposed by the remote ServiceRegistry through 23// ConnectToRemoteService. 24class CONTENT_EXPORT ServiceRegistry { 25 public: 26 virtual ~ServiceRegistry() {} 27 28 // Make the service created by |service_factory| available to the remote 29 // ServiceProvider. In response to each request for a service, 30 // |service_factory| will be run with an InterfaceRequest<Interface> 31 // representing that request. 32 template <typename Interface> 33 void AddService(const base::Callback<void(mojo::InterfaceRequest<Interface>)> 34 service_factory) { 35 AddService(Interface::Name_, 36 base::Bind(&ServiceRegistry::ForwardToServiceFactory<Interface>, 37 service_factory)); 38 } 39 virtual void AddService( 40 const std::string& service_name, 41 const base::Callback<void(mojo::ScopedMessagePipeHandle)> 42 service_factory) = 0; 43 44 // Remove future access to the service implementing Interface. Existing 45 // connections to the service are unaffected. 46 template <typename Interface> 47 void RemoveService() { 48 RemoveService(Interface::Name_); 49 } 50 virtual void RemoveService(const std::string& service_name) = 0; 51 52 // Connect to an interface provided by the remote service provider. 53 template <typename Interface> 54 void ConnectToRemoteService(mojo::InterfacePtr<Interface>* ptr) { 55 mojo::MessagePipe pipe; 56 ptr->Bind(pipe.handle0.Pass()); 57 ConnectToRemoteService(Interface::Name_, pipe.handle1.Pass()); 58 } 59 virtual void ConnectToRemoteService(const base::StringPiece& name, 60 mojo::ScopedMessagePipeHandle handle) = 0; 61 62 private: 63 template <typename Interface> 64 static void ForwardToServiceFactory( 65 const base::Callback<void(mojo::InterfaceRequest<Interface>)> 66 service_factory, 67 mojo::ScopedMessagePipeHandle handle) { 68 service_factory.Run(mojo::MakeRequest<Interface>(handle.Pass())); 69 } 70}; 71 72} // namespace content 73 74#endif // CONTENT_PUBLIC_COMMON_SERVICE_REGISTRY_H_ 75