Skip to content

Commit

Permalink
[headless] Shutdown gracefully upon Ctrl-C, Ctrl-break, and console c…
Browse files Browse the repository at this point in the history
…losure

Migrate //headless to using //content's console control handler
facilities. This reduces complexity in //headless and should slow the
tide of crashes coming from child procs, where //headless was not
previously installing a console control handler.

Bug: 1473516
Change-Id: I320058d4806f3e80d82adefa72014ba727170629
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4807610
Auto-Submit: Greg Thompson <grt@chromium.org>
Reviewed-by: Sami Kyöstilä <skyostil@chromium.org>
Reviewed-by: Rakina Zata Amni <rakina@chromium.org>
Reviewed-by: Bruce Dawson <brucedawson@chromium.org>
Reviewed-by: Peter Kvitek <kvitekp@chromium.org>
Reviewed-by: Andrey Kosyakov <caseq@chromium.org>
Commit-Queue: Rakina Zata Amni <rakina@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1188201}
  • Loading branch information
GregTho authored and Chromium LUCI CQ committed Aug 25, 2023
1 parent 28ae174 commit 0c5b1f5
Show file tree
Hide file tree
Showing 11 changed files with 31 additions and 96 deletions.
3 changes: 2 additions & 1 deletion chrome/browser/chrome_content_browser_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4897,7 +4897,8 @@ bool ChromeContentBrowserClient::IsUtilityCetCompatible(
return true;
}

void ChromeContentBrowserClient::SessionEnding() {
void ChromeContentBrowserClient::SessionEnding(
absl::optional<DWORD> control_type) {
chrome::SessionEnding();
}

Expand Down
2 changes: 1 addition & 1 deletion chrome/browser/chrome_content_browser_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,7 @@ class ChromeContentBrowserClient : public content::ContentBrowserClient {
std::wstring GetLPACCapabilityNameForNetworkService() override;
bool IsUtilityCetCompatible(const std::string& utility_sub_type) override;
bool IsRendererCodeIntegrityEnabled() override;
void SessionEnding() override;
void SessionEnding(absl::optional<DWORD> control_type) override;
bool ShouldEnableAudioProcessHighPriority() override;
#endif
void ExposeInterfacesToRenderer(
Expand Down
2 changes: 1 addition & 1 deletion content/app/content_main_runner_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,7 @@ mojo::ScopedMessagePipeHandle MaybeAcceptMojoInvitation() {

#if BUILDFLAG(IS_WIN)
void HandleConsoleControlEventOnBrowserUiThread(DWORD control_type) {
GetContentClient()->browser()->SessionEnding();
GetContentClient()->browser()->SessionEnding(control_type);
}

// A console control event handler for browser processes that initiates end
Expand Down
6 changes: 4 additions & 2 deletions content/public/browser/content_browser_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -1495,8 +1495,10 @@ class CONTENT_EXPORT ContentBrowserClient {
// This is called on the UI thread.
virtual bool IsRendererCodeIntegrityEnabled();

// Performs a fast and orderly shutdown of the browser.
virtual void SessionEnding() {}
// Performs a fast and orderly shutdown of the browser. If present,
// `control_type` is a CTRL_* value from a Windows console control handler;
// see https://learn.microsoft.com/en-us/windows/console/handlerroutine.
virtual void SessionEnding(absl::optional<DWORD> control_type) {}

// Returns true if the audio process should run with high priority. false
// otherwise.
Expand Down
4 changes: 0 additions & 4 deletions headless/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -383,10 +383,6 @@ component("headless_non_renderer") {
sources += [ "lib/browser/headless_browser_main_parts_posix.cc" ]
}

if (is_win) {
sources += [ "lib/browser/headless_browser_main_parts_win.cc" ]
}

if (use_aura) {
sources += [
"lib/browser/headless_browser_impl_aura.cc",
Expand Down
2 changes: 1 addition & 1 deletion headless/lib/browser/headless_browser_main_parts.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class HEADLESS_EXPORT HeadlessBrowserMainParts
#if BUILDFLAG(IS_MAC)
void PreCreateMainMessageLoop() override;
#endif
#if BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_WIN)
#if BUILDFLAG(IS_POSIX)
void PostCreateMainMessageLoop() override;
#endif
void QuitMainMessageLoop();
Expand Down
86 changes: 0 additions & 86 deletions headless/lib/browser/headless_browser_main_parts_win.cc

This file was deleted.

8 changes: 8 additions & 0 deletions headless/lib/browser/headless_content_browser_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,14 @@ HeadlessContentBrowserClient::GetGeolocationManager() {
#endif
}

#if BUILDFLAG(IS_WIN)
void HeadlessContentBrowserClient::SessionEnding(
absl::optional<DWORD> control_type) {
DCHECK_LT(control_type.value_or(0), 0x7fu);
browser_->ShutdownWithExitCode(control_type.value_or(0) + 0x80u);
}
#endif

#if defined(HEADLESS_USE_POLICY)
std::vector<std::unique_ptr<content::NavigationThrottle>>
HeadlessContentBrowserClient::CreateThrottlesForNavigation(
Expand Down
3 changes: 3 additions & 0 deletions headless/lib/browser/headless_content_browser_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ class HeadlessContentBrowserClient : public content::ContentBrowserClient {

bool CanAcceptUntrustedExchangesIfNeeded() override;
device::GeolocationManager* GetGeolocationManager() override;
#if BUILDFLAG(IS_WIN)
void SessionEnding(absl::optional<DWORD> control_type) override;
#endif

#if defined(HEADLESS_USE_POLICY)
std::vector<std::unique_ptr<content::NavigationThrottle>>
Expand Down
8 changes: 8 additions & 0 deletions headless/lib/headless_content_main_delegate.cc
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,14 @@ absl::optional<int> HeadlessContentMainDelegate::PreBrowserMain() {
return absl::nullopt;
}

#if BUILDFLAG(IS_WIN)
bool HeadlessContentMainDelegate::ShouldHandleConsoleControlEvents() {
// Handle console control events so that orderly shutdown can be performed by
// HeadlessContentBrowserClient's override of SessionEnding.
return true;
}
#endif

content::ContentClient* HeadlessContentMainDelegate::CreateContentClient() {
return &content_client_;
}
Expand Down
3 changes: 3 additions & 0 deletions headless/lib/headless_content_main_delegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ class HEADLESS_EXPORT HeadlessContentMainDelegate
const std::string& process_type,
content::MainFunctionParams main_function_params) override;
absl::optional<int> PreBrowserMain() override;
#if BUILDFLAG(IS_WIN)
bool ShouldHandleConsoleControlEvents() override;
#endif
content::ContentClient* CreateContentClient() override;
content::ContentBrowserClient* CreateContentBrowserClient() override;
content::ContentUtilityClient* CreateContentUtilityClient() override;
Expand Down

0 comments on commit 0c5b1f5

Please sign in to comment.