Chromium Code Reviews| 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 CrashDumpObserver::Client::Client() {} | |
| 24 | |
| 25 CrashDumpObserver::Client::~Client() {} | |
| 26 | |
| 27 // static | |
| 28 CrashDumpObserver* CrashDumpObserver::instance_ = NULL; | |
|
Lei Zhang
2016/10/05 16:57:45
nit: nullptr
| |
| 29 | |
| 30 // static | |
| 31 CrashDumpObserver* CrashDumpObserver::GetInstance() { | |
| 32 CHECK(instance_); | |
| 33 return instance_; | |
| 34 } | |
| 35 | |
| 36 CrashDumpObserver::CrashDumpObserver() { | |
| 37 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 38 DCHECK(!instance_); | |
| 39 instance_ = this; | |
| 40 notification_registrar_.Add(this, | |
| 41 content::NOTIFICATION_RENDERER_PROCESS_TERMINATED, | |
| 42 content::NotificationService::AllSources()); | |
| 43 notification_registrar_.Add(this, | |
| 44 content::NOTIFICATION_RENDERER_PROCESS_CLOSED, | |
| 45 content::NotificationService::AllSources()); | |
| 46 BrowserChildProcessObserver::Add(this); | |
| 47 } | |
| 48 | |
| 49 CrashDumpObserver::~CrashDumpObserver() { | |
| 50 instance_ = nullptr; | |
| 51 BrowserChildProcessObserver::Remove(this); | |
| 52 } | |
| 53 | |
| 54 void CrashDumpObserver::OnChildExit(int child_process_id, | |
| 55 base::ProcessHandle pid, | |
| 56 content::ProcessType process_type, | |
| 57 base::TerminationStatus termination_status, | |
| 58 base::android::ApplicationState app_state) { | |
| 59 base::AutoLock auto_lock(registered_clients_lock_); | |
| 60 base::SequencedWorkerPool* pool = BrowserThread::GetBlockingPool(); | |
| 61 base::SequencedWorkerPool::SequenceToken token = pool->GetSequenceToken(); | |
| 62 | |
| 63 for (auto& client : registered_clients_) { | |
| 64 pool->PostSequencedWorkerTask( | |
| 65 token, FROM_HERE, | |
| 66 base::Bind(&Client::OnChildExit, client, child_process_id, pid, | |
| 67 process_type, termination_status, app_state)); | |
| 68 } | |
| 69 } | |
| 70 | |
| 71 void CrashDumpObserver::RegisterClient(scoped_refptr<Client> client) { | |
| 72 base::AutoLock auto_lock(registered_clients_lock_); | |
| 73 if (!ContainsValue(registered_clients_, client)) | |
| 74 registered_clients_.push_back(std::move(client)); | |
| 75 } | |
| 76 | |
| 77 void CrashDumpObserver::UnregisterClient(scoped_refptr<Client> client) { | |
|
Lei Zhang
2016/10/05 18:00:00
I don't see anyone ever calling this.
Tobias Sargeant
2016/10/06 17:42:22
True, but it seems like it's reasonable to have it
| |
| 78 base::AutoLock auto_lock(registered_clients_lock_); | |
| 79 registered_clients_.remove(client); | |
| 80 } | |
| 81 | |
| 82 void CrashDumpObserver::BrowserChildProcessStarted( | |
| 83 int child_process_id, | |
| 84 content::FileDescriptorInfo* mappings) { | |
| 85 base::AutoLock auto_lock(registered_clients_lock_); | |
| 86 for (auto& client : registered_clients_) { | |
| 87 client->OnChildStart(child_process_id, mappings); | |
| 88 } | |
| 89 } | |
| 90 | |
| 91 void CrashDumpObserver::BrowserChildProcessHostDisconnected( | |
| 92 const content::ChildProcessData& data) { | |
| 93 OnChildExit(data.id, data.handle, | |
| 94 static_cast<content::ProcessType>(data.process_type), | |
| 95 base::TERMINATION_STATUS_MAX_ENUM, | |
| 96 base::android::APPLICATION_STATE_UNKNOWN); | |
| 97 } | |
| 98 | |
| 99 void CrashDumpObserver::BrowserChildProcessCrashed( | |
| 100 const content::ChildProcessData& data, | |
| 101 int exit_code) { | |
| 102 OnChildExit(data.id, data.handle, | |
| 103 static_cast<content::ProcessType>(data.process_type), | |
| 104 base::TERMINATION_STATUS_ABNORMAL_TERMINATION, | |
| 105 base::android::APPLICATION_STATE_UNKNOWN); | |
| 106 } | |
| 107 | |
| 108 void CrashDumpObserver::Observe(int type, | |
| 109 const content::NotificationSource& source, | |
| 110 const content::NotificationDetails& details) { | |
| 111 content::RenderProcessHost* rph = | |
| 112 content::Source<content::RenderProcessHost>(source).ptr(); | |
| 113 base::TerminationStatus term_status = base::TERMINATION_STATUS_MAX_ENUM; | |
| 114 base::android::ApplicationState app_state = | |
| 115 base::android::APPLICATION_STATE_UNKNOWN; | |
| 116 switch (type) { | |
| 117 case content::NOTIFICATION_RENDERER_PROCESS_TERMINATED: { | |
| 118 // NOTIFICATION_RENDERER_PROCESS_TERMINATED is sent when the renderer | |
| 119 // process is cleanly shutdown. However, we still need to close the | |
| 120 // minidump_fd we kept open. | |
| 121 term_status = base::TERMINATION_STATUS_NORMAL_TERMINATION; | |
| 122 break; | |
| 123 } | |
| 124 case content::NOTIFICATION_RENDERER_PROCESS_CLOSED: { | |
| 125 // We do not care about android fast shutdowns as it is a known case where | |
| 126 // the renderer is intentionally killed when we are done with it. | |
| 127 if (rph->FastShutdownStarted()) { | |
| 128 break; | |
| 129 } | |
| 130 content::RenderProcessHost::RendererClosedDetails* process_details = | |
| 131 content::Details<content::RenderProcessHost::RendererClosedDetails>( | |
| 132 details) | |
| 133 .ptr(); | |
| 134 term_status = process_details->status; | |
| 135 app_state = base::android::ApplicationStatusListener::GetState(); | |
| 136 break; | |
| 137 } | |
| 138 default: | |
| 139 NOTREACHED(); | |
| 140 return; | |
| 141 } | |
| 142 | |
| 143 OnChildExit(rph->GetID(), rph->GetHandle(), content::PROCESS_TYPE_RENDERER, | |
| 144 term_status, app_state); | |
| 145 } | |
| 146 | |
| 147 } // namespace breakpad | |
| OLD | NEW |