Chromium Code Reviews| Index: components/crash/content/browser/crash_micro_dump_manager_android.cc |
| diff --git a/components/crash/content/browser/crash_micro_dump_manager_android.cc b/components/crash/content/browser/crash_micro_dump_manager_android.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..89734e7fb627efabd0d0db619ecebb1adbe619f7 |
| --- /dev/null |
| +++ b/components/crash/content/browser/crash_micro_dump_manager_android.cc |
| @@ -0,0 +1,157 @@ |
| +// Copyright 2015 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 "components/crash/content/browser/crash_micro_dump_manager_android.h" |
| + |
| +#include <fcntl.h> |
| +#include <stdlib.h> |
| +#include <sys/socket.h> |
| +#include <sys/types.h> |
| +#include <unistd.h> |
| + |
| +#include "base/bind.h" |
| +#include "base/files/scoped_file.h" |
| +#include "base/logging.h" |
| +#include "base/posix/eintr_wrapper.h" |
| +#include "base/stl_util.h" |
| +#include "content/public/browser/browser_thread.h" |
| +#include "content/public/browser/child_process_data.h" |
| +#include "content/public/browser/notification_service.h" |
| +#include "content/public/browser/notification_types.h" |
| +#include "content/public/browser/render_process_host.h" |
| + |
| +using content::BrowserThread; |
| + |
| +namespace breakpad { |
| + |
| +// static |
| +CrashMicroDumpManager* CrashMicroDumpManager::instance_ = nullptr; |
| + |
| +// static |
| +CrashMicroDumpManager* CrashMicroDumpManager::GetInstance() { |
| + CHECK(instance_); |
| + return instance_; |
| +} |
| + |
| +CrashMicroDumpManager::CrashMicroDumpManager() { |
| + DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| + DCHECK(!instance_); |
| + |
| + instance_ = this; |
| + |
| + notification_registrar_.Add(this, |
| + content::NOTIFICATION_RENDERER_PROCESS_TERMINATED, |
| + content::NotificationService::AllSources()); |
| + notification_registrar_.Add(this, |
| + content::NOTIFICATION_RENDERER_PROCESS_CLOSED, |
| + content::NotificationService::AllSources()); |
| + BrowserChildProcessObserver::Add(this); |
| +} |
| + |
| +CrashMicroDumpManager::~CrashMicroDumpManager() { |
| + instance_ = nullptr; |
| + BrowserChildProcessObserver::Remove(this); |
| + { |
| + base::AutoLock auto_lock(child_process_id_to_pipe_fd_lock_); |
| + for (const auto& iter : child_process_id_to_pipe_fd_) { |
| + IGNORE_EINTR(close(iter.second)); |
| + } |
| + } |
| +} |
| + |
| +bool SocketPair(int* fd1, int* fd2) { |
|
Torne
2015/12/16 14:48:35
Don't we have a utility somewhere that does this?
mnaganov (inactive)
2015/12/16 19:10:12
It's good you've asked! I initially copied this co
|
| + int pipe_fds[2]; |
| + if (socketpair(AF_UNIX, SOCK_STREAM, 0, pipe_fds) != 0) { |
| + PLOG(ERROR) << "socketpair()"; |
| + return false; |
| + } |
| + |
| + // Set both ends to be non-blocking. |
| + if (fcntl(pipe_fds[0], F_SETFL, O_NONBLOCK) == -1 || |
| + fcntl(pipe_fds[1], F_SETFL, O_NONBLOCK) == -1) { |
| + PLOG(ERROR) << "fcntl(O_NONBLOCK)"; |
| + if (IGNORE_EINTR(close(pipe_fds[0])) < 0) |
| + PLOG(ERROR) << "close"; |
| + if (IGNORE_EINTR(close(pipe_fds[1])) < 0) |
| + PLOG(ERROR) << "close"; |
| + return false; |
| + } |
| + |
| + *fd1 = pipe_fds[0]; |
| + *fd2 = pipe_fds[1]; |
| + |
| + return true; |
| +} |
| + |
| +base::File CrashMicroDumpManager::CreateCrashInfoChannel(int child_process_id) { |
| + DCHECK_CURRENTLY_ON(BrowserThread::PROCESS_LAUNCHER); |
| + int local_pipe_fd = -1, child_pipe_fd = -1; |
| + if (!SocketPair(&local_pipe_fd, &child_pipe_fd)) { |
| + return base::File(); |
| + } |
| + { |
| + base::AutoLock auto_lock(child_process_id_to_pipe_fd_lock_); |
| + DCHECK(!ContainsKey(child_process_id_to_pipe_fd_, child_process_id)); |
| + child_process_id_to_pipe_fd_[child_process_id] = local_pipe_fd; |
| + } |
| + return base::File(child_pipe_fd); |
| +} |
| + |
| +// static |
| +void CrashMicroDumpManager::HandleChildTermination(int pipe_fd) { |
| + DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
| + CHECK(instance_); |
| + |
| + base::ScopedFD pipe(pipe_fd); |
| + int exit_code; |
| + ssize_t num_read = HANDLE_EINTR(read(pipe_fd, &exit_code, sizeof(exit_code))); |
| + if (num_read <= 0) { |
| + // The child process hasn't written anything into the pipe. Assuming |
| + // that it didn't crash, thus we need to perform a clean exit. |
| + exit(0); |
| + } else { |
| + LOG(FATAL) << "Renderer process crash detected. Terminating browser."; |
| + } |
| +} |
| + |
| +void CrashMicroDumpManager::BrowserChildProcessHostDisconnected( |
| + const content::ChildProcessData& data) { |
| + OnChildExit(data.id); |
| +} |
| + |
| +void CrashMicroDumpManager::BrowserChildProcessCrashed( |
| + const content::ChildProcessData& data, |
| + int exit_code) { |
| + OnChildExit(data.id); |
| +} |
| + |
| +void CrashMicroDumpManager::Observe( |
| + int type, |
| + const content::NotificationSource& source, |
| + const content::NotificationDetails& details) { |
| + content::RenderProcessHost* rph = |
| + content::Source<content::RenderProcessHost>(source).ptr(); |
| + OnChildExit(rph->GetID()); |
| +} |
| + |
| +void CrashMicroDumpManager::OnChildExit(int child_process_id) { |
| + int pipe_fd = -1; |
| + { |
| + base::AutoLock auto_lock(child_process_id_to_pipe_fd_lock_); |
| + const auto& iter = child_process_id_to_pipe_fd_.find(child_process_id); |
| + if (iter == child_process_id_to_pipe_fd_.end()) { |
| + // We might get a NOTIFICATION_RENDERER_PROCESS_TERMINATED and a |
| + // NOTIFICATION_RENDERER_PROCESS_CLOSED. |
| + return; |
| + } |
| + pipe_fd = iter->second; |
| + child_process_id_to_pipe_fd_.erase(iter); |
| + } |
| + DCHECK(pipe_fd != -1); |
| + BrowserThread::PostTask( |
| + BrowserThread::FILE, FROM_HERE, |
| + base::Bind(&CrashMicroDumpManager::HandleChildTermination, pipe_fd)); |
| +} |
| + |
| +} // namespace breakpad |