Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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.h" | 5 #include "content/browser/child_process_launcher.h" |
| 6 | 6 |
| 7 #include <utility> // For std::pair. | 7 #include <utility> // For std::pair. |
| 8 | 8 |
| 9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 37 | 37 |
| 38 // Having the functionality of ChildProcessLauncher be in an internal | 38 // Having the functionality of ChildProcessLauncher be in an internal |
| 39 // ref counted object allows us to automatically terminate the process when the | 39 // ref counted object allows us to automatically terminate the process when the |
| 40 // parent class destructs, while still holding on to state that we need. | 40 // parent class destructs, while still holding on to state that we need. |
| 41 class ChildProcessLauncher::Context | 41 class ChildProcessLauncher::Context |
| 42 : public base::RefCountedThreadSafe<ChildProcessLauncher::Context> { | 42 : public base::RefCountedThreadSafe<ChildProcessLauncher::Context> { |
| 43 public: | 43 public: |
| 44 Context() | 44 Context() |
| 45 : client_(NULL), | 45 : client_(NULL), |
| 46 client_thread_id_(BrowserThread::UI), | 46 client_thread_id_(BrowserThread::UI), |
| 47 starting_(true) | 47 starting_(true), |
| 48 terminate_child_on_shutdown_(true) | |
| 48 #if defined(OS_LINUX) | 49 #if defined(OS_LINUX) |
| 49 , zygote_(false) | 50 , zygote_(false) |
| 50 #endif | 51 #endif |
| 51 { | 52 { |
| 52 } | 53 } |
| 53 | 54 |
| 54 void Launch( | 55 void Launch( |
| 55 #if defined(OS_WIN) | 56 #if defined(OS_WIN) |
| 56 const FilePath& exposed_dir, | 57 const FilePath& exposed_dir, |
| 57 #elif defined(OS_POSIX) | 58 #elif defined(OS_POSIX) |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 80 cmd_line)); | 81 cmd_line)); |
| 81 } | 82 } |
| 82 | 83 |
| 83 void ResetClient() { | 84 void ResetClient() { |
| 84 // No need for locking as this function gets called on the same thread that | 85 // No need for locking as this function gets called on the same thread that |
| 85 // client_ would be used. | 86 // client_ would be used. |
| 86 CHECK(BrowserThread::CurrentlyOn(client_thread_id_)); | 87 CHECK(BrowserThread::CurrentlyOn(client_thread_id_)); |
| 87 client_ = NULL; | 88 client_ = NULL; |
| 88 } | 89 } |
| 89 | 90 |
| 91 // Controls whether the child process should be terminated on browser | |
| 92 // shutdown. Default behavior is to terminate the child.. | |
|
jam
2011/05/24 20:46:13
nit: one period at end. also, comments usually go
ananta
2011/05/24 20:56:26
Done.
| |
| 93 void set_terminate_child_on_shutdown(bool terminate_on_shutdown) { | |
| 94 terminate_child_on_shutdown_ = terminate_on_shutdown; | |
| 95 } | |
| 96 | |
| 90 private: | 97 private: |
| 91 friend class base::RefCountedThreadSafe<ChildProcessLauncher::Context>; | 98 friend class base::RefCountedThreadSafe<ChildProcessLauncher::Context>; |
| 92 friend class ChildProcessLauncher; | 99 friend class ChildProcessLauncher; |
| 93 | 100 |
| 94 ~Context() { | 101 ~Context() { |
| 95 Terminate(); | 102 Terminate(); |
| 96 } | 103 } |
| 97 | 104 |
| 98 void LaunchInternal( | 105 void LaunchInternal( |
| 99 #if defined(OS_WIN) | 106 #if defined(OS_WIN) |
| (...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 203 client_->OnProcessLaunched(); | 210 client_->OnProcessLaunched(); |
| 204 } else { | 211 } else { |
| 205 Terminate(); | 212 Terminate(); |
| 206 } | 213 } |
| 207 } | 214 } |
| 208 | 215 |
| 209 void Terminate() { | 216 void Terminate() { |
| 210 if (!process_.handle()) | 217 if (!process_.handle()) |
| 211 return; | 218 return; |
| 212 | 219 |
| 220 if (!terminate_child_on_shutdown_) { | |
|
jam
2011/05/24 20:46:13
nit: this file doesn't use brace brackets for one
ananta
2011/05/24 20:56:26
Done.
ananta
2011/05/24 20:56:26
Done.
| |
| 221 return; | |
| 222 } | |
| 223 | |
| 213 // On Posix, EnsureProcessTerminated can lead to 2 seconds of sleep! So | 224 // On Posix, EnsureProcessTerminated can lead to 2 seconds of sleep! So |
| 214 // don't this on the UI/IO threads. | 225 // don't this on the UI/IO threads. |
| 215 BrowserThread::PostTask( | 226 BrowserThread::PostTask( |
| 216 BrowserThread::PROCESS_LAUNCHER, FROM_HERE, | 227 BrowserThread::PROCESS_LAUNCHER, FROM_HERE, |
| 217 NewRunnableFunction( | 228 NewRunnableFunction( |
| 218 &ChildProcessLauncher::Context::TerminateInternal, | 229 &ChildProcessLauncher::Context::TerminateInternal, |
| 219 #if defined(OS_LINUX) | 230 #if defined(OS_LINUX) |
| 220 zygote_, | 231 zygote_, |
| 221 #endif | 232 #endif |
| 222 process_.handle())); | 233 process_.handle())); |
| (...skipping 27 matching lines...) Expand all Loading... | |
| 250 ProcessWatcher::EnsureProcessTerminated(handle); | 261 ProcessWatcher::EnsureProcessTerminated(handle); |
| 251 } | 262 } |
| 252 #endif // OS_POSIX | 263 #endif // OS_POSIX |
| 253 process.Close(); | 264 process.Close(); |
| 254 } | 265 } |
| 255 | 266 |
| 256 Client* client_; | 267 Client* client_; |
| 257 BrowserThread::ID client_thread_id_; | 268 BrowserThread::ID client_thread_id_; |
| 258 base::Process process_; | 269 base::Process process_; |
| 259 bool starting_; | 270 bool starting_; |
| 271 bool terminate_child_on_shutdown_; | |
| 260 | 272 |
| 261 #if defined(OS_LINUX) | 273 #if defined(OS_LINUX) |
| 262 bool zygote_; | 274 bool zygote_; |
| 263 #endif | 275 #endif |
| 264 }; | 276 }; |
| 265 | 277 |
| 266 | 278 |
| 267 ChildProcessLauncher::ChildProcessLauncher( | 279 ChildProcessLauncher::ChildProcessLauncher( |
| 268 #if defined(OS_WIN) | 280 #if defined(OS_WIN) |
| 269 const FilePath& exposed_dir, | 281 const FilePath& exposed_dir, |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 326 } | 338 } |
| 327 | 339 |
| 328 void ChildProcessLauncher::SetProcessBackgrounded(bool background) { | 340 void ChildProcessLauncher::SetProcessBackgrounded(bool background) { |
| 329 BrowserThread::PostTask( | 341 BrowserThread::PostTask( |
| 330 BrowserThread::PROCESS_LAUNCHER, FROM_HERE, | 342 BrowserThread::PROCESS_LAUNCHER, FROM_HERE, |
| 331 NewRunnableMethod( | 343 NewRunnableMethod( |
| 332 context_.get(), | 344 context_.get(), |
| 333 &ChildProcessLauncher::Context::SetProcessBackgrounded, | 345 &ChildProcessLauncher::Context::SetProcessBackgrounded, |
| 334 background)); | 346 background)); |
| 335 } | 347 } |
| 348 | |
| 349 void ChildProcessLauncher::SetTerminateChildOnShutdown( | |
| 350 bool terminate_on_shutdown) { | |
| 351 if (context_) { | |
|
jam
2011/05/24 20:46:13
ditto
ananta
2011/05/24 20:56:26
Done.
| |
| 352 context_->set_terminate_child_on_shutdown(terminate_on_shutdown); | |
| 353 } | |
| 354 } | |
| 355 | |
| OLD | NEW |