| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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/common/child_process.h" | 5 #include "chrome/common/child_process.h" |
| 6 | 6 |
| 7 #include "base/message_loop.h" | 7 #include "base/message_loop.h" |
| 8 #include "chrome/common/child_thread.h" | 8 #include "chrome/common/child_thread.h" |
| 9 | 9 |
| 10 ChildProcess* ChildProcess::child_process_; | 10 ChildProcess* ChildProcess::child_process_; |
| 11 | 11 |
| 12 ChildProcess::ChildProcess() | 12 ChildProcess::ChildProcess(ChildThread* child_thread) |
| 13 : ref_count_(0), | 13 : child_thread_(child_thread), |
| 14 shutdown_event_(true, false), | 14 ref_count_(0), |
| 15 io_thread_("Chrome_ChildIOThread") { | 15 shutdown_event_(true, false) { |
| 16 DCHECK(!child_process_); | 16 DCHECK(!child_process_); |
| 17 child_process_ = this; | 17 child_process_ = this; |
| 18 | 18 if (child_thread_.get()) // null in unittests. |
| 19 io_thread_.StartWithOptions(base::Thread::Options(MessageLoop::TYPE_IO, 0)); | 19 child_thread_->Run(); |
| 20 } | 20 } |
| 21 | 21 |
| 22 ChildProcess::~ChildProcess() { | 22 ChildProcess::~ChildProcess() { |
| 23 DCHECK(child_process_ == this); | 23 DCHECK(child_process_ == this); |
| 24 | 24 |
| 25 // Signal this event before destroying the child process. That way all | 25 // Signal this event before destroying the child process. That way all |
| 26 // background threads can cleanup. | 26 // background threads can cleanup. |
| 27 // For example, in the renderer the RenderThread instances will be able to | 27 // For example, in the renderer the RenderThread instances will be able to |
| 28 // notice shutdown before the render process begins waiting for them to exit. | 28 // notice shutdown before the render process begins waiting for them to exit. |
| 29 shutdown_event_.Signal(); | 29 shutdown_event_.Signal(); |
| 30 | 30 |
| 31 if (child_thread_.get()) |
| 32 child_thread_->Stop(); |
| 33 |
| 31 child_process_ = NULL; | 34 child_process_ = NULL; |
| 32 } | 35 } |
| 33 | 36 |
| 34 void ChildProcess::AddRefProcess() { | 37 void ChildProcess::AddRefProcess() { |
| 35 DCHECK(!main_thread_.get() || // null in unittests. | 38 DCHECK(!child_thread_.get() || // null in unittests. |
| 36 MessageLoop::current() == main_thread_->message_loop()); | 39 MessageLoop::current() == child_thread_->message_loop()); |
| 37 ref_count_++; | 40 ref_count_++; |
| 38 } | 41 } |
| 39 | 42 |
| 40 void ChildProcess::ReleaseProcess() { | 43 void ChildProcess::ReleaseProcess() { |
| 41 DCHECK(!main_thread_.get() || // null in unittests. | 44 DCHECK(!child_thread_.get() || // null in unittests. |
| 42 MessageLoop::current() == main_thread_->message_loop()); | 45 MessageLoop::current() == child_thread_->message_loop()); |
| 43 DCHECK(ref_count_); | 46 DCHECK(ref_count_); |
| 44 DCHECK(child_process_); | 47 DCHECK(child_process_); |
| 45 if (--ref_count_) | 48 if (--ref_count_) |
| 46 return; | 49 return; |
| 47 | 50 |
| 48 if (main_thread_.get()) // null in unittests. | 51 if (child_thread_.get()) // null in unittests. |
| 49 main_thread_->OnProcessFinalRelease(); | 52 child_thread_->OnProcessFinalRelease(); |
| 50 } | 53 } |
| 51 | 54 |
| 52 base::WaitableEvent* ChildProcess::GetShutDownEvent() { | 55 base::WaitableEvent* ChildProcess::GetShutDownEvent() { |
| 53 DCHECK(child_process_); | 56 DCHECK(child_process_); |
| 54 return &child_process_->shutdown_event_; | 57 return &child_process_->shutdown_event_; |
| 55 } | 58 } |
| OLD | NEW |