Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(596)

Side by Side Diff: mojo/edk/system/channel_endpoint.h

Issue 1396783004: Convert mojo::system::ChannelEndpointClient to use our new refcounting stuff (instead of base's). (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « mojo/edk/system/channel.cc ('k') | mojo/edk/system/channel_endpoint.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 <memory> 8 #include <memory>
9 9
10 #include "base/memory/ref_counted.h"
11 #include "mojo/edk/system/channel_endpoint_id.h" 10 #include "mojo/edk/system/channel_endpoint_id.h"
12 #include "mojo/edk/system/message_in_transit_queue.h" 11 #include "mojo/edk/system/message_in_transit_queue.h"
13 #include "mojo/edk/system/mutex.h" 12 #include "mojo/edk/system/mutex.h"
14 #include "mojo/edk/system/ref_counted.h" 13 #include "mojo/edk/system/ref_counted.h"
14 #include "mojo/edk/system/ref_ptr.h"
15 #include "mojo/public/cpp/system/macros.h" 15 #include "mojo/public/cpp/system/macros.h"
16 16
17 namespace mojo { 17 namespace mojo {
18 namespace system { 18 namespace system {
19 19
20 class Channel; 20 class Channel;
21 class ChannelEndpointClient; 21 class ChannelEndpointClient;
22 class MessageInTransit; 22 class MessageInTransit;
23 23
24 // TODO(vtl): The plan: 24 // TODO(vtl): The plan:
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after
120 // called.) 120 // called.)
121 bool EnqueueMessage(std::unique_ptr<MessageInTransit> message); 121 bool EnqueueMessage(std::unique_ptr<MessageInTransit> message);
122 122
123 // Called to *replace* current client with a new client (which must differ 123 // Called to *replace* current client with a new client (which must differ
124 // from the existing client). This must not be called after 124 // from the existing client). This must not be called after
125 // |DetachFromClient()| has been called. 125 // |DetachFromClient()| has been called.
126 // 126 //
127 // This returns true in the typical case, and false if this endpoint has been 127 // This returns true in the typical case, and false if this endpoint has been
128 // detached from the channel, in which case the caller should probably call 128 // detached from the channel, in which case the caller should probably call
129 // its (new) client's |OnDetachFromChannel()|. 129 // its (new) client's |OnDetachFromChannel()|.
130 bool ReplaceClient(ChannelEndpointClient* client, unsigned client_port); 130 bool ReplaceClient(RefPtr<ChannelEndpointClient>&& client,
131 unsigned client_port);
131 132
132 // Called before the |ChannelEndpointClient| gives up its reference to this 133 // Called before the |ChannelEndpointClient| gives up its reference to this
133 // object. 134 // object.
134 void DetachFromClient(); 135 void DetachFromClient();
135 136
136 // Methods called by |Channel|: 137 // Methods called by |Channel|:
137 138
138 // Called when the |Channel| takes a reference to this object. This will send 139 // Called when the |Channel| takes a reference to this object. This will send
139 // all queue messages (in |channel_message_queue_|). 140 // all queue messages (in |channel_message_queue_|).
140 // TODO(vtl): Maybe rename this "OnAttach"? 141 // TODO(vtl): Maybe rename this "OnAttach"?
(...skipping 12 matching lines...) Expand all
153 FRIEND_MAKE_REF_COUNTED(ChannelEndpoint); 154 FRIEND_MAKE_REF_COUNTED(ChannelEndpoint);
154 155
155 // Constructor for a |ChannelEndpoint| with the given client (specified by 156 // Constructor for a |ChannelEndpoint| with the given client (specified by
156 // |client| and |client_port|). Optionally takes messages from 157 // |client| and |client_port|). Optionally takes messages from
157 // |*message_queue| if |message_queue| is non-null. 158 // |*message_queue| if |message_queue| is non-null.
158 // 159 //
159 // |client| may be null if this endpoint will never need to receive messages, 160 // |client| may be null if this endpoint will never need to receive messages,
160 // in which case |message_queue| should not be null. In that case, this 161 // in which case |message_queue| should not be null. In that case, this
161 // endpoint will simply send queued messages upon being attached to a 162 // endpoint will simply send queued messages upon being attached to a
162 // |Channel| and immediately detach itself. 163 // |Channel| and immediately detach itself.
163 ChannelEndpoint(ChannelEndpointClient* client, 164 ChannelEndpoint(RefPtr<ChannelEndpointClient>&& client,
164 unsigned client_port, 165 unsigned client_port,
165 MessageInTransitQueue* message_queue = nullptr); 166 MessageInTransitQueue* message_queue = nullptr);
166 167
167 friend class base::RefCountedThreadSafe<ChannelEndpoint>;
168 ~ChannelEndpoint(); 168 ~ChannelEndpoint();
169 169
170 bool WriteMessageNoLock(std::unique_ptr<MessageInTransit> message) 170 bool WriteMessageNoLock(std::unique_ptr<MessageInTransit> message)
171 MOJO_EXCLUSIVE_LOCKS_REQUIRED(mutex_); 171 MOJO_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
172 172
173 // Helper for |OnReadMessage()|, handling messages for the client. 173 // Helper for |OnReadMessage()|, handling messages for the client.
174 void OnReadMessageForClient(std::unique_ptr<MessageInTransit> message); 174 void OnReadMessageForClient(std::unique_ptr<MessageInTransit> message);
175 175
176 // Moves |state_| from |RUNNING| to |DEAD|. |channel_| must be non-null, but 176 // Moves |state_| from |RUNNING| to |DEAD|. |channel_| must be non-null, but
177 // this does not call |channel_->DetachEndpoint()|. 177 // this does not call |channel_->DetachEndpoint()|.
178 void DieNoLock() MOJO_EXCLUSIVE_LOCKS_REQUIRED(mutex_); 178 void DieNoLock() MOJO_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
179 179
180 Mutex mutex_; 180 Mutex mutex_;
181 181
182 enum class State { 182 enum class State {
183 // |AttachAndRun()| has not been called yet (|channel_| is null). 183 // |AttachAndRun()| has not been called yet (|channel_| is null).
184 PAUSED, 184 PAUSED,
185 // |AttachAndRun()| has been called, but not |DetachFromChannel()| 185 // |AttachAndRun()| has been called, but not |DetachFromChannel()|
186 // (|channel_| is non-null and valid). 186 // (|channel_| is non-null and valid).
187 RUNNING, 187 RUNNING,
188 // |DetachFromChannel()| has been called (|channel_| is null). 188 // |DetachFromChannel()| has been called (|channel_| is null).
189 DEAD 189 DEAD
190 }; 190 };
191 State state_ MOJO_GUARDED_BY(mutex_); 191 State state_ MOJO_GUARDED_BY(mutex_);
192 192
193 // |client_| must be valid whenever it is non-null. Before |*client_| gives up 193 // |client_| must be valid whenever it is non-null. Before |*client_| gives up
194 // its reference to this object, it must call |DetachFromClient()|. 194 // its reference to this object, it must call |DetachFromClient()|.
195 // NOTE: This is a |scoped_refptr<>|, rather than a raw pointer, since the 195 // NOTE: This is a |RefPtr<>|, rather than a raw pointer, since the |Channel|
196 // |Channel| needs to keep the |MessagePipe| alive for the "proxy-proxy" case. 196 // needs to keep the client (e.g., |MessagePipe|) alive for the "proxy-proxy"
197 // Possibly we'll be able to eliminate that case when we have full 197 // case.
198 // multiprocess support.
199 // WARNING: |ChannelEndpointClient| methods must not be called under |mutex_|. 198 // WARNING: |ChannelEndpointClient| methods must not be called under |mutex_|.
200 // Thus to make such a call, a reference must first be taken under |mutex_| 199 // Thus to make such a call, a reference must first be taken under |mutex_|
201 // and the lock released. 200 // and the lock released.
202 // TODO(vtl): Annotate the above rule using |MOJO_ACQUIRED_{BEFORE,AFTER}()|, 201 // TODO(vtl): Annotate the above rule using |MOJO_ACQUIRED_{BEFORE,AFTER}()|,
203 // once clang actually checks such annotations. 202 // once clang actually checks such annotations.
204 // https://github.com/domokit/mojo/issues/313 203 // https://github.com/domokit/mojo/issues/313
205 // WARNING: Beware of interactions with |ReplaceClient()|. By the time the 204 // WARNING: Beware of interactions with |ReplaceClient()|. By the time the
206 // call is made, the client may have changed. This must be detected and dealt 205 // call is made, the client may have changed. This must be detected and dealt
207 // with. 206 // with.
208 scoped_refptr<ChannelEndpointClient> client_ MOJO_GUARDED_BY(mutex_); 207 RefPtr<ChannelEndpointClient> client_ MOJO_GUARDED_BY(mutex_);
209 unsigned client_port_ MOJO_GUARDED_BY(mutex_); 208 unsigned client_port_ MOJO_GUARDED_BY(mutex_);
210 209
211 // |channel_| must be valid whenever it is non-null. Before |*channel_| gives 210 // |channel_| must be valid whenever it is non-null. Before |*channel_| gives
212 // up its reference to this object, it must call |DetachFromChannel()|. 211 // up its reference to this object, it must call |DetachFromChannel()|.
213 // |local_id_| and |remote_id_| are valid if and only |channel_| is non-null. 212 // |local_id_| and |remote_id_| are valid if and only |channel_| is non-null.
214 Channel* channel_ MOJO_GUARDED_BY(mutex_); 213 Channel* channel_ MOJO_GUARDED_BY(mutex_);
215 ChannelEndpointId local_id_ MOJO_GUARDED_BY(mutex_); 214 ChannelEndpointId local_id_ MOJO_GUARDED_BY(mutex_);
216 ChannelEndpointId remote_id_ MOJO_GUARDED_BY(mutex_); 215 ChannelEndpointId remote_id_ MOJO_GUARDED_BY(mutex_);
217 216
218 // This queue is used before we're running on a channel and ready to send 217 // This queue is used before we're running on a channel and ready to send
219 // messages to the channel. 218 // messages to the channel.
220 MessageInTransitQueue channel_message_queue_ MOJO_GUARDED_BY(mutex_); 219 MessageInTransitQueue channel_message_queue_ MOJO_GUARDED_BY(mutex_);
221 220
222 MOJO_DISALLOW_COPY_AND_ASSIGN(ChannelEndpoint); 221 MOJO_DISALLOW_COPY_AND_ASSIGN(ChannelEndpoint);
223 }; 222 };
224 223
225 } // namespace system 224 } // namespace system
226 } // namespace mojo 225 } // namespace mojo
227 226
228 #endif // MOJO_EDK_SYSTEM_CHANNEL_ENDPOINT_H_ 227 #endif // MOJO_EDK_SYSTEM_CHANNEL_ENDPOINT_H_
OLDNEW
« no previous file with comments | « mojo/edk/system/channel.cc ('k') | mojo/edk/system/channel_endpoint.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698