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

Side by Side Diff: mojo/edk/system/channel.cc

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.h ('k') | mojo/edk/system/channel_endpoint.h » ('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 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 #include "mojo/edk/system/channel.h" 5 #include "mojo/edk/system/channel.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <utility> 8 #include <utility>
9 9
10 #include "base/bind.h" 10 #include "base/bind.h"
(...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after
169 void Channel::SerializeEndpointWithClosedPeer( 169 void Channel::SerializeEndpointWithClosedPeer(
170 void* destination, 170 void* destination,
171 MessageInTransitQueue* message_queue) { 171 MessageInTransitQueue* message_queue) {
172 // We can actually just pass no client to |SerializeEndpointWithLocalPeer()|. 172 // We can actually just pass no client to |SerializeEndpointWithLocalPeer()|.
173 SerializeEndpointWithLocalPeer(destination, message_queue, nullptr, 0); 173 SerializeEndpointWithLocalPeer(destination, message_queue, nullptr, 0);
174 } 174 }
175 175
176 RefPtr<ChannelEndpoint> Channel::SerializeEndpointWithLocalPeer( 176 RefPtr<ChannelEndpoint> Channel::SerializeEndpointWithLocalPeer(
177 void* destination, 177 void* destination,
178 MessageInTransitQueue* message_queue, 178 MessageInTransitQueue* message_queue,
179 ChannelEndpointClient* endpoint_client, 179 RefPtr<ChannelEndpointClient>&& endpoint_client,
180 unsigned endpoint_client_port) { 180 unsigned endpoint_client_port) {
181 DCHECK(destination); 181 DCHECK(destination);
182 // Allow |endpoint_client| to be null, for use by 182 // Allow |endpoint_client| to be null, for use by
183 // |SerializeEndpointWithClosedPeer()|. 183 // |SerializeEndpointWithClosedPeer()|.
184 184
185 auto endpoint = MakeRefCounted<ChannelEndpoint>( 185 auto endpoint = MakeRefCounted<ChannelEndpoint>(
186 endpoint_client, endpoint_client_port, message_queue); 186 std::move(endpoint_client), endpoint_client_port, message_queue);
187 187
188 SerializedEndpoint* s = static_cast<SerializedEndpoint*>(destination); 188 SerializedEndpoint* s = static_cast<SerializedEndpoint*>(destination);
189 s->receiver_endpoint_id = AttachAndRunEndpoint(endpoint.Clone()); 189 s->receiver_endpoint_id = AttachAndRunEndpoint(endpoint.Clone());
190 DVLOG(2) << "Serializing endpoint with local or closed peer (remote ID = " 190 DVLOG(2) << "Serializing endpoint with local or closed peer (remote ID = "
191 << s->receiver_endpoint_id << ")"; 191 << s->receiver_endpoint_id << ")";
192 192
193 return endpoint; 193 return endpoint;
194 } 194 }
195 195
196 void Channel::SerializeEndpointWithRemotePeer( 196 void Channel::SerializeEndpointWithRemotePeer(
197 void* destination, 197 void* destination,
198 MessageInTransitQueue* message_queue, 198 MessageInTransitQueue* message_queue,
199 RefPtr<ChannelEndpoint>&& peer_endpoint) { 199 RefPtr<ChannelEndpoint>&& peer_endpoint) {
200 DCHECK(destination); 200 DCHECK(destination);
201 DCHECK(peer_endpoint); 201 DCHECK(peer_endpoint);
202 202
203 DLOG(WARNING) << "Direct message pipe passing across multiple channels not " 203 DLOG(WARNING) << "Direct message pipe passing across multiple channels not "
204 "yet implemented; will proxy"; 204 "yet implemented; will proxy";
205 // Create and set up an |EndpointRelayer| to proxy. 205 // Create and set up an |EndpointRelayer| to proxy.
206 // TODO(vtl): If we were to own/track the relayer directly (rather than owning 206 // TODO(vtl): If we were to own/track the relayer directly (rather than owning
207 // it via its |ChannelEndpoint|s), then we might be able to make 207 // it via its |ChannelEndpoint|s), then we might be able to make
208 // |ChannelEndpoint|'s |client_| pointer a raw pointer. 208 // |ChannelEndpoint|'s |client_| pointer a raw pointer.
209 scoped_refptr<EndpointRelayer> relayer(new EndpointRelayer()); 209 auto relayer = MakeRefCounted<EndpointRelayer>();
210 auto endpoint = 210 auto endpoint =
211 MakeRefCounted<ChannelEndpoint>(relayer.get(), 0, message_queue); 211 MakeRefCounted<ChannelEndpoint>(relayer.Clone(), 0, message_queue);
212 relayer->Init(endpoint.Clone(), peer_endpoint.Clone()); 212 relayer->Init(endpoint.Clone(), peer_endpoint.Clone());
213 peer_endpoint->ReplaceClient(relayer.get(), 1); 213 peer_endpoint->ReplaceClient(std::move(relayer), 1);
214 214
215 SerializedEndpoint* s = static_cast<SerializedEndpoint*>(destination); 215 SerializedEndpoint* s = static_cast<SerializedEndpoint*>(destination);
216 s->receiver_endpoint_id = AttachAndRunEndpoint(std::move(endpoint)); 216 s->receiver_endpoint_id = AttachAndRunEndpoint(std::move(endpoint));
217 DVLOG(2) << "Serializing endpoint with remote peer (remote ID = " 217 DVLOG(2) << "Serializing endpoint with remote peer (remote ID = "
218 << s->receiver_endpoint_id << ")"; 218 << s->receiver_endpoint_id << ")";
219 } 219 }
220 220
221 scoped_refptr<IncomingEndpoint> Channel::DeserializeEndpoint( 221 RefPtr<IncomingEndpoint> Channel::DeserializeEndpoint(const void* source) {
222 const void* source) {
223 const SerializedEndpoint* s = static_cast<const SerializedEndpoint*>(source); 222 const SerializedEndpoint* s = static_cast<const SerializedEndpoint*>(source);
224 ChannelEndpointId local_id = s->receiver_endpoint_id; 223 ChannelEndpointId local_id = s->receiver_endpoint_id;
225 // No need to check the validity of |local_id| -- if it's not valid, it simply 224 // No need to check the validity of |local_id| -- if it's not valid, it simply
226 // won't be in |incoming_endpoints_|. 225 // won't be in |incoming_endpoints_|.
227 DVLOG_IF(2, !local_id.is_valid() || !local_id.is_remote()) 226 DVLOG_IF(2, !local_id.is_valid() || !local_id.is_remote())
228 << "Attempt to get incoming endpoint for invalid ID " << local_id; 227 << "Attempt to get incoming endpoint for invalid ID " << local_id;
229 228
230 MutexLocker locker(&mutex_); 229 MutexLocker locker(&mutex_);
231 230
232 auto it = incoming_endpoints_.find(local_id); 231 auto it = incoming_endpoints_.find(local_id);
233 if (it == incoming_endpoints_.end()) { 232 if (it == incoming_endpoints_.end()) {
234 LOG(ERROR) << "Failed to deserialize endpoint (ID = " << local_id << ")"; 233 LOG(ERROR) << "Failed to deserialize endpoint (ID = " << local_id << ")";
235 return nullptr; 234 return nullptr;
236 } 235 }
237 236
238 DVLOG(2) << "Deserializing endpoint (new local ID = " << local_id << ")"; 237 DVLOG(2) << "Deserializing endpoint (new local ID = " << local_id << ")";
239 238
240 scoped_refptr<IncomingEndpoint> rv; 239 RefPtr<IncomingEndpoint> rv = std::move(it->second);
241 rv.swap(it->second);
242 incoming_endpoints_.erase(it); 240 incoming_endpoints_.erase(it);
243 return rv; 241 return rv;
244 } 242 }
245 243
246 size_t Channel::GetSerializedPlatformHandleSize() const { 244 size_t Channel::GetSerializedPlatformHandleSize() const {
247 // TODO(vtl): Having to lock |mutex_| here is a bit unfortunate. Maybe we 245 // TODO(vtl): Having to lock |mutex_| here is a bit unfortunate. Maybe we
248 // should get the size in |Init()| and cache it? 246 // should get the size in |Init()| and cache it?
249 MutexLocker locker(&mutex_); 247 MutexLocker locker(&mutex_);
250 return raw_channel_->GetSerializedPlatformHandleSize(); 248 return raw_channel_->GetSerializedPlatformHandleSize();
251 } 249 }
(...skipping 210 matching lines...) Expand 10 before | Expand all | Expand 10 after
462 } 460 }
463 461
464 // Conversely, the remote end should be "local". 462 // Conversely, the remote end should be "local".
465 if (!remote_id.is_valid() || remote_id.is_remote()) { 463 if (!remote_id.is_valid() || remote_id.is_remote()) {
466 DVLOG(2) << "Received attach and run endpoint with invalid remote ID"; 464 DVLOG(2) << "Received attach and run endpoint with invalid remote ID";
467 return false; 465 return false;
468 } 466 }
469 467
470 // Create/initialize an |IncomingEndpoint| and thus an endpoint (outside the 468 // Create/initialize an |IncomingEndpoint| and thus an endpoint (outside the
471 // lock). 469 // lock).
472 scoped_refptr<IncomingEndpoint> incoming_endpoint(new IncomingEndpoint()); 470 auto incoming_endpoint = MakeRefCounted<IncomingEndpoint>();
473 RefPtr<ChannelEndpoint> endpoint = incoming_endpoint->Init(); 471 RefPtr<ChannelEndpoint> endpoint = incoming_endpoint->Init();
474 472
475 bool success = true; 473 bool success = true;
476 { 474 {
477 MutexLocker locker(&mutex_); 475 MutexLocker locker(&mutex_);
478 476
479 if (local_id_to_endpoint_map_.find(local_id) == 477 if (local_id_to_endpoint_map_.find(local_id) ==
480 local_id_to_endpoint_map_.end()) { 478 local_id_to_endpoint_map_.end()) {
481 DCHECK(incoming_endpoints_.find(local_id) == incoming_endpoints_.end()); 479 DCHECK(incoming_endpoints_.find(local_id) == incoming_endpoints_.end());
482 480
(...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after
622 << ", local ID " << local_id << ", remote ID " << remote_id; 620 << ", local ID " << local_id << ", remote ID " << remote_id;
623 std::unique_ptr<MessageInTransit> message(new MessageInTransit( 621 std::unique_ptr<MessageInTransit> message(new MessageInTransit(
624 MessageInTransit::Type::CHANNEL, subtype, num_bytes, bytes)); 622 MessageInTransit::Type::CHANNEL, subtype, num_bytes, bytes));
625 message->set_source_id(local_id); 623 message->set_source_id(local_id);
626 message->set_destination_id(remote_id); 624 message->set_destination_id(remote_id);
627 return WriteMessage(std::move(message)); 625 return WriteMessage(std::move(message));
628 } 626 }
629 627
630 } // namespace system 628 } // namespace system
631 } // namespace mojo 629 } // namespace mojo
OLDNEW
« no previous file with comments | « mojo/edk/system/channel.h ('k') | mojo/edk/system/channel_endpoint.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698