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/lazy_instance.h" | |
11 #include "base/logging.h" | |
12 #include "base/stl_util.h" | |
13 #include "base/threading/sequenced_worker_pool.h" | |
14 #include "content/public/browser/browser_thread.h" | |
15 #include "content/public/browser/child_process_data.h" | |
16 #include "content/public/browser/notification_service.h" | |
17 #include "content/public/browser/notification_types.h" | |
18 #include "content/public/browser/render_process_host.h" | |
19 | |
20 using content::BrowserThread; | |
21 | |
22 namespace breakpad { | |
23 | |
24 CrashDumpObserver::Client::Client() {} | |
25 | |
26 CrashDumpObserver::Client::~Client() {} | |
27 | |
28 namespace { | |
29 base::LazyInstance<CrashDumpObserver> g_instance = LAZY_INSTANCE_INITIALIZER; | |
30 } | |
31 | |
32 // static | |
33 void CrashDumpObserver::Create() { | |
34 GetInstance(); | |
35 } | |
36 | |
37 // static | |
38 CrashDumpObserver* CrashDumpObserver::GetInstance() { | |
39 return g_instance.Pointer(); | |
40 } | |
41 | |
42 CrashDumpObserver::CrashDumpObserver() { | |
43 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
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::OnChildExit(int child_process_id, | |
58 base::ProcessHandle pid, | |
59 content::ProcessType process_type, | |
60 base::TerminationStatus termination_status, | |
61 base::android::ApplicationState app_state) { | |
62 base::AutoLock auto_lock(registered_clients_lock_); | |
63 base::SequencedWorkerPool* pool = BrowserThread::GetBlockingPool(); | |
64 base::SequencedWorkerPool::SequenceToken token = pool->GetSequenceToken(); | |
65 | |
66 for (auto& client : registered_clients_) { | |
67 pool->PostSequencedWorkerTask( | |
michaelbai
2016/11/29 23:31:32
WebView wants know this immediately, sets all WebV
Tobias Sargeant
2016/12/08 16:41:42
The notifications this code relies upon are alread
| |
68 token, FROM_HERE, | |
69 base::Bind(&Client::OnChildExit, client, child_process_id, pid, | |
70 process_type, termination_status, app_state)); | |
71 } | |
72 } | |
73 | |
74 void CrashDumpObserver::RegisterClient(scoped_refptr<Client> client) { | |
75 base::AutoLock auto_lock(registered_clients_lock_); | |
76 if (!ContainsValue(registered_clients_, client)) | |
77 registered_clients_.push_back(std::move(client)); | |
78 } | |
79 | |
80 void CrashDumpObserver::UnregisterClient(scoped_refptr<Client> client) { | |
81 base::AutoLock auto_lock(registered_clients_lock_); | |
82 registered_clients_.erase(std::remove(registered_clients_.begin(), | |
83 registered_clients_.end(), client), | |
84 registered_clients_.end()); | |
85 } | |
86 | |
87 void CrashDumpObserver::BrowserChildProcessStarted( | |
88 int child_process_id, | |
89 content::FileDescriptorInfo* mappings) { | |
90 std::vector<scoped_refptr<Client>> registered_clients_copy; | |
91 { | |
92 base::AutoLock auto_lock(registered_clients_lock_); | |
93 registered_clients_copy = registered_clients_; | |
94 } | |
95 for (auto& client : registered_clients_copy) { | |
96 client->OnChildStart(child_process_id, mappings); | |
97 } | |
98 } | |
99 | |
100 void CrashDumpObserver::BrowserChildProcessHostDisconnected( | |
101 const content::ChildProcessData& data) { | |
102 OnChildExit(data.id, data.handle, | |
103 static_cast<content::ProcessType>(data.process_type), | |
104 base::TERMINATION_STATUS_MAX_ENUM, | |
105 base::android::APPLICATION_STATE_UNKNOWN); | |
106 } | |
107 | |
108 void CrashDumpObserver::BrowserChildProcessCrashed( | |
109 const content::ChildProcessData& data, | |
110 int exit_code) { | |
111 OnChildExit(data.id, data.handle, | |
112 static_cast<content::ProcessType>(data.process_type), | |
113 base::TERMINATION_STATUS_ABNORMAL_TERMINATION, | |
114 base::android::APPLICATION_STATE_UNKNOWN); | |
115 } | |
116 | |
117 void CrashDumpObserver::Observe(int type, | |
118 const content::NotificationSource& source, | |
119 const content::NotificationDetails& details) { | |
120 content::RenderProcessHost* rph = | |
121 content::Source<content::RenderProcessHost>(source).ptr(); | |
122 base::TerminationStatus term_status = base::TERMINATION_STATUS_MAX_ENUM; | |
123 base::android::ApplicationState app_state = | |
124 base::android::APPLICATION_STATE_UNKNOWN; | |
125 switch (type) { | |
126 case content::NOTIFICATION_RENDERER_PROCESS_TERMINATED: { | |
127 // NOTIFICATION_RENDERER_PROCESS_TERMINATED is sent when the renderer | |
128 // process is cleanly shutdown. | |
129 term_status = base::TERMINATION_STATUS_NORMAL_TERMINATION; | |
130 break; | |
131 } | |
132 case content::NOTIFICATION_RENDERER_PROCESS_CLOSED: { | |
133 // We do not care about android fast shutdowns as it is a known case where | |
134 // the renderer is intentionally killed when we are done with it. | |
135 if (rph->FastShutdownStarted()) { | |
136 break; | |
137 } | |
138 content::RenderProcessHost::RendererClosedDetails* process_details = | |
139 content::Details<content::RenderProcessHost::RendererClosedDetails>( | |
140 details) | |
141 .ptr(); | |
142 term_status = process_details->status; | |
143 app_state = base::android::ApplicationStatusListener::GetState(); | |
144 break; | |
145 } | |
146 default: | |
147 NOTREACHED(); | |
148 return; | |
149 } | |
150 | |
151 OnChildExit(rph->GetID(), rph->GetHandle(), content::PROCESS_TYPE_RENDERER, | |
152 term_status, app_state); | |
153 } | |
154 | |
155 } // namespace breakpad | |
OLD | NEW |