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 <windows.h> | 5 #include <windows.h> |
6 | 6 |
7 #include "chrome/common/child_process.h" | 7 #include "chrome/common/child_process.h" |
8 | 8 |
9 #include "base/atomic_ref_count.h" | 9 #include "base/atomic_ref_count.h" |
10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
11 #include "base/command_line.h" | 11 #include "base/command_line.h" |
12 #include "base/string_util.h" | 12 #include "base/string_util.h" |
| 13 #include "base/waitable_event.h" |
13 #include "chrome/common/chrome_switches.h" | 14 #include "chrome/common/chrome_switches.h" |
14 #include "webkit/glue/webkit_glue.h" | 15 #include "webkit/glue/webkit_glue.h" |
15 | 16 |
16 ChildProcess* ChildProcess::child_process_; | 17 ChildProcess* ChildProcess::child_process_; |
17 MessageLoop* ChildProcess::main_thread_loop_; | 18 MessageLoop* ChildProcess::main_thread_loop_; |
18 static base::AtomicRefCount ref_count; | 19 static base::AtomicRefCount ref_count; |
19 HANDLE ChildProcess::shutdown_event_; | 20 base::WaitableEvent* ChildProcess::shutdown_event_; |
20 | 21 |
21 | 22 |
22 ChildProcess::ChildProcess() { | 23 ChildProcess::ChildProcess() { |
23 DCHECK(!child_process_); | 24 DCHECK(!child_process_); |
24 } | 25 } |
25 | 26 |
26 ChildProcess::~ChildProcess() { | 27 ChildProcess::~ChildProcess() { |
27 DCHECK(child_process_ == this); | 28 DCHECK(child_process_ == this); |
28 } | 29 } |
29 | 30 |
(...skipping 14 matching lines...) Expand all Loading... |
44 // static | 45 // static |
45 bool ChildProcess::ProcessRefCountIsZero() { | 46 bool ChildProcess::ProcessRefCountIsZero() { |
46 return base::AtomicRefCountIsZero(&ref_count); | 47 return base::AtomicRefCountIsZero(&ref_count); |
47 } | 48 } |
48 | 49 |
49 void ChildProcess::OnFinalRelease() { | 50 void ChildProcess::OnFinalRelease() { |
50 DCHECK(main_thread_loop_); | 51 DCHECK(main_thread_loop_); |
51 main_thread_loop_->PostTask(FROM_HERE, new MessageLoop::QuitTask()); | 52 main_thread_loop_->PostTask(FROM_HERE, new MessageLoop::QuitTask()); |
52 } | 53 } |
53 | 54 |
54 HANDLE ChildProcess::GetShutDownEvent() { | 55 base::WaitableEvent* ChildProcess::GetShutDownEvent() { |
55 return shutdown_event_; | 56 return shutdown_event_; |
56 } | 57 } |
57 | 58 |
58 // On error, it is OK to leave the global pointers, as long as the only | 59 // On error, it is OK to leave the global pointers, as long as the only |
59 // non-NULL pointers are valid. GlobalCleanup will always get called, which | 60 // non-NULL pointers are valid. GlobalCleanup will always get called, which |
60 // will delete any non-NULL services. | 61 // will delete any non-NULL services. |
61 bool ChildProcess::GlobalInit(const std::wstring &channel_name, | 62 bool ChildProcess::GlobalInit(const std::wstring &channel_name, |
62 ChildProcessFactoryInterface *factory) { | 63 ChildProcessFactoryInterface *factory) { |
63 // OK to be called multiple times. | 64 // OK to be called multiple times. |
64 if (main_thread_loop_) | 65 if (main_thread_loop_) |
65 return true; | 66 return true; |
66 | 67 |
67 if (channel_name.empty()) { | 68 if (channel_name.empty()) { |
68 NOTREACHED() << "Unable to get the channel name"; | 69 NOTREACHED() << "Unable to get the channel name"; |
69 return false; | 70 return false; |
70 } | 71 } |
71 | 72 |
72 // Remember the current message loop, so we can communicate with this thread | 73 // Remember the current message loop, so we can communicate with this thread |
73 // again when we need to shutdown (see ReleaseProcess). | 74 // again when we need to shutdown (see ReleaseProcess). |
74 main_thread_loop_ = MessageLoop::current(); | 75 main_thread_loop_ = MessageLoop::current(); |
75 | 76 |
76 // An event that will be signalled when we shutdown. | 77 // An event that will be signalled when we shutdown. |
77 shutdown_event_ = CreateEvent(NULL, TRUE, FALSE, NULL); | 78 shutdown_event_ = new base::WaitableEvent(true, false); |
78 | 79 |
79 child_process_ = factory->Create(channel_name); | 80 child_process_ = factory->Create(channel_name); |
80 | 81 |
81 CommandLine command_line; | 82 CommandLine command_line; |
82 if (command_line.HasSwitch(switches::kUserAgent)) { | 83 if (command_line.HasSwitch(switches::kUserAgent)) { |
83 webkit_glue::SetUserAgent(WideToUTF8( | 84 webkit_glue::SetUserAgent(WideToUTF8( |
84 command_line.GetSwitchValue(switches::kUserAgent))); | 85 command_line.GetSwitchValue(switches::kUserAgent))); |
85 } | 86 } |
86 | 87 |
87 return true; | 88 return true; |
88 } | 89 } |
89 | 90 |
90 void ChildProcess::GlobalCleanup() { | 91 void ChildProcess::GlobalCleanup() { |
91 // Signal this event before destroying the child process. That way all | 92 // Signal this event before destroying the child process. That way all |
92 // background threads. | 93 // background threads. |
93 // For example, in the renderer the RenderThread instances will be able to | 94 // For example, in the renderer the RenderThread instances will be able to |
94 // notice shutdown before the render process begins waiting for them to exit. | 95 // notice shutdown before the render process begins waiting for them to exit. |
95 SetEvent(shutdown_event_); | 96 shutdown_event_->Signal(); |
96 | 97 |
97 // Destroy the child process first to force all background threads to | 98 // Destroy the child process first to force all background threads to |
98 // terminate before we bring down other resources. (We null pointers | 99 // terminate before we bring down other resources. (We null pointers |
99 // just in case.) | 100 // just in case.) |
100 child_process_->Cleanup(); | 101 child_process_->Cleanup(); |
101 delete child_process_; | 102 delete child_process_; |
102 child_process_ = NULL; | 103 child_process_ = NULL; |
103 | 104 |
104 main_thread_loop_ = NULL; | 105 main_thread_loop_ = NULL; |
105 | 106 |
106 CloseHandle(shutdown_event_); | 107 delete shutdown_event_; |
107 shutdown_event_ = NULL; | 108 shutdown_event_ = NULL; |
108 } | 109 } |
109 | 110 |
OLD | NEW |