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

Unified Diff: mojo/system/channel.cc

Issue 584723002: Mojo: Add Channel::AttachEndpoint(), which takes a ChannelEndpoint. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 3 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « mojo/system/channel.h ('k') | mojo/system/channel_endpoint.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: mojo/system/channel.cc
diff --git a/mojo/system/channel.cc b/mojo/system/channel.cc
index 98dc511668e31f02987d84c3dab5b7534b1afe10..047fac54d275659cbb5e15d3e973d5a6be208c53 100644
--- a/mojo/system/channel.cc
+++ b/mojo/system/channel.cc
@@ -92,13 +92,14 @@ void Channel::WillShutdownSoon() {
is_shutting_down_ = true;
}
-MessageInTransit::EndpointId Channel::AttachMessagePipeEndpoint(
- scoped_refptr<MessagePipe> message_pipe,
- unsigned port) {
- DCHECK(message_pipe.get());
- DCHECK(port == 0 || port == 1);
+// Note: |endpoint| being a |scoped_refptr| makes this function safe, since it
+// keeps the endpoint alive even after the lock is released. Otherwise, there's
+// the temptation to simply pass the result of |new ChannelEndpoint(...)|
+// directly to this function, which wouldn't be sufficient for safety.
+MessageInTransit::EndpointId Channel::AttachEndpoint(
+ scoped_refptr<ChannelEndpoint> endpoint) {
+ DCHECK(endpoint.get());
- scoped_refptr<ChannelEndpoint> endpoint;
MessageInTransit::EndpointId local_id;
{
base::AutoLock locker(lock_);
@@ -113,13 +114,12 @@ MessageInTransit::EndpointId Channel::AttachMessagePipeEndpoint(
local_id = next_local_id_;
next_local_id_++;
- endpoint = new ChannelEndpoint(message_pipe.get(), port);
local_id_to_endpoint_map_[local_id] = endpoint;
}
endpoint->AttachToChannel(this, local_id);
// This might fail if that port got an |OnPeerClose()| before attaching.
- if (message_pipe->Attach(port, endpoint.get()))
+ if (endpoint->message_pipe_->Attach(endpoint->port_, endpoint.get()))
return local_id;
// Note: If it failed, quite possibly the endpoint info was removed from that
@@ -130,8 +130,8 @@ MessageInTransit::EndpointId Channel::AttachMessagePipeEndpoint(
base::AutoLock locker(lock_);
IdToEndpointMap::iterator it = local_id_to_endpoint_map_.find(local_id);
if (it != local_id_to_endpoint_map_.end() &&
- it->second->message_pipe_.get() == message_pipe.get() &&
- it->second->port_ == port) {
+ it->second->message_pipe_.get() == endpoint->message_pipe_.get() &&
+ it->second->port_ == endpoint->port_) {
DCHECK_EQ(it->second->state_, ChannelEndpoint::STATE_NORMAL);
// TODO(vtl): FIXME -- This is wrong. We need to specify (to
// |AttachMessagePipeEndpoint()| who's going to be responsible for calling
@@ -146,6 +146,16 @@ MessageInTransit::EndpointId Channel::AttachMessagePipeEndpoint(
return MessageInTransit::kInvalidEndpointId;
}
+MessageInTransit::EndpointId Channel::AttachMessagePipeEndpoint(
+ scoped_refptr<MessagePipe> message_pipe,
+ unsigned port) {
+ DCHECK(message_pipe.get());
+ DCHECK(port == 0 || port == 1);
+
+ return AttachEndpoint(
+ make_scoped_refptr(new ChannelEndpoint(message_pipe.get(), port)));
+}
+
bool Channel::RunMessagePipeEndpoint(MessageInTransit::EndpointId local_id,
MessageInTransit::EndpointId remote_id) {
scoped_refptr<ChannelEndpoint> endpoint;
« no previous file with comments | « mojo/system/channel.h ('k') | mojo/system/channel_endpoint.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698