Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(367)

Side by Side Diff: components/crash/content/browser/crash_dump_observer_android.cc

Issue 2393853002: Refactor CrashDump*Manager to use a shared CrashDumpObserver singleton. (Closed)
Patch Set: Review changes: remove refcounting, ability to unregister clients, move responsibility for posting to clients Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 DCHECK(g_instance == nullptr);
Bernhard Bauer 2016/12/08 17:41:25 Maybe add a comment that if this check fails in a
Tobias Sargeant 2016/12/12 11:49:51 Done.
31 g_instance.Get();
32 }
33
34 // static
35 CrashDumpObserver* CrashDumpObserver::GetInstance() {
36 DCHECK(!(g_instance == nullptr));
37 return g_instance.Pointer();
38 }
39
40 CrashDumpObserver::CrashDumpObserver() {
41 notification_registrar_.Add(this,
42 content::NOTIFICATION_RENDERER_PROCESS_TERMINATED,
43 content::NotificationService::AllSources());
44 notification_registrar_.Add(this,
45 content::NOTIFICATION_RENDERER_PROCESS_CLOSED,
46 content::NotificationService::AllSources());
47 BrowserChildProcessObserver::Add(this);
48 }
49
50 CrashDumpObserver::~CrashDumpObserver() {
51 BrowserChildProcessObserver::Remove(this);
52 base::AutoLock auto_lock(registered_clients_lock_);
Bernhard Bauer 2016/12/08 17:41:25 I'm not sure you really need the lock here -- if t
Tobias Sargeant 2016/12/12 11:49:51 Removed it (and the explicit clear of registered_c
53 registered_clients_.clear();
54 }
55
56 void CrashDumpObserver::RegisterClient(std::unique_ptr<Client> client) {
57 DCHECK_CURRENTLY_ON(BrowserThread::UI);
58 base::AutoLock auto_lock(registered_clients_lock_);
59 registered_clients_.push_back(std::move(client));
60 }
61
62 void CrashDumpObserver::OnChildExit(int child_process_id,
63 base::ProcessHandle pid,
64 content::ProcessType process_type,
65 base::TerminationStatus termination_status,
66 base::android::ApplicationState app_state) {
67 DCHECK_CURRENTLY_ON(BrowserThread::UI);
68 std::vector<Client*> registered_clients_copy;
69 {
70 base::AutoLock auto_lock(registered_clients_lock_);
71 for (auto& client : registered_clients_)
72 registered_clients_copy.push_back(client.get());
73 }
74 for (auto& client : registered_clients_copy) {
75 client->OnChildExit(child_process_id, pid, process_type, termination_status,
76 app_state);
77 }
78 }
79
80 void CrashDumpObserver::BrowserChildProcessStarted(
81 int child_process_id,
82 content::FileDescriptorInfo* mappings) {
83 DCHECK_CURRENTLY_ON(BrowserThread::PROCESS_LAUNCHER);
84 std::vector<Client*> registered_clients_copy;
85 {
86 base::AutoLock auto_lock(registered_clients_lock_);
87 for (auto& client : registered_clients_)
88 registered_clients_copy.push_back(client.get());
89 }
90 for (auto& client : registered_clients_copy) {
91 client->OnChildStart(child_process_id, mappings);
92 }
93 }
94
95 void CrashDumpObserver::BrowserChildProcessHostDisconnected(
96 const content::ChildProcessData& data) {
97 DCHECK_CURRENTLY_ON(BrowserThread::UI);
98 OnChildExit(data.id, data.handle,
99 static_cast<content::ProcessType>(data.process_type),
100 base::TERMINATION_STATUS_MAX_ENUM,
101 base::android::ApplicationStatusListener::GetState());
102 }
103
104 void CrashDumpObserver::BrowserChildProcessCrashed(
105 const content::ChildProcessData& data,
106 int exit_code) {
107 DCHECK_CURRENTLY_ON(BrowserThread::UI);
108 OnChildExit(data.id, data.handle,
109 static_cast<content::ProcessType>(data.process_type),
110 base::TERMINATION_STATUS_ABNORMAL_TERMINATION,
111 base::android::APPLICATION_STATE_UNKNOWN);
112 }
113
114 void CrashDumpObserver::Observe(int type,
115 const content::NotificationSource& source,
116 const content::NotificationDetails& details) {
117 DCHECK_CURRENTLY_ON(BrowserThread::UI);
118 content::RenderProcessHost* rph =
119 content::Source<content::RenderProcessHost>(source).ptr();
120 base::TerminationStatus term_status = base::TERMINATION_STATUS_MAX_ENUM;
121 base::android::ApplicationState app_state =
122 base::android::APPLICATION_STATE_UNKNOWN;
123 switch (type) {
124 case content::NOTIFICATION_RENDERER_PROCESS_TERMINATED: {
125 // NOTIFICATION_RENDERER_PROCESS_TERMINATED is sent when the renderer
126 // process is cleanly shutdown.
127 term_status = base::TERMINATION_STATUS_NORMAL_TERMINATION;
128 break;
129 }
130 case content::NOTIFICATION_RENDERER_PROCESS_CLOSED: {
131 // We do not care about android fast shutdowns as it is a known case where
132 // the renderer is intentionally killed when we are done with it.
133 if (rph->FastShutdownStarted()) {
134 break;
135 }
136 content::RenderProcessHost::RendererClosedDetails* process_details =
137 content::Details<content::RenderProcessHost::RendererClosedDetails>(
138 details)
139 .ptr();
140 term_status = process_details->status;
141 app_state = base::android::ApplicationStatusListener::GetState();
142 break;
143 }
144 default:
145 NOTREACHED();
146 return;
147 }
148
149 OnChildExit(rph->GetID(), rph->GetHandle(), content::PROCESS_TYPE_RENDERER,
150 term_status, app_state);
151 }
152
153 } // namespace breakpad
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698