| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "ipc/ipc_test_base.h" | 5 #include "ipc/ipc_test_base.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/memory/ptr_util.h" | 9 #include "base/memory/ptr_util.h" |
| 10 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
| 11 #include "base/single_thread_task_runner.h" | 11 #include "base/single_thread_task_runner.h" |
| 12 #include "base/threading/thread_task_runner_handle.h" | 12 #include "base/threading/thread_task_runner_handle.h" |
| 13 #include "build/build_config.h" | 13 #include "build/build_config.h" |
| 14 #include "ipc/ipc_channel_mojo.h" | 14 #include "ipc/ipc_channel_mojo.h" |
| 15 | 15 |
| 16 IPCChannelMojoTestBase::IPCChannelMojoTestBase() = default; | 16 #if defined(OS_MACOSX) && !defined(OS_IOS) |
| 17 #include "base/mac/mach_port_broker.h" |
| 18 |
| 19 namespace { |
| 20 base::MachPortBroker* g_mach_broker = nullptr; |
| 21 } |
| 22 #endif |
| 23 |
| 24 IPCChannelMojoTestBase::IPCChannelMojoTestBase() { |
| 25 #if defined(OS_MACOSX) && !defined(OS_IOS) |
| 26 if (!g_mach_broker) { |
| 27 g_mach_broker = new base::MachPortBroker("mojo_test"); |
| 28 CHECK(g_mach_broker->Init()); |
| 29 mojo::edk::SetMachPortProvider(g_mach_broker); |
| 30 } |
| 31 #endif |
| 32 } |
| 17 IPCChannelMojoTestBase::~IPCChannelMojoTestBase() = default; | 33 IPCChannelMojoTestBase::~IPCChannelMojoTestBase() = default; |
| 18 | 34 |
| 19 void IPCChannelMojoTestBase::Init(const std::string& test_client_name) { | 35 void IPCChannelMojoTestBase::Init(const std::string& test_client_name) { |
| 20 InitWithCustomMessageLoop(test_client_name, | 36 InitWithCustomMessageLoop(test_client_name, |
| 21 base::MakeUnique<base::MessageLoop>()); | 37 base::MakeUnique<base::MessageLoop>()); |
| 22 } | 38 } |
| 23 | 39 |
| 24 void IPCChannelMojoTestBase::InitWithCustomMessageLoop( | 40 void IPCChannelMojoTestBase::InitWithCustomMessageLoop( |
| 25 const std::string& test_client_name, | 41 const std::string& test_client_name, |
| 26 std::unique_ptr<base::MessageLoop> message_loop) { | 42 std::unique_ptr<base::MessageLoop> message_loop) { |
| 43 #if defined(OS_MACOSX) && !defined(OS_IOS) |
| 44 // This lock needs to be held while launching the child because the Mach port |
| 45 // broker only allows task ports to be received from known child processes. |
| 46 // However, it can only know the child process's pid after the child has |
| 47 // launched. To prevent a race where the child process sends its task port |
| 48 // before the pid has been registered, the lock needs to be held over both |
| 49 // launch and child pid registration. |
| 50 base::AutoLock lock(g_mach_broker->GetLock()); |
| 51 #endif |
| 27 handle_ = helper_.StartChild(test_client_name); | 52 handle_ = helper_.StartChild(test_client_name); |
| 53 #if defined(OS_MACOSX) && !defined(OS_IOS) |
| 54 g_mach_broker->AddPlaceholderForPid(helper_.test_child().Handle()); |
| 55 #endif |
| 28 message_loop_ = std::move(message_loop); | 56 message_loop_ = std::move(message_loop); |
| 29 } | 57 } |
| 30 | 58 |
| 31 bool IPCChannelMojoTestBase::WaitForClientShutdown() { | 59 bool IPCChannelMojoTestBase::WaitForClientShutdown() { |
| 32 return helper_.WaitForChildTestShutdown(); | 60 int retval = helper_.WaitForChildTestShutdown(); |
| 61 #if defined(OS_MACOSX) && !defined(OS_IOS) |
| 62 base::AutoLock lock(g_mach_broker->GetLock()); |
| 63 g_mach_broker->InvalidatePid(helper_.test_child().Handle()); |
| 64 #endif |
| 65 return retval; |
| 33 } | 66 } |
| 34 | 67 |
| 35 void IPCChannelMojoTestBase::TearDown() { | 68 void IPCChannelMojoTestBase::TearDown() { |
| 36 if (message_loop_) | 69 if (message_loop_) |
| 37 base::RunLoop().RunUntilIdle(); | 70 base::RunLoop().RunUntilIdle(); |
| 38 } | 71 } |
| 39 | 72 |
| 40 void IPCChannelMojoTestBase::CreateChannel(IPC::Listener* listener) { | 73 void IPCChannelMojoTestBase::CreateChannel(IPC::Listener* listener) { |
| 41 channel_ = | 74 channel_ = |
| 42 IPC::ChannelMojo::Create(TakeHandle(), IPC::Channel::MODE_SERVER, | 75 IPC::ChannelMojo::Create(TakeHandle(), IPC::Channel::MODE_SERVER, |
| (...skipping 28 matching lines...) Expand all Loading... |
| 71 } | 104 } |
| 72 | 105 |
| 73 void IpcChannelMojoTestClient::Close() { | 106 void IpcChannelMojoTestClient::Close() { |
| 74 channel_->Close(); | 107 channel_->Close(); |
| 75 | 108 |
| 76 base::RunLoop run_loop; | 109 base::RunLoop run_loop; |
| 77 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, | 110 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
| 78 run_loop.QuitClosure()); | 111 run_loop.QuitClosure()); |
| 79 run_loop.Run(); | 112 run_loop.Run(); |
| 80 } | 113 } |
| OLD | NEW |