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