summaryrefslogtreecommitdiffstats
path: root/mojo/public/cpp/bindings/interface_impl.h
blob: 6e68aa78e0891e3bcdf9d241fa871f20953d6165 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef MOJO_PUBLIC_CPP_BINDINGS_INTERFACE_IMPL_H_
#define MOJO_PUBLIC_CPP_BINDINGS_INTERFACE_IMPL_H_

#include "mojo/public/cpp/bindings/interface_request.h"
#include "mojo/public/cpp/bindings/lib/interface_impl_internal.h"
#include "mojo/public/cpp/system/macros.h"

namespace mojo {

// InterfaceImpl<..> is designed to be the base class of an interface
// implementation. It may be bound to a pipe or a proxy, see BindToPipe and
// BindToProxy.
template <typename Interface>
class InterfaceImpl : public internal::InterfaceImplBase<Interface> {
 public:
  typedef typename Interface::Client Client;

  InterfaceImpl() : internal_state_(this) {}
  virtual ~InterfaceImpl() {}

  // Returns a proxy to the client interface. This is null upon construction,
  // and becomes non-null after OnClientConnected. NOTE: It remains non-null
  // until this instance is deleted.
  Client* client() { return internal_state_.client(); }

  // Called when the client has connected to this instance.
  virtual void OnConnectionEstablished() {}

  // Called when the client is no longer connected to this instance. NOTE: The
  // client() method continues to return a non-null pointer after this method
  // is called. After this method is called, any method calls made on client()
  // will be silently ignored.
  virtual void OnConnectionError() {}

  // DO NOT USE. Exposed only for internal use and for testing.
  internal::InterfaceImplState<Interface>* internal_state() {
    return &internal_state_;
  }

 private:
  internal::InterfaceImplState<Interface> internal_state_;
  MOJO_DISALLOW_COPY_AND_ASSIGN(InterfaceImpl);
};

// Takes an instance of an InterfaceImpl<..> subclass and binds it to the given
// MessagePipe. The instance is returned for convenience in member initializer
// lists, etc. If the pipe is closed, the instance's OnConnectionError method
// will be called.
//
// The instance is also bound to the current thread. Its methods will only be
// called on the current thread, and if the current thread exits, then it will
// also be deleted, and along with it, its end point of the pipe will be closed.
//
// Before returning, the instance's OnConnectionEstablished method is called.
template <typename Impl>
Impl* BindToPipe(Impl* instance,
                 ScopedMessagePipeHandle handle,
                 const MojoAsyncWaiter* waiter = GetDefaultAsyncWaiter()) {
  instance->internal_state()->Bind(handle.Pass(), waiter);
  return instance;
}

// Takes an instance of an InterfaceImpl<..> subclass and binds it to the given
// InterfacePtr<..>. The instance is returned for convenience in member
// initializer lists, etc. If the pipe is closed, the instance's
// OnConnectionError method will be called.
//
// The instance is also bound to the current thread. Its methods will only be
// called on the current thread, and if the current thread exits, then it will
// also be deleted, and along with it, its end point of the pipe will be closed.
//
// Before returning, the instance's OnConnectionEstablished method is called.
template <typename Impl, typename Interface>
Impl* BindToProxy(Impl* instance,
                  InterfacePtr<Interface>* ptr,
                  const MojoAsyncWaiter* waiter = GetDefaultAsyncWaiter()) {
  instance->internal_state()->BindProxy(ptr, waiter);
  return instance;
}

// Takes an instance of an InterfaceImpl<..> subclass and binds it to the given
// InterfaceRequest<..>. The instance is returned for convenience in member
// initializer lists, etc. If the pipe is closed, the instance's
// OnConnectionError method will be called.
//
// The instance is also bound to the current thread. Its methods will only be
// called on the current thread, and if the current thread exits, then it will
// also be deleted, and along with it, its end point of the pipe will be closed.
//
// Before returning, the instance will receive a SetClient call, providing it
// with a proxy to the client on the other end of the pipe.
template <typename Impl, typename Interface>
Impl* BindToRequest(Impl* instance,
                    InterfaceRequest<Interface>* request,
                    const MojoAsyncWaiter* waiter = GetDefaultAsyncWaiter()) {
  return BindToPipe(instance, request->PassMessagePipe(), waiter);
}

}  // namespace mojo

#endif  // MOJO_PUBLIC_CPP_BINDINGS_INTERFACE_IMPL_H_