1 // Copyright 2013 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_BINDINGS_REMOTE_PTR_H_
6 #define MOJO_PUBLIC_BINDINGS_REMOTE_PTR_H_
10 #include "mojo/public/bindings/interface.h"
11 #include "mojo/public/bindings/lib/connector.h"
12 #include "mojo/public/system/macros.h"
16 // A RemotePtr is a smart-pointer for managing the connection of a message pipe
17 // to an interface proxy.
21 // Given foo.mojom containing the following interfaces:
29 // interface FooClient {
33 // On the client side of a service, RemotePtr might be used like so:
35 // class FooClientImpl : public FooClient {
37 // explicit FooClientImpl(ScopedFooHandle handle)
38 // : foo_(handle.Pass(), this) {
41 // virtual void Pong() {
45 // mojo::RemotePtr<Foo> foo_;
48 // On the implementation side of a service, RemotePtr might be used like so:
50 // class FooImpl : public Foo {
52 // explicit FooImpl(ScopedFooClientHandle handle)
53 // : client_(handle.Pass(), this) {
55 // virtual void Ping() {
59 // mojo::RemotePtr<FooClient> client_;
64 // 1- It is valid to pass NULL for the peer if you are not interested in
65 // receiving incoming messages. Those messages will still be consumed.
67 // 2- You may optionally register an ErrorHandler on the RemotePtr to be
68 // notified if the peer has gone away. Alternatively, you may poll the
69 // |encountered_error()| method to check if the peer has gone away.
74 MOJO_MOVE_ONLY_TYPE_FOR_CPP_03(RemotePtr
, RValue
)
77 RemotePtr() : state_(NULL
) {}
78 explicit RemotePtr(typename Interface
<S
>::ScopedHandle interface_handle
,
79 typename
S::_Peer
* peer
= NULL
,
80 ErrorHandler
* error_handler
= NULL
,
81 MojoAsyncWaiter
* waiter
= GetDefaultAsyncWaiter())
82 : state_(new State(ScopedMessagePipeHandle(interface_handle
.Pass()), peer
,
83 error_handler
, waiter
)) {
86 // Move-only constructor and operator=.
87 RemotePtr(RValue other
) : state_(other
.object
->release()) {}
88 RemotePtr
& operator=(RValue other
) {
89 state_
= other
.object
->release();
97 bool is_null() const {
103 return &state_
->proxy
;
115 void reset(typename Interface
<S
>::ScopedHandle interface_handle
,
116 typename
S::_Peer
* peer
= NULL
,
117 ErrorHandler
* error_handler
= NULL
,
118 MojoAsyncWaiter
* waiter
= GetDefaultAsyncWaiter()) {
120 state_
= new State(ScopedMessagePipeHandle(interface_handle
.Pass()), peer
,
121 error_handler
, waiter
);
124 bool encountered_error() const {
126 return state_
->connector
.encountered_error();
131 State(ScopedMessagePipeHandle message_pipe
, typename
S::_Peer
* peer
,
132 ErrorHandler
* error_handler
, MojoAsyncWaiter
* waiter
)
133 : connector(message_pipe
.Pass(), waiter
),
136 connector
.set_error_handler(error_handler
);
138 connector
.set_incoming_receiver(&stub
);
140 internal::Connector connector
;
141 typename
S::_Proxy proxy
;
142 typename
S::_Peer::_Stub stub
;
146 State
* state
= state_
;
156 #endif // MOJO_PUBLIC_BINDINGS_REMOTE_PTR_H_