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.
5 #include "mojo/public/cpp/bindings/lib/connector.h"
7 #include "mojo/public/cpp/bindings/error_handler.h"
8 #include "mojo/public/cpp/environment/logging.h"
13 // ----------------------------------------------------------------------------
15 Connector::Connector(ScopedMessagePipeHandle message_pipe,
16 const MojoAsyncWaiter* waiter)
17 : error_handler_(nullptr),
19 message_pipe_(message_pipe.Pass()),
20 incoming_receiver_(nullptr),
24 enforce_errors_from_incoming_receiver_(true),
25 destroyed_flag_(nullptr) {
26 // Even though we don't have an incoming receiver, we still want to monitor
27 // the message pipe to know if is closed or encounters an error.
31 Connector::~Connector() {
33 *destroyed_flag_ = true;
38 void Connector::CloseMessagePipe() {
40 Close(message_pipe_.Pass());
43 ScopedMessagePipeHandle Connector::PassMessagePipe() {
45 return message_pipe_.Pass();
48 bool Connector::WaitForIncomingMessage() {
52 MojoResult rv = Wait(message_pipe_.get(),
53 MOJO_HANDLE_SIGNAL_READABLE,
54 MOJO_DEADLINE_INDEFINITE);
55 if (rv != MOJO_RESULT_OK) {
59 mojo_ignore_result(ReadSingleMessage(&rv));
60 return (rv == MOJO_RESULT_OK);
63 bool Connector::Accept(Message* message) {
64 MOJO_CHECK(message_pipe_.is_valid());
73 WriteMessageRaw(message_pipe_.get(),
75 message->data_num_bytes(),
76 message->mutable_handles()->empty()
78 : reinterpret_cast<const MojoHandle*>(
79 &message->mutable_handles()->front()),
80 static_cast<uint32_t>(message->mutable_handles()->size()),
81 MOJO_WRITE_MESSAGE_FLAG_NONE);
85 // The handles were successfully transferred, so we don't need the message
86 // to track their lifetime any longer.
87 message->mutable_handles()->clear();
89 case MOJO_RESULT_FAILED_PRECONDITION:
90 // There's no point in continuing to write to this pipe since the other
91 // end is gone. Avoid writing any future messages. Hide write failures
92 // from the caller since we'd like them to continue consuming any backlog
93 // of incoming messages before regarding the message pipe as closed.
96 case MOJO_RESULT_BUSY:
97 // We'd get a "busy" result if one of the message's handles is:
98 // - |message_pipe_|'s own handle;
99 // - simultaneously being used on another thread; or
100 // - in a "busy" state that prohibits it from being transferred (e.g.,
101 // a data pipe handle in the middle of a two-phase read/write,
102 // regardless of which thread that two-phase read/write is happening
104 // TODO(vtl): I wonder if this should be a |MOJO_DCHECK()|. (But, until
105 // crbug.com/389666, etc. are resolved, this will make tests fail quickly
106 // rather than hanging.)
107 MOJO_CHECK(false) << "Race condition or other bug detected";
110 // This particular write was rejected, presumably because of bad input.
111 // The pipe is not necessarily in a bad state.
118 void Connector::CallOnHandleReady(void* closure, MojoResult result) {
119 Connector* self = static_cast<Connector*>(closure);
120 self->OnHandleReady(result);
123 void Connector::OnHandleReady(MojoResult result) {
124 MOJO_CHECK(async_wait_id_ != 0);
126 if (result != MOJO_RESULT_OK) {
130 ReadAllAvailableMessages();
131 // At this point, this object might have been deleted. Return.
134 void Connector::WaitToReadMore() {
135 MOJO_CHECK(!async_wait_id_);
136 async_wait_id_ = waiter_->AsyncWait(message_pipe_.get().value(),
137 MOJO_HANDLE_SIGNAL_READABLE,
138 MOJO_DEADLINE_INDEFINITE,
139 &Connector::CallOnHandleReady,
143 bool Connector::ReadSingleMessage(MojoResult* read_result) {
144 bool receiver_result = false;
146 // Detect if |this| was destroyed during message dispatch. Allow for the
147 // possibility of re-entering ReadMore() through message dispatch.
148 bool was_destroyed_during_dispatch = false;
149 bool* previous_destroyed_flag = destroyed_flag_;
150 destroyed_flag_ = &was_destroyed_during_dispatch;
152 MojoResult rv = ReadAndDispatchMessage(
153 message_pipe_.get(), incoming_receiver_, &receiver_result);
157 if (was_destroyed_during_dispatch) {
158 if (previous_destroyed_flag)
159 *previous_destroyed_flag = true; // Propagate flag.
162 destroyed_flag_ = previous_destroyed_flag;
164 if (rv == MOJO_RESULT_SHOULD_WAIT)
167 if (rv != MOJO_RESULT_OK ||
168 (enforce_errors_from_incoming_receiver_ && !receiver_result)) {
175 void Connector::ReadAllAvailableMessages() {
179 // Return immediately if |this| was destroyed. Do not touch any members!
180 if (!ReadSingleMessage(&rv))
183 if (rv == MOJO_RESULT_SHOULD_WAIT) {
190 void Connector::CancelWait() {
194 waiter_->CancelWait(async_wait_id_);
198 void Connector::NotifyError() {
202 error_handler_->OnConnectionError();
205 } // namespace internal