• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/interface_request.h"
9 #include "mojo/public/cpp/bindings/lib/interface_impl_internal.h"
10 #include "mojo/public/cpp/environment/environment.h"
11 #include "mojo/public/cpp/system/macros.h"
12 
13 namespace mojo {
14 
15 // InterfaceImpl<..> is designed to be the base class of an interface
16 // implementation. It may be bound to a pipe or a proxy, see BindToPipe and
17 // BindToProxy.
18 template <typename Interface>
19 class InterfaceImpl : public internal::InterfaceImplBase<Interface> {
20  public:
21   typedef typename Interface::Client Client;
22 
InterfaceImpl()23   InterfaceImpl() : internal_state_(this) {}
~InterfaceImpl()24   virtual ~InterfaceImpl() {}
25 
26   // Returns a proxy to the client interface. This is null upon construction,
27   // and becomes non-null after OnClientConnected. NOTE: It remains non-null
28   // until this instance is deleted.
client()29   Client* client() { return internal_state_.client(); }
30 
31   // Called when the client has connected to this instance.
OnConnectionEstablished()32   virtual void OnConnectionEstablished() {}
33 
34   // Called when the client is no longer connected to this instance. NOTE: The
35   // client() method continues to return a non-null pointer after this method
36   // is called. After this method is called, any method calls made on client()
37   // will be silently ignored.
OnConnectionError()38   virtual void OnConnectionError() {}
39 
40   // DO NOT USE. Exposed only for internal use and for testing.
internal_state()41   internal::InterfaceImplState<Interface>* internal_state() {
42     return &internal_state_;
43   }
44 
45  private:
46   internal::InterfaceImplState<Interface> internal_state_;
47   MOJO_DISALLOW_COPY_AND_ASSIGN(InterfaceImpl);
48 };
49 
50 // Takes an instance of an InterfaceImpl<..> subclass and binds it to the given
51 // MessagePipe. The instance is returned for convenience in member initializer
52 // lists, etc. If the pipe is closed, the instance's OnConnectionError method
53 // will be called.
54 //
55 // The instance is also bound to the current thread. Its methods will only be
56 // called on the current thread, and if the current thread exits, then it will
57 // also be deleted, and along with it, its end point of the pipe will be closed.
58 //
59 // Before returning, the instance's OnConnectionEstablished method is called.
60 template <typename Impl>
61 Impl* BindToPipe(
62     Impl* instance,
63     ScopedMessagePipeHandle handle,
64     const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) {
65   instance->internal_state()->Bind(handle.Pass(), waiter);
66   return instance;
67 }
68 
69 // Takes an instance of an InterfaceImpl<..> subclass and binds it to the given
70 // InterfacePtr<..>. The instance is returned for convenience in member
71 // initializer lists, etc. If the pipe is closed, the instance's
72 // OnConnectionError method will be called.
73 //
74 // The instance is also bound to the current thread. Its methods will only be
75 // called on the current thread, and if the current thread exits, then it will
76 // also be deleted, and along with it, its end point of the pipe will be closed.
77 //
78 // Before returning, the instance's OnConnectionEstablished method is called.
79 template <typename Impl, typename Interface>
80 Impl* BindToProxy(
81     Impl* instance,
82     InterfacePtr<Interface>* ptr,
83     const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) {
84   instance->internal_state()->BindProxy(ptr, waiter);
85   return instance;
86 }
87 
88 // Takes an instance of an InterfaceImpl<..> subclass and binds it to the given
89 // InterfaceRequest<..>. The instance is returned for convenience in member
90 // initializer lists, etc. If the pipe is closed, the instance's
91 // OnConnectionError method will be called.
92 //
93 // The instance is also bound to the current thread. Its methods will only be
94 // called on the current thread, and if the current thread exits, then it will
95 // also be deleted, and along with it, its end point of the pipe will be closed.
96 //
97 // Before returning, the instance will receive a SetClient call, providing it
98 // with a proxy to the client on the other end of the pipe.
99 template <typename Impl, typename Interface>
100 Impl* BindToRequest(
101     Impl* instance,
102     InterfaceRequest<Interface>* request,
103     const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) {
104   return BindToPipe(instance, request->PassMessagePipe(), waiter);
105 }
106 
107 }  // namespace mojo
108 
109 #endif  // MOJO_PUBLIC_CPP_BINDINGS_INTERFACE_IMPL_H_
110