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