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