OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "mojo/system/proxy_message_pipe_endpoint.h" | |
6 | |
7 #include <string.h> | |
8 | |
9 #include "base/logging.h" | |
10 #include "base/stl_util.h" | |
11 #include "mojo/system/channel.h" | |
12 | |
13 namespace mojo { | |
14 namespace system { | |
15 | |
16 ProxyMessagePipeEndpoint::ProxyMessagePipeEndpoint() | |
17 : local_id_(MessageInTransit::kInvalidEndpointId), | |
18 remote_id_(MessageInTransit::kInvalidEndpointId), | |
19 is_open_(true), | |
20 is_peer_open_(true) { | |
21 } | |
22 | |
23 ProxyMessagePipeEndpoint::~ProxyMessagePipeEndpoint() { | |
24 DCHECK(!is_running()); | |
25 DCHECK(!is_attached()); | |
26 AssertConsistentState(); | |
27 DCHECK(paused_message_queue_.empty()); | |
28 } | |
29 | |
30 void ProxyMessagePipeEndpoint::Close() { | |
31 DCHECK(is_open_); | |
32 is_open_ = false; | |
33 | |
34 DCHECK(is_attached()); | |
35 channel_->DetachMessagePipeEndpoint(local_id_); | |
36 channel_ = NULL; | |
37 local_id_ = MessageInTransit::kInvalidEndpointId; | |
38 remote_id_ = MessageInTransit::kInvalidEndpointId; | |
39 | |
40 for (std::deque<MessageInTransit*>::iterator it = | |
41 paused_message_queue_.begin(); | |
42 it != paused_message_queue_.end(); | |
43 ++it) { | |
44 (*it)->Destroy(); | |
45 } | |
46 paused_message_queue_.clear(); | |
47 } | |
48 | |
49 bool ProxyMessagePipeEndpoint::OnPeerClose() { | |
50 DCHECK(is_open_); | |
51 DCHECK(is_peer_open_); | |
52 | |
53 is_peer_open_ = false; | |
54 if (EnqueueMessage(MessageInTransit::Create( | |
55 MessageInTransit::TYPE_MESSAGE_PIPE, | |
56 MessageInTransit::SUBTYPE_MESSAGE_PIPE_PEER_CLOSED, | |
57 NULL, 0)) != MOJO_RESULT_OK) { | |
58 // TODO(vtl): Do something more sensible on error here? | |
59 LOG(WARNING) << "Failed to send peer closed control message"; | |
60 } | |
61 | |
62 // Return false -- to indicate that we should be destroyed -- if no messages | |
63 // are still enqueued. (Messages may still be enqueued if we're not running | |
64 // yet, but our peer was closed.) | |
65 return !paused_message_queue_.empty(); | |
66 } | |
67 | |
68 MojoResult ProxyMessagePipeEndpoint::EnqueueMessage(MessageInTransit* message) { | |
69 DCHECK(is_open_); | |
70 // If the our (local) peer isn't open, we should only be enqueueing our own | |
71 // control messages. | |
72 DCHECK(is_peer_open_ || | |
73 (message->type() == MessageInTransit::TYPE_MESSAGE_PIPE)); | |
darin (slow to review)
2013/11/06 18:26:00
perhaps it would be gratuitous to also check that
viettrungluu
2013/11/06 21:13:39
I'm not asserting that because there may be more c
| |
74 | |
75 MojoResult rv = MOJO_RESULT_OK; | |
76 | |
77 if (is_running()) { | |
78 message->set_source_id(local_id_); | |
79 message->set_destination_id(remote_id_); | |
80 if (!channel_->WriteMessage(message)) | |
81 rv = MOJO_RESULT_FAILED_PRECONDITION; | |
darin (slow to review)
2013/11/06 18:26:00
What does this error code mean in this context? It
viettrungluu
2013/11/06 21:13:39
Yes, and it indicates that this won't succeed with
| |
82 } else { | |
83 paused_message_queue_.push_back(message); | |
84 } | |
85 | |
86 return rv; | |
87 } | |
88 | |
89 void ProxyMessagePipeEndpoint::Attach(scoped_refptr<Channel> channel, | |
90 MessageInTransit::EndpointId local_id) { | |
91 DCHECK(channel.get()); | |
92 DCHECK_NE(local_id, MessageInTransit::kInvalidEndpointId); | |
93 | |
94 DCHECK(!is_attached()); | |
95 | |
96 AssertConsistentState(); | |
97 channel_ = channel; | |
98 local_id_ = local_id; | |
99 AssertConsistentState(); | |
100 } | |
101 | |
102 bool ProxyMessagePipeEndpoint::Run(MessageInTransit::EndpointId remote_id) { | |
103 // Assertions about arguments: | |
104 DCHECK_NE(remote_id, MessageInTransit::kInvalidEndpointId); | |
105 | |
106 // Assertions about current state: | |
107 DCHECK(is_attached()); | |
108 DCHECK(!is_running()); | |
109 | |
110 AssertConsistentState(); | |
111 remote_id_ = remote_id; | |
112 AssertConsistentState(); | |
113 | |
114 MojoResult result = MOJO_RESULT_OK; | |
115 for (std::deque<MessageInTransit*>::iterator it = | |
116 paused_message_queue_.begin(); | |
117 it != paused_message_queue_.end(); | |
118 ++it) { | |
119 result = EnqueueMessage(*it); | |
120 if (result != MOJO_RESULT_OK) { | |
121 // TODO(vtl): Do something more sensible on error here? | |
122 LOG(WARNING) << "Failed to send message"; | |
123 } | |
124 } | |
125 paused_message_queue_.clear(); | |
126 | |
127 // If the peer is not open, we should return false since we should be | |
128 // destroyed. | |
129 return is_peer_open_; | |
130 } | |
131 | |
132 #ifndef NDEBUG | |
133 void ProxyMessagePipeEndpoint::AssertConsistentState() const { | |
134 if (is_attached()) { | |
135 DCHECK_NE(local_id_, MessageInTransit::kInvalidEndpointId); | |
136 } else { // Not attached. | |
137 DCHECK_EQ(local_id_, MessageInTransit::kInvalidEndpointId); | |
138 DCHECK_EQ(remote_id_, MessageInTransit::kInvalidEndpointId); | |
139 } | |
140 } | |
141 #endif | |
142 | |
143 } // namespace system | |
144 } // namespace mojo | |
OLD | NEW |