OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 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/proxy_message_pipe_endpoint.h" | 5 #include "mojo/system/proxy_message_pipe_endpoint.h" |
6 | 6 |
7 #include <string.h> | 7 #include <string.h> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/stl_util.h" | 10 #include "base/stl_util.h" |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
44 (*it)->Destroy(); | 44 (*it)->Destroy(); |
45 } | 45 } |
46 paused_message_queue_.clear(); | 46 paused_message_queue_.clear(); |
47 } | 47 } |
48 | 48 |
49 bool ProxyMessagePipeEndpoint::OnPeerClose() { | 49 bool ProxyMessagePipeEndpoint::OnPeerClose() { |
50 DCHECK(is_open_); | 50 DCHECK(is_open_); |
51 DCHECK(is_peer_open_); | 51 DCHECK(is_peer_open_); |
52 | 52 |
53 is_peer_open_ = false; | 53 is_peer_open_ = false; |
54 if (EnqueueMessage(MessageInTransit::Create( | 54 MessageInTransit* message = |
55 MessageInTransit::kTypeMessagePipe, | 55 MessageInTransit::Create(MessageInTransit::kTypeMessagePipe, |
56 MessageInTransit::kSubtypeMessagePipePeerClosed, | 56 MessageInTransit::kSubtypeMessagePipePeerClosed, |
57 NULL, 0), NULL) != MOJO_RESULT_OK) { | 57 NULL, 0); |
| 58 if (CanEnqueueMessage(message, NULL) == MOJO_RESULT_OK) { |
| 59 EnqueueMessage(message, NULL); |
| 60 } else { |
| 61 message->Destroy(); |
58 // TODO(vtl): Do something more sensible on error here? | 62 // TODO(vtl): Do something more sensible on error here? |
59 LOG(WARNING) << "Failed to send peer closed control message"; | 63 LOG(WARNING) << "Failed to send peer closed control message"; |
60 } | 64 } |
61 | 65 |
62 // Return false -- to indicate that we should be destroyed -- if no messages | 66 // 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 | 67 // are still enqueued. (Messages may still be enqueued if we're not running |
64 // yet, but our peer was closed.) | 68 // yet, but our peer was closed.) |
65 return !paused_message_queue_.empty(); | 69 return !paused_message_queue_.empty(); |
66 } | 70 } |
67 | 71 |
68 MojoResult ProxyMessagePipeEndpoint::EnqueueMessage( | 72 MojoResult ProxyMessagePipeEndpoint::CanEnqueueMessage( |
| 73 const MessageInTransit* /*message*/, |
| 74 const std::vector<Dispatcher*>* dispatchers) { |
| 75 // TODO(vtl): Support sending handles over OS pipes. |
| 76 if (dispatchers) { |
| 77 NOTIMPLEMENTED(); |
| 78 return MOJO_RESULT_UNIMPLEMENTED; |
| 79 } |
| 80 return MOJO_RESULT_OK; |
| 81 } |
| 82 |
| 83 void ProxyMessagePipeEndpoint::EnqueueMessage( |
69 MessageInTransit* message, | 84 MessageInTransit* message, |
70 const std::vector<Dispatcher*>* dispatchers) { | 85 std::vector<scoped_refptr<Dispatcher> >* dispatchers) { |
71 DCHECK(is_open_); | 86 DCHECK(is_open_); |
72 // If our (local) peer isn't open, we should only be enqueueing our own | 87 // If our (local) peer isn't open, we should only be enqueueing our own |
73 // control messages. | 88 // control messages. |
74 DCHECK(is_peer_open_ || | 89 DCHECK(is_peer_open_ || |
75 (message->type() == MessageInTransit::kTypeMessagePipe)); | 90 (message->type() == MessageInTransit::kTypeMessagePipe)); |
76 | 91 |
77 // TODO(vtl): Support sending handles over OS pipes. | 92 // TODO(vtl) |
78 if (dispatchers) { | 93 DCHECK(!dispatchers || dispatchers->empty()); |
79 message->Destroy(); | |
80 NOTIMPLEMENTED(); | |
81 return MOJO_RESULT_UNIMPLEMENTED; | |
82 } | |
83 | |
84 MojoResult rv = MOJO_RESULT_OK; | |
85 | 94 |
86 if (is_running()) { | 95 if (is_running()) { |
87 message->set_source_id(local_id_); | 96 message->set_source_id(local_id_); |
88 message->set_destination_id(remote_id_); | 97 message->set_destination_id(remote_id_); |
| 98 // TODO(vtl): Figure out error handling here (where it's rather late) -- |
| 99 // maybe move whatever checks we can into |CanEnqueueMessage()|. |
89 if (!channel_->WriteMessage(message)) | 100 if (!channel_->WriteMessage(message)) |
90 rv = MOJO_RESULT_FAILED_PRECONDITION; | 101 LOG(WARNING) << "Failed to write message to channel"; |
91 } else { | 102 } else { |
92 paused_message_queue_.push_back(message); | 103 paused_message_queue_.push_back(message); |
93 } | 104 } |
94 | |
95 return rv; | |
96 } | 105 } |
97 | 106 |
98 void ProxyMessagePipeEndpoint::Attach(scoped_refptr<Channel> channel, | 107 void ProxyMessagePipeEndpoint::Attach(scoped_refptr<Channel> channel, |
99 MessageInTransit::EndpointId local_id) { | 108 MessageInTransit::EndpointId local_id) { |
100 DCHECK(channel.get()); | 109 DCHECK(channel.get()); |
101 DCHECK_NE(local_id, MessageInTransit::kInvalidEndpointId); | 110 DCHECK_NE(local_id, MessageInTransit::kInvalidEndpointId); |
102 | 111 |
103 DCHECK(!is_attached()); | 112 DCHECK(!is_attached()); |
104 | 113 |
105 AssertConsistentState(); | 114 AssertConsistentState(); |
106 channel_ = channel; | 115 channel_ = channel; |
107 local_id_ = local_id; | 116 local_id_ = local_id; |
108 AssertConsistentState(); | 117 AssertConsistentState(); |
109 } | 118 } |
110 | 119 |
111 bool ProxyMessagePipeEndpoint::Run(MessageInTransit::EndpointId remote_id) { | 120 bool ProxyMessagePipeEndpoint::Run(MessageInTransit::EndpointId remote_id) { |
112 // Assertions about arguments: | 121 // Assertions about arguments: |
113 DCHECK_NE(remote_id, MessageInTransit::kInvalidEndpointId); | 122 DCHECK_NE(remote_id, MessageInTransit::kInvalidEndpointId); |
114 | 123 |
115 // Assertions about current state: | 124 // Assertions about current state: |
116 DCHECK(is_attached()); | 125 DCHECK(is_attached()); |
117 DCHECK(!is_running()); | 126 DCHECK(!is_running()); |
118 | 127 |
119 AssertConsistentState(); | 128 AssertConsistentState(); |
120 remote_id_ = remote_id; | 129 remote_id_ = remote_id; |
121 AssertConsistentState(); | 130 AssertConsistentState(); |
122 | 131 |
123 MojoResult result = MOJO_RESULT_OK; | |
124 for (std::deque<MessageInTransit*>::iterator it = | 132 for (std::deque<MessageInTransit*>::iterator it = |
125 paused_message_queue_.begin(); | 133 paused_message_queue_.begin(); |
126 it != paused_message_queue_.end(); | 134 it != paused_message_queue_.end(); |
127 ++it) { | 135 ++it) { |
128 result = EnqueueMessage(*it, NULL); | 136 if (CanEnqueueMessage(*it, NULL) == MOJO_RESULT_OK) { |
129 if (result != MOJO_RESULT_OK) { | 137 EnqueueMessage(*it, NULL); |
| 138 } else { |
| 139 (*it)->Destroy(); |
130 // TODO(vtl): Do something more sensible on error here? | 140 // TODO(vtl): Do something more sensible on error here? |
131 LOG(WARNING) << "Failed to send message"; | 141 LOG(WARNING) << "Failed to send message"; |
| 142 // TODO(vtl): Abort? |
132 } | 143 } |
133 } | 144 } |
134 paused_message_queue_.clear(); | 145 paused_message_queue_.clear(); |
135 | 146 |
136 // If the peer is not open, we should return false since we should be | 147 // If the peer is not open, we should return false since we should be |
137 // destroyed. | 148 // destroyed. |
138 return is_peer_open_; | 149 return is_peer_open_; |
139 } | 150 } |
140 | 151 |
141 #ifndef NDEBUG | 152 #ifndef NDEBUG |
142 void ProxyMessagePipeEndpoint::AssertConsistentState() const { | 153 void ProxyMessagePipeEndpoint::AssertConsistentState() const { |
143 if (is_attached()) { | 154 if (is_attached()) { |
144 DCHECK_NE(local_id_, MessageInTransit::kInvalidEndpointId); | 155 DCHECK_NE(local_id_, MessageInTransit::kInvalidEndpointId); |
145 } else { // Not attached. | 156 } else { // Not attached. |
146 DCHECK_EQ(local_id_, MessageInTransit::kInvalidEndpointId); | 157 DCHECK_EQ(local_id_, MessageInTransit::kInvalidEndpointId); |
147 DCHECK_EQ(remote_id_, MessageInTransit::kInvalidEndpointId); | 158 DCHECK_EQ(remote_id_, MessageInTransit::kInvalidEndpointId); |
148 } | 159 } |
149 } | 160 } |
150 #endif | 161 #endif |
151 | 162 |
152 } // namespace system | 163 } // namespace system |
153 } // namespace mojo | 164 } // namespace mojo |
OLD | NEW |