| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/edk/system/slave_connection_manager.h" | 5 #include "mojo/edk/system/slave_connection_manager.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
| 9 #include "base/location.h" | 9 #include "base/location.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/message_loop/message_loop.h" | 11 #include "base/message_loop/message_loop.h" |
| 12 #include "mojo/edk/system/message_in_transit.h" | 12 #include "mojo/edk/system/message_in_transit.h" |
| 13 | 13 |
| 14 namespace mojo { | 14 namespace mojo { |
| 15 namespace system { | 15 namespace system { |
| 16 | 16 |
| 17 // SlaveConnectionManager ------------------------------------------------------ | 17 // SlaveConnectionManager ------------------------------------------------------ |
| 18 | 18 |
| 19 SlaveConnectionManager::SlaveConnectionManager() | 19 SlaveConnectionManager::SlaveConnectionManager() |
| 20 : slave_process_delegate_(), | 20 : creation_thread_task_runner_(base::MessageLoop::current()->task_runner()), |
| 21 slave_process_delegate_(), |
| 21 private_thread_("SlaveConnectionManagerPrivateThread"), | 22 private_thread_("SlaveConnectionManagerPrivateThread"), |
| 22 awaiting_ack_type_(NOT_AWAITING_ACK), | 23 awaiting_ack_type_(NOT_AWAITING_ACK), |
| 23 ack_result_(), | 24 ack_result_(), |
| 24 ack_peer_process_identifier_(), | 25 ack_peer_process_identifier_(), |
| 25 ack_platform_handle_(), | 26 ack_platform_handle_(), |
| 26 event_(false, false) { // Auto-reset, not initially signalled. | 27 event_(false, false) { // Auto-reset, not initially signalled. |
| 28 DCHECK(creation_thread_task_runner_); |
| 29 AssertOnCreationThread(); // Just make sure this assertion works correctly. |
| 27 } | 30 } |
| 28 | 31 |
| 29 SlaveConnectionManager::~SlaveConnectionManager() { | 32 SlaveConnectionManager::~SlaveConnectionManager() { |
| 30 DCHECK(!delegate_thread_task_runner_); | 33 AssertOnCreationThread(); |
| 31 DCHECK(!slave_process_delegate_); | 34 DCHECK(!slave_process_delegate_); |
| 32 DCHECK(!private_thread_.message_loop()); | 35 DCHECK(!private_thread_.message_loop()); |
| 33 DCHECK_EQ(awaiting_ack_type_, NOT_AWAITING_ACK); | 36 DCHECK_EQ(awaiting_ack_type_, NOT_AWAITING_ACK); |
| 34 DCHECK(!ack_result_); | 37 DCHECK(!ack_result_); |
| 35 DCHECK(!ack_peer_process_identifier_); | 38 DCHECK(!ack_peer_process_identifier_); |
| 36 DCHECK(!ack_platform_handle_); | 39 DCHECK(!ack_platform_handle_); |
| 37 } | 40 } |
| 38 | 41 |
| 39 void SlaveConnectionManager::Init( | 42 void SlaveConnectionManager::Init( |
| 40 scoped_refptr<base::TaskRunner> delegate_thread_task_runner, | |
| 41 embedder::SlaveProcessDelegate* slave_process_delegate, | 43 embedder::SlaveProcessDelegate* slave_process_delegate, |
| 42 embedder::ScopedPlatformHandle platform_handle) { | 44 embedder::ScopedPlatformHandle platform_handle) { |
| 43 DCHECK(delegate_thread_task_runner); | 45 AssertOnCreationThread(); |
| 44 DCHECK(slave_process_delegate); | 46 DCHECK(slave_process_delegate); |
| 45 DCHECK(platform_handle.is_valid()); | 47 DCHECK(platform_handle.is_valid()); |
| 46 DCHECK(!delegate_thread_task_runner_); | |
| 47 DCHECK(!slave_process_delegate_); | 48 DCHECK(!slave_process_delegate_); |
| 48 DCHECK(!private_thread_.message_loop()); | 49 DCHECK(!private_thread_.message_loop()); |
| 49 | 50 |
| 50 delegate_thread_task_runner_ = delegate_thread_task_runner; | |
| 51 AssertOnDelegateThread(); | |
| 52 slave_process_delegate_ = slave_process_delegate; | 51 slave_process_delegate_ = slave_process_delegate; |
| 53 CHECK(private_thread_.StartWithOptions( | 52 CHECK(private_thread_.StartWithOptions( |
| 54 base::Thread::Options(base::MessageLoop::TYPE_IO, 0))); | 53 base::Thread::Options(base::MessageLoop::TYPE_IO, 0))); |
| 55 private_thread_.message_loop()->PostTask( | 54 private_thread_.message_loop()->PostTask( |
| 56 FROM_HERE, | 55 FROM_HERE, |
| 57 base::Bind(&SlaveConnectionManager::InitOnPrivateThread, | 56 base::Bind(&SlaveConnectionManager::InitOnPrivateThread, |
| 58 base::Unretained(this), base::Passed(&platform_handle))); | 57 base::Unretained(this), base::Passed(&platform_handle))); |
| 59 event_.Wait(); | 58 event_.Wait(); |
| 60 } | 59 } |
| 61 | 60 |
| 62 void SlaveConnectionManager::Shutdown() { | 61 void SlaveConnectionManager::Shutdown() { |
| 63 AssertOnDelegateThread(); | 62 AssertOnCreationThread(); |
| 64 DCHECK(slave_process_delegate_); | 63 DCHECK(slave_process_delegate_); |
| 65 DCHECK(private_thread_.message_loop()); | 64 DCHECK(private_thread_.message_loop()); |
| 66 | 65 |
| 67 // The |Stop()| will actually finish all posted tasks. | 66 // The |Stop()| will actually finish all posted tasks. |
| 68 private_thread_.message_loop()->PostTask( | 67 private_thread_.message_loop()->PostTask( |
| 69 FROM_HERE, base::Bind(&SlaveConnectionManager::ShutdownOnPrivateThread, | 68 FROM_HERE, base::Bind(&SlaveConnectionManager::ShutdownOnPrivateThread, |
| 70 base::Unretained(this))); | 69 base::Unretained(this))); |
| 71 private_thread_.Stop(); | 70 private_thread_.Stop(); |
| 72 slave_process_delegate_ = nullptr; | 71 slave_process_delegate_ = nullptr; |
| 73 delegate_thread_task_runner_ = nullptr; | |
| 74 } | 72 } |
| 75 | 73 |
| 76 bool SlaveConnectionManager::AllowConnect( | 74 bool SlaveConnectionManager::AllowConnect( |
| 77 const ConnectionIdentifier& connection_id) { | 75 const ConnectionIdentifier& connection_id) { |
| 78 AssertNotOnPrivateThread(); | 76 AssertNotOnPrivateThread(); |
| 79 | 77 |
| 80 base::AutoLock locker(lock_); | 78 base::AutoLock locker(lock_); |
| 81 bool result = false; | 79 bool result = false; |
| 82 private_thread_.message_loop()->PostTask( | 80 private_thread_.message_loop()->PostTask( |
| 83 FROM_HERE, | 81 FROM_HERE, |
| (...skipping 198 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 282 AssertOnPrivateThread(); | 280 AssertOnPrivateThread(); |
| 283 | 281 |
| 284 // Ignore write errors, since we may still have some messages to read. | 282 // Ignore write errors, since we may still have some messages to read. |
| 285 if (error == RawChannel::Delegate::ERROR_WRITE) | 283 if (error == RawChannel::Delegate::ERROR_WRITE) |
| 286 return; | 284 return; |
| 287 | 285 |
| 288 raw_channel_->Shutdown(); | 286 raw_channel_->Shutdown(); |
| 289 raw_channel_.reset(); | 287 raw_channel_.reset(); |
| 290 | 288 |
| 291 DCHECK(slave_process_delegate_); | 289 DCHECK(slave_process_delegate_); |
| 292 delegate_thread_task_runner_->PostTask( | 290 creation_thread_task_runner_->PostTask( |
| 293 FROM_HERE, base::Bind(&embedder::SlaveProcessDelegate::OnMasterDisconnect, | 291 FROM_HERE, base::Bind(&embedder::SlaveProcessDelegate::OnMasterDisconnect, |
| 294 base::Unretained(slave_process_delegate_))); | 292 base::Unretained(slave_process_delegate_))); |
| 295 } | 293 } |
| 296 | 294 |
| 297 void SlaveConnectionManager::AssertOnDelegateThread() const { | 295 void SlaveConnectionManager::AssertOnCreationThread() const { |
| 298 DCHECK(base::MessageLoop::current()); | 296 DCHECK(base::MessageLoop::current()); |
| 299 DCHECK_EQ(base::MessageLoop::current()->task_runner(), | 297 DCHECK_EQ(base::MessageLoop::current()->task_runner(), |
| 300 delegate_thread_task_runner_); | 298 creation_thread_task_runner_); |
| 301 } | 299 } |
| 302 | 300 |
| 303 void SlaveConnectionManager::AssertNotOnPrivateThread() const { | 301 void SlaveConnectionManager::AssertNotOnPrivateThread() const { |
| 304 // This should only be called after |Init()| and before |Shutdown()|. (If not, | 302 // This should only be called after |Init()| and before |Shutdown()|. (If not, |
| 305 // the subsequent |DCHECK_NE()| is invalid, since the current thread may not | 303 // the subsequent |DCHECK_NE()| is invalid, since the current thread may not |
| 306 // have a message loop.) | 304 // have a message loop.) |
| 307 DCHECK(private_thread_.message_loop()); | 305 DCHECK(private_thread_.message_loop()); |
| 308 DCHECK_NE(base::MessageLoop::current(), private_thread_.message_loop()); | 306 DCHECK_NE(base::MessageLoop::current(), private_thread_.message_loop()); |
| 309 } | 307 } |
| 310 | 308 |
| 311 void SlaveConnectionManager::AssertOnPrivateThread() const { | 309 void SlaveConnectionManager::AssertOnPrivateThread() const { |
| 312 // This should only be called after |Init()| and before |Shutdown()|. | 310 // This should only be called after |Init()| and before |Shutdown()|. |
| 313 DCHECK(private_thread_.message_loop()); | 311 DCHECK(private_thread_.message_loop()); |
| 314 DCHECK_EQ(base::MessageLoop::current(), private_thread_.message_loop()); | 312 DCHECK_EQ(base::MessageLoop::current(), private_thread_.message_loop()); |
| 315 } | 313 } |
| 316 | 314 |
| 317 } // namespace system | 315 } // namespace system |
| 318 } // namespace mojo | 316 } // namespace mojo |
| OLD | NEW |