OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "mojo/public/cpp/bindings/lib/router.h" | 5 #include "mojo/public/cpp/bindings/lib/router.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 #include <utility> | 8 #include <utility> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
86 Router::Router(ScopedMessagePipeHandle message_pipe, | 86 Router::Router(ScopedMessagePipeHandle message_pipe, |
87 FilterChain filters, | 87 FilterChain filters, |
88 bool expects_sync_requests) | 88 bool expects_sync_requests) |
89 : thunk_(this), | 89 : thunk_(this), |
90 filters_(std::move(filters)), | 90 filters_(std::move(filters)), |
91 connector_(std::move(message_pipe), Connector::SINGLE_THREADED_SEND), | 91 connector_(std::move(message_pipe), Connector::SINGLE_THREADED_SEND), |
92 incoming_receiver_(nullptr), | 92 incoming_receiver_(nullptr), |
93 next_request_id_(0), | 93 next_request_id_(0), |
94 testing_mode_(false), | 94 testing_mode_(false), |
95 pending_task_for_messages_(false), | 95 pending_task_for_messages_(false), |
| 96 encountered_error_(false), |
96 weak_factory_(this) { | 97 weak_factory_(this) { |
97 filters_.SetSink(&thunk_); | 98 filters_.SetSink(&thunk_); |
98 if (expects_sync_requests) | 99 if (expects_sync_requests) |
99 connector_.RegisterSyncHandleWatch(); | 100 connector_.RegisterSyncHandleWatch(); |
100 connector_.set_incoming_receiver(filters_.GetHead()); | 101 connector_.set_incoming_receiver(filters_.GetHead()); |
| 102 connector_.set_connection_error_handler([this]() { OnConnectionError(); }); |
101 } | 103 } |
102 | 104 |
103 Router::~Router() {} | 105 Router::~Router() {} |
104 | 106 |
105 bool Router::Accept(Message* message) { | 107 bool Router::Accept(Message* message) { |
106 DCHECK(thread_checker_.CalledOnValidThread()); | 108 DCHECK(thread_checker_.CalledOnValidThread()); |
107 DCHECK(!message->has_flag(kMessageExpectsResponse)); | 109 DCHECK(!message->has_flag(kMessageExpectsResponse)); |
108 return connector_.Accept(message); | 110 return connector_.Accept(message); |
109 } | 111 } |
110 | 112 |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
198 if (!weak_self) | 200 if (!weak_self) |
199 return; | 201 return; |
200 | 202 |
201 if (!result && !testing_mode_) { | 203 if (!result && !testing_mode_) { |
202 connector_.RaiseError(); | 204 connector_.RaiseError(); |
203 break; | 205 break; |
204 } | 206 } |
205 } | 207 } |
206 | 208 |
207 pending_task_for_messages_ = false; | 209 pending_task_for_messages_ = false; |
| 210 |
| 211 // We may have already seen a connection error from the connector, but |
| 212 // haven't notified the user because we want to process all the queued |
| 213 // messages first. We should do it now. |
| 214 if (connector_.encountered_error() && !encountered_error_) |
| 215 OnConnectionError(); |
208 } | 216 } |
209 | 217 |
210 bool Router::HandleMessageInternal(Message* message) { | 218 bool Router::HandleMessageInternal(Message* message) { |
211 if (message->has_flag(kMessageExpectsResponse)) { | 219 if (message->has_flag(kMessageExpectsResponse)) { |
212 if (!incoming_receiver_) | 220 if (!incoming_receiver_) |
213 return false; | 221 return false; |
214 | 222 |
215 MessageReceiverWithStatus* responder = | 223 MessageReceiverWithStatus* responder = |
216 new ResponderThunk(weak_factory_.GetWeakPtr()); | 224 new ResponderThunk(weak_factory_.GetWeakPtr()); |
217 bool ok = incoming_receiver_->AcceptWithResponder(message, responder); | 225 bool ok = incoming_receiver_->AcceptWithResponder(message, responder); |
(...skipping 25 matching lines...) Expand all Loading... |
243 async_responders_.erase(it); | 251 async_responders_.erase(it); |
244 return responder->Accept(message); | 252 return responder->Accept(message); |
245 } else { | 253 } else { |
246 if (!incoming_receiver_) | 254 if (!incoming_receiver_) |
247 return false; | 255 return false; |
248 | 256 |
249 return incoming_receiver_->Accept(message); | 257 return incoming_receiver_->Accept(message); |
250 } | 258 } |
251 } | 259 } |
252 | 260 |
| 261 void Router::OnConnectionError() { |
| 262 DCHECK(!encountered_error_); |
| 263 |
| 264 if (!pending_messages_.empty()) { |
| 265 // After all the pending messages are processed, we will check whether an |
| 266 // error has been encountered and run the user's connection error handler |
| 267 // if necessary. |
| 268 DCHECK(pending_task_for_messages_); |
| 269 return; |
| 270 } |
| 271 |
| 272 encountered_error_ = true; |
| 273 error_handler_.Run(); |
| 274 } |
| 275 |
253 // ---------------------------------------------------------------------------- | 276 // ---------------------------------------------------------------------------- |
254 | 277 |
255 } // namespace internal | 278 } // namespace internal |
256 } // namespace mojo | 279 } // namespace mojo |
OLD | NEW |