OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 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 "components/crash/content/browser/crash_dump_observer_android.h" |
| 6 |
| 7 #include <unistd.h> |
| 8 |
| 9 #include "base/bind.h" |
| 10 #include "base/logging.h" |
| 11 #include "base/stl_util.h" |
| 12 #include "base/threading/sequenced_worker_pool.h" |
| 13 #include "content/public/browser/browser_thread.h" |
| 14 #include "content/public/browser/child_process_data.h" |
| 15 #include "content/public/browser/notification_service.h" |
| 16 #include "content/public/browser/notification_types.h" |
| 17 #include "content/public/browser/render_process_host.h" |
| 18 |
| 19 using content::BrowserThread; |
| 20 |
| 21 namespace breakpad { |
| 22 |
| 23 // static |
| 24 CrashDumpObserver* CrashDumpObserver::GetInstance() { |
| 25 return base::Singleton<CrashDumpObserver>::get(); |
| 26 } |
| 27 |
| 28 CrashDumpObserver::CrashDumpObserver() { |
| 29 notification_registrar_.Add(this, |
| 30 content::NOTIFICATION_RENDERER_PROCESS_TERMINATED, |
| 31 content::NotificationService::AllSources()); |
| 32 notification_registrar_.Add(this, |
| 33 content::NOTIFICATION_RENDERER_PROCESS_CLOSED, |
| 34 content::NotificationService::AllSources()); |
| 35 } |
| 36 |
| 37 CrashDumpObserver::~CrashDumpObserver() {} |
| 38 |
| 39 void CrashDumpObserver::OnChildExitOnBlockingPool( |
| 40 Client* client, |
| 41 int child_process_id, |
| 42 base::ProcessHandle pid, |
| 43 content::ProcessType process_type, |
| 44 base::TerminationStatus termination_status, |
| 45 base::android::ApplicationState app_state) { |
| 46 base::AutoLock auto_lock(registered_clients_lock_); |
| 47 // Only call Client::OnChildExit if we haven't been removed in the |
| 48 // interim. |
| 49 if (base::ContainsValue(registered_clients_, client)) { |
| 50 client->OnChildExit(child_process_id, pid, process_type, termination_status, |
| 51 app_state); |
| 52 } |
| 53 } |
| 54 |
| 55 void CrashDumpObserver::OnChildExit(int child_process_id, |
| 56 base::ProcessHandle pid, |
| 57 content::ProcessType process_type, |
| 58 base::TerminationStatus termination_status, |
| 59 base::android::ApplicationState app_state) { |
| 60 base::AutoLock auto_lock(registered_clients_lock_); |
| 61 base::SequencedWorkerPool* pool = BrowserThread::GetBlockingPool(); |
| 62 base::SequencedWorkerPool::SequenceToken token = pool->GetSequenceToken(); |
| 63 |
| 64 for (auto& client : registered_clients_) { |
| 65 pool->PostSequencedWorkerTask( |
| 66 token, FROM_HERE, |
| 67 base::Bind(&CrashDumpObserver::OnChildExitOnBlockingPool, |
| 68 base::Unretained(this), client, child_process_id, pid, |
| 69 process_type, termination_status, app_state)); |
| 70 } |
| 71 } |
| 72 |
| 73 void CrashDumpObserver::RegisterClient(Client* client) { |
| 74 base::AutoLock auto_lock(registered_clients_lock_); |
| 75 if (std::find(std::begin(registered_clients_), std::end(registered_clients_), |
| 76 client) != std::end(registered_clients_)) { |
| 77 return; |
| 78 } |
| 79 registered_clients_.push_back(client); |
| 80 } |
| 81 |
| 82 void CrashDumpObserver::UnregisterClient(Client* client) { |
| 83 base::AutoLock auto_lock(registered_clients_lock_); |
| 84 registered_clients_.remove(client); |
| 85 } |
| 86 |
| 87 void CrashDumpObserver::BrowserChildProcessStarted( |
| 88 int child_process_id, |
| 89 content::FileDescriptorInfo* mappings) { |
| 90 base::AutoLock auto_lock(registered_clients_lock_); |
| 91 for (auto& client : registered_clients_) { |
| 92 client->OnChildStart(child_process_id, mappings); |
| 93 } |
| 94 } |
| 95 |
| 96 void CrashDumpObserver::BrowserChildProcessHostDisconnected( |
| 97 const content::ChildProcessData& data) { |
| 98 OnChildExit(data.id, data.handle, |
| 99 static_cast<content::ProcessType>(data.process_type), |
| 100 base::TERMINATION_STATUS_MAX_ENUM, |
| 101 base::android::APPLICATION_STATE_UNKNOWN); |
| 102 } |
| 103 |
| 104 void CrashDumpObserver::BrowserChildProcessCrashed( |
| 105 const content::ChildProcessData& data, |
| 106 int exit_code) { |
| 107 OnChildExit(data.id, data.handle, |
| 108 static_cast<content::ProcessType>(data.process_type), |
| 109 base::TERMINATION_STATUS_ABNORMAL_TERMINATION, |
| 110 base::android::APPLICATION_STATE_UNKNOWN); |
| 111 } |
| 112 |
| 113 void CrashDumpObserver::Observe(int type, |
| 114 const content::NotificationSource& source, |
| 115 const content::NotificationDetails& details) { |
| 116 content::RenderProcessHost* rph = |
| 117 content::Source<content::RenderProcessHost>(source).ptr(); |
| 118 base::TerminationStatus term_status = base::TERMINATION_STATUS_MAX_ENUM; |
| 119 base::android::ApplicationState app_state = |
| 120 base::android::APPLICATION_STATE_UNKNOWN; |
| 121 switch (type) { |
| 122 case content::NOTIFICATION_RENDERER_PROCESS_TERMINATED: { |
| 123 // NOTIFICATION_RENDERER_PROCESS_TERMINATED is sent when the renderer |
| 124 // process is cleanly shutdown. However, we still need to close the |
| 125 // minidump_fd we kept open. |
| 126 term_status = base::TERMINATION_STATUS_NORMAL_TERMINATION; |
| 127 break; |
| 128 } |
| 129 case content::NOTIFICATION_RENDERER_PROCESS_CLOSED: { |
| 130 // We do not care about android fast shutdowns as it is a known case where |
| 131 // the renderer is intentionally killed when we are done with it. |
| 132 if (rph->FastShutdownStarted()) { |
| 133 break; |
| 134 } |
| 135 content::RenderProcessHost::RendererClosedDetails* process_details = |
| 136 content::Details<content::RenderProcessHost::RendererClosedDetails>( |
| 137 details) |
| 138 .ptr(); |
| 139 term_status = process_details->status; |
| 140 app_state = base::android::ApplicationStatusListener::GetState(); |
| 141 break; |
| 142 } |
| 143 default: |
| 144 NOTREACHED(); |
| 145 return; |
| 146 } |
| 147 |
| 148 OnChildExit(rph->GetID(), rph->GetHandle(), content::PROCESS_TYPE_RENDERER, |
| 149 term_status, app_state); |
| 150 } |
| 151 |
| 152 } // namespace breakpad |
OLD | NEW |