| 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" |
| 11 #include "base/memory/weak_ptr.h" | 11 #include "base/memory/weak_ptr.h" |
| 12 #include "base/message_loop/message_loop.h" | 12 #include "base/message_loop/message_loop.h" |
| 13 #include "base/run_loop.h" | 13 #include "base/run_loop.h" |
| 14 #include "base/strings/string_number_conversions.h" | 14 #include "base/strings/string_number_conversions.h" |
| 15 #include "base/synchronization/waitable_event.h" | 15 #include "base/synchronization/waitable_event.h" |
| 16 #include "base/threading/thread.h" | 16 #include "base/threading/thread.h" |
| 17 #include "content/public/common/content_switches.h" | 17 #include "content/public/common/content_switches.h" |
| 18 #include "content/public/test/test_launcher.h" | 18 #include "content/public/test/test_launcher.h" |
| 19 #include "mojo/edk/embedder/embedder.h" | 19 #include "mojo/edk/embedder/embedder.h" |
| 20 #include "mojo/edk/embedder/pending_process_connection.h" |
| 20 #include "mojo/edk/embedder/platform_channel_pair.h" | 21 #include "mojo/edk/embedder/platform_channel_pair.h" |
| 21 #include "mojo/edk/embedder/scoped_ipc_support.h" | 22 #include "mojo/edk/embedder/scoped_ipc_support.h" |
| 22 #include "mojo/public/cpp/bindings/interface_request.h" | 23 #include "mojo/public/cpp/bindings/interface_request.h" |
| 23 #include "services/catalog/store.h" | 24 #include "services/catalog/store.h" |
| 24 #include "services/service_manager/background/background_service_manager.h" | 25 #include "services/service_manager/background/background_service_manager.h" |
| 25 #include "services/service_manager/public/cpp/connector.h" | 26 #include "services/service_manager/public/cpp/connector.h" |
| 26 #include "services/service_manager/public/cpp/service.h" | 27 #include "services/service_manager/public/cpp/service.h" |
| 27 #include "services/service_manager/public/cpp/service_context.h" | 28 #include "services/service_manager/public/cpp/service_context.h" |
| 28 #include "services/service_manager/runner/common/client_util.h" | 29 #include "services/service_manager/runner/common/client_util.h" |
| 29 #include "services/service_manager/runner/common/switches.h" | 30 #include "services/service_manager/runner/common/switches.h" |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 61 test_launch_options->handles_to_inherit = &handle_passing_info_; | 62 test_launch_options->handles_to_inherit = &handle_passing_info_; |
| 62 #if defined(OFFICIAL_BUILD) | 63 #if defined(OFFICIAL_BUILD) |
| 63 CHECK(false) << "Launching mojo process with inherit_handles is insecure!"; | 64 CHECK(false) << "Launching mojo process with inherit_handles is insecure!"; |
| 64 #endif | 65 #endif |
| 65 #elif defined(OS_POSIX) | 66 #elif defined(OS_POSIX) |
| 66 test_launch_options->fds_to_remap = &handle_passing_info_; | 67 test_launch_options->fds_to_remap = &handle_passing_info_; |
| 67 #else | 68 #else |
| 68 #error "Unsupported" | 69 #error "Unsupported" |
| 69 #endif | 70 #endif |
| 70 service_manager::mojom::ServicePtr service = | 71 service_manager::mojom::ServicePtr service = |
| 71 service_manager::PassServiceRequestOnCommandLine(command_line, | 72 service_manager::PassServiceRequestOnCommandLine(process_connection_, |
| 72 child_token_); | 73 command_line); |
| 73 | 74 |
| 74 background_service_manager_->RegisterService( | 75 background_service_manager_->RegisterService( |
| 75 service_manager::Identity( | 76 service_manager::Identity( |
| 76 kTestName, service_manager::mojom::kRootUserID), | 77 kTestName, service_manager::mojom::kRootUserID), |
| 77 std::move(service), | 78 std::move(service), |
| 78 service_manager::mojom::PIDReceiverRequest(&pid_receiver_)); | 79 service_manager::mojom::PIDReceiverRequest(&pid_receiver_)); |
| 79 | 80 |
| 80 // ChildProcessLaunched may be called on an arbitrary thread, so track the | 81 // ChildProcessLaunched may be called on an arbitrary thread, so track the |
| 81 // current TaskRunner and post back to it when we want to send the PID. | 82 // current TaskRunner and post back to it when we want to send the PID. |
| 82 main_task_runner_ = base::ThreadTaskRunnerHandle::Get(); | 83 main_task_runner_ = base::ThreadTaskRunnerHandle::Get(); |
| 83 } | 84 } |
| 84 | 85 |
| 85 private: | 86 private: |
| 86 // content::TestState: | 87 // content::TestState: |
| 87 void ChildProcessLaunched(base::ProcessHandle handle, | 88 void ChildProcessLaunched(base::ProcessHandle handle, |
| 88 base::ProcessId pid) override { | 89 base::ProcessId pid) override { |
| 89 platform_channel_->ChildProcessLaunched(); | 90 platform_channel_->ChildProcessLaunched(); |
| 90 mojo::edk::ChildProcessLaunched( | 91 process_connection_.Connect(handle, platform_channel_->PassServerHandle()); |
| 91 handle, platform_channel_->PassServerHandle(), child_token_); | |
| 92 | 92 |
| 93 main_task_runner_->PostTask( | 93 main_task_runner_->PostTask( |
| 94 FROM_HERE, | 94 FROM_HERE, |
| 95 base::Bind(&MojoTestState::SetPID, weak_factory_.GetWeakPtr(), pid)); | 95 base::Bind(&MojoTestState::SetPID, weak_factory_.GetWeakPtr(), pid)); |
| 96 } | 96 } |
| 97 | 97 |
| 98 // Called on the main thread only. | 98 // Called on the main thread only. |
| 99 void SetPID(base::ProcessId pid) { | 99 void SetPID(base::ProcessId pid) { |
| 100 DCHECK(pid_receiver_.is_bound()); | 100 DCHECK(pid_receiver_.is_bound()); |
| 101 pid_receiver_->SetPID(pid); | 101 pid_receiver_->SetPID(pid); |
| 102 pid_receiver_.reset(); | 102 pid_receiver_.reset(); |
| 103 } | 103 } |
| 104 | 104 |
| 105 const std::string child_token_; | 105 mojo::edk::PendingProcessConnection process_connection_; |
| 106 service_manager::BackgroundServiceManager* const background_service_manager_; | 106 service_manager::BackgroundServiceManager* const background_service_manager_; |
| 107 | 107 |
| 108 // NOTE: HandlePassingInformation must remain valid through process launch, | 108 // NOTE: HandlePassingInformation must remain valid through process launch, |
| 109 // hence it lives here instead of within Init()'s stack. | 109 // hence it lives here instead of within Init()'s stack. |
| 110 mojo::edk::HandlePassingInformation handle_passing_info_; | 110 mojo::edk::HandlePassingInformation handle_passing_info_; |
| 111 | 111 |
| 112 std::unique_ptr<mojo::edk::PlatformChannelPair> platform_channel_; | 112 std::unique_ptr<mojo::edk::PlatformChannelPair> platform_channel_; |
| 113 service_manager::mojom::PIDReceiverPtr pid_receiver_; | 113 service_manager::mojom::PIDReceiverPtr pid_receiver_; |
| 114 scoped_refptr<base::TaskRunner> main_task_runner_ = nullptr; | 114 scoped_refptr<base::TaskRunner> main_task_runner_ = nullptr; |
| 115 | 115 |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 204 MojoTestConnector::~MojoTestConnector() {} | 204 MojoTestConnector::~MojoTestConnector() {} |
| 205 | 205 |
| 206 std::unique_ptr<content::TestState> MojoTestConnector::PrepareForTest( | 206 std::unique_ptr<content::TestState> MojoTestConnector::PrepareForTest( |
| 207 base::CommandLine* command_line, | 207 base::CommandLine* command_line, |
| 208 base::TestLauncher::LaunchOptions* test_launch_options) { | 208 base::TestLauncher::LaunchOptions* test_launch_options) { |
| 209 auto test_state = | 209 auto test_state = |
| 210 base::MakeUnique<MojoTestState>(&background_service_manager_); | 210 base::MakeUnique<MojoTestState>(&background_service_manager_); |
| 211 test_state->Init(command_line, test_launch_options); | 211 test_state->Init(command_line, test_launch_options); |
| 212 return test_state; | 212 return test_state; |
| 213 } | 213 } |
| OLD | NEW |