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) { | 25 bool ChannelEndpoint::EnqueueMessage(scoped_ptr<MessageInTransit> message) { |
26 DCHECK(message); | 26 DCHECK(message); |
27 | 27 |
28 base::AutoLock locker(lock_); | 28 base::AutoLock locker(lock_); |
| 29 |
29 if (!channel_) { | 30 if (!channel_) { |
30 // Generally, this should only happen if the channel is shut down for some | 31 // Generally, this should only happen if the channel is shut down for some |
31 // reason (with live message pipes on it). | 32 // reason (with live message pipes on it). |
32 return false; | 33 return false; |
33 } | 34 } |
34 DCHECK_NE(local_id_, MessageInTransit::kInvalidEndpointId); | 35 |
35 // TODO(vtl): Currently, we only support enqueueing messages when we're | 36 // TODO(vtl): Currently, this only works in the "running" case. |
36 // "running". | |
37 DCHECK_NE(remote_id_, MessageInTransit::kInvalidEndpointId); | 37 DCHECK_NE(remote_id_, MessageInTransit::kInvalidEndpointId); |
38 | 38 |
39 message->SerializeAndCloseDispatchers(channel_); | 39 return WriteMessageNoLock(message.Pass()); |
40 message->set_source_id(local_id_); | |
41 message->set_destination_id(remote_id_); | |
42 return channel_->WriteMessage(message.Pass()); | |
43 } | 40 } |
44 | 41 |
45 void ChannelEndpoint::DetachFromMessagePipe() { | 42 void ChannelEndpoint::DetachFromMessagePipe() { |
46 // TODO(vtl): Once |message_pipe_| is under |lock_|, we should null it out | 43 // 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. | 44 // here. For now, get the channel to do so for us. |
48 | 45 |
49 scoped_refptr<Channel> channel; | 46 scoped_refptr<Channel> channel; |
50 { | 47 { |
51 base::AutoLock locker(lock_); | 48 base::AutoLock locker(lock_); |
52 if (!channel_) | 49 if (!channel_) |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
92 local_id_ = MessageInTransit::kInvalidEndpointId; | 89 local_id_ = MessageInTransit::kInvalidEndpointId; |
93 remote_id_ = MessageInTransit::kInvalidEndpointId; | 90 remote_id_ = MessageInTransit::kInvalidEndpointId; |
94 } | 91 } |
95 | 92 |
96 ChannelEndpoint::~ChannelEndpoint() { | 93 ChannelEndpoint::~ChannelEndpoint() { |
97 DCHECK(!channel_); | 94 DCHECK(!channel_); |
98 DCHECK_EQ(local_id_, MessageInTransit::kInvalidEndpointId); | 95 DCHECK_EQ(local_id_, MessageInTransit::kInvalidEndpointId); |
99 DCHECK_EQ(remote_id_, MessageInTransit::kInvalidEndpointId); | 96 DCHECK_EQ(remote_id_, MessageInTransit::kInvalidEndpointId); |
100 } | 97 } |
101 | 98 |
| 99 bool ChannelEndpoint::WriteMessageNoLock(scoped_ptr<MessageInTransit> message) { |
| 100 DCHECK(message); |
| 101 |
| 102 lock_.AssertAcquired(); |
| 103 |
| 104 DCHECK(channel_); |
| 105 DCHECK_NE(local_id_, MessageInTransit::kInvalidEndpointId); |
| 106 DCHECK_NE(remote_id_, MessageInTransit::kInvalidEndpointId); |
| 107 |
| 108 message->SerializeAndCloseDispatchers(channel_); |
| 109 message->set_source_id(local_id_); |
| 110 message->set_destination_id(remote_id_); |
| 111 return channel_->WriteMessage(message.Pass()); |
| 112 } |
| 113 |
102 } // namespace system | 114 } // namespace system |
103 } // namespace mojo | 115 } // namespace mojo |
OLD | NEW |