| 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/system/message_pipe_test_utils.h" | 5 #include "mojo/system/message_pipe_test_utils.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/threading/platform_thread.h" // For |Sleep()|. | 8 #include "base/threading/platform_thread.h" // For |Sleep()|. |
| 9 #include "mojo/system/channel.h" |
| 9 #include "mojo/system/channel_endpoint.h" | 10 #include "mojo/system/channel_endpoint.h" |
| 11 #include "mojo/system/message_pipe.h" |
| 10 #include "mojo/system/waiter.h" | 12 #include "mojo/system/waiter.h" |
| 11 | 13 |
| 12 namespace mojo { | 14 namespace mojo { |
| 13 namespace system { | 15 namespace system { |
| 14 namespace test { | 16 namespace test { |
| 15 | 17 |
| 16 MojoResult WaitIfNecessary(scoped_refptr<MessagePipe> mp, | 18 MojoResult WaitIfNecessary(scoped_refptr<MessagePipe> mp, |
| 17 MojoHandleSignals signals, | 19 MojoHandleSignals signals, |
| 18 HandleSignalsState* signals_state) { | 20 HandleSignalsState* signals_state) { |
| 19 Waiter waiter; | 21 Waiter waiter; |
| (...skipping 13 matching lines...) Expand all Loading... |
| 33 ChannelThread::ChannelThread(embedder::PlatformSupport* platform_support) | 35 ChannelThread::ChannelThread(embedder::PlatformSupport* platform_support) |
| 34 : platform_support_(platform_support), | 36 : platform_support_(platform_support), |
| 35 test_io_thread_(base::TestIOThread::kManualStart) { | 37 test_io_thread_(base::TestIOThread::kManualStart) { |
| 36 } | 38 } |
| 37 | 39 |
| 38 ChannelThread::~ChannelThread() { | 40 ChannelThread::~ChannelThread() { |
| 39 Stop(); | 41 Stop(); |
| 40 } | 42 } |
| 41 | 43 |
| 42 void ChannelThread::Start(embedder::ScopedPlatformHandle platform_handle, | 44 void ChannelThread::Start(embedder::ScopedPlatformHandle platform_handle, |
| 43 scoped_refptr<MessagePipe> message_pipe) { | 45 scoped_refptr<ChannelEndpoint> channel_endpoint) { |
| 44 test_io_thread_.Start(); | 46 test_io_thread_.Start(); |
| 45 test_io_thread_.PostTaskAndWait( | 47 test_io_thread_.PostTaskAndWait( |
| 46 FROM_HERE, | 48 FROM_HERE, |
| 47 base::Bind(&ChannelThread::InitChannelOnIOThread, | 49 base::Bind(&ChannelThread::InitChannelOnIOThread, |
| 48 base::Unretained(this), | 50 base::Unretained(this), |
| 49 base::Passed(&platform_handle), | 51 base::Passed(&platform_handle), |
| 50 message_pipe)); | 52 channel_endpoint)); |
| 51 } | 53 } |
| 52 | 54 |
| 53 void ChannelThread::Stop() { | 55 void ChannelThread::Stop() { |
| 54 if (channel_.get()) { | 56 if (channel_.get()) { |
| 55 // Hack to flush write buffers before quitting. | 57 // Hack to flush write buffers before quitting. |
| 56 // TODO(vtl): Remove this once |Channel| has a | 58 // TODO(vtl): Remove this once |Channel| has a |
| 57 // |FlushWriteBufferAndShutdown()| (or whatever). | 59 // |FlushWriteBufferAndShutdown()| (or whatever). |
| 58 while (!channel_->IsWriteBufferEmpty()) | 60 while (!channel_->IsWriteBufferEmpty()) |
| 59 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(20)); | 61 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(20)); |
| 60 | 62 |
| 61 test_io_thread_.PostTaskAndWait( | 63 test_io_thread_.PostTaskAndWait( |
| 62 FROM_HERE, | 64 FROM_HERE, |
| 63 base::Bind(&ChannelThread::ShutdownChannelOnIOThread, | 65 base::Bind(&ChannelThread::ShutdownChannelOnIOThread, |
| 64 base::Unretained(this))); | 66 base::Unretained(this))); |
| 65 } | 67 } |
| 66 test_io_thread_.Stop(); | 68 test_io_thread_.Stop(); |
| 67 } | 69 } |
| 68 | 70 |
| 69 void ChannelThread::InitChannelOnIOThread( | 71 void ChannelThread::InitChannelOnIOThread( |
| 70 embedder::ScopedPlatformHandle platform_handle, | 72 embedder::ScopedPlatformHandle platform_handle, |
| 71 scoped_refptr<MessagePipe> message_pipe) { | 73 scoped_refptr<ChannelEndpoint> channel_endpoint) { |
| 72 CHECK_EQ(base::MessageLoop::current(), test_io_thread_.message_loop()); | 74 CHECK_EQ(base::MessageLoop::current(), test_io_thread_.message_loop()); |
| 73 CHECK(platform_handle.is_valid()); | 75 CHECK(platform_handle.is_valid()); |
| 74 | 76 |
| 75 // Create and initialize |Channel|. | 77 // Create and initialize |Channel|. |
| 76 channel_ = new Channel(platform_support_); | 78 channel_ = new Channel(platform_support_); |
| 77 CHECK(channel_->Init(RawChannel::Create(platform_handle.Pass()))); | 79 CHECK(channel_->Init(RawChannel::Create(platform_handle.Pass()))); |
| 78 | 80 |
| 79 // Attach the message pipe endpoint. | 81 // Attach the message pipe endpoint. |
| 80 // Note: On the "server" (parent process) side, we need not attach the | 82 // Note: On the "server" (parent process) side, we need not attach the |
| 81 // message pipe endpoint immediately. However, on the "client" (child | 83 // message pipe endpoint immediately. However, on the "client" (child |
| 82 // process) side, this *must* be done here -- otherwise, the |Channel| may | 84 // process) side, this *must* be done here -- otherwise, the |Channel| may |
| 83 // receive/process messages (which it can do as soon as it's hooked up to | 85 // receive/process messages (which it can do as soon as it's hooked up to |
| 84 // the IO thread message loop, and that message loop runs) before the | 86 // the IO thread message loop, and that message loop runs) before the |
| 85 // message pipe endpoint is attached. | 87 // message pipe endpoint is attached. |
| 86 CHECK_EQ(channel_->AttachEndpoint( | 88 CHECK_EQ(channel_->AttachEndpoint(channel_endpoint), |
| 87 make_scoped_refptr(new ChannelEndpoint(message_pipe.get(), 1))), | |
| 88 Channel::kBootstrapEndpointId); | 89 Channel::kBootstrapEndpointId); |
| 89 CHECK(channel_->RunMessagePipeEndpoint(Channel::kBootstrapEndpointId, | 90 CHECK(channel_->RunMessagePipeEndpoint(Channel::kBootstrapEndpointId, |
| 90 Channel::kBootstrapEndpointId)); | 91 Channel::kBootstrapEndpointId)); |
| 91 } | 92 } |
| 92 | 93 |
| 93 void ChannelThread::ShutdownChannelOnIOThread() { | 94 void ChannelThread::ShutdownChannelOnIOThread() { |
| 94 CHECK(channel_.get()); | 95 CHECK(channel_.get()); |
| 95 channel_->Shutdown(); | 96 channel_->Shutdown(); |
| 96 channel_ = NULL; | 97 channel_ = NULL; |
| 97 } | 98 } |
| 98 | 99 |
| 99 #if !defined(OS_IOS) | 100 #if !defined(OS_IOS) |
| 100 MultiprocessMessagePipeTestBase::MultiprocessMessagePipeTestBase() | 101 MultiprocessMessagePipeTestBase::MultiprocessMessagePipeTestBase() |
| 101 : channel_thread_(&platform_support_) { | 102 : channel_thread_(&platform_support_) { |
| 102 } | 103 } |
| 103 | 104 |
| 104 MultiprocessMessagePipeTestBase::~MultiprocessMessagePipeTestBase() { | 105 MultiprocessMessagePipeTestBase::~MultiprocessMessagePipeTestBase() { |
| 105 } | 106 } |
| 106 | 107 |
| 107 void MultiprocessMessagePipeTestBase::Init(scoped_refptr<MessagePipe> mp) { | 108 void MultiprocessMessagePipeTestBase::Init(scoped_refptr<ChannelEndpoint> ep) { |
| 108 channel_thread_.Start(helper_.server_platform_handle.Pass(), mp); | 109 channel_thread_.Start(helper_.server_platform_handle.Pass(), ep); |
| 109 } | 110 } |
| 110 #endif | 111 #endif |
| 111 | 112 |
| 112 } // namespace test | 113 } // namespace test |
| 113 } // namespace system | 114 } // namespace system |
| 114 } // namespace mojo | 115 } // namespace mojo |
| OLD | NEW |