OLD | NEW |
(Empty) | |
| 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" |
| 12 #include "mojo/public/cpp/environment/environment.h" |
| 13 |
| 14 namespace IPC { |
| 15 namespace internal { |
| 16 |
| 17 MessagePipeReader::MessagePipeReader() |
| 18 : pipe_wait_id_(0) { |
| 19 } |
| 20 |
| 21 MessagePipeReader::~MessagePipeReader() { |
| 22 Close(); |
| 23 } |
| 24 |
| 25 void MessagePipeReader::SetPipe(mojo::ScopedMessagePipeHandle handle) { |
| 26 DCHECK(!pipe_.is_valid()); |
| 27 pipe_ = handle.Pass(); |
| 28 StartWaiting(); |
| 29 } |
| 30 |
| 31 void MessagePipeReader::Close() { |
| 32 StopWaiting(); |
| 33 pipe_.reset(); |
| 34 OnPipeClosed(); |
| 35 } |
| 36 |
| 37 void MessagePipeReader::CloseWithError(MojoResult error) { |
| 38 OnPipeError(error); |
| 39 Close(); |
| 40 } |
| 41 |
| 42 // static |
| 43 void MessagePipeReader::InvokePipeIsReady(void* closure, MojoResult result) { |
| 44 reinterpret_cast<MessagePipeReader*>(closure)->PipeIsReady(result); |
| 45 } |
| 46 |
| 47 void MessagePipeReader::StartWaiting() { |
| 48 DCHECK(pipe_.is_valid()); |
| 49 DCHECK(!pipe_wait_id_); |
| 50 // Not using MOJO_HANDLE_SIGNAL_WRITABLE here, expecting buffer in |
| 51 // MessagePipe. |
| 52 // |
| 53 // TODO(morrita): Should we re-set the signal when we get new |
| 54 // message to send? |
| 55 pipe_wait_id_ = mojo::Environment::GetDefaultAsyncWaiter()->AsyncWait( |
| 56 pipe_.get().value(), |
| 57 MOJO_HANDLE_SIGNAL_READABLE, |
| 58 MOJO_DEADLINE_INDEFINITE, |
| 59 &InvokePipeIsReady, |
| 60 this); |
| 61 } |
| 62 |
| 63 void MessagePipeReader::StopWaiting() { |
| 64 if (!pipe_wait_id_) |
| 65 return; |
| 66 mojo::Environment::GetDefaultAsyncWaiter()->CancelWait(pipe_wait_id_); |
| 67 pipe_wait_id_ = 0; |
| 68 } |
| 69 |
| 70 void MessagePipeReader::PipeIsReady(MojoResult wait_result) { |
| 71 pipe_wait_id_ = 0; |
| 72 |
| 73 if (wait_result != MOJO_RESULT_OK) { |
| 74 // FAILED_PRECONDITION happens when the pipe is |
| 75 // closed before the waiter is scheduled in a backend thread. |
| 76 if (wait_result != MOJO_RESULT_ABORTED && |
| 77 wait_result != MOJO_RESULT_FAILED_PRECONDITION) { |
| 78 DLOG(WARNING) << "Pipe got error from the waiter. Closing: " |
| 79 << wait_result; |
| 80 OnPipeError(wait_result); |
| 81 } |
| 82 |
| 83 Close(); |
| 84 return; |
| 85 } |
| 86 |
| 87 while (pipe_.is_valid()) { |
| 88 MojoResult read_result = ReadMessageBytes(); |
| 89 if (read_result == MOJO_RESULT_SHOULD_WAIT) |
| 90 break; |
| 91 if (read_result != MOJO_RESULT_OK) { |
| 92 // FAILED_PRECONDITION means that all the received messages |
| 93 // got consumed and the peer is already closed. |
| 94 if (read_result != MOJO_RESULT_FAILED_PRECONDITION) { |
| 95 DLOG(WARNING) |
| 96 << "Pipe got error from ReadMessage(). Closing: " << read_result; |
| 97 OnPipeError(read_result); |
| 98 } |
| 99 |
| 100 Close(); |
| 101 break; |
| 102 } |
| 103 |
| 104 OnMessageArrived(); |
| 105 } |
| 106 |
| 107 if (pipe_.is_valid()) |
| 108 StartWaiting(); |
| 109 } |
| 110 |
| 111 MojoResult MessagePipeReader::ReadMessageBytes() { |
| 112 uint32_t num_bytes = data_buffer_.size(); |
| 113 uint32_t num_handles = handle_buffer_.size(); |
| 114 MojoResult result = MojoReadMessage(pipe_.get().value(), |
| 115 num_bytes ? &data_buffer_[0] : NULL, |
| 116 &num_bytes, |
| 117 num_handles ? &handle_buffer_[0] : NULL, |
| 118 &num_handles, |
| 119 MOJO_READ_MESSAGE_FLAG_NONE); |
| 120 data_buffer_.resize(num_bytes); |
| 121 handle_buffer_.resize(num_handles); |
| 122 if (result == MOJO_RESULT_RESOURCE_EXHAUSTED) { |
| 123 // MOJO_RESULT_RESOURCE_EXHAUSTED was asking the caller that |
| 124 // it needs more bufer. So we re-read it with resized buffers. |
| 125 result = MojoReadMessage(pipe_.get().value(), |
| 126 num_bytes ? &data_buffer_[0] : NULL, |
| 127 &num_bytes, |
| 128 num_handles ? &handle_buffer_[0] : NULL, |
| 129 &num_handles, |
| 130 MOJO_READ_MESSAGE_FLAG_NONE); |
| 131 } |
| 132 |
| 133 DCHECK(0 == num_bytes || data_buffer_.size() == num_bytes); |
| 134 DCHECK(0 == num_handles || handle_buffer_.size() == num_handles); |
| 135 return result; |
| 136 } |
| 137 |
| 138 void MessagePipeReader::DelayedDeleter::operator()( |
| 139 MessagePipeReader* ptr) const { |
| 140 ptr->Close(); |
| 141 base::MessageLoopProxy::current()->PostTask( |
| 142 FROM_HERE, base::Bind(&DeleteNow, ptr)); |
| 143 } |
| 144 |
| 145 } // namespace internal |
| 146 } // namespace IPC |
OLD | NEW |