OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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_EDK_SYSTEM_CHANNEL_ENDPOINT_H_ | 5 #ifndef MOJO_EDK_SYSTEM_CHANNEL_ENDPOINT_H_ |
6 #define MOJO_EDK_SYSTEM_CHANNEL_ENDPOINT_H_ | 6 #define MOJO_EDK_SYSTEM_CHANNEL_ENDPOINT_H_ |
7 | 7 |
8 #include "base/macros.h" | 8 #include "base/macros.h" |
9 #include "base/memory/ref_counted.h" | 9 #include "base/memory/ref_counted.h" |
10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
123 unsigned client_port, | 123 unsigned client_port, |
124 MessageInTransitQueue* message_queue = nullptr); | 124 MessageInTransitQueue* message_queue = nullptr); |
125 | 125 |
126 // Methods called by |ChannelEndpointClient|: | 126 // Methods called by |ChannelEndpointClient|: |
127 | 127 |
128 // Called to enqueue an outbound message. (If |AttachAndRun()| has not yet | 128 // Called to enqueue an outbound message. (If |AttachAndRun()| has not yet |
129 // been called, the message will be enqueued and sent when |AttachAndRun()| is | 129 // been called, the message will be enqueued and sent when |AttachAndRun()| is |
130 // called.) | 130 // called.) |
131 bool EnqueueMessage(scoped_ptr<MessageInTransit> message); | 131 bool EnqueueMessage(scoped_ptr<MessageInTransit> message); |
132 | 132 |
| 133 // Called to *replace* current client with a new client (which must differ |
| 134 // from the existing client). This must not be called after |
| 135 // |DetachFromClient()| has been called. |
| 136 // |
| 137 // This returns true in the typical case, and false if this endpoint has been |
| 138 // detached from the channel, in which case the caller should probably call |
| 139 // its (new) client's |OnDetachFromChannel()|. |
| 140 bool ReplaceClient(ChannelEndpointClient* client, unsigned client_port); |
| 141 |
133 // Called before the |ChannelEndpointClient| gives up its reference to this | 142 // Called before the |ChannelEndpointClient| gives up its reference to this |
134 // object. | 143 // object. |
135 void DetachFromClient(); | 144 void DetachFromClient(); |
136 | 145 |
137 // Methods called by |Channel|: | 146 // Methods called by |Channel|: |
138 | 147 |
139 // Called when the |Channel| takes a reference to this object. This will send | 148 // Called when the |Channel| takes a reference to this object. This will send |
140 // all queue messages (in |channel_message_queue_|). | 149 // all queue messages (in |channel_message_queue_|). |
141 // TODO(vtl): Maybe rename this "OnAttach"? | 150 // TODO(vtl): Maybe rename this "OnAttach"? |
142 void AttachAndRun(Channel* channel, | 151 void AttachAndRun(Channel* channel, |
143 ChannelEndpointId local_id, | 152 ChannelEndpointId local_id, |
144 ChannelEndpointId remote_id); | 153 ChannelEndpointId remote_id); |
145 | 154 |
146 // Called when the |Channel| receives a message for the |ChannelEndpoint|. | 155 // Called when the |Channel| receives a message for the |ChannelEndpoint|. |
147 void OnReadMessage(scoped_ptr<MessageInTransit> message); | 156 void OnReadMessage(scoped_ptr<MessageInTransit> message); |
148 | 157 |
149 // Called before the |Channel| gives up its reference to this object. | 158 // Called before the |Channel| gives up its reference to this object. |
150 void DetachFromChannel(); | 159 void DetachFromChannel(); |
151 | 160 |
152 private: | 161 private: |
153 friend class base::RefCountedThreadSafe<ChannelEndpoint>; | 162 friend class base::RefCountedThreadSafe<ChannelEndpoint>; |
154 ~ChannelEndpoint(); | 163 ~ChannelEndpoint(); |
155 | 164 |
156 // Must be called with |lock_| held. | 165 // Must be called with |lock_| held. |
157 bool WriteMessageNoLock(scoped_ptr<MessageInTransit> message); | 166 bool WriteMessageNoLock(scoped_ptr<MessageInTransit> message); |
158 | 167 |
| 168 // Resets |channel_| to null (and sets |is_detached_from_channel_|). This may |
| 169 // only be called if |channel_| is non-null. Must be called with |lock_| held. |
| 170 void ResetChannelNoLock(); |
| 171 |
159 // Protects the members below. | 172 // Protects the members below. |
160 base::Lock lock_; | 173 base::Lock lock_; |
161 | 174 |
162 // |client_| must be valid whenever it is non-null. Before |*client_| gives up | 175 // |client_| must be valid whenever it is non-null. Before |*client_| gives up |
163 // its reference to this object, it must call |DetachFromClient()|. | 176 // its reference to this object, it must call |DetachFromClient()|. |
164 // NOTE: This is a |scoped_refptr<>|, rather than a raw pointer, since the | 177 // NOTE: This is a |scoped_refptr<>|, rather than a raw pointer, since the |
165 // |Channel| needs to keep the |MessagePipe| alive for the "proxy-proxy" case. | 178 // |Channel| needs to keep the |MessagePipe| alive for the "proxy-proxy" case. |
166 // Possibly we'll be able to eliminate that case when we have full | 179 // Possibly we'll be able to eliminate that case when we have full |
167 // multiprocess support. | 180 // multiprocess support. |
168 // WARNING: |ChannelEndpointClient| methods must not be called under |lock_|. | 181 // WARNING: |ChannelEndpointClient| methods must not be called under |lock_|. |
169 // Thus to make such a call, a reference must first be taken under |lock_| and | 182 // Thus to make such a call, a reference must first be taken under |lock_| and |
170 // the lock released. | 183 // the lock released. |
| 184 // WARNING: Beware of interactions with |ReplaceClient()|. By the time the |
| 185 // call is made, the client may have changed. This must be detected and dealt |
| 186 // with. |
171 scoped_refptr<ChannelEndpointClient> client_; | 187 scoped_refptr<ChannelEndpointClient> client_; |
172 unsigned client_port_; | 188 unsigned client_port_; |
173 | 189 |
174 // |channel_| must be valid whenever it is non-null. Before |*channel_| gives | 190 // |channel_| must be valid whenever it is non-null. Before |*channel_| gives |
175 // up its reference to this object, it must call |DetachFromChannel()|. | 191 // up its reference to this object, it must call |DetachFromChannel()|. |
176 // |local_id_| and |remote_id_| are valid if and only |channel_| is non-null. | 192 // |local_id_| and |remote_id_| are valid if and only |channel_| is non-null. |
177 Channel* channel_; | 193 Channel* channel_; |
178 ChannelEndpointId local_id_; | 194 ChannelEndpointId local_id_; |
179 ChannelEndpointId remote_id_; | 195 ChannelEndpointId remote_id_; |
| 196 // This distinguishes the two cases of |channel| being null: not yet attached |
| 197 // versus detached. |
| 198 bool is_detached_from_channel_; |
180 | 199 |
181 // This queue is used before we're running on a channel and ready to send | 200 // This queue is used before we're running on a channel and ready to send |
182 // messages to the channel. | 201 // messages to the channel. |
183 MessageInTransitQueue channel_message_queue_; | 202 MessageInTransitQueue channel_message_queue_; |
184 | 203 |
185 DISALLOW_COPY_AND_ASSIGN(ChannelEndpoint); | 204 DISALLOW_COPY_AND_ASSIGN(ChannelEndpoint); |
186 }; | 205 }; |
187 | 206 |
188 } // namespace system | 207 } // namespace system |
189 } // namespace mojo | 208 } // namespace mojo |
190 | 209 |
191 #endif // MOJO_EDK_SYSTEM_CHANNEL_ENDPOINT_H_ | 210 #endif // MOJO_EDK_SYSTEM_CHANNEL_ENDPOINT_H_ |
OLD | NEW |