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 void ChannelEndpoint::TakeMessages(MessageInTransitQueue* message_queue) { |
| 26 DCHECK(paused_message_queue_.IsEmpty()); |
| 27 paused_message_queue_.Swap(message_queue); |
| 28 } |
| 29 |
25 bool ChannelEndpoint::EnqueueMessage(scoped_ptr<MessageInTransit> message) { | 30 bool ChannelEndpoint::EnqueueMessage(scoped_ptr<MessageInTransit> message) { |
26 DCHECK(message); | 31 DCHECK(message); |
27 | 32 |
28 base::AutoLock locker(lock_); | 33 base::AutoLock locker(lock_); |
29 | 34 |
30 if (!channel_) { | 35 if (!channel_ || remote_id_ == MessageInTransit::kInvalidEndpointId) { |
31 // Generally, this should only happen if the channel is shut down for some | 36 // We may reach here if we haven't been attached or run yet. |
32 // reason (with live message pipes on it). | 37 // TODO(vtl): We may also reach here if the channel is shut down early for |
33 return false; | 38 // some reason (with live message pipes on it). We can't check |state_| yet, |
| 39 // until it's protected under lock, but in this case we should return false |
| 40 // (and not enqueue any messages). |
| 41 paused_message_queue_.AddMessage(message.Pass()); |
| 42 return true; |
34 } | 43 } |
35 | 44 |
36 // TODO(vtl): Currently, this only works in the "running" case. | 45 // TODO(vtl): Currently, this only works in the "running" case. |
37 DCHECK_NE(remote_id_, MessageInTransit::kInvalidEndpointId); | 46 DCHECK_NE(remote_id_, MessageInTransit::kInvalidEndpointId); |
38 | 47 |
39 return WriteMessageNoLock(message.Pass()); | 48 return WriteMessageNoLock(message.Pass()); |
40 } | 49 } |
41 | 50 |
42 void ChannelEndpoint::DetachFromMessagePipe() { | 51 void ChannelEndpoint::DetachFromMessagePipe() { |
43 // TODO(vtl): Once |message_pipe_| is under |lock_|, we should null it out | 52 // TODO(vtl): Once |message_pipe_| is under |lock_|, we should null it out |
(...skipping 26 matching lines...) Expand all Loading... |
70 local_id_ = local_id; | 79 local_id_ = local_id; |
71 } | 80 } |
72 | 81 |
73 void ChannelEndpoint::Run(MessageInTransit::EndpointId remote_id) { | 82 void ChannelEndpoint::Run(MessageInTransit::EndpointId remote_id) { |
74 DCHECK_NE(remote_id, MessageInTransit::kInvalidEndpointId); | 83 DCHECK_NE(remote_id, MessageInTransit::kInvalidEndpointId); |
75 | 84 |
76 base::AutoLock locker(lock_); | 85 base::AutoLock locker(lock_); |
77 DCHECK(channel_); | 86 DCHECK(channel_); |
78 DCHECK_EQ(remote_id_, MessageInTransit::kInvalidEndpointId); | 87 DCHECK_EQ(remote_id_, MessageInTransit::kInvalidEndpointId); |
79 remote_id_ = remote_id; | 88 remote_id_ = remote_id; |
| 89 |
| 90 while (!paused_message_queue_.IsEmpty()) { |
| 91 LOG_IF(WARNING, !WriteMessageNoLock(paused_message_queue_.GetMessage())) |
| 92 << "Failed to write enqueue message to channel"; |
| 93 } |
80 } | 94 } |
81 | 95 |
82 void ChannelEndpoint::DetachFromChannel() { | 96 void ChannelEndpoint::DetachFromChannel() { |
83 base::AutoLock locker(lock_); | 97 base::AutoLock locker(lock_); |
84 DCHECK(channel_); | 98 DCHECK(channel_); |
85 DCHECK_NE(local_id_, MessageInTransit::kInvalidEndpointId); | 99 DCHECK_NE(local_id_, MessageInTransit::kInvalidEndpointId); |
86 // TODO(vtl): Once we combine "run" into "attach", |remote_id_| should valid | 100 // TODO(vtl): Once we combine "run" into "attach", |remote_id_| should valid |
87 // here as well. | 101 // here as well. |
88 channel_ = nullptr; | 102 channel_ = nullptr; |
89 local_id_ = MessageInTransit::kInvalidEndpointId; | 103 local_id_ = MessageInTransit::kInvalidEndpointId; |
(...skipping 16 matching lines...) Expand all Loading... |
106 DCHECK_NE(remote_id_, MessageInTransit::kInvalidEndpointId); | 120 DCHECK_NE(remote_id_, MessageInTransit::kInvalidEndpointId); |
107 | 121 |
108 message->SerializeAndCloseDispatchers(channel_); | 122 message->SerializeAndCloseDispatchers(channel_); |
109 message->set_source_id(local_id_); | 123 message->set_source_id(local_id_); |
110 message->set_destination_id(remote_id_); | 124 message->set_destination_id(remote_id_); |
111 return channel_->WriteMessage(message.Pass()); | 125 return channel_->WriteMessage(message.Pass()); |
112 } | 126 } |
113 | 127 |
114 } // namespace system | 128 } // namespace system |
115 } // namespace mojo | 129 } // namespace mojo |
OLD | NEW |