| 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_COMMON_MOJO_SERVICE_REGISTRY_IMPL_H_ | 5 #ifndef CONTENT_COMMON_MOJO_SERVICE_REGISTRY_IMPL_H_ |
| 6 #define CONTENT_COMMON_MOJO_SERVICE_REGISTRY_IMPL_H_ | 6 #define CONTENT_COMMON_MOJO_SERVICE_REGISTRY_IMPL_H_ |
| 7 | 7 |
| 8 #include <map> | 8 #include <map> |
| 9 #include <queue> | 9 #include <queue> |
| 10 #include <string> | 10 #include <string> |
| 11 #include <utility> | 11 #include <utility> |
| 12 | 12 |
| 13 #include "base/callback.h" | 13 #include "base/callback.h" |
| 14 #include "base/compiler_specific.h" | 14 #include "base/compiler_specific.h" |
| 15 #include "base/memory/weak_ptr.h" | 15 #include "base/memory/weak_ptr.h" |
| 16 #include "content/public/common/service_registry.h" | 16 #include "content/public/common/service_registry.h" |
| 17 #include "mojo/public/cpp/bindings/binding.h" | 17 #include "mojo/public/cpp/bindings/binding.h" |
| 18 #include "mojo/public/cpp/system/core.h" | 18 #include "mojo/public/cpp/system/core.h" |
| 19 #include "mojo/shell/public/interfaces/service_provider.mojom.h" | 19 #include "mojo/shell/public/interfaces/service_provider.mojom.h" |
| 20 | 20 |
| 21 namespace content { | 21 namespace content { |
| 22 | 22 |
| 23 class CONTENT_EXPORT ServiceRegistryImpl | 23 class CONTENT_EXPORT ServiceRegistryImpl |
| 24 : public ServiceRegistry, | 24 : public ServiceRegistry, |
| 25 public NON_EXPORTED_BASE(mojo::ServiceProvider) { | 25 public NON_EXPORTED_BASE(mojo::ServiceProvider) { |
| 26 public: | 26 public: |
| 27 using ServiceFactory = base::Callback<void(mojo::ScopedMessagePipeHandle)>; | |
| 28 | |
| 29 ServiceRegistryImpl(); | 27 ServiceRegistryImpl(); |
| 30 ~ServiceRegistryImpl() override; | 28 ~ServiceRegistryImpl() override; |
| 31 | 29 |
| 32 // Binds this ServiceProvider implementation to a message pipe endpoint. | 30 // Binds this ServiceProvider implementation to a message pipe endpoint. |
| 33 void Bind(mojo::InterfaceRequest<mojo::ServiceProvider> request); | 31 void Bind(mojo::InterfaceRequest<mojo::ServiceProvider> request); |
| 34 | 32 |
| 35 // Binds to a remote ServiceProvider. This will expose added services to the | 33 // Binds to a remote ServiceProvider. This will expose added services to the |
| 36 // remote ServiceProvider with the corresponding handle and enable | 34 // remote ServiceProvider with the corresponding handle and enable |
| 37 // ConnectToRemoteService to provide access to services exposed by the remote | 35 // ConnectToRemoteService to provide access to services exposed by the remote |
| 38 // ServiceProvider. | 36 // ServiceProvider. |
| 39 void BindRemoteServiceProvider(mojo::ServiceProviderPtr service_provider); | 37 void BindRemoteServiceProvider(mojo::ServiceProviderPtr service_provider); |
| 40 | 38 |
| 41 // Registers a local service factory to intercept ConnectToRemoteService | |
| 42 // requests instead of actually connecting to the remote registry. Used only | |
| 43 // for testing. | |
| 44 void AddServiceOverrideForTesting(const std::string& service_name, | |
| 45 const ServiceFactory& service_factory); | |
| 46 | |
| 47 // ServiceRegistry overrides. | 39 // ServiceRegistry overrides. |
| 48 void AddService(const std::string& service_name, | 40 void AddService(const std::string& service_name, |
| 49 const ServiceFactory service_factory) override; | 41 const base::Callback<void(mojo::ScopedMessagePipeHandle)> |
| 42 service_factory) override; |
| 50 void RemoveService(const std::string& service_name) override; | 43 void RemoveService(const std::string& service_name) override; |
| 51 void ConnectToRemoteService(const base::StringPiece& service_name, | 44 void ConnectToRemoteService(const base::StringPiece& service_name, |
| 52 mojo::ScopedMessagePipeHandle handle) override; | 45 mojo::ScopedMessagePipeHandle handle) override; |
| 53 | 46 |
| 54 bool IsBound() const; | 47 bool IsBound() const; |
| 55 | 48 |
| 56 base::WeakPtr<ServiceRegistry> GetWeakPtr(); | 49 base::WeakPtr<ServiceRegistry> GetWeakPtr(); |
| 57 | 50 |
| 58 private: | 51 private: |
| 59 // mojo::ServiceProvider overrides. | 52 // mojo::ServiceProvider overrides. |
| 60 void ConnectToService(const mojo::String& name, | 53 void ConnectToService(const mojo::String& name, |
| 61 mojo::ScopedMessagePipeHandle client_handle) override; | 54 mojo::ScopedMessagePipeHandle client_handle) override; |
| 62 | 55 |
| 63 void OnConnectionError(); | 56 void OnConnectionError(); |
| 64 | 57 |
| 65 mojo::Binding<mojo::ServiceProvider> binding_; | 58 mojo::Binding<mojo::ServiceProvider> binding_; |
| 66 mojo::ServiceProviderPtr remote_provider_; | 59 mojo::ServiceProviderPtr remote_provider_; |
| 67 | 60 |
| 68 std::map<std::string, ServiceFactory> service_factories_; | 61 std::map<std::string, base::Callback<void(mojo::ScopedMessagePipeHandle)> > |
| 62 service_factories_; |
| 69 std::queue<std::pair<std::string, mojo::MessagePipeHandle> > | 63 std::queue<std::pair<std::string, mojo::MessagePipeHandle> > |
| 70 pending_connects_; | 64 pending_connects_; |
| 71 | 65 |
| 72 std::map<std::string, ServiceFactory> service_overrides_; | |
| 73 | |
| 74 base::WeakPtrFactory<ServiceRegistry> weak_factory_; | 66 base::WeakPtrFactory<ServiceRegistry> weak_factory_; |
| 75 }; | 67 }; |
| 76 | 68 |
| 77 } // namespace content | 69 } // namespace content |
| 78 | 70 |
| 79 #endif // CONTENT_COMMON_MOJO_SERVICE_REGISTRY_IMPL_H_ | 71 #endif // CONTENT_COMMON_MOJO_SERVICE_REGISTRY_IMPL_H_ |
| OLD | NEW |