OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 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 "chrome/test/base/mojo_test_connector.h" | 5 #include "chrome/test/base/mojo_test_connector.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/callback.h" | 8 #include "base/callback.h" |
9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
10 #include "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
70 std::move(required_shell_classes)); | 70 std::move(required_shell_classes)); |
71 required_capabilities->Set("mojo:shell", std::move(shell_caps)); | 71 required_capabilities->Set("mojo:shell", std::move(shell_caps)); |
72 apps->Append(std::move(test_app_config)); | 72 apps->Append(std::move(test_app_config)); |
73 return base::WrapUnique(new shell::TestCatalogStore(std::move(apps))); | 73 return base::WrapUnique(new shell::TestCatalogStore(std::move(apps))); |
74 } | 74 } |
75 | 75 |
76 // BackgroundTestState maintains all the state necessary to bind the test to | 76 // BackgroundTestState maintains all the state necessary to bind the test to |
77 // mojo. This class is only used on the thread created by BackgroundShell. | 77 // mojo. This class is only used on the thread created by BackgroundShell. |
78 class BackgroundTestState { | 78 class BackgroundTestState { |
79 public: | 79 public: |
80 BackgroundTestState() {} | 80 BackgroundTestState() : child_token_(mojo::edk::GenerateRandomToken()) {} |
81 ~BackgroundTestState() {} | 81 ~BackgroundTestState() {} |
82 | 82 |
83 // Prepares the command line and other setup for connecting the test to mojo. | 83 // Prepares the command line and other setup for connecting the test to mojo. |
84 // Must be paired with a clal to ChildProcessLaunched(). | 84 // Must be paired with a call to ChildProcessLaunched(). |
85 void Connect(base::CommandLine* command_line, | 85 void Connect(base::CommandLine* command_line, |
86 shell::Shell* shell, | 86 shell::Shell* shell, |
87 const std::string& instance, | 87 const std::string& instance, |
88 base::TestLauncher::LaunchOptions* test_launch_options) { | 88 base::TestLauncher::LaunchOptions* test_launch_options) { |
89 command_line->AppendSwitch(MojoTestConnector::kTestSwitch); | 89 command_line->AppendSwitch(MojoTestConnector::kTestSwitch); |
90 command_line->AppendSwitch(switches::kChildProcess); | 90 command_line->AppendSwitch(switches::kChildProcess); |
91 mojo_ipc_channel_.reset(new mojo::edk::PlatformChannelPair); | 91 mojo_ipc_channel_.reset(new mojo::edk::PlatformChannelPair); |
92 mojo_ipc_channel_->PrepareToPassClientHandleToChildProcess( | 92 mojo_ipc_channel_->PrepareToPassClientHandleToChildProcess( |
93 command_line, &handle_passing_info_); | 93 command_line, &handle_passing_info_); |
94 #if defined(OS_WIN) | 94 #if defined(OS_WIN) |
95 test_launch_options->inherit_handles = true; | 95 test_launch_options->inherit_handles = true; |
96 test_launch_options->handles_to_inherit = &handle_passing_info_; | 96 test_launch_options->handles_to_inherit = &handle_passing_info_; |
97 #if defined(OFFICIAL_BUILD) | 97 #if defined(OFFICIAL_BUILD) |
98 CHECK(false) << "Launching mojo process with inherit_handles is insecure!"; | 98 CHECK(false) << "Launching mojo process with inherit_handles is insecure!"; |
99 #endif | 99 #endif |
100 #elif defined(OS_POSIX) | 100 #elif defined(OS_POSIX) |
101 test_launch_options->fds_to_remap = &handle_passing_info_; | 101 test_launch_options->fds_to_remap = &handle_passing_info_; |
102 #else | 102 #else |
103 #error "Unsupported" | 103 #error "Unsupported" |
104 #endif | 104 #endif |
105 shell::mojom::ShellClientPtr client = | 105 shell::mojom::ShellClientPtr client = |
106 shell::PassShellClientRequestOnCommandLine(command_line); | 106 shell::PassShellClientRequestOnCommandLine(command_line, child_token_); |
107 | 107 |
108 std::unique_ptr<shell::ConnectParams> params(new shell::ConnectParams); | 108 std::unique_ptr<shell::ConnectParams> params(new shell::ConnectParams); |
109 params->set_source(shell::CreateShellIdentity()); | 109 params->set_source(shell::CreateShellIdentity()); |
110 params->set_target( | 110 params->set_target( |
111 shell::Identity(kTestName, shell::mojom::kRootUserID, instance)); | 111 shell::Identity(kTestName, shell::mojom::kRootUserID, instance)); |
112 | 112 |
113 shell::mojom::ClientProcessConnectionPtr client_process_connection = | 113 shell::mojom::ClientProcessConnectionPtr client_process_connection = |
114 shell::mojom::ClientProcessConnection::New(); | 114 shell::mojom::ClientProcessConnection::New(); |
115 client_process_connection->shell_client = | 115 client_process_connection->shell_client = |
116 client.PassInterface().PassHandle(); | 116 client.PassInterface().PassHandle(); |
117 client_process_connection->pid_receiver_request = | 117 client_process_connection->pid_receiver_request = |
118 mojo::GetProxy(&pid_receiver_).PassMessagePipe(); | 118 mojo::GetProxy(&pid_receiver_).PassMessagePipe(); |
119 params->set_client_process_connection(std::move(client_process_connection)); | 119 params->set_client_process_connection(std::move(client_process_connection)); |
120 shell->Connect(std::move(params)); | 120 shell->Connect(std::move(params)); |
121 } | 121 } |
122 | 122 |
123 // Called after the test process has launched. Completes the registration done | 123 // Called after the test process has launched. Completes the registration done |
124 // in Connect(). | 124 // in Connect(). |
125 void ChildProcessLaunched(base::ProcessHandle handle, base::ProcessId pid) { | 125 void ChildProcessLaunched(base::ProcessHandle handle, base::ProcessId pid) { |
126 pid_receiver_->SetPID(pid); | 126 pid_receiver_->SetPID(pid); |
127 mojo_ipc_channel_->ChildProcessLaunched(); | 127 mojo_ipc_channel_->ChildProcessLaunched(); |
128 mojo::edk::ChildProcessLaunched( | 128 mojo::edk::ChildProcessLaunched( |
129 handle, mojo::edk::ScopedPlatformHandle(mojo::edk::PlatformHandle( | 129 handle, mojo::edk::ScopedPlatformHandle(mojo::edk::PlatformHandle( |
130 mojo_ipc_channel_->PassServerHandle().release().handle))); | 130 mojo_ipc_channel_->PassServerHandle().release().handle)), |
| 131 child_token_); |
131 } | 132 } |
132 | 133 |
133 private: | 134 private: |
134 // Used to back the NodeChannel between the parent and child node. | 135 // Used to back the NodeChannel between the parent and child node. |
| 136 const std::string child_token_; |
135 std::unique_ptr<mojo::edk::PlatformChannelPair> mojo_ipc_channel_; | 137 std::unique_ptr<mojo::edk::PlatformChannelPair> mojo_ipc_channel_; |
136 | 138 |
137 mojo::edk::HandlePassingInformation handle_passing_info_; | 139 mojo::edk::HandlePassingInformation handle_passing_info_; |
138 | 140 |
139 shell::mojom::PIDReceiverPtr pid_receiver_; | 141 shell::mojom::PIDReceiverPtr pid_receiver_; |
140 | 142 |
141 DISALLOW_COPY_AND_ASSIGN(BackgroundTestState); | 143 DISALLOW_COPY_AND_ASSIGN(BackgroundTestState); |
142 }; | 144 }; |
143 | 145 |
144 // Called used destroy BackgroundTestState on the background thread. | 146 // Called used destroy BackgroundTestState on the background thread. |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
238 MojoTestConnector::~MojoTestConnector() {} | 240 MojoTestConnector::~MojoTestConnector() {} |
239 | 241 |
240 std::unique_ptr<content::TestState> MojoTestConnector::PrepareForTest( | 242 std::unique_ptr<content::TestState> MojoTestConnector::PrepareForTest( |
241 base::CommandLine* command_line, | 243 base::CommandLine* command_line, |
242 base::TestLauncher::LaunchOptions* test_launch_options) { | 244 base::TestLauncher::LaunchOptions* test_launch_options) { |
243 std::unique_ptr<MojoTestState> test_state( | 245 std::unique_ptr<MojoTestState> test_state( |
244 new MojoTestState(&background_shell_)); | 246 new MojoTestState(&background_shell_)); |
245 test_state->Init(command_line, test_launch_options); | 247 test_state->Init(command_line, test_launch_options); |
246 return std::move(test_state); | 248 return std::move(test_state); |
247 } | 249 } |
OLD | NEW |