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 #include "mojo/system/channel_endpoint.h" | 5 #include "mojo/system/channel_endpoint.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "mojo/system/channel.h" | 8 #include "mojo/system/channel.h" |
9 #include "mojo/system/message_pipe.h" | 9 #include "mojo/system/message_pipe.h" |
10 | 10 |
11 namespace mojo { | 11 namespace mojo { |
12 namespace system { | 12 namespace system { |
13 | 13 |
14 ChannelEndpoint::ChannelEndpoint(MessagePipe* message_pipe, unsigned port) | 14 ChannelEndpoint::ChannelEndpoint(MessagePipe* message_pipe, unsigned port) |
15 : state_(STATE_NORMAL), | 15 : state_(STATE_NORMAL), |
16 message_pipe_(message_pipe), | 16 message_pipe_(message_pipe), |
17 port_(port), | 17 port_(port), |
18 channel_(), | 18 channel_(), |
19 local_id_(MessageInTransit::kInvalidEndpointId), | 19 local_id_(MessageInTransit::kInvalidEndpointId), |
20 remote_id_(MessageInTransit::kInvalidEndpointId) { | 20 remote_id_(MessageInTransit::kInvalidEndpointId) { |
21 DCHECK(message_pipe_.get()); | 21 DCHECK(message_pipe_.get()); |
22 DCHECK(port_ == 0 || port_ == 1); | 22 DCHECK(port_ == 0 || port_ == 1); |
23 } | 23 } |
24 | 24 |
| 25 bool ChannelEndpoint::EnqueueMessage(scoped_ptr<MessageInTransit> message) { |
| 26 DCHECK(message); |
| 27 |
| 28 base::AutoLock locker(lock_); |
| 29 if (!channel_) { |
| 30 // Generally, this should only happen if the channel is shut down for some |
| 31 // reason (with live message pipes on it). |
| 32 return false; |
| 33 } |
| 34 DCHECK_NE(local_id_, MessageInTransit::kInvalidEndpointId); |
| 35 // TODO(vtl): Currently, we only support enqueueing messages when we're |
| 36 // "running". |
| 37 DCHECK_NE(remote_id_, MessageInTransit::kInvalidEndpointId); |
| 38 |
| 39 message->SerializeAndCloseDispatchers(channel_); |
| 40 message->set_source_id(local_id_); |
| 41 message->set_destination_id(remote_id_); |
| 42 return channel_->WriteMessage(message.Pass()); |
| 43 } |
| 44 |
| 45 void ChannelEndpoint::DetachFromMessagePipe() { |
| 46 // TODO(vtl): Once |message_pipe_| is under |lock_|, we should null it out |
| 47 // here. For now, get the channel to do so for us. |
| 48 |
| 49 scoped_refptr<Channel> channel; |
| 50 { |
| 51 base::AutoLock locker(lock_); |
| 52 if (!channel_) |
| 53 return; |
| 54 DCHECK_NE(local_id_, MessageInTransit::kInvalidEndpointId); |
| 55 // TODO(vtl): Once we combine "run" into "attach", |remote_id_| should valid |
| 56 // here as well. |
| 57 channel = channel_; |
| 58 } |
| 59 // Don't call this under |lock_|, since it'll call us back. |
| 60 // TODO(vtl): This seems pretty suboptimal. |
| 61 channel->DetachMessagePipeEndpoint(local_id_, remote_id_); |
| 62 } |
| 63 |
25 void ChannelEndpoint::AttachToChannel(Channel* channel, | 64 void ChannelEndpoint::AttachToChannel(Channel* channel, |
26 MessageInTransit::EndpointId local_id) { | 65 MessageInTransit::EndpointId local_id) { |
27 DCHECK(channel); | 66 DCHECK(channel); |
28 DCHECK_NE(local_id, MessageInTransit::kInvalidEndpointId); | 67 DCHECK_NE(local_id, MessageInTransit::kInvalidEndpointId); |
29 | 68 |
30 base::AutoLock locker(lock_); | 69 base::AutoLock locker(lock_); |
31 DCHECK(!channel_); | 70 DCHECK(!channel_); |
32 DCHECK_EQ(local_id_, MessageInTransit::kInvalidEndpointId); | 71 DCHECK_EQ(local_id_, MessageInTransit::kInvalidEndpointId); |
33 channel_ = channel; | 72 channel_ = channel; |
34 local_id_ = local_id; | 73 local_id_ = local_id; |
(...skipping 20 matching lines...) Expand all Loading... |
55 } | 94 } |
56 | 95 |
57 ChannelEndpoint::~ChannelEndpoint() { | 96 ChannelEndpoint::~ChannelEndpoint() { |
58 DCHECK(!channel_); | 97 DCHECK(!channel_); |
59 DCHECK_EQ(local_id_, MessageInTransit::kInvalidEndpointId); | 98 DCHECK_EQ(local_id_, MessageInTransit::kInvalidEndpointId); |
60 DCHECK_EQ(remote_id_, MessageInTransit::kInvalidEndpointId); | 99 DCHECK_EQ(remote_id_, MessageInTransit::kInvalidEndpointId); |
61 } | 100 } |
62 | 101 |
63 } // namespace system | 102 } // namespace system |
64 } // namespace mojo | 103 } // namespace mojo |
OLD | NEW |