Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(138)

Side by Side Diff: base/mp/mp_child_process_host.cc

Issue 1625015: Refactor ChildProcess and related classes to create a framework outside of br... (Closed) Base URL: http://src.chromium.org/svn/trunk/src/
Patch Set: Created 10 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « base/mp/mp_child_process_host.h ('k') | base/mp/mp_child_process_launcher.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2010 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 "base/mp/mp_child_process_host.h"
6
7 #include "base/command_line.h"
8 #include "base/compiler_specific.h"
9 #include "base/file_path.h"
10 #include "base/histogram.h"
11 #include "base/logging.h"
12 #include "base/mp/mp_child_process_context.h"
13 #include "base/mp/mp_messages.h"
14 #include "base/path_service.h"
15 #include "base/process_util.h"
16 #include "base/singleton.h"
17 #include "base/string_util.h"
18 #include "base/waitable_event.h"
19
20 #if defined(OS_LINUX)
21 #include "base/linux_util.h"
22 #endif // OS_LINUX
23
24
25 namespace base {
26
27 namespace {
28
29 typedef std::list<MpChildProcessHost*> ChildProcessList;
30
31 } // namespace
32
33 MpChildProcessHost::MpChildProcessHost(MpChildProcessContext* context)
34 : ALLOW_THIS_IN_INITIALIZER_LIST(listener_(this)),
35 context_(context),
36 opening_channel_(false) {
37 Singleton<ChildProcessList>::get()->push_back(this);
38 }
39
40
41 MpChildProcessHost::~MpChildProcessHost() {
42 Singleton<ChildProcessList>::get()->remove(this);
43 }
44
45 void MpChildProcessHost::Launch(
46 #if defined(OS_WIN)
47 const FilePath& exposed_dir,
48 #elif defined(OS_POSIX)
49 bool use_zygote,
50 const base::environment_vector& environ,
51 #endif
52 CommandLine* cmd_line) {
53 child_process_.reset(new MpChildProcessLauncher(
54 #if defined(OS_WIN)
55 exposed_dir,
56 #elif defined(OS_POSIX)
57 use_zygote,
58 environ,
59 channel_->GetClientFileDescriptor(),
60 #endif
61 cmd_line,
62 context_,
63 &listener_));
64 }
65
66 bool MpChildProcessHost::CreateChannel() {
67 channel_id_ = context_->GenerateRandomChannelID(this);
68 channel_.reset(new IPC::Channel(
69 channel_id_, IPC::Channel::MODE_SERVER, &listener_));
70 if (!channel_->Connect())
71 return false;
72
73 opening_channel_ = true;
74
75 return true;
76 }
77
78 void MpChildProcessHost::InstanceCreated() {
79 Notify(CHILD_INSTANCE_CREATED);
80 }
81
82 bool MpChildProcessHost::Send(IPC::Message* msg) {
83 if (!channel_.get()) {
84 delete msg;
85 return false;
86 }
87 return channel_->Send(msg);
88 }
89
90 bool MpChildProcessHost::DidChildCrash() {
91 return child_process_->DidProcessCrash();
92 }
93
94 void MpChildProcessHost::OnChildDied() {
95 if (GetHandle() != base::kNullProcessHandle) {
96 bool did_crash = DidChildCrash();
97 if (did_crash) {
98 OnProcessCrashed();
99 // Report that this child process crashed.
100 Notify(CHILD_PROCESS_CRASHED);
101 UMA_HISTOGRAM_COUNTS("ChildProcess.Crashes", GetType());
102 }
103 // Notify in the main loop of the disconnection.
104 Notify(CHILD_PROCESS_HOST_DISCONNECTED);
105 }
106
107 delete this;
108 }
109
110 MpChildProcessHost::ListenerHook::ListenerHook(MpChildProcessHost* host)
111 : host_(host) {
112 }
113
114 void MpChildProcessHost::ListenerHook::OnMessageReceived(
115 const IPC::Message& msg) {
116 #ifdef IPC_MESSAGE_LOG_ENABLED
117 IPC::Logging* logger = IPC::Logging::current();
118 if (msg.type() == IPC_LOGGING_ID) {
119 logger->OnReceivedLoggingMessage(msg);
120 return;
121 }
122
123 if (logger->Enabled())
124 logger->OnPreDispatchMessage(msg);
125 #endif
126
127 bool msg_is_ok = true;
128 bool handled = host_->OnDispatchMessageReceived(msg, &msg_is_ok);
129
130 if (!handled) {
131 if (msg.type() == MultiProcessHostMsg_ShutdownRequest::ID) {
132 // Must remove the process from the list now, in case it gets used for a
133 // new instance before our watcher tells us that the process terminated.
134 Singleton<ChildProcessList>::get()->remove(host_);
135 if (host_->CanShutdown())
136 host_->Send(new MultiProcessMsg_Shutdown());
137 } else {
138 host_->OnMessageReceived(msg);
139 }
140 }
141
142 if (!msg_is_ok) {
143 base::KillProcess(host_->GetHandle(),
144 host_->context_->GetBadMessageResultCode(), false);
145 }
146
147 #ifdef IPC_MESSAGE_LOG_ENABLED
148 if (logger->Enabled())
149 logger->OnPostDispatchMessage(msg, host_->channel_id_);
150 #endif
151 }
152
153 void MpChildProcessHost::ListenerHook::OnChannelConnected(int32 peer_pid) {
154 host_->opening_channel_ = false;
155 host_->OnChannelConnected(peer_pid);
156
157 #if defined(IPC_MESSAGE_LOG_ENABLED)
158 bool enabled = IPC::Logging::current()->Enabled();
159 host_->Send(new MultiProcessMsg_SetIPCLoggingEnabled(enabled));
160 #endif
161
162 host_->Send(new MultiProcessMsg_AskBeforeShutdown());
163
164 // Notify in the main loop of the connection.
165 host_->Notify(CHILD_PROCESS_HOST_CONNECTED);
166 }
167
168 void MpChildProcessHost::ListenerHook::OnChannelError() {
169 host_->opening_channel_ = false;
170 host_->OnChannelError();
171
172 // This will delete host_, which will also destroy this!
173 host_->OnChildDied();
174 }
175
176 void MpChildProcessHost::ListenerHook::OnProcessLaunched() {
177 if (!host_->child_process_->GetHandle()) {
178 delete this;
179 return;
180 }
181
182 host_->OnProcessLaunched();
183 }
184
185 void MpChildProcessHost::ForceShutdown() {
186 Singleton<ChildProcessList>::get()->remove(this);
187 Send(new MultiProcessMsg_Shutdown());
188 }
189
190 } // namespace base
OLDNEW
« no previous file with comments | « base/mp/mp_child_process_host.h ('k') | base/mp/mp_child_process_launcher.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698