| OLD | NEW |
| (Empty) |
| 1 // Copyright 2016 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 "services/shell/tests/util.h" | |
| 6 | |
| 7 #include "base/base_paths.h" | |
| 8 #include "base/base_switches.h" | |
| 9 #include "base/bind.h" | |
| 10 #include "base/command_line.h" | |
| 11 #include "base/files/file_path.h" | |
| 12 #include "base/macros.h" | |
| 13 #include "base/message_loop/message_loop.h" | |
| 14 #include "base/path_service.h" | |
| 15 #include "base/process/process.h" | |
| 16 #include "base/run_loop.h" | |
| 17 #include "mojo/edk/embedder/embedder.h" | |
| 18 #include "mojo/edk/embedder/platform_channel_pair.h" | |
| 19 #include "mojo/edk/embedder/scoped_platform_handle.h" | |
| 20 #include "services/shell/public/cpp/connection.h" | |
| 21 #include "services/shell/public/cpp/connector.h" | |
| 22 #include "services/shell/public/interfaces/connector.mojom.h" | |
| 23 #include "services/shell/public/interfaces/service_factory.mojom.h" | |
| 24 #include "services/shell/runner/common/switches.h" | |
| 25 | |
| 26 namespace shell { | |
| 27 namespace test { | |
| 28 | |
| 29 namespace { | |
| 30 | |
| 31 void QuitLoop(base::RunLoop* loop) { | |
| 32 loop->Quit(); | |
| 33 } | |
| 34 | |
| 35 } // namespace | |
| 36 | |
| 37 std::unique_ptr<Connection> LaunchAndConnectToProcess( | |
| 38 const std::string& target_exe_name, | |
| 39 const Identity target, | |
| 40 shell::Connector* connector, | |
| 41 base::Process* process) { | |
| 42 base::FilePath target_path; | |
| 43 CHECK(base::PathService::Get(base::DIR_EXE, &target_path)); | |
| 44 target_path = target_path.AppendASCII(target_exe_name); | |
| 45 | |
| 46 base::CommandLine child_command_line(target_path); | |
| 47 // Forward the wait-for-debugger flag but nothing else - we don't want to | |
| 48 // stamp on the platform-channel flag. | |
| 49 if (base::CommandLine::ForCurrentProcess()->HasSwitch( | |
| 50 switches::kWaitForDebugger)) { | |
| 51 child_command_line.AppendSwitch(switches::kWaitForDebugger); | |
| 52 } | |
| 53 | |
| 54 // Create the channel to be shared with the target process. Pass one end | |
| 55 // on the command line. | |
| 56 mojo::edk::PlatformChannelPair platform_channel_pair; | |
| 57 mojo::edk::HandlePassingInformation handle_passing_info; | |
| 58 platform_channel_pair.PrepareToPassClientHandleToChildProcess( | |
| 59 &child_command_line, &handle_passing_info); | |
| 60 | |
| 61 // Generate a token for the child to find and connect to a primordial pipe | |
| 62 // and pass that as well. | |
| 63 std::string primordial_pipe_token = mojo::edk::GenerateRandomToken(); | |
| 64 child_command_line.AppendSwitchASCII(switches::kPrimordialPipeToken, | |
| 65 primordial_pipe_token); | |
| 66 | |
| 67 // Allocate the pipe locally. | |
| 68 std::string child_token = mojo::edk::GenerateRandomToken(); | |
| 69 mojo::ScopedMessagePipeHandle pipe = | |
| 70 mojo::edk::CreateParentMessagePipe(primordial_pipe_token, child_token); | |
| 71 | |
| 72 shell::mojom::ServicePtr client; | |
| 73 client.Bind( | |
| 74 mojo::InterfacePtrInfo<shell::mojom::Service>(std::move(pipe), 0u)); | |
| 75 shell::mojom::PIDReceiverPtr receiver; | |
| 76 | |
| 77 shell::Connector::ConnectParams params(target); | |
| 78 params.set_client_process_connection(std::move(client), GetProxy(&receiver)); | |
| 79 std::unique_ptr<shell::Connection> connection = connector->Connect(¶ms); | |
| 80 { | |
| 81 base::RunLoop loop; | |
| 82 connection->AddConnectionCompletedClosure(base::Bind(&QuitLoop, &loop)); | |
| 83 base::MessageLoop::ScopedNestableTaskAllower allow( | |
| 84 base::MessageLoop::current()); | |
| 85 loop.Run(); | |
| 86 } | |
| 87 | |
| 88 base::LaunchOptions options; | |
| 89 #if defined(OS_WIN) | |
| 90 options.handles_to_inherit = &handle_passing_info; | |
| 91 #elif defined(OS_POSIX) | |
| 92 options.fds_to_remap = &handle_passing_info; | |
| 93 #endif | |
| 94 *process = base::LaunchProcess(child_command_line, options); | |
| 95 DCHECK(process->IsValid()); | |
| 96 receiver->SetPID(process->Pid()); | |
| 97 mojo::edk::ChildProcessLaunched(process->Handle(), | |
| 98 platform_channel_pair.PassServerHandle(), | |
| 99 child_token); | |
| 100 return connection; | |
| 101 } | |
| 102 | |
| 103 } // namespace test | |
| 104 } // namespace shell | |
| OLD | NEW |