| 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) | |
| 49 #if defined(OS_LINUX) | 48 #if defined(OS_LINUX) |
| 50 , zygote_(false) | 49 , zygote_(false) |
| 51 #endif | 50 #endif |
| 52 { | 51 { |
| 53 } | 52 } |
| 54 | 53 |
| 55 void Launch( | 54 void Launch( |
| 56 #if defined(OS_WIN) | 55 #if defined(OS_WIN) |
| 57 const FilePath& exposed_dir, | 56 const FilePath& exposed_dir, |
| 58 #elif defined(OS_POSIX) | 57 #elif defined(OS_POSIX) |
| (...skipping 22 matching lines...) Expand all Loading... |
| 81 cmd_line)); | 80 cmd_line)); |
| 82 } | 81 } |
| 83 | 82 |
| 84 void ResetClient() { | 83 void ResetClient() { |
| 85 // No need for locking as this function gets called on the same thread that | 84 // No need for locking as this function gets called on the same thread that |
| 86 // client_ would be used. | 85 // client_ would be used. |
| 87 CHECK(BrowserThread::CurrentlyOn(client_thread_id_)); | 86 CHECK(BrowserThread::CurrentlyOn(client_thread_id_)); |
| 88 client_ = NULL; | 87 client_ = NULL; |
| 89 } | 88 } |
| 90 | 89 |
| 91 void set_terminate_child_on_shutdown(bool terminate_on_shutdown) { | |
| 92 terminate_child_on_shutdown_ = terminate_on_shutdown; | |
| 93 } | |
| 94 | |
| 95 private: | 90 private: |
| 96 friend class base::RefCountedThreadSafe<ChildProcessLauncher::Context>; | 91 friend class base::RefCountedThreadSafe<ChildProcessLauncher::Context>; |
| 97 friend class ChildProcessLauncher; | 92 friend class ChildProcessLauncher; |
| 98 | 93 |
| 99 ~Context() { | 94 ~Context() { |
| 100 Terminate(); | 95 Terminate(); |
| 101 } | 96 } |
| 102 | 97 |
| 103 void LaunchInternal( | 98 void LaunchInternal( |
| 104 #if defined(OS_WIN) | 99 #if defined(OS_WIN) |
| (...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 208 client_->OnProcessLaunched(); | 203 client_->OnProcessLaunched(); |
| 209 } else { | 204 } else { |
| 210 Terminate(); | 205 Terminate(); |
| 211 } | 206 } |
| 212 } | 207 } |
| 213 | 208 |
| 214 void Terminate() { | 209 void Terminate() { |
| 215 if (!process_.handle()) | 210 if (!process_.handle()) |
| 216 return; | 211 return; |
| 217 | 212 |
| 218 if (!terminate_child_on_shutdown_) | |
| 219 return; | |
| 220 | |
| 221 // On Posix, EnsureProcessTerminated can lead to 2 seconds of sleep! So | 213 // On Posix, EnsureProcessTerminated can lead to 2 seconds of sleep! So |
| 222 // don't this on the UI/IO threads. | 214 // don't this on the UI/IO threads. |
| 223 BrowserThread::PostTask( | 215 BrowserThread::PostTask( |
| 224 BrowserThread::PROCESS_LAUNCHER, FROM_HERE, | 216 BrowserThread::PROCESS_LAUNCHER, FROM_HERE, |
| 225 NewRunnableFunction( | 217 NewRunnableFunction( |
| 226 &ChildProcessLauncher::Context::TerminateInternal, | 218 &ChildProcessLauncher::Context::TerminateInternal, |
| 227 #if defined(OS_LINUX) | 219 #if defined(OS_LINUX) |
| 228 zygote_, | 220 zygote_, |
| 229 #endif | 221 #endif |
| 230 process_.handle())); | 222 process_.handle())); |
| (...skipping 27 matching lines...) Expand all Loading... |
| 258 ProcessWatcher::EnsureProcessTerminated(handle); | 250 ProcessWatcher::EnsureProcessTerminated(handle); |
| 259 } | 251 } |
| 260 #endif // OS_POSIX | 252 #endif // OS_POSIX |
| 261 process.Close(); | 253 process.Close(); |
| 262 } | 254 } |
| 263 | 255 |
| 264 Client* client_; | 256 Client* client_; |
| 265 BrowserThread::ID client_thread_id_; | 257 BrowserThread::ID client_thread_id_; |
| 266 base::Process process_; | 258 base::Process process_; |
| 267 bool starting_; | 259 bool starting_; |
| 268 // Controls whether the child process should be terminated on browser | |
| 269 // shutdown. Default behavior is to terminate the child. | |
| 270 bool terminate_child_on_shutdown_; | |
| 271 | 260 |
| 272 #if defined(OS_LINUX) | 261 #if defined(OS_LINUX) |
| 273 bool zygote_; | 262 bool zygote_; |
| 274 #endif | 263 #endif |
| 275 }; | 264 }; |
| 276 | 265 |
| 277 | 266 |
| 278 ChildProcessLauncher::ChildProcessLauncher( | 267 ChildProcessLauncher::ChildProcessLauncher( |
| 279 #if defined(OS_WIN) | 268 #if defined(OS_WIN) |
| 280 const FilePath& exposed_dir, | 269 const FilePath& exposed_dir, |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 337 } | 326 } |
| 338 | 327 |
| 339 void ChildProcessLauncher::SetProcessBackgrounded(bool background) { | 328 void ChildProcessLauncher::SetProcessBackgrounded(bool background) { |
| 340 BrowserThread::PostTask( | 329 BrowserThread::PostTask( |
| 341 BrowserThread::PROCESS_LAUNCHER, FROM_HERE, | 330 BrowserThread::PROCESS_LAUNCHER, FROM_HERE, |
| 342 NewRunnableMethod( | 331 NewRunnableMethod( |
| 343 context_.get(), | 332 context_.get(), |
| 344 &ChildProcessLauncher::Context::SetProcessBackgrounded, | 333 &ChildProcessLauncher::Context::SetProcessBackgrounded, |
| 345 background)); | 334 background)); |
| 346 } | 335 } |
| 347 | |
| 348 void ChildProcessLauncher::SetTerminateChildOnShutdown( | |
| 349 bool terminate_on_shutdown) { | |
| 350 if (context_) | |
| 351 context_->set_terminate_child_on_shutdown(terminate_on_shutdown); | |
| 352 } | |
| 353 | |
| OLD | NEW |