[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "ipc/mojo/ipc_message_pipe_reader.h" |
| 6 | |
| 7 | #include "base/bind.h" |
| 8 | #include "base/bind_helpers.h" |
| 9 | #include "base/location.h" |
| 10 | #include "base/logging.h" |
| 11 | #include "base/message_loop/message_loop_proxy.h" |
morrita | dcc28ab | 2015-01-10 01:49:21 | [diff] [blame] | 12 | #include "ipc/mojo/async_handle_waiter.h" |
morrita | d68bedf4 | 2014-11-25 23:35:57 | [diff] [blame] | 13 | #include "ipc/mojo/ipc_channel_mojo.h" |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 14 | |
| 15 | namespace IPC { |
| 16 | namespace internal { |
| 17 | |
morrita | d68bedf4 | 2014-11-25 23:35:57 | [diff] [blame] | 18 | MessagePipeReader::MessagePipeReader(mojo::ScopedMessagePipeHandle handle, |
| 19 | MessagePipeReader::Delegate* delegate) |
morrita | dcc28ab | 2015-01-10 01:49:21 | [diff] [blame] | 20 | : pipe_(handle.Pass()), |
| 21 | delegate_(delegate), |
| 22 | async_waiter_( |
| 23 | new AsyncHandleWaiter(base::Bind(&MessagePipeReader::PipeIsReady, |
morrita | b447214 | 2015-04-20 21:20:12 | [diff] [blame^] | 24 | base::Unretained(this)))), |
| 25 | pending_send_error_(MOJO_RESULT_OK) { |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 26 | } |
| 27 | |
| 28 | MessagePipeReader::~MessagePipeReader() { |
morrita | b447214 | 2015-04-20 21:20:12 | [diff] [blame^] | 29 | // The pipe should be closed before deletion. |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 30 | CHECK(!IsValid()); |
morrita | b447214 | 2015-04-20 21:20:12 | [diff] [blame^] | 31 | DCHECK_EQ(pending_send_error_, MOJO_RESULT_OK); |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | void MessagePipeReader::Close() { |
morrita | b447214 | 2015-04-20 21:20:12 | [diff] [blame^] | 35 | // All pending errors should be signaled before Close(). |
| 36 | DCHECK_EQ(pending_send_error_, MOJO_RESULT_OK); |
morrita | dcc28ab | 2015-01-10 01:49:21 | [diff] [blame] | 37 | async_waiter_.reset(); |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 38 | pipe_.reset(); |
| 39 | OnPipeClosed(); |
| 40 | } |
| 41 | |
| 42 | void MessagePipeReader::CloseWithError(MojoResult error) { |
| 43 | OnPipeError(error); |
| 44 | Close(); |
| 45 | } |
| 46 | |
morrita | b447214 | 2015-04-20 21:20:12 | [diff] [blame^] | 47 | void MessagePipeReader::CloseWithErrorIfPending() { |
| 48 | if (pending_send_error_ == MOJO_RESULT_OK) |
| 49 | return; |
| 50 | MojoResult error = pending_send_error_; |
| 51 | pending_send_error_ = MOJO_RESULT_OK; |
| 52 | CloseWithError(error); |
| 53 | return; |
| 54 | } |
| 55 | |
| 56 | void MessagePipeReader::CloseWithErrorLater(MojoResult error) { |
| 57 | pending_send_error_ = error; |
| 58 | } |
| 59 | |
morrita | d68bedf4 | 2014-11-25 23:35:57 | [diff] [blame] | 60 | bool MessagePipeReader::Send(scoped_ptr<Message> message) { |
| 61 | DCHECK(IsValid()); |
| 62 | |
| 63 | message->TraceMessageBegin(); |
| 64 | std::vector<MojoHandle> handles; |
| 65 | MojoResult result = MOJO_RESULT_OK; |
morrita | 4b5c28e2 | 2015-01-14 21:17:06 | [diff] [blame] | 66 | result = ChannelMojo::ReadFromMessageAttachmentSet(message.get(), &handles); |
morrita | d68bedf4 | 2014-11-25 23:35:57 | [diff] [blame] | 67 | if (result == MOJO_RESULT_OK) { |
| 68 | result = MojoWriteMessage(handle(), |
| 69 | message->data(), |
| 70 | static_cast<uint32>(message->size()), |
| 71 | handles.empty() ? nullptr : &handles[0], |
| 72 | static_cast<uint32>(handles.size()), |
| 73 | MOJO_WRITE_MESSAGE_FLAG_NONE); |
| 74 | } |
| 75 | |
| 76 | if (result != MOJO_RESULT_OK) { |
| 77 | std::for_each(handles.begin(), handles.end(), &MojoClose); |
morrita | b447214 | 2015-04-20 21:20:12 | [diff] [blame^] | 78 | // We cannot call CloseWithError() here as Send() is protected by |
| 79 | // ChannelMojo's lock and CloseWithError() could re-enter ChannelMojo. We |
| 80 | // cannot call CloseWithError() also because Send() can be called from |
| 81 | // non-UI thread while OnPipeError() expects to be called on IO thread. |
| 82 | CloseWithErrorLater(result); |
morrita | d68bedf4 | 2014-11-25 23:35:57 | [diff] [blame] | 83 | return false; |
| 84 | } |
| 85 | |
| 86 | return true; |
| 87 | } |
| 88 | |
morrita | d68bedf4 | 2014-11-25 23:35:57 | [diff] [blame] | 89 | void MessagePipeReader::OnMessageReceived() { |
| 90 | Message message(data_buffer().empty() ? "" : &data_buffer()[0], |
| 91 | static_cast<uint32>(data_buffer().size())); |
| 92 | |
| 93 | std::vector<MojoHandle> handle_buffer; |
| 94 | TakeHandleBuffer(&handle_buffer); |
morrita | d68bedf4 | 2014-11-25 23:35:57 | [diff] [blame] | 95 | MojoResult write_result = |
morrita | 4b5c28e2 | 2015-01-14 21:17:06 | [diff] [blame] | 96 | ChannelMojo::WriteToMessageAttachmentSet(handle_buffer, &message); |
morrita | d68bedf4 | 2014-11-25 23:35:57 | [diff] [blame] | 97 | if (write_result != MOJO_RESULT_OK) { |
| 98 | CloseWithError(write_result); |
| 99 | return; |
| 100 | } |
morrita | d68bedf4 | 2014-11-25 23:35:57 | [diff] [blame] | 101 | |
| 102 | message.TraceMessageEnd(); |
| 103 | delegate_->OnMessageReceived(message); |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 104 | } |
| 105 | |
morrita | d68bedf4 | 2014-11-25 23:35:57 | [diff] [blame] | 106 | void MessagePipeReader::OnPipeClosed() { |
| 107 | if (!delegate_) |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 108 | return; |
morrita | d68bedf4 | 2014-11-25 23:35:57 | [diff] [blame] | 109 | delegate_->OnPipeClosed(this); |
| 110 | delegate_ = nullptr; |
| 111 | } |
| 112 | |
| 113 | void MessagePipeReader::OnPipeError(MojoResult error) { |
| 114 | if (!delegate_) |
| 115 | return; |
| 116 | delegate_->OnPipeError(this); |
| 117 | } |
| 118 | |
| 119 | MojoResult MessagePipeReader::ReadMessageBytes() { |
| 120 | DCHECK(handle_buffer_.empty()); |
| 121 | |
| 122 | uint32_t num_bytes = static_cast<uint32_t>(data_buffer_.size()); |
| 123 | uint32_t num_handles = 0; |
| 124 | MojoResult result = MojoReadMessage(pipe_.get().value(), |
| 125 | num_bytes ? &data_buffer_[0] : nullptr, |
| 126 | &num_bytes, |
| 127 | nullptr, |
| 128 | &num_handles, |
| 129 | MOJO_READ_MESSAGE_FLAG_NONE); |
| 130 | data_buffer_.resize(num_bytes); |
| 131 | handle_buffer_.resize(num_handles); |
| 132 | if (result == MOJO_RESULT_RESOURCE_EXHAUSTED) { |
| 133 | // MOJO_RESULT_RESOURCE_EXHAUSTED was asking the caller that |
| 134 | // it needs more bufer. So we re-read it with resized buffers. |
| 135 | result = MojoReadMessage(pipe_.get().value(), |
| 136 | num_bytes ? &data_buffer_[0] : nullptr, |
| 137 | &num_bytes, |
| 138 | num_handles ? &handle_buffer_[0] : nullptr, |
| 139 | &num_handles, |
| 140 | MOJO_READ_MESSAGE_FLAG_NONE); |
| 141 | } |
| 142 | |
| 143 | DCHECK(0 == num_bytes || data_buffer_.size() == num_bytes); |
| 144 | DCHECK(0 == num_handles || handle_buffer_.size() == num_handles); |
| 145 | return result; |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 146 | } |
| 147 | |
morrita | dcc28ab | 2015-01-10 01:49:21 | [diff] [blame] | 148 | void MessagePipeReader::ReadAvailableMessages() { |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 149 | while (pipe_.is_valid()) { |
| 150 | MojoResult read_result = ReadMessageBytes(); |
| 151 | if (read_result == MOJO_RESULT_SHOULD_WAIT) |
| 152 | break; |
| 153 | if (read_result != MOJO_RESULT_OK) { |
| 154 | // FAILED_PRECONDITION means that all the received messages |
| 155 | // got consumed and the peer is already closed. |
| 156 | if (read_result != MOJO_RESULT_FAILED_PRECONDITION) { |
| 157 | DLOG(WARNING) |
| 158 | << "Pipe got error from ReadMessage(). Closing: " << read_result; |
| 159 | OnPipeError(read_result); |
| 160 | } |
| 161 | |
| 162 | Close(); |
| 163 | break; |
| 164 | } |
| 165 | |
| 166 | OnMessageReceived(); |
| 167 | } |
| 168 | |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 169 | } |
| 170 | |
morrita | dcc28ab | 2015-01-10 01:49:21 | [diff] [blame] | 171 | void MessagePipeReader::ReadMessagesThenWait() { |
| 172 | while (true) { |
| 173 | ReadAvailableMessages(); |
| 174 | if (!pipe_.is_valid()) |
| 175 | break; |
| 176 | // |Wait()| is safe to call only after all messages are read. |
| 177 | // If can fail with |MOJO_RESULT_ALREADY_EXISTS| otherwise. |
| 178 | // Also, we don't use MOJO_HANDLE_SIGNAL_WRITABLE here, expecting buffer in |
| 179 | // MessagePipe. |
tkent | 5eab51f | 2015-03-25 04:40:10 | [diff] [blame] | 180 | MojoResult result = |
| 181 | async_waiter_->Wait(pipe_.get().value(), MOJO_HANDLE_SIGNAL_READABLE); |
morrita | dcc28ab | 2015-01-10 01:49:21 | [diff] [blame] | 182 | // If the result is |MOJO_RESULT_ALREADY_EXISTS|, there could be messages |
| 183 | // that have been arrived after the last |ReadAvailableMessages()|. |
| 184 | // We have to consume then and retry in that case. |
| 185 | if (result != MOJO_RESULT_ALREADY_EXISTS) { |
| 186 | if (result != MOJO_RESULT_OK) { |
morrita | a3889aa | 2015-03-16 22:40:51 | [diff] [blame] | 187 | LOG(ERROR) << "Failed to wait on the pipe. Result is " << result; |
morrita | dcc28ab | 2015-01-10 01:49:21 | [diff] [blame] | 188 | OnPipeError(result); |
| 189 | Close(); |
| 190 | } |
| 191 | |
| 192 | break; |
| 193 | } |
| 194 | } |
morrita | d68bedf4 | 2014-11-25 23:35:57 | [diff] [blame] | 195 | } |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 196 | |
morrita | dcc28ab | 2015-01-10 01:49:21 | [diff] [blame] | 197 | void MessagePipeReader::PipeIsReady(MojoResult wait_result) { |
morrita | b447214 | 2015-04-20 21:20:12 | [diff] [blame^] | 198 | CloseWithErrorIfPending(); |
| 199 | if (!IsValid()) { |
| 200 | // There was a pending error and it closed the pipe. |
| 201 | // We cannot do the work anymore. |
| 202 | return; |
| 203 | } |
| 204 | |
morrita | dcc28ab | 2015-01-10 01:49:21 | [diff] [blame] | 205 | if (wait_result != MOJO_RESULT_OK) { |
tkent | 5eab51f | 2015-03-25 04:40:10 | [diff] [blame] | 206 | if (wait_result != MOJO_RESULT_ABORTED) { |
| 207 | // FAILED_PRECONDITION happens every time the peer is dead so |
| 208 | // it isn't worth polluting the log message. |
| 209 | LOG_IF(WARNING, wait_result != MOJO_RESULT_FAILED_PRECONDITION) |
| 210 | << "Pipe got error from the waiter. Closing: " << wait_result; |
| 211 | OnPipeError(wait_result); |
| 212 | } |
morrita | dcc28ab | 2015-01-10 01:49:21 | [diff] [blame] | 213 | |
| 214 | Close(); |
morrita | d68bedf4 | 2014-11-25 23:35:57 | [diff] [blame] | 215 | return; |
morrita | dcc28ab | 2015-01-10 01:49:21 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | ReadMessagesThenWait(); |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | void MessagePipeReader::DelayedDeleter::operator()( |
| 222 | MessagePipeReader* ptr) const { |
| 223 | ptr->Close(); |
| 224 | base::MessageLoopProxy::current()->PostTask( |
| 225 | FROM_HERE, base::Bind(&DeleteNow, ptr)); |
| 226 | } |
| 227 | |
| 228 | } // namespace internal |
| 229 | } // namespace IPC |