Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(393)

Side by Side Diff: chrome/test/base/mojo_test_connector.cc

Issue 2680973006: Mojo EDK: Add safe process connection API (Closed)
Patch Set: . Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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"
30 #include "services/service_manager/runner/host/service_process_launcher.h" 31 #include "services/service_manager/runner/host/service_process_launcher.h"
31 #include "services/service_manager/service_manager.h" 32 #include "services/service_manager/service_manager.h"
32 #include "services/service_manager/switches.h" 33 #include "services/service_manager/switches.h"
33 34
34 namespace { 35 namespace {
35 36
36 const char kTestRunnerName[] = "mash_browser_tests"; 37 const char kTestRunnerName[] = "mash_browser_tests";
37 const char kTestName[] = "content_browser"; 38 const char kTestName[] = "content_browser";
38 39
39 // State created per test to register a client process with the background 40 // State created per test to register a client process with the background
40 // service manager. 41 // service manager.
41 class MojoTestState : public content::TestState { 42 class MojoTestState : public content::TestState {
42 public: 43 public:
43 explicit MojoTestState( 44 explicit MojoTestState(
44 service_manager::BackgroundServiceManager* background_service_manager) 45 service_manager::BackgroundServiceManager* background_service_manager)
45 : child_token_(mojo::edk::GenerateRandomToken()), 46 : background_service_manager_(background_service_manager),
46 background_service_manager_(background_service_manager),
47 weak_factory_(this) {} 47 weak_factory_(this) {}
48 ~MojoTestState() override {} 48 ~MojoTestState() override {}
49 49
50 void Init(base::CommandLine* command_line, 50 void Init(base::CommandLine* command_line,
51 base::TestLauncher::LaunchOptions* test_launch_options) { 51 base::TestLauncher::LaunchOptions* test_launch_options) {
52 command_line->AppendSwitch(MojoTestConnector::kTestSwitch); 52 command_line->AppendSwitch(MojoTestConnector::kTestSwitch);
53 command_line->AppendSwitch(switches::kChildProcess); 53 command_line->AppendSwitch(switches::kChildProcess);
54 54
55 platform_channel_ = base::MakeUnique<mojo::edk::PlatformChannelPair>(); 55 platform_channel_ = base::MakeUnique<mojo::edk::PlatformChannelPair>();
56 56
57 platform_channel_->PrepareToPassClientHandleToChildProcess( 57 platform_channel_->PrepareToPassClientHandleToChildProcess(
58 command_line, &handle_passing_info_); 58 command_line, &handle_passing_info_);
59 #if defined(OS_WIN) 59 #if defined(OS_WIN)
60 test_launch_options->inherit_handles = true; 60 test_launch_options->inherit_handles = true;
61 test_launch_options->handles_to_inherit = &handle_passing_info_; 61 test_launch_options->handles_to_inherit = &handle_passing_info_;
62 #if defined(OFFICIAL_BUILD) 62 #if defined(OFFICIAL_BUILD)
63 CHECK(false) << "Launching mojo process with inherit_handles is insecure!"; 63 CHECK(false) << "Launching mojo process with inherit_handles is insecure!";
64 #endif 64 #endif
65 #elif defined(OS_POSIX) 65 #elif defined(OS_POSIX)
66 test_launch_options->fds_to_remap = &handle_passing_info_; 66 test_launch_options->fds_to_remap = &handle_passing_info_;
67 #else 67 #else
68 #error "Unsupported" 68 #error "Unsupported"
69 #endif 69 #endif
70 service_manager::mojom::ServicePtr service = 70 service_manager::mojom::ServicePtr service =
71 service_manager::PassServiceRequestOnCommandLine(command_line, 71 service_manager::PassServiceRequestOnCommandLine(&process_connection_,
72 child_token_); 72 command_line);
73 73
74 background_service_manager_->RegisterService( 74 background_service_manager_->RegisterService(
75 service_manager::Identity( 75 service_manager::Identity(
76 kTestName, service_manager::mojom::kRootUserID), 76 kTestName, service_manager::mojom::kRootUserID),
77 std::move(service), 77 std::move(service),
78 service_manager::mojom::PIDReceiverRequest(&pid_receiver_)); 78 service_manager::mojom::PIDReceiverRequest(&pid_receiver_));
79 79
80 // ChildProcessLaunched may be called on an arbitrary thread, so track the 80 // 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. 81 // current TaskRunner and post back to it when we want to send the PID.
82 main_task_runner_ = base::ThreadTaskRunnerHandle::Get(); 82 main_task_runner_ = base::ThreadTaskRunnerHandle::Get();
83 } 83 }
84 84
85 private: 85 private:
86 // content::TestState: 86 // content::TestState:
87 void ChildProcessLaunched(base::ProcessHandle handle, 87 void ChildProcessLaunched(base::ProcessHandle handle,
88 base::ProcessId pid) override { 88 base::ProcessId pid) override {
89 platform_channel_->ChildProcessLaunched(); 89 platform_channel_->ChildProcessLaunched();
90 mojo::edk::ChildProcessLaunched( 90 process_connection_.Connect(handle, platform_channel_->PassServerHandle());
91 handle, platform_channel_->PassServerHandle(), child_token_);
92 91
93 main_task_runner_->PostTask( 92 main_task_runner_->PostTask(
94 FROM_HERE, 93 FROM_HERE,
95 base::Bind(&MojoTestState::SetPID, weak_factory_.GetWeakPtr(), pid)); 94 base::Bind(&MojoTestState::SetPID, weak_factory_.GetWeakPtr(), pid));
96 } 95 }
97 96
98 // Called on the main thread only. 97 // Called on the main thread only.
99 void SetPID(base::ProcessId pid) { 98 void SetPID(base::ProcessId pid) {
100 DCHECK(pid_receiver_.is_bound()); 99 DCHECK(pid_receiver_.is_bound());
101 pid_receiver_->SetPID(pid); 100 pid_receiver_->SetPID(pid);
102 pid_receiver_.reset(); 101 pid_receiver_.reset();
103 } 102 }
104 103
105 const std::string child_token_; 104 mojo::edk::PendingProcessConnection process_connection_;
106 service_manager::BackgroundServiceManager* const background_service_manager_; 105 service_manager::BackgroundServiceManager* const background_service_manager_;
107 106
108 // NOTE: HandlePassingInformation must remain valid through process launch, 107 // NOTE: HandlePassingInformation must remain valid through process launch,
109 // hence it lives here instead of within Init()'s stack. 108 // hence it lives here instead of within Init()'s stack.
110 mojo::edk::HandlePassingInformation handle_passing_info_; 109 mojo::edk::HandlePassingInformation handle_passing_info_;
111 110
112 std::unique_ptr<mojo::edk::PlatformChannelPair> platform_channel_; 111 std::unique_ptr<mojo::edk::PlatformChannelPair> platform_channel_;
113 service_manager::mojom::PIDReceiverPtr pid_receiver_; 112 service_manager::mojom::PIDReceiverPtr pid_receiver_;
114 scoped_refptr<base::TaskRunner> main_task_runner_ = nullptr; 113 scoped_refptr<base::TaskRunner> main_task_runner_ = nullptr;
115 114
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after
204 MojoTestConnector::~MojoTestConnector() {} 203 MojoTestConnector::~MojoTestConnector() {}
205 204
206 std::unique_ptr<content::TestState> MojoTestConnector::PrepareForTest( 205 std::unique_ptr<content::TestState> MojoTestConnector::PrepareForTest(
207 base::CommandLine* command_line, 206 base::CommandLine* command_line,
208 base::TestLauncher::LaunchOptions* test_launch_options) { 207 base::TestLauncher::LaunchOptions* test_launch_options) {
209 auto test_state = 208 auto test_state =
210 base::MakeUnique<MojoTestState>(&background_service_manager_); 209 base::MakeUnique<MojoTestState>(&background_service_manager_);
211 test_state->Init(command_line, test_launch_options); 210 test_state->Init(command_line, test_launch_options);
212 return test_state; 211 return test_state;
213 } 212 }
OLDNEW
« no previous file with comments | « chrome/service/service_utility_process_host.cc ('k') | chrome/utility/importer/firefox_importer_unittest_utils_mac.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698