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