| 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/connector.h" | 5 #include "mojo/public/cpp/bindings/connector.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" |
| 11 #include "base/location.h" | 11 #include "base/location.h" |
| 12 #include "base/logging.h" | 12 #include "base/logging.h" |
| 13 #include "base/macros.h" | 13 #include "base/macros.h" |
| 14 #include "base/synchronization/lock.h" | 14 #include "base/synchronization/lock.h" |
| 15 #include "mojo/public/cpp/bindings/lib/may_auto_lock.h" | 15 #include "mojo/public/cpp/bindings/lib/may_auto_lock.h" |
| 16 #include "mojo/public/cpp/bindings/sync_handle_watcher.h" | 16 #include "mojo/public/cpp/bindings/sync_handle_watcher.h" |
| 17 | 17 |
| 18 namespace mojo { | 18 namespace mojo { |
| 19 | 19 |
| 20 Connector::Connector(ScopedMessagePipeHandle message_pipe, | 20 Connector::Connector(ScopedMessagePipeHandle message_pipe, |
| 21 ConnectorConfig config, | 21 ConnectorConfig config, |
| 22 scoped_refptr<base::SingleThreadTaskRunner> runner) | 22 scoped_refptr<base::SingleThreadTaskRunner> runner) |
| 23 : message_pipe_(std::move(message_pipe)), | 23 : message_pipe_(std::move(message_pipe)), |
| 24 task_runner_(std::move(runner)), | 24 task_runner_(std::move(runner)), |
| 25 lock_(config == MULTI_THREADED_SEND ? new base::Lock : nullptr), | |
| 26 weak_factory_(this) { | 25 weak_factory_(this) { |
| 26 if (config == MULTI_THREADED_SEND) |
| 27 lock_.emplace(); |
| 28 |
| 27 weak_self_ = weak_factory_.GetWeakPtr(); | 29 weak_self_ = weak_factory_.GetWeakPtr(); |
| 28 // Even though we don't have an incoming receiver, we still want to monitor | 30 // 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. | 31 // the message pipe to know if is closed or encounters an error. |
| 30 WaitToReadMore(); | 32 WaitToReadMore(); |
| 31 } | 33 } |
| 32 | 34 |
| 33 Connector::~Connector() { | 35 Connector::~Connector() { |
| 34 { | 36 { |
| 35 // Allow for quick destruction on any thread if the pipe is already closed. | 37 // Allow for quick destruction on any thread if the pipe is already closed. |
| 36 base::AutoLock lock(connected_lock_); | 38 base::AutoLock lock(connected_lock_); |
| 37 if (!connected_) | 39 if (!connected_) |
| 38 return; | 40 return; |
| 39 } | 41 } |
| 40 | 42 |
| 41 DCHECK(thread_checker_.CalledOnValidThread()); | 43 DCHECK(thread_checker_.CalledOnValidThread()); |
| 42 CancelWait(); | 44 CancelWait(); |
| 43 } | 45 } |
| 44 | 46 |
| 45 void Connector::CloseMessagePipe() { | 47 void Connector::CloseMessagePipe() { |
| 46 // Throw away the returned message pipe. | 48 // Throw away the returned message pipe. |
| 47 PassMessagePipe(); | 49 PassMessagePipe(); |
| 48 } | 50 } |
| 49 | 51 |
| 50 ScopedMessagePipeHandle Connector::PassMessagePipe() { | 52 ScopedMessagePipeHandle Connector::PassMessagePipe() { |
| 51 DCHECK(thread_checker_.CalledOnValidThread()); | 53 DCHECK(thread_checker_.CalledOnValidThread()); |
| 52 | 54 |
| 53 CancelWait(); | 55 CancelWait(); |
| 54 internal::MayAutoLock locker(lock_.get()); | 56 internal::MayAutoLock locker(&lock_); |
| 55 ScopedMessagePipeHandle message_pipe = std::move(message_pipe_); | 57 ScopedMessagePipeHandle message_pipe = std::move(message_pipe_); |
| 56 weak_factory_.InvalidateWeakPtrs(); | 58 weak_factory_.InvalidateWeakPtrs(); |
| 57 sync_handle_watcher_callback_count_ = 0; | 59 sync_handle_watcher_callback_count_ = 0; |
| 58 | 60 |
| 59 base::AutoLock lock(connected_lock_); | 61 base::AutoLock lock(connected_lock_); |
| 60 connected_ = false; | 62 connected_ = false; |
| 61 return message_pipe; | 63 return message_pipe; |
| 62 } | 64 } |
| 63 | 65 |
| 64 void Connector::RaiseError() { | 66 void Connector::RaiseError() { |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 111 | 113 |
| 112 bool Connector::Accept(Message* message) { | 114 bool Connector::Accept(Message* message) { |
| 113 DCHECK(lock_ || thread_checker_.CalledOnValidThread()); | 115 DCHECK(lock_ || thread_checker_.CalledOnValidThread()); |
| 114 | 116 |
| 115 // It shouldn't hurt even if |error_| may be changed by a different thread at | 117 // It shouldn't hurt even if |error_| may be changed by a different thread at |
| 116 // the same time. The outcome is that we may write into |message_pipe_| after | 118 // the same time. The outcome is that we may write into |message_pipe_| after |
| 117 // encountering an error, which should be fine. | 119 // encountering an error, which should be fine. |
| 118 if (error_) | 120 if (error_) |
| 119 return false; | 121 return false; |
| 120 | 122 |
| 121 internal::MayAutoLock locker(lock_.get()); | 123 internal::MayAutoLock locker(&lock_); |
| 122 | 124 |
| 123 if (!message_pipe_.is_valid() || drop_writes_) | 125 if (!message_pipe_.is_valid() || drop_writes_) |
| 124 return true; | 126 return true; |
| 125 | 127 |
| 126 MojoResult rv = | 128 MojoResult rv = |
| 127 WriteMessageNew(message_pipe_.get(), message->TakeMojoMessage(), | 129 WriteMessageNew(message_pipe_.get(), message->TakeMojoMessage(), |
| 128 MOJO_WRITE_MESSAGE_FLAG_NONE); | 130 MOJO_WRITE_MESSAGE_FLAG_NONE); |
| 129 | 131 |
| 130 switch (rv) { | 132 switch (rv) { |
| 131 case MOJO_RESULT_OK: | 133 case MOJO_RESULT_OK: |
| (...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 306 // receiving messages. We need to wait until the user starts receiving | 308 // receiving messages. We need to wait until the user starts receiving |
| 307 // messages again. | 309 // messages again. |
| 308 force_async_handler = true; | 310 force_async_handler = true; |
| 309 } | 311 } |
| 310 | 312 |
| 311 if (!force_pipe_reset && force_async_handler) | 313 if (!force_pipe_reset && force_async_handler) |
| 312 force_pipe_reset = true; | 314 force_pipe_reset = true; |
| 313 | 315 |
| 314 if (force_pipe_reset) { | 316 if (force_pipe_reset) { |
| 315 CancelWait(); | 317 CancelWait(); |
| 316 internal::MayAutoLock locker(lock_.get()); | 318 internal::MayAutoLock locker(&lock_); |
| 317 message_pipe_.reset(); | 319 message_pipe_.reset(); |
| 318 MessagePipe dummy_pipe; | 320 MessagePipe dummy_pipe; |
| 319 message_pipe_ = std::move(dummy_pipe.handle0); | 321 message_pipe_ = std::move(dummy_pipe.handle0); |
| 320 } else { | 322 } else { |
| 321 CancelWait(); | 323 CancelWait(); |
| 322 } | 324 } |
| 323 | 325 |
| 324 if (force_async_handler) { | 326 if (force_async_handler) { |
| 325 if (!paused_) | 327 if (!paused_) |
| 326 WaitToReadMore(); | 328 WaitToReadMore(); |
| 327 } else { | 329 } else { |
| 328 error_ = true; | 330 error_ = true; |
| 329 if (!connection_error_handler_.is_null()) | 331 if (!connection_error_handler_.is_null()) |
| 330 connection_error_handler_.Run(); | 332 connection_error_handler_.Run(); |
| 331 } | 333 } |
| 332 } | 334 } |
| 333 | 335 |
| 334 void Connector::EnsureSyncWatcherExists() { | 336 void Connector::EnsureSyncWatcherExists() { |
| 335 if (sync_watcher_) | 337 if (sync_watcher_) |
| 336 return; | 338 return; |
| 337 sync_watcher_.reset(new SyncHandleWatcher( | 339 sync_watcher_.reset(new SyncHandleWatcher( |
| 338 message_pipe_.get(), MOJO_HANDLE_SIGNAL_READABLE, | 340 message_pipe_.get(), MOJO_HANDLE_SIGNAL_READABLE, |
| 339 base::Bind(&Connector::OnSyncHandleWatcherHandleReady, | 341 base::Bind(&Connector::OnSyncHandleWatcherHandleReady, |
| 340 base::Unretained(this)))); | 342 base::Unretained(this)))); |
| 341 } | 343 } |
| 342 | 344 |
| 343 } // namespace mojo | 345 } // namespace mojo |
| OLD | NEW |