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 namespace { |
| 24 base::LazyInstance<CrashDumpObserver> g_instance = LAZY_INSTANCE_INITIALIZER; |
| 25 } |
| 26 |
| 27 // static |
| 28 void CrashDumpObserver::Create() { |
| 29 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 30 // If this DCHECK fails in a unit test then a previously executing |
| 31 // test that makes use of CrashDumpObserver forgot to create a |
| 32 // ShadowingAtExitManager. |
| 33 DCHECK(g_instance == nullptr); |
| 34 g_instance.Get(); |
| 35 } |
| 36 |
| 37 // static |
| 38 CrashDumpObserver* CrashDumpObserver::GetInstance() { |
| 39 DCHECK(!(g_instance == nullptr)); |
| 40 return g_instance.Pointer(); |
| 41 } |
| 42 |
| 43 CrashDumpObserver::CrashDumpObserver() { |
| 44 notification_registrar_.Add(this, |
| 45 content::NOTIFICATION_RENDERER_PROCESS_TERMINATED, |
| 46 content::NotificationService::AllSources()); |
| 47 notification_registrar_.Add(this, |
| 48 content::NOTIFICATION_RENDERER_PROCESS_CLOSED, |
| 49 content::NotificationService::AllSources()); |
| 50 BrowserChildProcessObserver::Add(this); |
| 51 } |
| 52 |
| 53 CrashDumpObserver::~CrashDumpObserver() { |
| 54 BrowserChildProcessObserver::Remove(this); |
| 55 } |
| 56 |
| 57 void CrashDumpObserver::RegisterClient(std::unique_ptr<Client> client) { |
| 58 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 59 base::AutoLock auto_lock(registered_clients_lock_); |
| 60 registered_clients_.push_back(std::move(client)); |
| 61 } |
| 62 |
| 63 void CrashDumpObserver::OnChildExit(int child_process_id, |
| 64 base::ProcessHandle pid, |
| 65 content::ProcessType process_type, |
| 66 base::TerminationStatus termination_status, |
| 67 base::android::ApplicationState app_state) { |
| 68 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 69 std::vector<Client*> registered_clients_copy; |
| 70 { |
| 71 base::AutoLock auto_lock(registered_clients_lock_); |
| 72 for (auto& client : registered_clients_) |
| 73 registered_clients_copy.push_back(client.get()); |
| 74 } |
| 75 for (auto& client : registered_clients_copy) { |
| 76 client->OnChildExit(child_process_id, pid, process_type, termination_status, |
| 77 app_state); |
| 78 } |
| 79 } |
| 80 |
| 81 void CrashDumpObserver::BrowserChildProcessStarted( |
| 82 int child_process_id, |
| 83 content::FileDescriptorInfo* mappings) { |
| 84 DCHECK_CURRENTLY_ON(BrowserThread::PROCESS_LAUNCHER); |
| 85 std::vector<Client*> registered_clients_copy; |
| 86 { |
| 87 base::AutoLock auto_lock(registered_clients_lock_); |
| 88 for (auto& client : registered_clients_) |
| 89 registered_clients_copy.push_back(client.get()); |
| 90 } |
| 91 for (auto& client : registered_clients_copy) { |
| 92 client->OnChildStart(child_process_id, mappings); |
| 93 } |
| 94 } |
| 95 |
| 96 void CrashDumpObserver::BrowserChildProcessHostDisconnected( |
| 97 const content::ChildProcessData& data) { |
| 98 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 99 OnChildExit(data.id, data.handle, |
| 100 static_cast<content::ProcessType>(data.process_type), |
| 101 base::TERMINATION_STATUS_MAX_ENUM, |
| 102 base::android::ApplicationStatusListener::GetState()); |
| 103 } |
| 104 |
| 105 void CrashDumpObserver::BrowserChildProcessCrashed( |
| 106 const content::ChildProcessData& data, |
| 107 int exit_code) { |
| 108 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 109 OnChildExit(data.id, data.handle, |
| 110 static_cast<content::ProcessType>(data.process_type), |
| 111 base::TERMINATION_STATUS_ABNORMAL_TERMINATION, |
| 112 base::android::APPLICATION_STATE_UNKNOWN); |
| 113 } |
| 114 |
| 115 void CrashDumpObserver::Observe(int type, |
| 116 const content::NotificationSource& source, |
| 117 const content::NotificationDetails& details) { |
| 118 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 119 content::RenderProcessHost* rph = |
| 120 content::Source<content::RenderProcessHost>(source).ptr(); |
| 121 base::TerminationStatus term_status = base::TERMINATION_STATUS_MAX_ENUM; |
| 122 base::android::ApplicationState app_state = |
| 123 base::android::APPLICATION_STATE_UNKNOWN; |
| 124 switch (type) { |
| 125 case content::NOTIFICATION_RENDERER_PROCESS_TERMINATED: { |
| 126 // NOTIFICATION_RENDERER_PROCESS_TERMINATED is sent when the renderer |
| 127 // process is cleanly shutdown. |
| 128 term_status = base::TERMINATION_STATUS_NORMAL_TERMINATION; |
| 129 break; |
| 130 } |
| 131 case content::NOTIFICATION_RENDERER_PROCESS_CLOSED: { |
| 132 // We do not care about android fast shutdowns as it is a known case where |
| 133 // the renderer is intentionally killed when we are done with it. |
| 134 if (rph->FastShutdownStarted()) { |
| 135 break; |
| 136 } |
| 137 content::RenderProcessHost::RendererClosedDetails* process_details = |
| 138 content::Details<content::RenderProcessHost::RendererClosedDetails>( |
| 139 details) |
| 140 .ptr(); |
| 141 term_status = process_details->status; |
| 142 app_state = base::android::ApplicationStatusListener::GetState(); |
| 143 break; |
| 144 } |
| 145 default: |
| 146 NOTREACHED(); |
| 147 return; |
| 148 } |
| 149 |
| 150 OnChildExit(rph->GetID(), rph->GetHandle(), content::PROCESS_TYPE_RENDERER, |
| 151 term_status, app_state); |
| 152 } |
| 153 |
| 154 } // namespace breakpad |
OLD | NEW |