feat: add focusOnNavigation flag to WebPreferences (#49511)

* feat: add focusOnNavigation webPreference

Co-authored-by: Kyle Cutler <kycutler@microsoft.com>

* WebContentsView tests

Co-authored-by: Kyle Cutler <kycutler@microsoft.com>

* fix

Co-authored-by: Kyle Cutler <kycutler@microsoft.com>

* fix

Co-authored-by: Kyle Cutler <kycutler@microsoft.com>

---------

Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com>
Co-authored-by: Kyle Cutler <kycutler@microsoft.com>
This commit is contained in:
trop[bot]
2026-02-04 15:44:03 -05:00
committed by GitHub
parent 0abdb91b78
commit 2dbdf223b7
7 changed files with 77 additions and 0 deletions

View File

@@ -2094,6 +2094,10 @@ void WebContents::ReadyToCommitNavigation(
// Only focus for top-level contents.
if (type_ != Type::kBrowserWindow)
return;
// Don't focus if focusOnNavigation is disabled.
auto* prefs = WebContentsPreferences::From(web_contents());
if (prefs && !prefs->ShouldFocusOnNavigation())
return;
web_contents()->SetInitialFocus();
}

View File

@@ -149,6 +149,7 @@ void WebContentsPreferences::Clear() {
preload_path_ = std::nullopt;
v8_cache_options_ = blink::mojom::V8CacheOptions::kDefault;
deprecated_paste_enabled_ = false;
focus_on_navigation_ = true;
#if BUILDFLAG(IS_MAC)
scroll_bounce_ = false;
@@ -249,6 +250,8 @@ void WebContentsPreferences::SetFromDictionary(
web_preferences.Get(options::kEnableDeprecatedPaste,
&deprecated_paste_enabled_);
web_preferences.Get(options::kFocusOnNavigation, &focus_on_navigation_);
#if BUILDFLAG(IS_MAC)
web_preferences.Get(options::kScrollBounce, &scroll_bounce_);
#endif

View File

@@ -78,6 +78,7 @@ class WebContentsPreferences
bool ShouldDisablePopups() const { return disable_popups_; }
bool IsWebSecurityEnabled() const { return web_security_; }
std::optional<base::FilePath> GetPreloadPath() const { return preload_path_; }
bool ShouldFocusOnNavigation() const { return focus_on_navigation_; }
bool IsSandboxed() const;
private:
@@ -134,6 +135,7 @@ class WebContentsPreferences
std::optional<base::FilePath> preload_path_;
blink::mojom::V8CacheOptions v8_cache_options_;
bool deprecated_paste_enabled_ = false;
bool focus_on_navigation_;
#if BUILDFLAG(IS_MAC)
bool scroll_bounce_;

View File

@@ -222,6 +222,9 @@ inline constexpr std::string_view kSpellcheck = "spellcheck";
inline constexpr std::string_view kEnableDeprecatedPaste =
"enableDeprecatedPaste";
// Whether to focus the webContents on navigation.
inline constexpr std::string_view kFocusOnNavigation = "focusOnNavigation";
inline constexpr std::string_view kModal = "modal";
} // namespace options