| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 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/connector.h" | 5 #include "mojo/public/cpp/bindings/lib/connector.h" |
| 6 | 6 |
| 7 #include <utility> |
| 8 |
| 7 #include "base/logging.h" | 9 #include "base/logging.h" |
| 8 #include "base/macros.h" | 10 #include "base/macros.h" |
| 9 #include "base/synchronization/lock.h" | 11 #include "base/synchronization/lock.h" |
| 10 | 12 |
| 11 namespace mojo { | 13 namespace mojo { |
| 12 namespace internal { | 14 namespace internal { |
| 13 | 15 |
| 14 namespace { | 16 namespace { |
| 15 | 17 |
| 16 // Similar to base::AutoLock, except that it does nothing if |lock| passed into | 18 // Similar to base::AutoLock, except that it does nothing if |lock| passed into |
| (...skipping 18 matching lines...) Expand all Loading... |
| 35 }; | 37 }; |
| 36 | 38 |
| 37 } // namespace | 39 } // namespace |
| 38 | 40 |
| 39 // ---------------------------------------------------------------------------- | 41 // ---------------------------------------------------------------------------- |
| 40 | 42 |
| 41 Connector::Connector(ScopedMessagePipeHandle message_pipe, | 43 Connector::Connector(ScopedMessagePipeHandle message_pipe, |
| 42 ConnectorConfig config, | 44 ConnectorConfig config, |
| 43 const MojoAsyncWaiter* waiter) | 45 const MojoAsyncWaiter* waiter) |
| 44 : waiter_(waiter), | 46 : waiter_(waiter), |
| 45 message_pipe_(message_pipe.Pass()), | 47 message_pipe_(std::move(message_pipe)), |
| 46 incoming_receiver_(nullptr), | 48 incoming_receiver_(nullptr), |
| 47 async_wait_id_(0), | 49 async_wait_id_(0), |
| 48 error_(false), | 50 error_(false), |
| 49 drop_writes_(false), | 51 drop_writes_(false), |
| 50 enforce_errors_from_incoming_receiver_(true), | 52 enforce_errors_from_incoming_receiver_(true), |
| 51 paused_(false), | 53 paused_(false), |
| 52 destroyed_flag_(nullptr), | 54 destroyed_flag_(nullptr), |
| 53 lock_(config == MULTI_THREADED_SEND ? new base::Lock : nullptr) { | 55 lock_(config == MULTI_THREADED_SEND ? new base::Lock : nullptr) { |
| 54 // Even though we don't have an incoming receiver, we still want to monitor | 56 // Even though we don't have an incoming receiver, we still want to monitor |
| 55 // the message pipe to know if is closed or encounters an error. | 57 // the message pipe to know if is closed or encounters an error. |
| 56 WaitToReadMore(); | 58 WaitToReadMore(); |
| 57 } | 59 } |
| 58 | 60 |
| 59 Connector::~Connector() { | 61 Connector::~Connector() { |
| 60 DCHECK(thread_checker_.CalledOnValidThread()); | 62 DCHECK(thread_checker_.CalledOnValidThread()); |
| 61 | 63 |
| 62 if (destroyed_flag_) | 64 if (destroyed_flag_) |
| 63 *destroyed_flag_ = true; | 65 *destroyed_flag_ = true; |
| 64 | 66 |
| 65 CancelWait(); | 67 CancelWait(); |
| 66 } | 68 } |
| 67 | 69 |
| 68 void Connector::CloseMessagePipe() { | 70 void Connector::CloseMessagePipe() { |
| 69 DCHECK(thread_checker_.CalledOnValidThread()); | 71 DCHECK(thread_checker_.CalledOnValidThread()); |
| 70 | 72 |
| 71 CancelWait(); | 73 CancelWait(); |
| 72 MayAutoLock locker(lock_.get()); | 74 MayAutoLock locker(lock_.get()); |
| 73 Close(message_pipe_.Pass()); | 75 Close(std::move(message_pipe_)); |
| 74 } | 76 } |
| 75 | 77 |
| 76 ScopedMessagePipeHandle Connector::PassMessagePipe() { | 78 ScopedMessagePipeHandle Connector::PassMessagePipe() { |
| 77 DCHECK(thread_checker_.CalledOnValidThread()); | 79 DCHECK(thread_checker_.CalledOnValidThread()); |
| 78 | 80 |
| 79 CancelWait(); | 81 CancelWait(); |
| 80 MayAutoLock locker(lock_.get()); | 82 MayAutoLock locker(lock_.get()); |
| 81 return message_pipe_.Pass(); | 83 return std::move(message_pipe_); |
| 82 } | 84 } |
| 83 | 85 |
| 84 void Connector::RaiseError() { | 86 void Connector::RaiseError() { |
| 85 DCHECK(thread_checker_.CalledOnValidThread()); | 87 DCHECK(thread_checker_.CalledOnValidThread()); |
| 86 | 88 |
| 87 HandleError(true, true); | 89 HandleError(true, true); |
| 88 } | 90 } |
| 89 | 91 |
| 90 bool Connector::WaitForIncomingMessage(MojoDeadline deadline) { | 92 bool Connector::WaitForIncomingMessage(MojoDeadline deadline) { |
| 91 DCHECK(thread_checker_.CalledOnValidThread()); | 93 DCHECK(thread_checker_.CalledOnValidThread()); |
| (...skipping 193 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 285 if (paused_) { | 287 if (paused_) { |
| 286 // If the user has paused receiving messages, we shouldn't call the error | 288 // If the user has paused receiving messages, we shouldn't call the error |
| 287 // handler right away. We need to wait until the user starts receiving | 289 // handler right away. We need to wait until the user starts receiving |
| 288 // messages again. | 290 // messages again. |
| 289 force_async_handler = true; | 291 force_async_handler = true; |
| 290 } | 292 } |
| 291 | 293 |
| 292 if (force_pipe_reset) { | 294 if (force_pipe_reset) { |
| 293 CancelWait(); | 295 CancelWait(); |
| 294 MayAutoLock locker(lock_.get()); | 296 MayAutoLock locker(lock_.get()); |
| 295 Close(message_pipe_.Pass()); | 297 Close(std::move(message_pipe_)); |
| 296 MessagePipe dummy_pipe; | 298 MessagePipe dummy_pipe; |
| 297 message_pipe_ = dummy_pipe.handle0.Pass(); | 299 message_pipe_ = std::move(dummy_pipe.handle0); |
| 298 } else { | 300 } else { |
| 299 CancelWait(); | 301 CancelWait(); |
| 300 } | 302 } |
| 301 | 303 |
| 302 if (force_async_handler) { | 304 if (force_async_handler) { |
| 303 // |dummy_pipe.handle1| has been destructed. Reading the pipe will | 305 // |dummy_pipe.handle1| has been destructed. Reading the pipe will |
| 304 // eventually cause a read error on |message_pipe_| and set error state. | 306 // eventually cause a read error on |message_pipe_| and set error state. |
| 305 if (!paused_) | 307 if (!paused_) |
| 306 WaitToReadMore(); | 308 WaitToReadMore(); |
| 307 } else { | 309 } else { |
| 308 error_ = true; | 310 error_ = true; |
| 309 connection_error_handler_.Run(); | 311 connection_error_handler_.Run(); |
| 310 } | 312 } |
| 311 } | 313 } |
| 312 | 314 |
| 313 } // namespace internal | 315 } // namespace internal |
| 314 } // namespace mojo | 316 } // namespace mojo |
| OLD | NEW |