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

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

Issue 651253002: Enforce handle ownership in base::Process (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Add empty line Created 6 years, 2 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 (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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/browser_child_process_host_impl.h" 5 #include "content/browser/browser_child_process_host_impl.h"
6 6
7 #include "base/base_switches.h" 7 #include "base/base_switches.h"
8 #include "base/bind.h" 8 #include "base/bind.h"
9 #include "base/command_line.h" 9 #include "base/command_line.h"
10 #include "base/files/file_path.h" 10 #include "base/files/file_path.h"
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after
105 AddFilter(new HistogramMessageFilter); 105 AddFilter(new HistogramMessageFilter);
106 106
107 g_child_process_list.Get().push_back(this); 107 g_child_process_list.Get().push_back(this);
108 GetContentClient()->browser()->BrowserChildProcessHostCreated(this); 108 GetContentClient()->browser()->BrowserChildProcessHostCreated(this);
109 109
110 power_monitor_message_broadcaster_.Init(); 110 power_monitor_message_broadcaster_.Init();
111 } 111 }
112 112
113 BrowserChildProcessHostImpl::~BrowserChildProcessHostImpl() { 113 BrowserChildProcessHostImpl::~BrowserChildProcessHostImpl() {
114 g_child_process_list.Get().remove(this); 114 g_child_process_list.Get().remove(this);
115
116 #if defined(OS_WIN)
117 DeleteProcessWaitableEvent(early_exit_watcher_.GetWatchedEvent());
118 #endif
119 } 115 }
120 116
121 // static 117 // static
122 void BrowserChildProcessHostImpl::TerminateAll() { 118 void BrowserChildProcessHostImpl::TerminateAll() {
123 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 119 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
124 // Make a copy since the BrowserChildProcessHost dtor mutates the original 120 // Make a copy since the BrowserChildProcessHost dtor mutates the original
125 // list. 121 // list.
126 BrowserChildProcessList copy = g_child_process_list.Get(); 122 BrowserChildProcessList copy = g_child_process_list.Get();
127 for (BrowserChildProcessList::iterator it = copy.begin(); 123 for (BrowserChildProcessList::iterator it = copy.begin();
128 it != copy.end(); ++it) { 124 it != copy.end(); ++it) {
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
166 162
167 ChildProcessHost* BrowserChildProcessHostImpl::GetHost() const { 163 ChildProcessHost* BrowserChildProcessHostImpl::GetHost() const {
168 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 164 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
169 return child_process_host_.get(); 165 return child_process_host_.get();
170 } 166 }
171 167
172 base::ProcessHandle BrowserChildProcessHostImpl::GetHandle() const { 168 base::ProcessHandle BrowserChildProcessHostImpl::GetHandle() const {
173 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 169 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
174 DCHECK(child_process_.get()) 170 DCHECK(child_process_.get())
175 << "Requesting a child process handle before launching."; 171 << "Requesting a child process handle before launching.";
176 DCHECK(child_process_->GetHandle()) 172 DCHECK(child_process_->GetProcess().IsValid())
177 << "Requesting a child process handle before launch has completed OK."; 173 << "Requesting a child process handle before launch has completed OK.";
178 return child_process_->GetHandle(); 174 return child_process_->GetProcess().Handle();
179 } 175 }
180 176
181 void BrowserChildProcessHostImpl::SetName(const base::string16& name) { 177 void BrowserChildProcessHostImpl::SetName(const base::string16& name) {
182 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 178 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
183 data_.name = name; 179 data_.name = name;
184 } 180 }
185 181
186 void BrowserChildProcessHostImpl::SetHandle(base::ProcessHandle handle) { 182 void BrowserChildProcessHostImpl::SetHandle(base::ProcessHandle handle) {
187 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 183 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
188 data_.handle = handle; 184 data_.handle = handle;
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
232 228
233 bool BrowserChildProcessHostImpl::OnMessageReceived( 229 bool BrowserChildProcessHostImpl::OnMessageReceived(
234 const IPC::Message& message) { 230 const IPC::Message& message) {
235 return delegate_->OnMessageReceived(message); 231 return delegate_->OnMessageReceived(message);
236 } 232 }
237 233
238 void BrowserChildProcessHostImpl::OnChannelConnected(int32 peer_pid) { 234 void BrowserChildProcessHostImpl::OnChannelConnected(int32 peer_pid) {
239 #if defined(OS_WIN) 235 #if defined(OS_WIN)
240 // From this point onward, the exit of the child process is detected by an 236 // From this point onward, the exit of the child process is detected by an
241 // error on the IPC channel. 237 // error on the IPC channel.
242 DeleteProcessWaitableEvent(early_exit_watcher_.GetWatchedEvent());
243 early_exit_watcher_.StopWatching(); 238 early_exit_watcher_.StopWatching();
244 #endif 239 #endif
245 240
246 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 241 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
247 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, 242 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
248 base::Bind(&NotifyProcessHostConnected, data_)); 243 base::Bind(&NotifyProcessHostConnected, data_));
249 244
250 delegate_->OnChannelConnected(peer_pid); 245 delegate_->OnChannelConnected(peer_pid);
251 } 246 }
252 247
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
314 bool BrowserChildProcessHostImpl::Send(IPC::Message* message) { 309 bool BrowserChildProcessHostImpl::Send(IPC::Message* message) {
315 return child_process_host_->Send(message); 310 return child_process_host_->Send(message);
316 } 311 }
317 312
318 void BrowserChildProcessHostImpl::OnProcessLaunchFailed() { 313 void BrowserChildProcessHostImpl::OnProcessLaunchFailed() {
319 delegate_->OnProcessLaunchFailed(); 314 delegate_->OnProcessLaunchFailed();
320 delete delegate_; // Will delete us 315 delete delegate_; // Will delete us
321 } 316 }
322 317
323 void BrowserChildProcessHostImpl::OnProcessLaunched() { 318 void BrowserChildProcessHostImpl::OnProcessLaunched() {
324 base::ProcessHandle handle = child_process_->GetHandle(); 319 const base::Process& process = child_process_->GetProcess();
325 if (!handle) { 320 DCHECK(process.IsValid());
326 delete delegate_; // Will delete us
327 return;
328 }
329 321
330 #if defined(OS_WIN) 322 #if defined(OS_WIN)
331 // Start a WaitableEventWatcher that will invoke OnProcessExitedEarly if the 323 // Start a WaitableEventWatcher that will invoke OnProcessExitedEarly if the
332 // child process exits. This watcher is stopped once the IPC channel is 324 // child process exits. This watcher is stopped once the IPC channel is
333 // connected and the exit of the child process is detecter by an error on the 325 // connected and the exit of the child process is detecter by an error on the
334 // IPC channel thereafter. 326 // IPC channel thereafter.
335 DCHECK(!early_exit_watcher_.GetWatchedEvent()); 327 DCHECK(!early_exit_watcher_.GetWatchedObject());
336 early_exit_watcher_.StartWatching( 328 early_exit_watcher_.StartWatching(process.Handle(), this);
337 new base::WaitableEvent(handle),
338 base::Bind(&BrowserChildProcessHostImpl::OnProcessExitedEarly,
339 base::Unretained(this)));
340 #endif 329 #endif
341 330
342 data_.handle = handle; 331 // TODO(rvargas) crbug.com/417532: Don't store a handle.
332 data_.handle = process.Handle();
343 delegate_->OnProcessLaunched(); 333 delegate_->OnProcessLaunched();
344 } 334 }
345 335
346 #if defined(OS_WIN) 336 #if defined(OS_WIN)
347 337
348 void BrowserChildProcessHostImpl::DeleteProcessWaitableEvent( 338 void BrowserChildProcessHostImpl::OnObjectSignaled(HANDLE object) {
349 base::WaitableEvent* event) {
350 if (!event)
351 return;
352
353 // The WaitableEvent does not own the process handle so ensure it does not
354 // close it.
355 event->Release();
356
357 delete event;
358 }
359
360 void BrowserChildProcessHostImpl::OnProcessExitedEarly(
361 base::WaitableEvent* event) {
362 DeleteProcessWaitableEvent(event);
363 OnChildDisconnected(); 339 OnChildDisconnected();
364 } 340 }
365 341
366 #endif 342 #endif
367 343
368 } // namespace content 344 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/browser_child_process_host_impl.h ('k') | content/browser/child_process_launcher.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698