| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2012 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_thread.h" | |
| 6 | |
| 7 #include "base/allocator/allocator_extension.h" | |
| 8 #include "base/command_line.h" | |
| 9 #include "base/message_loop.h" | |
| 10 #include "base/process.h" | |
| 11 #include "base/process_util.h" | |
| 12 #include "base/string_util.h" | |
| 13 #include "base/tracked_objects.h" | |
| 14 #include "components/tracing/child_trace_message_filter.h" | |
| 15 #include "content/common/child_histogram_message_filter.h" | |
| 16 #include "content/common/child_process.h" | |
| 17 #include "content/common/child_process_messages.h" | |
| 18 #include "content/common/child_resource_message_filter.h" | |
| 19 #include "content/common/fileapi/file_system_dispatcher.h" | |
| 20 #include "content/common/quota_dispatcher.h" | |
| 21 #include "content/common/resource_dispatcher.h" | |
| 22 #include "content/common/socket_stream_dispatcher.h" | |
| 23 #include "content/common/thread_safe_sender.h" | |
| 24 #include "content/public/common/content_switches.h" | |
| 25 #include "ipc/ipc_logging.h" | |
| 26 #include "ipc/ipc_switches.h" | |
| 27 #include "ipc/ipc_sync_channel.h" | |
| 28 #include "ipc/ipc_sync_message_filter.h" | |
| 29 #include "webkit/glue/webkit_glue.h" | |
| 30 | |
| 31 #if defined(OS_WIN) | |
| 32 #include "content/common/handle_enumerator_win.h" | |
| 33 #endif | |
| 34 | |
| 35 using tracked_objects::ThreadData; | |
| 36 | |
| 37 namespace content { | |
| 38 namespace { | |
| 39 | |
| 40 // How long to wait for a connection to the browser process before giving up. | |
| 41 const int kConnectionTimeoutS = 15; | |
| 42 | |
| 43 // This isn't needed on Windows because there the sandbox's job object | |
| 44 // terminates child processes automatically. For unsandboxed processes (i.e. | |
| 45 // plugins), PluginThread has EnsureTerminateMessageFilter. | |
| 46 #if defined(OS_POSIX) | |
| 47 | |
| 48 class SuicideOnChannelErrorFilter : public IPC::ChannelProxy::MessageFilter { | |
| 49 public: | |
| 50 // IPC::ChannelProxy::MessageFilter | |
| 51 virtual void OnChannelError() OVERRIDE { | |
| 52 // For renderer/worker processes: | |
| 53 // On POSIX, at least, one can install an unload handler which loops | |
| 54 // forever and leave behind a renderer process which eats 100% CPU forever. | |
| 55 // | |
| 56 // This is because the terminate signals (ViewMsg_ShouldClose and the error | |
| 57 // from the IPC channel) are routed to the main message loop but never | |
| 58 // processed (because that message loop is stuck in V8). | |
| 59 // | |
| 60 // One could make the browser SIGKILL the renderers, but that leaves open a | |
| 61 // large window where a browser failure (or a user, manually terminating | |
| 62 // the browser because "it's stuck") will leave behind a process eating all | |
| 63 // the CPU. | |
| 64 // | |
| 65 // So, we install a filter on the channel so that we can process this event | |
| 66 // here and kill the process. | |
| 67 // | |
| 68 // We want to kill this process after giving it 30 seconds to run the exit | |
| 69 // handlers. SIGALRM has a default disposition of terminating the | |
| 70 // application. | |
| 71 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kChildCleanExit)) | |
| 72 alarm(30); | |
| 73 else | |
| 74 _exit(0); | |
| 75 } | |
| 76 | |
| 77 protected: | |
| 78 virtual ~SuicideOnChannelErrorFilter() {} | |
| 79 }; | |
| 80 | |
| 81 #endif // OS(POSIX) | |
| 82 | |
| 83 } // namespace | |
| 84 | |
| 85 ChildThread::ChildThread() | |
| 86 : channel_connected_factory_(this) { | |
| 87 channel_name_ = CommandLine::ForCurrentProcess()->GetSwitchValueASCII( | |
| 88 switches::kProcessChannelID); | |
| 89 Init(); | |
| 90 } | |
| 91 | |
| 92 ChildThread::ChildThread(const std::string& channel_name) | |
| 93 : channel_name_(channel_name), | |
| 94 channel_connected_factory_(this) { | |
| 95 Init(); | |
| 96 } | |
| 97 | |
| 98 void ChildThread::Init() { | |
| 99 on_channel_error_called_ = false; | |
| 100 message_loop_ = base::MessageLoop::current(); | |
| 101 channel_.reset( | |
| 102 new IPC::SyncChannel(channel_name_, | |
| 103 IPC::Channel::MODE_CLIENT, | |
| 104 this, | |
| 105 ChildProcess::current()->io_message_loop_proxy(), | |
| 106 true, | |
| 107 ChildProcess::current()->GetShutDownEvent())); | |
| 108 #ifdef IPC_MESSAGE_LOG_ENABLED | |
| 109 IPC::Logging::GetInstance()->SetIPCSender(this); | |
| 110 #endif | |
| 111 | |
| 112 resource_dispatcher_.reset(new ResourceDispatcher(this)); | |
| 113 socket_stream_dispatcher_.reset(new SocketStreamDispatcher()); | |
| 114 file_system_dispatcher_.reset(new FileSystemDispatcher()); | |
| 115 quota_dispatcher_.reset(new QuotaDispatcher()); | |
| 116 | |
| 117 sync_message_filter_ = | |
| 118 new IPC::SyncMessageFilter(ChildProcess::current()->GetShutDownEvent()); | |
| 119 thread_safe_sender_ = new ThreadSafeSender(base::MessageLoopProxy::current(), | |
| 120 sync_message_filter_.get()); | |
| 121 histogram_message_filter_ = new ChildHistogramMessageFilter(); | |
| 122 resource_message_filter_ = | |
| 123 new ChildResourceMessageFilter(resource_dispatcher()); | |
| 124 | |
| 125 channel_->AddFilter(histogram_message_filter_.get()); | |
| 126 channel_->AddFilter(sync_message_filter_.get()); | |
| 127 channel_->AddFilter(new tracing::ChildTraceMessageFilter( | |
| 128 ChildProcess::current()->io_message_loop_proxy())); | |
| 129 channel_->AddFilter(resource_message_filter_.get()); | |
| 130 | |
| 131 #if defined(OS_POSIX) | |
| 132 // Check that --process-type is specified so we don't do this in unit tests | |
| 133 // and single-process mode. | |
| 134 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kProcessType)) | |
| 135 channel_->AddFilter(new SuicideOnChannelErrorFilter()); | |
| 136 #endif | |
| 137 | |
| 138 base::MessageLoop::current()->PostDelayedTask( | |
| 139 FROM_HERE, | |
| 140 base::Bind(&ChildThread::EnsureConnected, | |
| 141 channel_connected_factory_.GetWeakPtr()), | |
| 142 base::TimeDelta::FromSeconds(kConnectionTimeoutS)); | |
| 143 } | |
| 144 | |
| 145 ChildThread::~ChildThread() { | |
| 146 #ifdef IPC_MESSAGE_LOG_ENABLED | |
| 147 IPC::Logging::GetInstance()->SetIPCSender(NULL); | |
| 148 #endif | |
| 149 | |
| 150 channel_->RemoveFilter(histogram_message_filter_.get()); | |
| 151 channel_->RemoveFilter(sync_message_filter_.get()); | |
| 152 | |
| 153 // The ChannelProxy object caches a pointer to the IPC thread, so need to | |
| 154 // reset it as it's not guaranteed to outlive this object. | |
| 155 // NOTE: this also has the side-effect of not closing the main IPC channel to | |
| 156 // the browser process. This is needed because this is the signal that the | |
| 157 // browser uses to know that this process has died, so we need it to be alive | |
| 158 // until this process is shut down, and the OS closes the handle | |
| 159 // automatically. We used to watch the object handle on Windows to do this, | |
| 160 // but it wasn't possible to do so on POSIX. | |
| 161 channel_->ClearIPCTaskRunner(); | |
| 162 } | |
| 163 | |
| 164 void ChildThread::OnChannelConnected(int32 peer_pid) { | |
| 165 channel_connected_factory_.InvalidateWeakPtrs(); | |
| 166 } | |
| 167 | |
| 168 void ChildThread::OnChannelError() { | |
| 169 set_on_channel_error_called(true); | |
| 170 base::MessageLoop::current()->Quit(); | |
| 171 } | |
| 172 | |
| 173 bool ChildThread::Send(IPC::Message* msg) { | |
| 174 DCHECK(base::MessageLoop::current() == message_loop()); | |
| 175 if (!channel_) { | |
| 176 delete msg; | |
| 177 return false; | |
| 178 } | |
| 179 | |
| 180 return channel_->Send(msg); | |
| 181 } | |
| 182 | |
| 183 void ChildThread::AddRoute(int32 routing_id, IPC::Listener* listener) { | |
| 184 DCHECK(base::MessageLoop::current() == message_loop()); | |
| 185 | |
| 186 router_.AddRoute(routing_id, listener); | |
| 187 } | |
| 188 | |
| 189 void ChildThread::RemoveRoute(int32 routing_id) { | |
| 190 DCHECK(base::MessageLoop::current() == message_loop()); | |
| 191 | |
| 192 router_.RemoveRoute(routing_id); | |
| 193 } | |
| 194 | |
| 195 webkit_glue::ResourceLoaderBridge* ChildThread::CreateBridge( | |
| 196 const webkit_glue::ResourceLoaderBridge::RequestInfo& request_info) { | |
| 197 return resource_dispatcher()->CreateBridge(request_info); | |
| 198 } | |
| 199 | |
| 200 base::SharedMemory* ChildThread::AllocateSharedMemory(size_t buf_size) { | |
| 201 return AllocateSharedMemory(buf_size, this); | |
| 202 } | |
| 203 | |
| 204 // static | |
| 205 base::SharedMemory* ChildThread::AllocateSharedMemory( | |
| 206 size_t buf_size, | |
| 207 IPC::Sender* sender) { | |
| 208 scoped_ptr<base::SharedMemory> shared_buf; | |
| 209 #if defined(OS_WIN) | |
| 210 shared_buf.reset(new base::SharedMemory); | |
| 211 if (!shared_buf->CreateAndMapAnonymous(buf_size)) { | |
| 212 NOTREACHED(); | |
| 213 return NULL; | |
| 214 } | |
| 215 #else | |
| 216 // On POSIX, we need to ask the browser to create the shared memory for us, | |
| 217 // since this is blocked by the sandbox. | |
| 218 base::SharedMemoryHandle shared_mem_handle; | |
| 219 if (sender->Send(new ChildProcessHostMsg_SyncAllocateSharedMemory( | |
| 220 buf_size, &shared_mem_handle))) { | |
| 221 if (base::SharedMemory::IsHandleValid(shared_mem_handle)) { | |
| 222 shared_buf.reset(new base::SharedMemory(shared_mem_handle, false)); | |
| 223 if (!shared_buf->Map(buf_size)) { | |
| 224 NOTREACHED() << "Map failed"; | |
| 225 return NULL; | |
| 226 } | |
| 227 } else { | |
| 228 NOTREACHED() << "Browser failed to allocate shared memory"; | |
| 229 return NULL; | |
| 230 } | |
| 231 } else { | |
| 232 NOTREACHED() << "Browser allocation request message failed"; | |
| 233 return NULL; | |
| 234 } | |
| 235 #endif | |
| 236 return shared_buf.release(); | |
| 237 } | |
| 238 | |
| 239 bool ChildThread::OnMessageReceived(const IPC::Message& msg) { | |
| 240 // Resource responses are sent to the resource dispatcher. | |
| 241 if (resource_dispatcher_->OnMessageReceived(msg)) | |
| 242 return true; | |
| 243 if (socket_stream_dispatcher_->OnMessageReceived(msg)) | |
| 244 return true; | |
| 245 if (file_system_dispatcher_->OnMessageReceived(msg)) | |
| 246 return true; | |
| 247 if (quota_dispatcher_->OnMessageReceived(msg)) | |
| 248 return true; | |
| 249 | |
| 250 bool handled = true; | |
| 251 IPC_BEGIN_MESSAGE_MAP(ChildThread, msg) | |
| 252 IPC_MESSAGE_HANDLER(ChildProcessMsg_Shutdown, OnShutdown) | |
| 253 #if defined(IPC_MESSAGE_LOG_ENABLED) | |
| 254 IPC_MESSAGE_HANDLER(ChildProcessMsg_SetIPCLoggingEnabled, | |
| 255 OnSetIPCLoggingEnabled) | |
| 256 #endif | |
| 257 IPC_MESSAGE_HANDLER(ChildProcessMsg_SetProfilerStatus, | |
| 258 OnSetProfilerStatus) | |
| 259 IPC_MESSAGE_HANDLER(ChildProcessMsg_GetChildProfilerData, | |
| 260 OnGetChildProfilerData) | |
| 261 IPC_MESSAGE_HANDLER(ChildProcessMsg_DumpHandles, OnDumpHandles) | |
| 262 #if defined(USE_TCMALLOC) | |
| 263 IPC_MESSAGE_HANDLER(ChildProcessMsg_GetTcmallocStats, OnGetTcmallocStats) | |
| 264 #endif | |
| 265 IPC_MESSAGE_UNHANDLED(handled = false) | |
| 266 IPC_END_MESSAGE_MAP() | |
| 267 | |
| 268 if (handled) | |
| 269 return true; | |
| 270 | |
| 271 if (msg.routing_id() == MSG_ROUTING_CONTROL) | |
| 272 return OnControlMessageReceived(msg); | |
| 273 | |
| 274 return router_.OnMessageReceived(msg); | |
| 275 } | |
| 276 | |
| 277 bool ChildThread::OnControlMessageReceived(const IPC::Message& msg) { | |
| 278 return false; | |
| 279 } | |
| 280 | |
| 281 void ChildThread::OnShutdown() { | |
| 282 base::MessageLoop::current()->Quit(); | |
| 283 } | |
| 284 | |
| 285 #if defined(IPC_MESSAGE_LOG_ENABLED) | |
| 286 void ChildThread::OnSetIPCLoggingEnabled(bool enable) { | |
| 287 if (enable) | |
| 288 IPC::Logging::GetInstance()->Enable(); | |
| 289 else | |
| 290 IPC::Logging::GetInstance()->Disable(); | |
| 291 } | |
| 292 #endif // IPC_MESSAGE_LOG_ENABLED | |
| 293 | |
| 294 void ChildThread::OnSetProfilerStatus(ThreadData::Status status) { | |
| 295 ThreadData::InitializeAndSetTrackingStatus(status); | |
| 296 } | |
| 297 | |
| 298 void ChildThread::OnGetChildProfilerData(int sequence_number) { | |
| 299 tracked_objects::ProcessDataSnapshot process_data; | |
| 300 ThreadData::Snapshot(false, &process_data); | |
| 301 | |
| 302 Send(new ChildProcessHostMsg_ChildProfilerData(sequence_number, | |
| 303 process_data)); | |
| 304 } | |
| 305 | |
| 306 void ChildThread::OnDumpHandles() { | |
| 307 #if defined(OS_WIN) | |
| 308 scoped_refptr<HandleEnumerator> handle_enum( | |
| 309 new HandleEnumerator( | |
| 310 CommandLine::ForCurrentProcess()->HasSwitch( | |
| 311 switches::kAuditAllHandles))); | |
| 312 handle_enum->EnumerateHandles(); | |
| 313 Send(new ChildProcessHostMsg_DumpHandlesDone); | |
| 314 return; | |
| 315 #endif | |
| 316 | |
| 317 NOTIMPLEMENTED(); | |
| 318 } | |
| 319 | |
| 320 #if defined(USE_TCMALLOC) | |
| 321 void ChildThread::OnGetTcmallocStats() { | |
| 322 std::string result; | |
| 323 char buffer[1024 * 32]; | |
| 324 base::allocator::GetStats(buffer, sizeof(buffer)); | |
| 325 result.append(buffer); | |
| 326 Send(new ChildProcessHostMsg_TcmallocStats(result)); | |
| 327 } | |
| 328 #endif | |
| 329 | |
| 330 ChildThread* ChildThread::current() { | |
| 331 return ChildProcess::current() ? | |
| 332 ChildProcess::current()->main_thread() : NULL; | |
| 333 } | |
| 334 | |
| 335 bool ChildThread::IsWebFrameValid(WebKit::WebFrame* frame) { | |
| 336 // Return false so that it is overridden in any process in which it is used. | |
| 337 return false; | |
| 338 } | |
| 339 | |
| 340 void ChildThread::OnProcessFinalRelease() { | |
| 341 if (on_channel_error_called_) { | |
| 342 base::MessageLoop::current()->Quit(); | |
| 343 return; | |
| 344 } | |
| 345 | |
| 346 // The child process shutdown sequence is a request response based mechanism, | |
| 347 // where we send out an initial feeler request to the child process host | |
| 348 // instance in the browser to verify if it's ok to shutdown the child process. | |
| 349 // The browser then sends back a response if it's ok to shutdown. This avoids | |
| 350 // race conditions if the process refcount is 0 but there's an IPC message | |
| 351 // inflight that would addref it. | |
| 352 Send(new ChildProcessHostMsg_ShutdownRequest); | |
| 353 } | |
| 354 | |
| 355 void ChildThread::EnsureConnected() { | |
| 356 LOG(INFO) << "ChildThread::EnsureConnected()"; | |
| 357 base::KillProcess(base::GetCurrentProcessHandle(), 0, false); | |
| 358 } | |
| 359 | |
| 360 } // namespace content | |
| OLD | NEW |