OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef MOJO_PUBLIC_BINDINGS_REMOTE_PTR_H_ | 5 #ifndef MOJO_PUBLIC_BINDINGS_REMOTE_PTR_H_ |
6 #define MOJO_PUBLIC_BINDINGS_REMOTE_PTR_H_ | 6 #define MOJO_PUBLIC_BINDINGS_REMOTE_PTR_H_ |
7 | 7 |
8 #include <assert.h> | 8 #include <assert.h> |
9 | 9 |
10 #include "mojo/public/bindings/lib/connector.h" | 10 #include "mojo/public/bindings/lib/connector.h" |
11 #include "mojo/public/system/macros.h" | 11 #include "mojo/public/system/macros.h" |
12 | 12 |
13 namespace mojo { | 13 namespace mojo { |
14 | 14 |
15 // A RemotePtr is a smart-pointer for managing the connection of a message pipe | 15 // A RemotePtr is a smart-pointer for managing the connection of a message pipe |
16 // to an interface proxy. | 16 // to an interface proxy. |
17 // | 17 // |
18 // EXAMPLE | 18 // EXAMPLE: |
19 // | 19 // |
20 // On the client side of a service, RemotePtr might be used like so: | 20 // On the client side of a service, RemotePtr might be used like so: |
21 // | 21 // |
22 // class FooClientImpl : public FooClientStub { | 22 // class FooClientImpl : public FooClientStub { |
23 // public: | 23 // public: |
24 // explicit FooClientImpl(const mojo::MessagePipeHandle& message_pipe) | 24 // explicit FooClientImpl(const mojo::MessagePipeHandle& message_pipe) |
25 // : foo_(message_pipe, this) { | 25 // : foo_(message_pipe, this) { |
26 // foo_.Ping(); | 26 // foo_.Ping(); |
27 // } | 27 // } |
28 // virtual void Pong() { | 28 // virtual void Pong() { |
(...skipping 10 matching lines...) Expand all Loading... |
39 // explicit FooImpl(const mojo::MessagePipeHandle& message_pipe) | 39 // explicit FooImpl(const mojo::MessagePipeHandle& message_pipe) |
40 // : client_(message_pipe, this) { | 40 // : client_(message_pipe, this) { |
41 // } | 41 // } |
42 // virtual void Ping() { | 42 // virtual void Ping() { |
43 // client_->Pong(); | 43 // client_->Pong(); |
44 // } | 44 // } |
45 // private: | 45 // private: |
46 // mojo::RemotePtr<FooClient> client_; | 46 // mojo::RemotePtr<FooClient> client_; |
47 // }; | 47 // }; |
48 // | 48 // |
| 49 // NOTE: |
| 50 // |
| 51 // 1- It is valid to pass NULL for the peer if you are not interested in |
| 52 // receiving incoming messages. Those messages will still be consumed. |
| 53 // |
| 54 // 2- You may optionally register an ErrorHandler on the RemotePtr to be |
| 55 // notified if the peer has gone away. Alternatively, you may poll the |
| 56 // |encountered_error()| method to check if the peer has gone away. |
| 57 // |
49 template <typename S> | 58 template <typename S> |
50 class RemotePtr { | 59 class RemotePtr { |
51 struct State; | 60 struct State; |
52 MOJO_MOVE_ONLY_TYPE_FOR_CPP_03(RemotePtr, RValue); | 61 MOJO_MOVE_ONLY_TYPE_FOR_CPP_03(RemotePtr, RValue); |
53 | 62 |
54 public: | 63 public: |
55 RemotePtr() : state_(NULL) {} | 64 RemotePtr() : state_(NULL) {} |
56 explicit RemotePtr(ScopedMessagePipeHandle message_pipe, | 65 explicit RemotePtr(ScopedMessagePipeHandle message_pipe, |
57 typename S::_Peer* peer = NULL, | 66 typename S::_Peer* peer = NULL, |
| 67 ErrorHandler* error_handler = NULL, |
58 MojoAsyncWaiter* waiter = GetDefaultAsyncWaiter()) | 68 MojoAsyncWaiter* waiter = GetDefaultAsyncWaiter()) |
59 : state_(new State(message_pipe.Pass(), peer, waiter)) { | 69 : state_(new State(message_pipe.Pass(), peer, error_handler, waiter)) { |
60 } | 70 } |
61 | 71 |
62 // Move-only constructor and operator=. | 72 // Move-only constructor and operator=. |
63 RemotePtr(RValue other) : state_(other.object->release()) {} | 73 RemotePtr(RValue other) : state_(other.object->release()) {} |
64 RemotePtr& operator=(RValue other) { | 74 RemotePtr& operator=(RValue other) { |
65 state_ = other.object->release(); | 75 state_ = other.object->release(); |
66 return *this; | 76 return *this; |
67 } | 77 } |
68 | 78 |
69 ~RemotePtr() { | 79 ~RemotePtr() { |
(...skipping 13 matching lines...) Expand all Loading... |
83 return get(); | 93 return get(); |
84 } | 94 } |
85 | 95 |
86 void reset() { | 96 void reset() { |
87 delete state_; | 97 delete state_; |
88 state_ = NULL; | 98 state_ = NULL; |
89 } | 99 } |
90 | 100 |
91 void reset(ScopedMessagePipeHandle message_pipe, | 101 void reset(ScopedMessagePipeHandle message_pipe, |
92 typename S::_Peer* peer = NULL, | 102 typename S::_Peer* peer = NULL, |
| 103 ErrorHandler* error_handler = NULL, |
93 MojoAsyncWaiter* waiter = GetDefaultAsyncWaiter()) { | 104 MojoAsyncWaiter* waiter = GetDefaultAsyncWaiter()) { |
94 delete state_; | 105 delete state_; |
95 state_ = new State(message_pipe.Pass(), peer, waiter); | 106 state_ = new State(message_pipe.Pass(), peer, error_handler, waiter); |
96 } | 107 } |
97 | 108 |
98 bool encountered_error() const { | 109 bool encountered_error() const { |
99 assert(state_); | 110 assert(state_); |
100 return state_->connector.encountered_error(); | 111 return state_->connector.encountered_error(); |
101 } | 112 } |
102 | 113 |
103 private: | 114 private: |
104 struct State { | 115 struct State { |
105 State(ScopedMessagePipeHandle message_pipe, typename S::_Peer* peer, | 116 State(ScopedMessagePipeHandle message_pipe, typename S::_Peer* peer, |
106 MojoAsyncWaiter* waiter) | 117 ErrorHandler* error_handler, MojoAsyncWaiter* waiter) |
107 : connector(message_pipe.Pass(), waiter), | 118 : connector(message_pipe.Pass(), waiter), |
108 proxy(&connector), | 119 proxy(&connector), |
109 stub(peer) { | 120 stub(peer) { |
| 121 connector.set_error_handler(error_handler); |
110 if (peer) | 122 if (peer) |
111 connector.SetIncomingReceiver(&stub); | 123 connector.set_incoming_receiver(&stub); |
112 } | 124 } |
113 internal::Connector connector; | 125 internal::Connector connector; |
114 typename S::_Proxy proxy; | 126 typename S::_Proxy proxy; |
115 typename S::_Peer::_Stub stub; | 127 typename S::_Peer::_Stub stub; |
116 }; | 128 }; |
117 | 129 |
118 State* release() { | 130 State* release() { |
119 State* state = state_; | 131 State* state = state_; |
120 state_ = NULL; | 132 state_ = NULL; |
121 return state; | 133 return state; |
122 } | 134 } |
123 | 135 |
124 State* state_; | 136 State* state_; |
125 }; | 137 }; |
126 | 138 |
127 } // namespace mojo | 139 } // namespace mojo |
128 | 140 |
129 #endif // MOJO_PUBLIC_BINDINGS_REMOTE_PTR_H_ | 141 #endif // MOJO_PUBLIC_BINDINGS_REMOTE_PTR_H_ |
OLD | NEW |