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.
5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_INTERFACE_IMPL_H_
6 #define MOJO_PUBLIC_CPP_BINDINGS_INTERFACE_IMPL_H_
8 #include "mojo/public/cpp/bindings/lib/interface_impl_internal.h"
9 #include "mojo/public/cpp/system/macros.h"
13 // InterfaceImpl<..> is designed to be the base class of an interface
14 // implementation. It may be bound to a pipe or a proxy, see BindToPipe and
17 // NOTE: A base class of WithErrorHandler<Interface> is used to avoid multiple
18 // inheritance. This base class inserts the signature of ErrorHandler into the
20 template <typename Interface>
21 class InterfaceImpl : public WithErrorHandler<Interface> {
23 InterfaceImpl() : internal_state_(this) {}
24 virtual ~InterfaceImpl() {}
26 // Subclasses must handle connection errors.
27 virtual void OnConnectionError() = 0;
29 // DO NOT USE. Exposed only for internal use and for testing.
30 internal::InterfaceImplState<Interface>* internal_state() {
31 return &internal_state_;
35 internal::InterfaceImplState<Interface> internal_state_;
36 MOJO_DISALLOW_COPY_AND_ASSIGN(InterfaceImpl);
39 // Takes an instance of an InterfaceImpl<..> subclass and binds it to the given
40 // MessagePipe. The instance is returned for convenience in member initializer
41 // lists, etc. If the pipe is closed, the instance's OnConnectionError method
44 // The instance is also bound to the current thread. Its methods will only be
45 // called on the current thread, and if the current thread exits, then it will
46 // also be deleted, and along with it, its end point of the pipe will be closed.
48 // Before returning, the instance will receive a SetClient call, providing it
49 // with a proxy to the client on the other end of the pipe.
50 template <typename Impl>
51 Impl* BindToPipe(Impl* instance,
52 ScopedMessagePipeHandle handle,
53 MojoAsyncWaiter* waiter = GetDefaultAsyncWaiter()) {
54 instance->internal_state()->Bind(handle.Pass(), waiter);
58 // Takes an instance of an InterfaceImpl<..> subclass and binds it to the given
59 // InterfacePtr<..>. The instance is returned for convenience in member
60 // initializer lists, etc. If the pipe is closed, the instance's
61 // OnConnectionError method will be called.
63 // The instance is also bound to the current thread. Its methods will only be
64 // called on the current thread, and if the current thread exits, then it will
65 // also be deleted, and along with it, its end point of the pipe will be closed.
67 // Before returning, the instance will receive a SetClient call, providing it
68 // with a proxy to the client on the other end of the pipe.
69 template <typename Impl, typename Interface>
70 Impl* BindToProxy(Impl* instance,
71 InterfacePtr<Interface>* ptr,
72 MojoAsyncWaiter* waiter = GetDefaultAsyncWaiter()) {
73 instance->internal_state()->BindProxy(ptr, waiter);
79 #endif // MOJO_PUBLIC_CPP_BINDINGS_INTERFACE_IMPL_H_