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

Side by Side Diff: content/browser/child_process_launcher_helper.cc

Issue 2686983003: Avoid a thread hop when initializing IPC to new processes (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 2017 The Chromium Authors. All rights reserved. 1 // Copyright 2017 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 "content/browser/child_process_launcher_helper.h" 5 #include "content/browser/child_process_launcher_helper.h"
6 6
7 #include "base/metrics/histogram_macros.h" 7 #include "base/metrics/histogram_macros.h"
8 #include "content/browser/child_process_launcher.h" 8 #include "content/browser/child_process_launcher.h"
9 #include "content/public/common/content_switches.h" 9 #include "content/public/common/content_switches.h"
10 #include "content/public/common/sandboxed_process_launcher_delegate.h" 10 #include "content/public/common/sandboxed_process_launcher_delegate.h"
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
47 zygote = other.zygote; 47 zygote = other.zygote;
48 #endif 48 #endif
49 return *this; 49 return *this;
50 } 50 }
51 51
52 ChildProcessLauncherHelper::ChildProcessLauncherHelper( 52 ChildProcessLauncherHelper::ChildProcessLauncherHelper(
53 int child_process_id, 53 int child_process_id,
54 BrowserThread::ID client_thread_id, 54 BrowserThread::ID client_thread_id,
55 std::unique_ptr<base::CommandLine> command_line, 55 std::unique_ptr<base::CommandLine> command_line,
56 std::unique_ptr<SandboxedProcessLauncherDelegate> delegate, 56 std::unique_ptr<SandboxedProcessLauncherDelegate> delegate,
57 std::unique_ptr<mojo::edk::PendingProcessConnection> pending_connection,
58 const mojo::edk::ProcessErrorCallback& process_error_callback,
57 const base::WeakPtr<ChildProcessLauncher>& child_process_launcher, 59 const base::WeakPtr<ChildProcessLauncher>& child_process_launcher,
58 bool terminate_on_shutdown) 60 bool terminate_on_shutdown)
59 : child_process_id_(child_process_id), 61 : child_process_id_(child_process_id),
60 client_thread_id_(client_thread_id), 62 client_thread_id_(client_thread_id),
61 command_line_(std::move(command_line)), 63 command_line_(std::move(command_line)),
62 delegate_(std::move(delegate)), 64 delegate_(std::move(delegate)),
65 pending_connection_(std::move(pending_connection)),
66 process_error_callback_(process_error_callback),
63 child_process_launcher_(child_process_launcher), 67 child_process_launcher_(child_process_launcher),
64 terminate_on_shutdown_(terminate_on_shutdown) { 68 terminate_on_shutdown_(terminate_on_shutdown) {}
65 }
66 69
67 ChildProcessLauncherHelper::~ChildProcessLauncherHelper() { 70 ChildProcessLauncherHelper::~ChildProcessLauncherHelper() {
68 } 71 }
69 72
70 void ChildProcessLauncherHelper::StartLaunchOnClientThread() { 73 void ChildProcessLauncherHelper::StartLaunchOnClientThread() {
71 DCHECK_CURRENTLY_ON(client_thread_id_); 74 DCHECK_CURRENTLY_ON(client_thread_id_);
72 75
73 BeforeLaunchOnClientThread(); 76 BeforeLaunchOnClientThread();
74 77
75 mojo_server_handle_ = PrepareMojoPipeHandlesOnClientThread(); 78 mojo::edk::ScopedPlatformHandle server_handle =
76 if (!mojo_server_handle_.is_valid()) { 79 PrepareMojoPipeHandlesOnClientThread();
80 if (!server_handle.is_valid()) {
77 mojo::edk::PlatformChannelPair channel_pair; 81 mojo::edk::PlatformChannelPair channel_pair;
78 mojo_server_handle_ = channel_pair.PassServerHandle(); 82 server_handle = channel_pair.PassServerHandle();
79 mojo_client_handle_ = channel_pair.PassClientHandle(); 83 mojo_client_handle_ = channel_pair.PassClientHandle();
80 } 84 }
81 85
82 BrowserThread::PostTask( 86 BrowserThread::PostTask(
83 BrowserThread::PROCESS_LAUNCHER, FROM_HERE, 87 BrowserThread::PROCESS_LAUNCHER, FROM_HERE,
84 base::Bind(&ChildProcessLauncherHelper::LaunchOnLauncherThread, this)); 88 base::Bind(&ChildProcessLauncherHelper::LaunchOnLauncherThread, this,
89 base::Passed(&pending_connection_),
90 base::Passed(&server_handle)));
85 } 91 }
86 92
87 void ChildProcessLauncherHelper::LaunchOnLauncherThread() { 93 void ChildProcessLauncherHelper::LaunchOnLauncherThread(
94 std::unique_ptr<mojo::edk::PendingProcessConnection> pending_connection,
95 mojo::edk::ScopedPlatformHandle server_handle) {
88 DCHECK_CURRENTLY_ON(BrowserThread::PROCESS_LAUNCHER); 96 DCHECK_CURRENTLY_ON(BrowserThread::PROCESS_LAUNCHER);
89 97
90 begin_launch_time_ = base::TimeTicks::Now(); 98 begin_launch_time_ = base::TimeTicks::Now();
91 99
92 std::unique_ptr<FileMappedForLaunch> files_to_register = GetFilesToMap(); 100 std::unique_ptr<FileMappedForLaunch> files_to_register = GetFilesToMap();
93 101
94 bool is_synchronous_launch = true; 102 bool is_synchronous_launch = true;
95 int launch_result = LAUNCH_RESULT_FAILURE; 103 int launch_result = LAUNCH_RESULT_FAILURE;
96 base::LaunchOptions options; 104 base::LaunchOptions options;
97 BeforeLaunchOnLauncherThread(*files_to_register, &options); 105 BeforeLaunchOnLauncherThread(*files_to_register, &options);
98 106
99 Process process = LaunchProcessOnLauncherThread(options, 107 Process process = LaunchProcessOnLauncherThread(
100 std::move(files_to_register), 108 options, std::move(files_to_register), &is_synchronous_launch,
101 &is_synchronous_launch, 109 &launch_result, &pending_connection, &server_handle);
102 &launch_result);
103 110
104 AfterLaunchOnLauncherThread(process, options); 111 AfterLaunchOnLauncherThread(process, options);
105 112
106 if (is_synchronous_launch) { 113 if (is_synchronous_launch) {
107 PostLaunchOnLauncherThread(std::move(process), launch_result, false); 114 // If launch was synchronous, the helper must not have taken ownership of
115 // |pending_connection| or |server_handle|.
116 DCHECK(pending_connection);
117 DCHECK(server_handle.is_valid());
118 PostLaunchOnLauncherThread(std::move(process), launch_result, false,
119 std::move(pending_connection),
120 std::move(server_handle));
108 } 121 }
109 } 122 }
110 123
111 void ChildProcessLauncherHelper::PostLaunchOnLauncherThread( 124 void ChildProcessLauncherHelper::PostLaunchOnLauncherThread(
112 ChildProcessLauncherHelper::Process process, 125 ChildProcessLauncherHelper::Process process,
113 int launch_result, 126 int launch_result,
114 bool post_launch_on_client_thread_called) { 127 bool post_launch_on_client_thread_called,
128 std::unique_ptr<mojo::edk::PendingProcessConnection> pending_connection,
129 mojo::edk::ScopedPlatformHandle server_handle) {
115 // Release the client handle now that the process has been started (the pipe 130 // Release the client handle now that the process has been started (the pipe
116 // may not signal when the process dies otherwise and we would not detect the 131 // may not signal when the process dies otherwise and we would not detect the
117 // child process died). 132 // child process died).
118 mojo_client_handle_.reset(); 133 mojo_client_handle_.reset();
119 134
120 if (process.process.IsValid()) { 135 if (process.process.IsValid()) {
121 RecordHistogramsOnLauncherThread( 136 RecordHistogramsOnLauncherThread(
122 base::TimeTicks::Now() - begin_launch_time_); 137 base::TimeTicks::Now() - begin_launch_time_);
138
139 DCHECK(pending_connection);
140 DCHECK(server_handle.is_valid());
141 pending_connection->Connect(process.process.Handle(),
142 std::move(server_handle),
143 process_error_callback_);
123 } 144 }
124 145
125 if (!post_launch_on_client_thread_called) { 146 if (!post_launch_on_client_thread_called) {
126 BrowserThread::PostTask( 147 BrowserThread::PostTask(
127 client_thread_id_, FROM_HERE, 148 client_thread_id_, FROM_HERE,
128 base::Bind(&ChildProcessLauncherHelper::PostLaunchOnClientThread, 149 base::Bind(&ChildProcessLauncherHelper::PostLaunchOnClientThread,
129 this, base::Passed(&process), launch_result)); 150 this, base::Passed(&process), launch_result));
130 } 151 }
131 } 152 }
132 153
133 void ChildProcessLauncherHelper::PostLaunchOnClientThread( 154 void ChildProcessLauncherHelper::PostLaunchOnClientThread(
134 ChildProcessLauncherHelper::Process process, 155 ChildProcessLauncherHelper::Process process,
135 int error_code) { 156 int error_code) {
136 if (child_process_launcher_) { 157 if (child_process_launcher_) {
137 child_process_launcher_->Notify( 158 child_process_launcher_->Notify(std::move(process), error_code);
138 std::move(process), std::move(mojo_server_handle_), error_code);
139 } else if (process.process.IsValid() && terminate_on_shutdown_) { 159 } else if (process.process.IsValid() && terminate_on_shutdown_) {
140 // Client is gone, terminate the process. 160 // Client is gone, terminate the process.
141 ForceNormalProcessTerminationAsync(std::move(process)); 161 ForceNormalProcessTerminationAsync(std::move(process));
142 } 162 }
143 } 163 }
144 164
145 std::string ChildProcessLauncherHelper::GetProcessType() { 165 std::string ChildProcessLauncherHelper::GetProcessType() {
146 return command_line()->GetSwitchValueASCII(switches::kProcessType); 166 return command_line()->GetSwitchValueASCII(switches::kProcessType);
147 } 167 }
148 168
149 // static 169 // static
150 void ChildProcessLauncherHelper::ForceNormalProcessTerminationAsync( 170 void ChildProcessLauncherHelper::ForceNormalProcessTerminationAsync(
151 ChildProcessLauncherHelper::Process process) { 171 ChildProcessLauncherHelper::Process process) {
152 if (BrowserThread::CurrentlyOn(BrowserThread::PROCESS_LAUNCHER)) { 172 if (BrowserThread::CurrentlyOn(BrowserThread::PROCESS_LAUNCHER)) {
153 ForceNormalProcessTerminationSync(std::move(process)); 173 ForceNormalProcessTerminationSync(std::move(process));
154 return; 174 return;
155 } 175 }
156 // On Posix, EnsureProcessTerminated can lead to 2 seconds of sleep! 176 // On Posix, EnsureProcessTerminated can lead to 2 seconds of sleep!
157 // So don't do this on the UI/IO threads. 177 // So don't do this on the UI/IO threads.
158 BrowserThread::PostTask( 178 BrowserThread::PostTask(
159 BrowserThread::PROCESS_LAUNCHER, FROM_HERE, 179 BrowserThread::PROCESS_LAUNCHER, FROM_HERE,
160 base::Bind(&ChildProcessLauncherHelper::ForceNormalProcessTerminationSync, 180 base::Bind(&ChildProcessLauncherHelper::ForceNormalProcessTerminationSync,
161 base::Passed(&process))); 181 base::Passed(&process)));
162 } 182 }
163 183
164 } // namespace internal 184 } // namespace internal
165 } // namespace content 185 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/child_process_launcher_helper.h ('k') | content/browser/child_process_launcher_helper_android.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698