feat: add webContents.forcefullyCrashRenderer() to forcefully terminate a renderer process (#25580)

* feat: add webContents.forcefullyCrashRenderer() to forcefully terminate a renderer process

* chore: fix up docs and tests
This commit is contained in:
Samuel Attard 2020-10-02 14:50:24 -07:00 committed by GitHub
parent d64b9c20bd
commit a189dc779e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 113 additions and 0 deletions

View file

@ -21,6 +21,7 @@
#include "base/threading/thread_task_runner_handle.h"
#include "base/values.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/hang_monitor/hang_crash_dump.h"
#include "chrome/browser/ssl/security_state_tab_helper.h"
#include "content/browser/renderer_host/frame_tree_node.h" // nogncheck
#include "content/browser/renderer_host/render_frame_host_manager.h" // nogncheck
@ -1763,6 +1764,30 @@ bool WebContents::IsCrashed() const {
return web_contents()->IsCrashed();
}
void WebContents::ForcefullyCrashRenderer() {
content::RenderWidgetHostView* view =
web_contents()->GetRenderWidgetHostView();
if (!view)
return;
content::RenderWidgetHost* rwh = view->GetRenderWidgetHost();
if (!rwh)
return;
content::RenderProcessHost* rph = rwh->GetProcess();
if (rph) {
#if defined(OS_LINUX) || defined(OS_CHROMEOS)
// A generic |CrashDumpHungChildProcess()| is not implemented for Linux.
// Instead we send an explicit IPC to crash on the renderer's IO thread.
rph->ForceCrash();
#else
// Try to generate a crash report for the hung process.
CrashDumpHungChildProcess(rph->GetProcess().Handle());
rph->Shutdown(content::RESULT_CODE_HUNG);
#endif
}
}
void WebContents::SetUserAgent(const std::string& user_agent) {
web_contents()->SetUserAgentOverride(
blink::UserAgentOverride::UserAgentOnly(user_agent), false);
@ -2921,6 +2946,8 @@ v8::Local<v8::ObjectTemplate> WebContents::FillObjectTemplate(
.SetMethod("_goForward", &WebContents::GoForward)
.SetMethod("_goToOffset", &WebContents::GoToOffset)
.SetMethod("isCrashed", &WebContents::IsCrashed)
.SetMethod("forcefullyCrashRenderer",
&WebContents::ForcefullyCrashRenderer)
.SetMethod("setUserAgent", &WebContents::SetUserAgent)
.SetMethod("getUserAgent", &WebContents::GetUserAgent)
.SetMethod("savePage", &WebContents::SavePage)