| OLD | NEW |
| (Empty) |
| 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 #include <utility> | |
| 10 | |
| 11 #include "base/bind.h" | |
| 12 #include "base/callback.h" | |
| 13 #include "base/strings/string_piece.h" | |
| 14 #include "content/common/content_export.h" | |
| 15 #include "mojo/public/cpp/bindings/interface_ptr.h" | |
| 16 #include "mojo/public/cpp/bindings/interface_request.h" | |
| 17 #include "mojo/public/cpp/system/core.h" | |
| 18 #include "services/shell/public/interfaces/interface_provider.mojom.h" | |
| 19 | |
| 20 namespace content { | |
| 21 | |
| 22 // A ServiceRegistry exposes local services that have been added using | |
| 23 // AddService to a paired remote ServiceRegistry and provides local access to | |
| 24 // services exposed by the remote ServiceRegistry through | |
| 25 // ConnectToRemoteService. | |
| 26 class CONTENT_EXPORT ServiceRegistry { | |
| 27 public: | |
| 28 static ServiceRegistry* Create(); | |
| 29 virtual ~ServiceRegistry() {} | |
| 30 | |
| 31 // Binds this ServiceProvider implementation to a message pipe endpoint. | |
| 32 virtual void Bind(shell::mojom::InterfaceProviderRequest request) = 0; | |
| 33 | |
| 34 // ServiceRegistry is created with a bound InterfaceProviderPtr for remote | |
| 35 // interfaces, this is the server end of that pipe which should be passed | |
| 36 // to the remote end. | |
| 37 virtual shell::mojom::InterfaceProviderRequest TakeRemoteRequest() = 0; | |
| 38 | |
| 39 // Make the service created by |service_factory| available to the remote | |
| 40 // ServiceProvider. In response to each request for a service, | |
| 41 // |service_factory| will be run with an InterfaceRequest<Interface> | |
| 42 // representing that request. Adding a factory for an already registered | |
| 43 // service will override the factory. Existing connections to the service are | |
| 44 // unaffected. | |
| 45 // | |
| 46 // If a non-null |task_runner| is passed, the factory will be invoked on that | |
| 47 // TaskRunner. | |
| 48 template <typename Interface> | |
| 49 void AddService(const base::Callback<void(mojo::InterfaceRequest<Interface>)>& | |
| 50 service_factory, | |
| 51 const scoped_refptr<base::SingleThreadTaskRunner>& | |
| 52 task_runner = nullptr) { | |
| 53 AddService(Interface::Name_, | |
| 54 base::Bind(&ServiceRegistry::ForwardToServiceFactory<Interface>, | |
| 55 service_factory), | |
| 56 task_runner); | |
| 57 } | |
| 58 virtual void AddService( | |
| 59 const std::string& service_name, | |
| 60 const base::Callback<void(mojo::ScopedMessagePipeHandle)>& | |
| 61 service_factory, | |
| 62 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner) = 0; | |
| 63 | |
| 64 // Remove future access to the service implementing Interface. Existing | |
| 65 // connections to the service are unaffected. | |
| 66 template <typename Interface> | |
| 67 void RemoveService() { | |
| 68 RemoveService(Interface::Name_); | |
| 69 } | |
| 70 virtual void RemoveService(const std::string& service_name) = 0; | |
| 71 | |
| 72 // Connect to an interface provided by the remote service provider. | |
| 73 template <typename Interface> | |
| 74 void ConnectToRemoteService(mojo::InterfaceRequest<Interface> ptr) { | |
| 75 ConnectToRemoteService(Interface::Name_, ptr.PassMessagePipe()); | |
| 76 } | |
| 77 virtual void ConnectToRemoteService(base::StringPiece name, | |
| 78 mojo::ScopedMessagePipeHandle handle) = 0; | |
| 79 | |
| 80 // Registers a local service factory to intercept ConnectToRemoteService | |
| 81 // requests instead of actually connecting to the remote registry. Used only | |
| 82 // for testing. | |
| 83 virtual void AddServiceOverrideForTesting( | |
| 84 const std::string& service_name, | |
| 85 const base::Callback<void(mojo::ScopedMessagePipeHandle)>& | |
| 86 service_factory) = 0; | |
| 87 | |
| 88 // Removes all local service factories registered | |
| 89 // by AddServiceOverrideForTesting. Used only for testing. | |
| 90 virtual void ClearServiceOverridesForTesting() = 0; | |
| 91 | |
| 92 private: | |
| 93 template <typename Interface> | |
| 94 static void ForwardToServiceFactory( | |
| 95 const base::Callback<void(mojo::InterfaceRequest<Interface>)>& | |
| 96 service_factory, | |
| 97 mojo::ScopedMessagePipeHandle handle) { | |
| 98 service_factory.Run(mojo::MakeRequest<Interface>(std::move(handle))); | |
| 99 } | |
| 100 }; | |
| 101 | |
| 102 } // namespace content | |
| 103 | |
| 104 #endif // CONTENT_PUBLIC_COMMON_SERVICE_REGISTRY_H_ | |
| OLD | NEW |