OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "mojo/public/cpp/bindings/lib/connector.h" | |
6 | |
7 #include "mojo/public/cpp/bindings/error_handler.h" | |
8 #include "mojo/public/cpp/environment/logging.h" | |
9 | |
10 namespace mojo { | |
11 namespace internal { | |
12 | |
13 // ---------------------------------------------------------------------------- | |
14 | |
15 Connector::Connector(ScopedMessagePipeHandle message_pipe, | |
16 const MojoAsyncWaiter* waiter) | |
17 : error_handler_(nullptr), | |
18 waiter_(waiter), | |
19 message_pipe_(message_pipe.Pass()), | |
20 incoming_receiver_(nullptr), | |
21 async_wait_id_(0), | |
22 error_(false), | |
23 drop_writes_(false), | |
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. | |
28 WaitToReadMore(); | |
29 } | |
30 | |
31 Connector::~Connector() { | |
32 if (destroyed_flag_) | |
33 *destroyed_flag_ = true; | |
34 | |
35 CancelWait(); | |
36 } | |
37 | |
38 void Connector::CloseMessagePipe() { | |
39 CancelWait(); | |
40 Close(message_pipe_.Pass()); | |
41 } | |
42 | |
43 ScopedMessagePipeHandle Connector::PassMessagePipe() { | |
44 CancelWait(); | |
45 return message_pipe_.Pass(); | |
46 } | |
47 | |
48 bool Connector::WaitForIncomingMessage() { | |
49 if (error_) | |
50 return false; | |
51 | |
52 MojoResult rv = Wait(message_pipe_.get(), | |
53 MOJO_HANDLE_SIGNAL_READABLE, | |
54 MOJO_DEADLINE_INDEFINITE, | |
55 nullptr); | |
56 if (rv != MOJO_RESULT_OK) { | |
57 NotifyError(); | |
58 return false; | |
59 } | |
60 mojo_ignore_result(ReadSingleMessage(&rv)); | |
61 return (rv == MOJO_RESULT_OK); | |
62 } | |
63 | |
64 bool Connector::Accept(Message* message) { | |
65 MOJO_CHECK(message_pipe_.is_valid()); | |
66 | |
67 if (error_) | |
68 return false; | |
69 | |
70 if (drop_writes_) | |
71 return true; | |
72 | |
73 MojoResult rv = | |
74 WriteMessageRaw(message_pipe_.get(), | |
75 message->data(), | |
76 message->data_num_bytes(), | |
77 message->mutable_handles()->empty() | |
78 ? nullptr | |
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); | |
83 | |
84 switch (rv) { | |
85 case MOJO_RESULT_OK: | |
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(); | |
89 break; | |
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. | |
95 drop_writes_ = true; | |
96 break; | |
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 | |
104 // on). | |
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"; | |
109 return false; | |
110 default: | |
111 // This particular write was rejected, presumably because of bad input. | |
112 // The pipe is not necessarily in a bad state. | |
113 return false; | |
114 } | |
115 return true; | |
116 } | |
117 | |
118 // static | |
119 void Connector::CallOnHandleReady(void* closure, MojoResult result) { | |
120 Connector* self = static_cast<Connector*>(closure); | |
121 self->OnHandleReady(result); | |
122 } | |
123 | |
124 void Connector::OnHandleReady(MojoResult result) { | |
125 MOJO_CHECK(async_wait_id_ != 0); | |
126 async_wait_id_ = 0; | |
127 if (result != MOJO_RESULT_OK) { | |
128 NotifyError(); | |
129 return; | |
130 } | |
131 ReadAllAvailableMessages(); | |
132 // At this point, this object might have been deleted. Return. | |
133 } | |
134 | |
135 void Connector::WaitToReadMore() { | |
136 MOJO_CHECK(!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, | |
141 this); | |
142 } | |
143 | |
144 bool Connector::ReadSingleMessage(MojoResult* read_result) { | |
145 bool receiver_result = false; | |
146 | |
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; | |
152 | |
153 MojoResult rv = ReadAndDispatchMessage( | |
154 message_pipe_.get(), incoming_receiver_, &receiver_result); | |
155 if (read_result) | |
156 *read_result = rv; | |
157 | |
158 if (was_destroyed_during_dispatch) { | |
159 if (previous_destroyed_flag) | |
160 *previous_destroyed_flag = true; // Propagate flag. | |
161 return false; | |
162 } | |
163 destroyed_flag_ = previous_destroyed_flag; | |
164 | |
165 if (rv == MOJO_RESULT_SHOULD_WAIT) | |
166 return true; | |
167 | |
168 if (rv != MOJO_RESULT_OK || | |
169 (enforce_errors_from_incoming_receiver_ && !receiver_result)) { | |
170 NotifyError(); | |
171 return false; | |
172 } | |
173 return true; | |
174 } | |
175 | |
176 void Connector::ReadAllAvailableMessages() { | |
177 while (!error_) { | |
178 MojoResult rv; | |
179 | |
180 // Return immediately if |this| was destroyed. Do not touch any members! | |
181 if (!ReadSingleMessage(&rv)) | |
182 return; | |
183 | |
184 if (rv == MOJO_RESULT_SHOULD_WAIT) { | |
185 WaitToReadMore(); | |
186 break; | |
187 } | |
188 } | |
189 } | |
190 | |
191 void Connector::CancelWait() { | |
192 if (!async_wait_id_) | |
193 return; | |
194 | |
195 waiter_->CancelWait(async_wait_id_); | |
196 async_wait_id_ = 0; | |
197 } | |
198 | |
199 void Connector::NotifyError() { | |
200 error_ = true; | |
201 CancelWait(); | |
202 if (error_handler_) | |
203 error_handler_->OnConnectionError(); | |
204 } | |
205 | |
206 } // namespace internal | |
207 } // namespace mojo | |
OLD | NEW |