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 "base/bind.h" | 11 #include "base/bind.h" |
11 #include "base/logging.h" | 12 #include "base/logging.h" |
12 #include "base/stl_util.h" | 13 #include "base/stl_util.h" |
13 #include "base/sync_socket.h" | 14 #include "base/sync_socket.h" |
14 #include "content/public/browser/browser_thread.h" | 15 #include "content/public/browser/browser_thread.h" |
15 #include "content/public/browser/child_process_data.h" | 16 #include "content/public/browser/child_process_data.h" |
16 #include "content/public/browser/notification_service.h" | 17 #include "content/public/browser/notification_service.h" |
17 #include "content/public/browser/notification_types.h" | 18 #include "content/public/browser/notification_types.h" |
18 #include "content/public/browser/render_process_host.h" | 19 #include "content/public/browser/render_process_host.h" |
20 #include "content/public/browser/render_view_host.h" | |
21 #include "content/public/browser/render_widget_host.h" | |
22 #include "content/public/browser/render_widget_host_iterator.h" | |
23 #include "content/public/browser/web_contents.h" | |
19 | 24 |
20 using content::BrowserThread; | 25 using content::BrowserThread; |
21 | 26 |
22 namespace android_webview { | 27 namespace android_webview { |
23 | 28 |
29 namespace { | |
30 | |
31 void GetAwRenderProcessGoneDelegatesForRenderProcess( | |
32 int render_process_id, | |
33 std::vector<AwRenderProcessGoneDelegate*>* delegates) { | |
34 content::RenderProcessHost* rph = | |
35 content::RenderProcessHost::FromID(render_process_id); | |
36 if (!rph) | |
37 return; | |
38 | |
39 std::unique_ptr<content::RenderWidgetHostIterator> widgets( | |
40 content::RenderWidgetHost::GetRenderWidgetHosts()); | |
41 while (content::RenderWidgetHost* widget = widgets->GetNextHost()) { | |
42 content::RenderViewHost* view = content::RenderViewHost::From(widget); | |
43 if (view && rph == view->GetProcess()) { | |
44 content::WebContents* wc = content::WebContents::FromRenderViewHost(view); | |
45 if (wc) { | |
46 AwRenderProcessGoneDelegate* delegate = | |
47 AwRenderProcessGoneDelegate::FromWebContents(wc); | |
48 if (delegate) | |
49 delegates->push_back(delegate); | |
50 } | |
51 } | |
52 } | |
53 } | |
54 | |
55 void OnRenderProcessGone(int child_process_id) { | |
Tobias Sargeant
2017/01/13 10:48:38
I think it would be good to name this and the belo
michaelbai
2017/01/13 21:01:38
Changed to OnRenderProcessGoneDetail(), and rewrot
| |
56 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
57 std::vector<AwRenderProcessGoneDelegate*> delegates; | |
58 GetAwRenderProcessGoneDelegatesForRenderProcess(child_process_id, &delegates); | |
59 for (auto delegate : delegates) | |
60 delegate->OnRenderProcessGone(child_process_id); | |
61 } | |
62 | |
63 void OnRenderProcessGoneWithDetail(int child_process_id, bool crashed) { | |
64 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
65 std::vector<AwRenderProcessGoneDelegate*> delegates; | |
66 GetAwRenderProcessGoneDelegatesForRenderProcess(child_process_id, &delegates); | |
67 for (auto delegate : delegates) { | |
68 if (!delegate->OnRenderProcessGoneWithDetail(child_process_id, crashed)) { | |
69 // Keeps this log unchanged, CTS test uses it to detect crash. | |
70 LOG(ERROR) << "Render process's abnormal termination wasn't handled by" | |
71 << " all associated webviews, triggering application crash"; | |
72 // For some reason, FATAL message didn't show up in adb logcat. | |
Torne
2017/01/13 13:24:08
This normally works, I think; we should probably l
michaelbai
2017/01/13 21:01:38
We shouldn't try to fix LOG issue in this patch, I
Torne
2017/01/16 12:20:32
We use LOG(FATAL) in other places and I'm pretty s
michaelbai
2017/01/17 17:09:42
Would you like point me where it still work? it wi
michaelbai
2017/01/18 03:01:26
It seems Android O issue, the message showed up no
| |
73 LOG(FATAL) << "Crash"; | |
74 } | |
75 } | |
76 } | |
77 | |
78 } // namespace | |
79 | |
24 AwBrowserTerminator::AwBrowserTerminator() {} | 80 AwBrowserTerminator::AwBrowserTerminator() {} |
25 | 81 |
26 AwBrowserTerminator::~AwBrowserTerminator() {} | 82 AwBrowserTerminator::~AwBrowserTerminator() {} |
27 | 83 |
28 void AwBrowserTerminator::OnChildStart(int child_process_id, | 84 void AwBrowserTerminator::OnChildStart(int child_process_id, |
29 content::FileDescriptorInfo* mappings) { | 85 content::FileDescriptorInfo* mappings) { |
30 DCHECK_CURRENTLY_ON(content::BrowserThread::PROCESS_LAUNCHER); | 86 DCHECK_CURRENTLY_ON(content::BrowserThread::PROCESS_LAUNCHER); |
31 | 87 |
32 base::AutoLock auto_lock(child_process_id_to_pipe_lock_); | 88 base::AutoLock auto_lock(child_process_id_to_pipe_lock_); |
33 DCHECK(!ContainsKey(child_process_id_to_pipe_, child_process_id)); | 89 DCHECK(!ContainsKey(child_process_id_to_pipe_, child_process_id)); |
34 | 90 |
35 auto local_pipe = base::MakeUnique<base::SyncSocket>(); | 91 auto local_pipe = base::MakeUnique<base::SyncSocket>(); |
36 auto child_pipe = base::MakeUnique<base::SyncSocket>(); | 92 auto child_pipe = base::MakeUnique<base::SyncSocket>(); |
37 if (base::SyncSocket::CreatePair(local_pipe.get(), child_pipe.get())) { | 93 if (base::SyncSocket::CreatePair(local_pipe.get(), child_pipe.get())) { |
38 child_process_id_to_pipe_[child_process_id] = std::move(local_pipe); | 94 child_process_id_to_pipe_[child_process_id] = std::move(local_pipe); |
39 mappings->Transfer(kAndroidWebViewCrashSignalDescriptor, | 95 mappings->Transfer(kAndroidWebViewCrashSignalDescriptor, |
40 base::ScopedFD(dup(child_pipe->handle()))); | 96 base::ScopedFD(dup(child_pipe->handle()))); |
41 } | 97 } |
42 } | 98 } |
43 | 99 |
44 void AwBrowserTerminator::ProcessTerminationStatus( | 100 void AwBrowserTerminator::ProcessTerminationStatus( |
101 int child_process_id, | |
45 std::unique_ptr<base::SyncSocket> pipe) { | 102 std::unique_ptr<base::SyncSocket> pipe) { |
103 bool crashed = false; | |
104 | |
105 // If the child process hasn't written anything into the pipe. This implies | |
106 // that it was terminated via SIGKILL by the low memory killer. | |
46 if (pipe->Peek() >= sizeof(int)) { | 107 if (pipe->Peek() >= sizeof(int)) { |
47 int exit_code; | 108 int exit_code; |
48 pipe->Receive(&exit_code, sizeof(exit_code)); | 109 pipe->Receive(&exit_code, sizeof(exit_code)); |
49 LOG(FATAL) << "Renderer process crash detected (code " << exit_code | 110 LOG(ERROR) << "Renderer process crash detected (code " << exit_code |
50 << "). Terminating browser."; | 111 << "). Terminating browser."; |
Torne
2017/01/13 13:24:08
We're not terminating the browser necessarily any
michaelbai
2017/01/13 21:01:38
Done.
| |
51 } else { | 112 crashed = true; |
52 // The child process hasn't written anything into the pipe. This implies | |
53 // that it was terminated via SIGKILL by the low memory killer, and thus we | |
54 // need to perform a clean exit. | |
55 exit(0); | |
56 } | 113 } |
114 | |
115 BrowserThread::PostTask( | |
116 BrowserThread::UI, FROM_HERE, | |
117 base::Bind(&OnRenderProcessGoneWithDetail, child_process_id, crashed)); | |
57 } | 118 } |
58 | 119 |
59 void AwBrowserTerminator::OnChildExit( | 120 void AwBrowserTerminator::OnChildExit( |
60 int child_process_id, | 121 int child_process_id, |
61 base::ProcessHandle pid, | 122 base::ProcessHandle pid, |
62 content::ProcessType process_type, | 123 content::ProcessType process_type, |
63 base::TerminationStatus termination_status, | 124 base::TerminationStatus termination_status, |
64 base::android::ApplicationState app_state) { | 125 base::android::ApplicationState app_state) { |
126 OnRenderProcessGone(child_process_id); | |
Tobias Sargeant
2017/01/13 10:48:38
Don't do this here; OnChildExit can be called more
michaelbai
2017/01/13 21:01:38
Thanks, it was added for experiment, I didn't thin
| |
65 std::unique_ptr<base::SyncSocket> pipe; | 127 std::unique_ptr<base::SyncSocket> pipe; |
66 | 128 |
67 { | 129 { |
68 base::AutoLock auto_lock(child_process_id_to_pipe_lock_); | 130 base::AutoLock auto_lock(child_process_id_to_pipe_lock_); |
69 const auto& iter = child_process_id_to_pipe_.find(child_process_id); | 131 const auto& iter = child_process_id_to_pipe_.find(child_process_id); |
70 if (iter == child_process_id_to_pipe_.end()) { | 132 if (iter == child_process_id_to_pipe_.end()) { |
71 // We might get a NOTIFICATION_RENDERER_PROCESS_TERMINATED and a | 133 // We might get a NOTIFICATION_RENDERER_PROCESS_TERMINATED and a |
72 // NOTIFICATION_RENDERER_PROCESS_CLOSED. | 134 // NOTIFICATION_RENDERER_PROCESS_CLOSED. |
73 return; | 135 return; |
74 } | 136 } |
75 pipe = std::move(iter->second); | 137 pipe = std::move(iter->second); |
76 child_process_id_to_pipe_.erase(iter); | 138 child_process_id_to_pipe_.erase(iter); |
77 } | 139 } |
78 if (termination_status == base::TERMINATION_STATUS_NORMAL_TERMINATION) | 140 if (termination_status == base::TERMINATION_STATUS_NORMAL_TERMINATION) |
79 return; | 141 return; |
Tobias Sargeant
2017/01/13 10:48:38
You may need to organise for the *WithDetail deleg
michaelbai
2017/01/13 21:01:38
Here, we don't know whether render process crashed
| |
80 DCHECK(pipe->handle() != base::SyncSocket::kInvalidHandle); | 142 DCHECK(pipe->handle() != base::SyncSocket::kInvalidHandle); |
81 BrowserThread::PostTask( | 143 BrowserThread::PostTask( |
82 BrowserThread::FILE, FROM_HERE, | 144 BrowserThread::FILE, FROM_HERE, |
83 base::Bind(&AwBrowserTerminator::ProcessTerminationStatus, | 145 base::Bind(&AwBrowserTerminator::ProcessTerminationStatus, |
146 child_process_id, | |
84 base::Passed(std::move(pipe)))); | 147 base::Passed(std::move(pipe)))); |
85 } | 148 } |
86 | 149 |
87 } // namespace breakpad | 150 } // namespace breakpad |
OLD | NEW |