1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
|
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/browser/browser_child_process_host.h"
#include "base/command_line.h"
#include "base/file_path.h"
#include "base/lazy_instance.h"
#include "base/logging.h"
#include "base/metrics/histogram.h"
#include "base/path_service.h"
#include "base/process_util.h"
#include "base/stl_util.h"
#include "base/string_util.h"
#include "content/browser/browser_thread.h"
#include "content/browser/content_browser_client.h"
#include "content/browser/renderer_host/resource_message_filter.h"
#include "content/browser/trace_message_filter.h"
#include "content/common/content_switches.h"
#include "content/common/notification_service.h"
#include "content/common/plugin_messages.h"
#include "content/common/process_watcher.h"
#include "content/common/result_codes.h"
namespace {
typedef std::list<BrowserChildProcessHost*> ChildProcessList;
static base::LazyInstance<ChildProcessList> g_child_process_list(
base::LINKER_INITIALIZED);
// The NotificationTask is used to notify about plugin process connection/
// disconnection. It is needed because the notifications in the
// NotificationService must happen in the main thread.
class ChildNotificationTask : public Task {
public:
ChildNotificationTask(
int notification_type, ChildProcessInfo* info)
: notification_type_(notification_type), info_(*info) { }
virtual void Run() {
NotificationService::current()->
Notify(notification_type_, NotificationService::AllSources(),
Details<ChildProcessInfo>(&info_));
}
private:
int notification_type_;
ChildProcessInfo info_;
};
} // namespace
BrowserChildProcessHost::BrowserChildProcessHost(
ChildProcessInfo::ProcessType type)
: ChildProcessInfo(type, -1),
ALLOW_THIS_IN_INITIALIZER_LIST(client_(this)) {
AddFilter(new TraceMessageFilter);
g_child_process_list.Get().push_back(this);
}
BrowserChildProcessHost::~BrowserChildProcessHost() {
g_child_process_list.Get().remove(this);
}
// static
void BrowserChildProcessHost::TerminateAll() {
// Make a copy since the ChildProcessHost dtor mutates the original list.
ChildProcessList copy = g_child_process_list.Get();
STLDeleteElements(©);
}
void BrowserChildProcessHost::Launch(
#if defined(OS_WIN)
const FilePath& exposed_dir,
#elif defined(OS_POSIX)
bool use_zygote,
const base::environment_vector& environ,
#endif
CommandLine* cmd_line) {
content::GetContentClient()->browser()->AppendExtraCommandLineSwitches(
cmd_line, id());
child_process_.reset(new ChildProcessLauncher(
#if defined(OS_WIN)
exposed_dir,
#elif defined(OS_POSIX)
use_zygote,
environ,
channel()->GetClientFileDescriptor(),
#endif
cmd_line,
&client_));
}
base::ProcessHandle BrowserChildProcessHost::GetChildProcessHandle() const {
DCHECK(child_process_.get())
<< "Requesting a child process handle before launching.";
DCHECK(child_process_->GetHandle())
<< "Requesting a child process handle before launch has completed OK.";
return child_process_->GetHandle();
}
void BrowserChildProcessHost::ForceShutdown() {
g_child_process_list.Get().remove(this);
ChildProcessHost::ForceShutdown();
}
void BrowserChildProcessHost::SetTerminateChildOnShutdown(
bool terminate_on_shutdown) {
child_process_->SetTerminateChildOnShutdown(terminate_on_shutdown);
}
void BrowserChildProcessHost::Notify(int type) {
BrowserThread::PostTask(
BrowserThread::UI, FROM_HERE, new ChildNotificationTask(type, this));
}
base::TerminationStatus BrowserChildProcessHost::GetChildTerminationStatus(
int* exit_code) {
return child_process_->GetChildTerminationStatus(exit_code);
}
void BrowserChildProcessHost::OnChildDied() {
// This may be called by both the channel's OnChannelError handler
// as well as the process launcher's OnProcessLaunched handler, so
// we need to be careful about the state of the process launcher here.
if (child_process_.get() && !child_process_->IsStarting())
set_handle(child_process_->GetHandle());
if (handle() != base::kNullProcessHandle) {
int exit_code;
base::TerminationStatus status = GetChildTerminationStatus(&exit_code);
switch (status) {
case base::TERMINATION_STATUS_PROCESS_CRASHED:
case base::TERMINATION_STATUS_ABNORMAL_TERMINATION: {
OnProcessCrashed(exit_code);
// Report that this child process crashed.
Notify(content::NOTIFICATION_CHILD_PROCESS_CRASHED);
UMA_HISTOGRAM_COUNTS("ChildProcess.Crashes", this->type());
break;
}
case base::TERMINATION_STATUS_PROCESS_WAS_KILLED: {
OnProcessWasKilled(exit_code);
// Report that this child process was killed.
Notify(content::NOTIFICATION_CHILD_PROCESS_WAS_KILLED);
UMA_HISTOGRAM_COUNTS("ChildProcess.Kills", this->type());
break;
}
default:
break;
}
// Notify in the main loop of the disconnection.
Notify(content::NOTIFICATION_CHILD_PROCESS_HOST_DISCONNECTED);
}
ChildProcessHost::OnChildDied();
}
void BrowserChildProcessHost::ShutdownStarted() {
// Must remove the process from the list now, in case it gets used for a
// new instance before our watcher tells us that the process terminated.
g_child_process_list.Get().remove(this);
}
BrowserChildProcessHost::ClientHook::ClientHook(BrowserChildProcessHost* host)
: host_(host) {
}
void BrowserChildProcessHost::ClientHook::OnProcessLaunched() {
if (!host_->child_process_->GetHandle()) {
host_->OnChildDied();
return;
}
host_->set_handle(host_->child_process_->GetHandle());
host_->OnProcessLaunched();
}
BrowserChildProcessHost::Iterator::Iterator()
: all_(true), type_(UNKNOWN_PROCESS) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)) <<
"ChildProcessInfo::Iterator must be used on the IO thread.";
iterator_ = g_child_process_list.Get().begin();
}
BrowserChildProcessHost::Iterator::Iterator(ChildProcessInfo::ProcessType type)
: all_(false), type_(type) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)) <<
"ChildProcessInfo::Iterator must be used on the IO thread.";
iterator_ = g_child_process_list.Get().begin();
if (!Done() && (*iterator_)->type() != type_)
++(*this);
}
BrowserChildProcessHost* BrowserChildProcessHost::Iterator::operator++() {
do {
++iterator_;
if (Done())
break;
if (!all_ && (*iterator_)->type() != type_)
continue;
return *iterator_;
} while (true);
return NULL;
}
bool BrowserChildProcessHost::Iterator::Done() {
return iterator_ == g_child_process_list.Get().end();
}
|