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"
9 #include "mojo/public/cpp/bindings/error_handler.h"
10 #include "mojo/public/cpp/environment/logging.h"
15 // ----------------------------------------------------------------------------
17 Connector::Connector(ScopedMessagePipeHandle message_pipe,
18 const MojoAsyncWaiter* waiter)
19 : error_handler_(NULL),
21 message_pipe_(message_pipe.Pass()),
22 incoming_receiver_(NULL),
26 enforce_errors_from_incoming_receiver_(true),
27 destroyed_flag_(NULL) {
28 // Even though we don't have an incoming receiver, we still want to monitor
29 // the message pipe to know if is closed or encounters an error.
33 Connector::~Connector() {
35 *destroyed_flag_ = true;
40 void Connector::CloseMessagePipe() {
42 Close(message_pipe_.Pass());
45 ScopedMessagePipeHandle Connector::PassMessagePipe() {
47 return message_pipe_.Pass();
50 bool Connector::WaitForIncomingMessage() {
54 MojoResult rv = Wait(message_pipe_.get(),
55 MOJO_HANDLE_SIGNAL_READABLE,
56 MOJO_DEADLINE_INDEFINITE);
57 if (rv != MOJO_RESULT_OK) {
61 mojo_ignore_result(ReadSingleMessage(&rv));
62 return (rv == MOJO_RESULT_OK);
65 bool Connector::Accept(Message* message) {
66 MOJO_DCHECK(message_pipe_.is_valid());
74 MojoResult rv = WriteMessageRaw(
77 message->data_num_bytes(),
78 message->mutable_handles()->empty() ? NULL :
79 reinterpret_cast<const MojoHandle*>(
80 &message->mutable_handles()->front()),
81 static_cast<uint32_t>(message->mutable_handles()->size()),
82 MOJO_WRITE_MESSAGE_FLAG_NONE);
86 // The handles were successfully transferred, so we don't need the message
87 // to track their lifetime any longer.
88 message->mutable_handles()->clear();
90 case MOJO_RESULT_FAILED_PRECONDITION:
91 // There's no point in continuing to write to this pipe since the other
92 // end is gone. Avoid writing any future messages. Hide write failures
93 // from the caller since we'd like them to continue consuming any backlog
94 // of incoming messages before regarding the message pipe as closed.
97 case MOJO_RESULT_BUSY:
98 // We'd get a "busy" result if one of the message's handles is:
99 // - |message_pipe_|'s own handle;
100 // - simultaneously being used on another thread; or
101 // - in a "busy" state that prohibits it from being transferred (e.g.,
102 // a data pipe handle in the middle of a two-phase read/write,
103 // regardless of which thread that two-phase read/write is happening
105 // TODO(vtl): I wonder if this should be a |MOJO_DCHECK()|. (But, until
106 // crbug.com/389666, etc. are resolved, this will make tests fail quickly
107 // rather than hanging.)
108 MOJO_CHECK(false) << "Race condition or other bug detected";
111 // This particular write was rejected, presumably because of bad input.
112 // The pipe is not necessarily in a bad state.
119 void Connector::CallOnHandleReady(void* closure, MojoResult result) {
120 Connector* self = static_cast<Connector*>(closure);
121 self->OnHandleReady(result);
124 void Connector::OnHandleReady(MojoResult result) {
125 MOJO_DCHECK(async_wait_id_ != 0);
127 if (result != MOJO_RESULT_OK) {
131 ReadAllAvailableMessages();
132 // At this point, this object might have been deleted. Return.
135 void Connector::WaitToReadMore() {
136 MOJO_DCHECK(!async_wait_id_);
137 async_wait_id_ = waiter_->AsyncWait(message_pipe_.get().value(),
138 MOJO_HANDLE_SIGNAL_READABLE,
139 MOJO_DEADLINE_INDEFINITE,
140 &Connector::CallOnHandleReady,
144 bool Connector::ReadSingleMessage(MojoResult* read_result) {
145 bool receiver_result = false;
147 // Detect if |this| was destroyed during message dispatch. Allow for the
148 // possibility of re-entering ReadMore() through message dispatch.
149 bool was_destroyed_during_dispatch = false;
150 bool* previous_destroyed_flag = destroyed_flag_;
151 destroyed_flag_ = &was_destroyed_during_dispatch;
153 MojoResult rv = ReadAndDispatchMessage(
154 message_pipe_.get(), incoming_receiver_, &receiver_result);
158 if (was_destroyed_during_dispatch) {
159 if (previous_destroyed_flag)
160 *previous_destroyed_flag = true; // Propagate flag.
163 destroyed_flag_ = previous_destroyed_flag;
165 if (rv == MOJO_RESULT_SHOULD_WAIT)
168 if (rv != MOJO_RESULT_OK ||
169 (enforce_errors_from_incoming_receiver_ && !receiver_result)) {
176 void Connector::ReadAllAvailableMessages() {
180 // Return immediately if |this| was destroyed. Do not touch any members!
181 if (!ReadSingleMessage(&rv))
184 if (rv == MOJO_RESULT_SHOULD_WAIT) {
191 void Connector::CancelWait() {
195 waiter_->CancelWait(async_wait_id_);
199 void Connector::NotifyError() {
201 // The error handler might destroyed |this|. Also, after an error, all method
203 if (destroyed_flag_) {
204 *destroyed_flag_ = true; // Propagate flag.
207 error_handler_->OnConnectionError();
210 } // namespace internal