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

Side by Side Diff: content/common/child_process_host.cc

Issue 8787004: Make ChildProcessHost be used through an interface in content/public, instead of by inheritence. (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 9 years 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 | « content/common/child_process_host.h ('k') | content/common/child_process_host_impl.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) 2011 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 "content/common/child_process_host.h"
6
7 #include <limits>
8
9 #include "base/atomicops.h"
10 #include "base/command_line.h"
11 #include "base/file_path.h"
12 #include "base/logging.h"
13 #include "base/metrics/histogram.h"
14 #include "base/path_service.h"
15 #include "base/process_util.h"
16 #include "base/rand_util.h"
17 #include "base/stringprintf.h"
18 #include "base/third_party/dynamic_annotations/dynamic_annotations.h"
19 #include "content/common/child_process_messages.h"
20 #include "content/public/common/child_process_host_delegate.h"
21 #include "content/public/common/content_paths.h"
22 #include "content/public/common/content_switches.h"
23 #include "ipc/ipc_logging.h"
24
25 #if defined(OS_LINUX)
26 #include "base/linux_util.h"
27 #elif defined(OS_WIN)
28 #include "content/common/font_cache_dispatcher_win.h"
29 #endif // OS_LINUX
30
31 #if defined(OS_MACOSX)
32 namespace {
33
34 // Given |path| identifying a Mac-style child process executable path, adjusts
35 // it to correspond to |feature|. For a child process path such as
36 // ".../Chromium Helper.app/Contents/MacOS/Chromium Helper", the transformed
37 // path for feature "NP" would be
38 // ".../Chromium Helper NP.app/Contents/MacOS/Chromium Helper NP". The new
39 // path is returned.
40 FilePath TransformPathForFeature(const FilePath& path,
41 const std::string& feature) {
42 std::string basename = path.BaseName().value();
43
44 FilePath macos_path = path.DirName();
45 const char kMacOSName[] = "MacOS";
46 DCHECK_EQ(kMacOSName, macos_path.BaseName().value());
47
48 FilePath contents_path = macos_path.DirName();
49 const char kContentsName[] = "Contents";
50 DCHECK_EQ(kContentsName, contents_path.BaseName().value());
51
52 FilePath helper_app_path = contents_path.DirName();
53 const char kAppExtension[] = ".app";
54 std::string basename_app = basename;
55 basename_app.append(kAppExtension);
56 DCHECK_EQ(basename_app, helper_app_path.BaseName().value());
57
58 FilePath root_path = helper_app_path.DirName();
59
60 std::string new_basename = basename;
61 new_basename.append(1, ' ');
62 new_basename.append(feature);
63 std::string new_basename_app = new_basename;
64 new_basename_app.append(kAppExtension);
65
66 FilePath new_path = root_path.Append(new_basename_app)
67 .Append(kContentsName)
68 .Append(kMacOSName)
69 .Append(new_basename);
70
71 return new_path;
72 }
73
74 } // namespace
75 #endif // OS_MACOSX
76
77 ChildProcessHost::ChildProcessHost(content::ChildProcessHostDelegate* delegate)
78 : delegate_(delegate),
79 peer_handle_(base::kNullProcessHandle),
80 opening_channel_(false) {
81 #if defined(OS_WIN)
82 AddFilter(new FontCacheDispatcher());
83 #endif
84 }
85
86 ChildProcessHost::~ChildProcessHost() {
87 for (size_t i = 0; i < filters_.size(); ++i) {
88 filters_[i]->OnChannelClosing();
89 filters_[i]->OnFilterRemoved();
90 }
91
92 base::CloseProcessHandle(peer_handle_);
93 }
94
95 void ChildProcessHost::AddFilter(IPC::ChannelProxy::MessageFilter* filter) {
96 filters_.push_back(filter);
97
98 if (channel_.get())
99 filter->OnFilterAdded(channel_.get());
100 }
101
102 // static
103 FilePath ChildProcessHost::GetChildPath(int flags) {
104 FilePath child_path;
105
106 child_path = CommandLine::ForCurrentProcess()->GetSwitchValuePath(
107 switches::kBrowserSubprocessPath);
108
109 #if defined(OS_LINUX)
110 // Use /proc/self/exe rather than our known binary path so updates
111 // can't swap out the binary from underneath us.
112 // When running under Valgrind, forking /proc/self/exe ends up forking the
113 // Valgrind executable, which then crashes. However, it's almost safe to
114 // assume that the updates won't happen while testing with Valgrind tools.
115 if (child_path.empty() && flags & CHILD_ALLOW_SELF && !RunningOnValgrind())
116 child_path = FilePath("/proc/self/exe");
117 #endif
118
119 // On most platforms, the child executable is the same as the current
120 // executable.
121 if (child_path.empty())
122 PathService::Get(content::CHILD_PROCESS_EXE, &child_path);
123
124 #if defined(OS_MACOSX)
125 DCHECK(!(flags & CHILD_NO_PIE && flags & CHILD_ALLOW_HEAP_EXECUTION));
126
127 // If needed, choose an executable with special flags set that inform the
128 // kernel to enable or disable specific optional process-wide features.
129 if (flags & CHILD_NO_PIE) {
130 // "NP" is "No PIE". This results in Chromium Helper NP.app or
131 // Google Chrome Helper NP.app.
132 child_path = TransformPathForFeature(child_path, "NP");
133 } else if (flags & CHILD_ALLOW_HEAP_EXECUTION) {
134 // "EH" is "Executable Heap". A non-executable heap is only available to
135 // 32-bit processes on Mac OS X 10.7. Most code can and should run with a
136 // non-executable heap, but the "EH" feature is provided to allow code
137 // intolerant of a non-executable heap to work properly on 10.7. This
138 // results in Chromium Helper EH.app or Google Chrome Helper EH.app.
139 child_path = TransformPathForFeature(child_path, "EH");
140 }
141 #endif
142
143 return child_path;
144 }
145
146 void ChildProcessHost::ForceShutdown() {
147 Send(new ChildProcessMsg_Shutdown());
148 }
149
150 bool ChildProcessHost::CreateChannel() {
151 channel_id_ = GenerateRandomChannelID(this);
152 channel_.reset(new IPC::Channel(
153 channel_id_, IPC::Channel::MODE_SERVER, this));
154 if (!channel_->Connect())
155 return false;
156
157 for (size_t i = 0; i < filters_.size(); ++i)
158 filters_[i]->OnFilterAdded(channel_.get());
159
160 // Make sure these messages get sent first.
161 #if defined(IPC_MESSAGE_LOG_ENABLED)
162 bool enabled = IPC::Logging::GetInstance()->Enabled();
163 Send(new ChildProcessMsg_SetIPCLoggingEnabled(enabled));
164 #endif
165
166 Send(new ChildProcessMsg_AskBeforeShutdown());
167
168 opening_channel_ = true;
169
170 return true;
171 }
172
173 bool ChildProcessHost::Send(IPC::Message* message) {
174 if (!channel_.get()) {
175 delete message;
176 return false;
177 }
178 return channel_->Send(message);
179 }
180
181 void ChildProcessHost::AllocateSharedMemory(
182 uint32 buffer_size, base::ProcessHandle child_process_handle,
183 base::SharedMemoryHandle* shared_memory_handle) {
184 base::SharedMemory shared_buf;
185 if (!shared_buf.CreateAndMapAnonymous(buffer_size)) {
186 *shared_memory_handle = base::SharedMemory::NULLHandle();
187 NOTREACHED() << "Cannot map shared memory buffer";
188 return;
189 }
190 shared_buf.GiveToProcess(child_process_handle, shared_memory_handle);
191 }
192
193 std::string ChildProcessHost::GenerateRandomChannelID(void* instance) {
194 // Note: the string must start with the current process id, this is how
195 // child processes determine the pid of the parent.
196 // Build the channel ID. This is composed of a unique identifier for the
197 // parent browser process, an identifier for the child instance, and a random
198 // component. We use a random component so that a hacked child process can't
199 // cause denial of service by causing future named pipe creation to fail.
200 return base::StringPrintf("%d.%p.%d",
201 base::GetCurrentProcId(), instance,
202 base::RandInt(0, std::numeric_limits<int>::max()));
203 }
204
205 int ChildProcessHost::GenerateChildProcessUniqueId() {
206 // This function must be threadsafe.
207 static base::subtle::Atomic32 last_unique_child_id = 0;
208 return base::subtle::NoBarrier_AtomicIncrement(&last_unique_child_id, 1);
209 }
210
211 bool ChildProcessHost::OnMessageReceived(const IPC::Message& msg) {
212 #ifdef IPC_MESSAGE_LOG_ENABLED
213 IPC::Logging* logger = IPC::Logging::GetInstance();
214 if (msg.type() == IPC_LOGGING_ID) {
215 logger->OnReceivedLoggingMessage(msg);
216 return true;
217 }
218
219 if (logger->Enabled())
220 logger->OnPreDispatchMessage(msg);
221 #endif
222
223 bool handled = false;
224 for (size_t i = 0; i < filters_.size(); ++i) {
225 if (filters_[i]->OnMessageReceived(msg)) {
226 handled = true;
227 break;
228 }
229 }
230
231 if (!handled) {
232 handled = true;
233 IPC_BEGIN_MESSAGE_MAP(ChildProcessHost, msg)
234 IPC_MESSAGE_HANDLER(ChildProcessHostMsg_ShutdownRequest,
235 OnShutdownRequest)
236 IPC_MESSAGE_HANDLER(ChildProcessHostMsg_SyncAllocateSharedMemory,
237 OnAllocateSharedMemory)
238 IPC_MESSAGE_UNHANDLED(handled = false)
239 IPC_END_MESSAGE_MAP()
240
241 if (!handled)
242 handled = delegate_->OnMessageReceived(msg);
243 }
244
245 #ifdef IPC_MESSAGE_LOG_ENABLED
246 if (logger->Enabled())
247 logger->OnPostDispatchMessage(msg, channel_id_);
248 #endif
249 return handled;
250 }
251
252 void ChildProcessHost::OnChannelConnected(int32 peer_pid) {
253 if (!base::OpenProcessHandle(peer_pid, &peer_handle_)) {
254 NOTREACHED();
255 }
256 opening_channel_ = false;
257 delegate_->OnChannelConnected(peer_pid);
258 for (size_t i = 0; i < filters_.size(); ++i)
259 filters_[i]->OnChannelConnected(peer_pid);
260 }
261
262 void ChildProcessHost::OnChannelError() {
263 opening_channel_ = false;
264 delegate_->OnChannelError();
265
266 for (size_t i = 0; i < filters_.size(); ++i)
267 filters_[i]->OnChannelError();
268
269 // This will delete host_, which will also destroy this!
270 delegate_->OnChildDisconnected();
271 }
272
273 void ChildProcessHost::OnAllocateSharedMemory(
274 uint32 buffer_size,
275 base::SharedMemoryHandle* handle) {
276 AllocateSharedMemory(buffer_size, peer_handle_, handle);
277 }
278
279 void ChildProcessHost::OnShutdownRequest() {
280 if (delegate_->CanShutdown())
281 Send(new ChildProcessMsg_Shutdown());
282 }
OLDNEW
« no previous file with comments | « content/common/child_process_host.h ('k') | content/common/child_process_host_impl.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698