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