service_provider_impl.h revision 6e8cce623b6e4fe0c9e4af605d675dd9d0338c38
16e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)// Copyright 2014 The Chromium Authors. All rights reserved.
26e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
36e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)// found in the LICENSE file.
46e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
56e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)#ifndef MOJO_PUBLIC_APPLICATION_SERVICE_PROVIDER_IMPL_H_
66e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)#define MOJO_PUBLIC_APPLICATION_SERVICE_PROVIDER_IMPL_H_
76e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
86e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)#include "mojo/public/cpp/application/lib/service_connector.h"
96e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)#include "mojo/public/interfaces/application/service_provider.mojom.h"
106e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
116e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)namespace mojo {
126e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)namespace internal {
136e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)class WeakServiceProvider;
146e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)class ServiceConnectorBase;
156e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)}
166e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
176e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)// Implements a registry that can be used to expose services to another app.
186e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)class ServiceProviderImpl : public InterfaceImpl<ServiceProvider> {
196e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) public:
206e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  ServiceProviderImpl();
216e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  virtual ~ServiceProviderImpl();
226e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
236e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  template <typename Interface>
246e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  void AddService(InterfaceFactory<Interface>* factory) {
256e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    AddServiceConnector(
266e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)        new internal::InterfaceFactoryConnector<Interface>(factory));
276e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  }
286e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
296e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  // Returns an instance of a ServiceProvider that weakly wraps this impl's
306e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  // connection to some other app. Whereas the lifetime of an instance of
316e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  // ServiceProviderImpl is bound to the lifetime of the pipe it
326e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  // encapsulates, the lifetime of the ServiceProvider instance returned by this
336e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  // method is assumed by the caller. After the pipe is closed
346e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  // ConnectToService() calls on this object will be silently dropped.
356e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  // This method must only be called once per ServiceProviderImpl.
366e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  ServiceProvider* CreateRemoteServiceProvider();
376e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
386e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) private:
396e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  typedef std::map<std::string, internal::ServiceConnectorBase*>
406e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      NameToServiceConnectorMap;
416e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
426e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  friend class internal::WeakServiceProvider;
436e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
446e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  // Overridden from ServiceProvider:
456e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  virtual void ConnectToService(
466e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      const String& service_name,
476e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      ScopedMessagePipeHandle client_handle) MOJO_OVERRIDE;
486e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
496e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  // Overridden from InterfaceImpl:
506e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  virtual void OnConnectionError() MOJO_OVERRIDE;
516e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
526e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  void AddServiceConnector(
536e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      internal::ServiceConnectorBase* service_connector);
546e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  void RemoveServiceConnector(
556e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      internal::ServiceConnectorBase* service_connector);
566e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
576e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  void ClearRemote();
586e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
596e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  NameToServiceConnectorMap service_connectors_;
606e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
616e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  internal::WeakServiceProvider* remote_;
626e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
636e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  MOJO_DISALLOW_COPY_AND_ASSIGN(ServiceProviderImpl);
646e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)};
656e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
666e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)}  // namespace mojo
676e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
686e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)#endif  // MOJO_PUBLIC_APPLICATION_SERVICE_PROVIDER_IMPL_H_
69