OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "android_webview/browser/aw_browser_terminator.h" | 5 #include "android_webview/browser/aw_browser_terminator.h" |
6 | 6 |
7 #include <unistd.h> | 7 #include <unistd.h> |
8 | 8 |
| 9 #include "android_webview/browser/aw_render_process_gone_delegate.h" |
9 #include "android_webview/common/aw_descriptors.h" | 10 #include "android_webview/common/aw_descriptors.h" |
10 #include "android_webview/common/crash_reporter/aw_microdump_crash_reporter.h" | 11 #include "android_webview/common/crash_reporter/aw_microdump_crash_reporter.h" |
11 #include "base/bind.h" | 12 #include "base/bind.h" |
12 #include "base/logging.h" | 13 #include "base/logging.h" |
13 #include "base/stl_util.h" | 14 #include "base/stl_util.h" |
14 #include "base/sync_socket.h" | 15 #include "base/sync_socket.h" |
15 #include "content/public/browser/browser_thread.h" | 16 #include "content/public/browser/browser_thread.h" |
16 #include "content/public/browser/child_process_data.h" | 17 #include "content/public/browser/child_process_data.h" |
17 #include "content/public/browser/notification_service.h" | 18 #include "content/public/browser/notification_service.h" |
18 #include "content/public/browser/notification_types.h" | 19 #include "content/public/browser/notification_types.h" |
19 #include "content/public/browser/render_process_host.h" | 20 #include "content/public/browser/render_process_host.h" |
| 21 #include "content/public/browser/render_view_host.h" |
| 22 #include "content/public/browser/render_widget_host.h" |
| 23 #include "content/public/browser/render_widget_host_iterator.h" |
| 24 #include "content/public/browser/web_contents.h" |
20 | 25 |
21 using content::BrowserThread; | 26 using content::BrowserThread; |
22 | 27 |
23 namespace android_webview { | 28 namespace android_webview { |
24 | 29 |
| 30 namespace { |
| 31 |
| 32 void GetAwRenderProcessGoneDelegatesForRenderProcess( |
| 33 int render_process_id, |
| 34 std::vector<AwRenderProcessGoneDelegate*>* delegates) { |
| 35 content::RenderProcessHost* rph = |
| 36 content::RenderProcessHost::FromID(render_process_id); |
| 37 if (!rph) |
| 38 return; |
| 39 |
| 40 std::unique_ptr<content::RenderWidgetHostIterator> widgets( |
| 41 content::RenderWidgetHost::GetRenderWidgetHosts()); |
| 42 while (content::RenderWidgetHost* widget = widgets->GetNextHost()) { |
| 43 content::RenderViewHost* view = content::RenderViewHost::From(widget); |
| 44 if (view && rph == view->GetProcess()) { |
| 45 content::WebContents* wc = content::WebContents::FromRenderViewHost(view); |
| 46 if (wc) { |
| 47 AwRenderProcessGoneDelegate* delegate = |
| 48 AwRenderProcessGoneDelegate::FromWebContents(wc); |
| 49 if (delegate) |
| 50 delegates->push_back(delegate); |
| 51 } |
| 52 } |
| 53 } |
| 54 } |
| 55 |
| 56 void OnRenderProcessGone(int child_process_id) { |
| 57 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 58 std::vector<AwRenderProcessGoneDelegate*> delegates; |
| 59 GetAwRenderProcessGoneDelegatesForRenderProcess(child_process_id, &delegates); |
| 60 for (auto delegate : delegates) |
| 61 delegate->OnRenderProcessGone(child_process_id); |
| 62 } |
| 63 |
| 64 void OnRenderProcessGoneDetail(int child_process_id, bool crashed) { |
| 65 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 66 std::vector<AwRenderProcessGoneDelegate*> delegates; |
| 67 GetAwRenderProcessGoneDelegatesForRenderProcess(child_process_id, &delegates); |
| 68 for (auto delegate : delegates) { |
| 69 if (!delegate->OnRenderProcessGoneDetail(child_process_id, crashed)) { |
| 70 // Keeps this log unchanged, CTS test uses it to detect crash. |
| 71 LOG(FATAL) << "Render process's abnormal termination wasn't handled by" |
| 72 << " all associated webviews, triggering application crash"; |
| 73 } |
| 74 } |
| 75 } |
| 76 |
| 77 } // namespace |
| 78 |
25 AwBrowserTerminator::AwBrowserTerminator() {} | 79 AwBrowserTerminator::AwBrowserTerminator() {} |
26 | 80 |
27 AwBrowserTerminator::~AwBrowserTerminator() {} | 81 AwBrowserTerminator::~AwBrowserTerminator() {} |
28 | 82 |
29 void AwBrowserTerminator::OnChildStart(int child_process_id, | 83 void AwBrowserTerminator::OnChildStart(int child_process_id, |
30 content::FileDescriptorInfo* mappings) { | 84 content::FileDescriptorInfo* mappings) { |
31 DCHECK_CURRENTLY_ON(content::BrowserThread::PROCESS_LAUNCHER); | 85 DCHECK_CURRENTLY_ON(content::BrowserThread::PROCESS_LAUNCHER); |
32 | 86 |
33 base::AutoLock auto_lock(child_process_id_to_pipe_lock_); | 87 base::AutoLock auto_lock(child_process_id_to_pipe_lock_); |
34 DCHECK(!ContainsKey(child_process_id_to_pipe_, child_process_id)); | 88 DCHECK(!ContainsKey(child_process_id_to_pipe_, child_process_id)); |
35 | 89 |
36 auto local_pipe = base::MakeUnique<base::SyncSocket>(); | 90 auto local_pipe = base::MakeUnique<base::SyncSocket>(); |
37 auto child_pipe = base::MakeUnique<base::SyncSocket>(); | 91 auto child_pipe = base::MakeUnique<base::SyncSocket>(); |
38 if (base::SyncSocket::CreatePair(local_pipe.get(), child_pipe.get())) { | 92 if (base::SyncSocket::CreatePair(local_pipe.get(), child_pipe.get())) { |
39 child_process_id_to_pipe_[child_process_id] = std::move(local_pipe); | 93 child_process_id_to_pipe_[child_process_id] = std::move(local_pipe); |
40 mappings->Transfer(kAndroidWebViewCrashSignalDescriptor, | 94 mappings->Transfer(kAndroidWebViewCrashSignalDescriptor, |
41 base::ScopedFD(dup(child_pipe->handle()))); | 95 base::ScopedFD(dup(child_pipe->handle()))); |
42 } | 96 } |
43 } | 97 } |
44 | 98 |
45 void AwBrowserTerminator::ProcessTerminationStatus( | 99 void AwBrowserTerminator::ProcessTerminationStatus( |
| 100 int child_process_id, |
46 std::unique_ptr<base::SyncSocket> pipe) { | 101 std::unique_ptr<base::SyncSocket> pipe) { |
| 102 bool crashed = false; |
| 103 |
| 104 // If the child process hasn't written anything into the pipe. This implies |
| 105 // that it was terminated via SIGKILL by the low memory killer. |
47 if (pipe->Peek() >= sizeof(int)) { | 106 if (pipe->Peek() >= sizeof(int)) { |
48 int exit_code; | 107 int exit_code; |
49 pipe->Receive(&exit_code, sizeof(exit_code)); | 108 pipe->Receive(&exit_code, sizeof(exit_code)); |
50 crash_reporter::SuppressDumpGeneration(); | 109 crash_reporter::SuppressDumpGeneration(); |
51 LOG(FATAL) << "Renderer process crash detected (code " << exit_code | 110 LOG(ERROR) << "Renderer process crash detected (code " << exit_code << ")."; |
52 << "). Terminating browser."; | 111 crashed = true; |
53 } else { | |
54 // The child process hasn't written anything into the pipe. This implies | |
55 // that it was terminated via SIGKILL by the low memory killer, and thus we | |
56 // need to perform a clean exit. | |
57 exit(0); | |
58 } | 112 } |
| 113 |
| 114 BrowserThread::PostTask( |
| 115 BrowserThread::UI, FROM_HERE, |
| 116 base::Bind(&OnRenderProcessGoneDetail, child_process_id, crashed)); |
59 } | 117 } |
60 | 118 |
61 void AwBrowserTerminator::OnChildExit( | 119 void AwBrowserTerminator::OnChildExit( |
62 int child_process_id, | 120 int child_process_id, |
63 base::ProcessHandle pid, | 121 base::ProcessHandle pid, |
64 content::ProcessType process_type, | 122 content::ProcessType process_type, |
65 base::TerminationStatus termination_status, | 123 base::TerminationStatus termination_status, |
66 base::android::ApplicationState app_state) { | 124 base::android::ApplicationState app_state) { |
67 std::unique_ptr<base::SyncSocket> pipe; | 125 std::unique_ptr<base::SyncSocket> pipe; |
68 | 126 |
69 { | 127 { |
70 base::AutoLock auto_lock(child_process_id_to_pipe_lock_); | 128 base::AutoLock auto_lock(child_process_id_to_pipe_lock_); |
71 const auto& iter = child_process_id_to_pipe_.find(child_process_id); | 129 const auto& iter = child_process_id_to_pipe_.find(child_process_id); |
72 if (iter == child_process_id_to_pipe_.end()) { | 130 if (iter == child_process_id_to_pipe_.end()) { |
73 // We might get a NOTIFICATION_RENDERER_PROCESS_TERMINATED and a | 131 // We might get a NOTIFICATION_RENDERER_PROCESS_TERMINATED and a |
74 // NOTIFICATION_RENDERER_PROCESS_CLOSED. | 132 // NOTIFICATION_RENDERER_PROCESS_CLOSED. |
75 return; | 133 return; |
76 } | 134 } |
77 pipe = std::move(iter->second); | 135 pipe = std::move(iter->second); |
78 child_process_id_to_pipe_.erase(iter); | 136 child_process_id_to_pipe_.erase(iter); |
79 } | 137 } |
80 if (termination_status == base::TERMINATION_STATUS_NORMAL_TERMINATION) | 138 if (termination_status == base::TERMINATION_STATUS_NORMAL_TERMINATION) |
81 return; | 139 return; |
| 140 OnRenderProcessGone(child_process_id); |
82 DCHECK(pipe->handle() != base::SyncSocket::kInvalidHandle); | 141 DCHECK(pipe->handle() != base::SyncSocket::kInvalidHandle); |
83 BrowserThread::PostTask( | 142 BrowserThread::PostTask( |
84 BrowserThread::FILE, FROM_HERE, | 143 BrowserThread::FILE, FROM_HERE, |
85 base::Bind(&AwBrowserTerminator::ProcessTerminationStatus, | 144 base::Bind(&AwBrowserTerminator::ProcessTerminationStatus, |
| 145 child_process_id, |
86 base::Passed(std::move(pipe)))); | 146 base::Passed(std::move(pipe)))); |
87 } | 147 } |
88 | 148 |
89 } // namespace breakpad | 149 } // namespace breakpad |
OLD | NEW |