Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / mojo / public / cpp / bindings / interface_impl.h
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 MOJO_PUBLIC_CPP_BINDINGS_INTERFACE_IMPL_H_
6 #define MOJO_PUBLIC_CPP_BINDINGS_INTERFACE_IMPL_H_
7
8 #include "mojo/public/cpp/bindings/lib/interface_impl_internal.h"
9 #include "mojo/public/cpp/system/macros.h"
10
11 namespace mojo {
12
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
15 // BindToProxy.
16 //
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
19 // inheritance chain.
20 template <typename Interface>
21 class InterfaceImpl : public WithErrorHandler<Interface> {
22  public:
23   InterfaceImpl() : internal_state_(this) {}
24   virtual ~InterfaceImpl() {}
25
26   // Subclasses must handle connection errors.
27   virtual void OnConnectionError() = 0;
28
29   // DO NOT USE. Exposed only for internal use and for testing.
30   internal::InterfaceImplState<Interface>* internal_state() {
31     return &internal_state_;
32   }
33
34  private:
35   internal::InterfaceImplState<Interface> internal_state_;
36   MOJO_DISALLOW_COPY_AND_ASSIGN(InterfaceImpl);
37 };
38
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
42 // will be called.
43 //
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.
47 //
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);
55   return instance;
56 }
57
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.
62 //
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.
66 //
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);
74   return instance;
75 }
76
77 }  // namespace mojo
78
79 #endif  // MOJO_PUBLIC_CPP_BINDINGS_INTERFACE_IMPL_H_