From cd89341bfe7eb5e856e98c8cac9b29fe5f77f926 Mon Sep 17 00:00:00 2001 From: Marshall Greenblatt Date: Wed, 19 Aug 2026 18:04:14 -0400 Subject: [PATCH] chrome: Preserve request context for Inspect Element (fixes #4168) Keep CEF-created unique OTR profiles when resolving the DevTools profile so Inspect Element does not create the popup with the parent profile. Add coverage for a non-global request context and verify that the DevTools popup shares it. --- patch/patch.cfg | 4 +++ patch/patches/chrome_browser_browser.patch | 21 ++++++++++++--- tests/ceftests/life_span_unittest.cc | 30 +++++++++++++++++++--- 3 files changed, 48 insertions(+), 7 deletions(-) diff --git a/patch/patch.cfg b/patch/patch.cfg index c6e3d45a0..2bc83e3e4 100644 --- a/patch/patch.cfg +++ b/patch/patch.cfg @@ -264,6 +264,10 @@ patches = [ # OnOpenURLFromTab. # https://github.com/chromiumembedded/cef/issues/3735 # + # Preserve the Profile for DevTools opened from CEF-managed unique OTR + # request contexts. + # https://github.com/chromiumembedded/cef/issues/4168 + # # Fix Picture-in-Picture window icon in Windows taskbar by using the # favicon from the opener WebContents. # diff --git a/patch/patches/chrome_browser_browser.patch b/patch/patches/chrome_browser_browser.patch index 559910ab0..b5ea7b474 100644 --- a/patch/patches/chrome_browser_browser.patch +++ b/patch/patches/chrome_browser_browser.patch @@ -34,10 +34,23 @@ index 08949a69415dfa9f836e00929a34330a34546ce2..e417f50fa68e274b80c0d45bdaa45c7b NOTREACHED(); } diff --git chrome/browser/devtools/devtools_window.cc chrome/browser/devtools/devtools_window.cc -index 9d28b02fa09bf331ed9b381ba25b56f0a30ec365..0d0bf347d77c4c8eda4e69969798c69557af6bdd 100644 +index 9d28b02fa09bf331ed9b381ba25b56f0a30ec365..5ffa364977080aaab9819d789e73544334410271 100644 --- chrome/browser/devtools/devtools_window.cc +++ chrome/browser/devtools/devtools_window.cc -@@ -1380,6 +1380,14 @@ DevToolsWindow* DevToolsWindow::Create( +@@ -854,6 +854,12 @@ Profile* DevToolsWindow::GetProfileForDevToolsWindow( + if (profile->IsPrimaryOTRProfile() || profile->IsDevToolsOTRProfile()) { + return profile; + } ++#if BUILDFLAG(ENABLE_CEF) ++ if (profile->IsOffTheRecord() && ++ profile->GetOTRProfileID().IsUniqueForCEF()) { ++ return profile; ++ } ++#endif + return profile->GetOriginalProfile(); + } + +@@ -1380,6 +1386,14 @@ DevToolsWindow* DevToolsWindow::Create( if (!devtools_ui_controller || !devtools_ui_controller->CanDockDevtools()) { can_dock = false; } @@ -52,7 +65,7 @@ index 9d28b02fa09bf331ed9b381ba25b56f0a30ec365..0d0bf347d77c4c8eda4e69969798c695 } #endif // !BUILDFLAG(IS_ANDROID) -@@ -1844,7 +1852,9 @@ void DevToolsWindow::OpenInNewTab(const GURL& url) { +@@ -1844,7 +1858,9 @@ void DevToolsWindow::OpenInNewTab(const GURL& url) { /*navigation_handle_callback=*/{})) { #if BUILDFLAG(IS_ANDROID) NOTIMPLEMENTED(); @@ -63,7 +76,7 @@ index 9d28b02fa09bf331ed9b381ba25b56f0a30ec365..0d0bf347d77c4c8eda4e69969798c695 chrome::ScopedTabbedBrowserDisplayer displayer(profile_); chrome::AddSelectedTabWithURL(displayer.browser_window_interface(), fixed_url, ui::PAGE_TRANSITION_LINK); -@@ -2050,12 +2060,31 @@ void DevToolsWindow::CreateDevToolsBrowser() { +@@ -2050,12 +2066,31 @@ void DevToolsWindow::CreateDevToolsBrowser() { Browser::CreationStatus::kOk) { return; } diff --git a/tests/ceftests/life_span_unittest.cc b/tests/ceftests/life_span_unittest.cc index 36e2e94b5..47968714e 100644 --- a/tests/ceftests/life_span_unittest.cc +++ b/tests/ceftests/life_span_unittest.cc @@ -3,6 +3,7 @@ // can be found in the LICENSE file. #include "include/base/cef_callback.h" +#include "include/cef_request_context_handler.h" #include "include/test/cef_test_helpers.h" #include "include/wrapper/cef_closure_task.h" #include "tests/ceftests/routing_test_handler.h" @@ -399,7 +400,10 @@ const cef_color_t kDevToolsBackgroundColor = CefColorSetARGB(255, 128, 64, 32); // Test OnBeforeDevToolsPopup callback class DevToolsPopupTestHandler : public TestHandler { public: - DevToolsPopupTestHandler() = default; + DevToolsPopupTestHandler(bool unique_request_context = false, + bool inspect_element = false) + : unique_request_context_(unique_request_context), + inspect_element_(inspect_element) {} DevToolsPopupTestHandler(const DevToolsPopupTestHandler&) = delete; DevToolsPopupTestHandler& operator=(const DevToolsPopupTestHandler&) = delete; @@ -412,7 +416,12 @@ class DevToolsPopupTestHandler : public TestHandler { ""; AddResource(kDevToolsTestUrl, html, "text/html"); - CreateBrowser(kDevToolsTestUrl); + CefRefPtr request_context; + if (unique_request_context_) { + CefRequestContextSettings settings; + request_context = CefRequestContext::CreateContext(settings, nullptr); + } + CreateBrowser(kDevToolsTestUrl, request_context); SetTestTimeout(); } @@ -428,6 +437,8 @@ class DevToolsPopupTestHandler : public TestHandler { EXPECT_FALSE(got_devtools_after_created_); got_devtools_after_created_.yes(); EXPECT_TRUE(browser->IsPopup()); + EXPECT_TRUE(browser->GetHost()->GetRequestContext()->IsSame( + main_browser_->GetHost()->GetRequestContext())); devtools_browser_ = browser; } } @@ -454,8 +465,10 @@ class DevToolsPopupTestHandler : public TestHandler { // Set a custom background color to verify it's passed to the callback settings.background_color = kDevToolsBackgroundColor; + const CefPoint inspect_point = + inspect_element_ ? CefPoint(10, 10) : CefPoint(); main_browser_->GetHost()->ShowDevTools(windowInfo, this, settings, - CefPoint()); + inspect_point); } else if (devtools_browser_ && browser->IsSame(devtools_browser_)) { EXPECT_FALSE(got_devtools_load_end_); got_devtools_load_end_.yes(); @@ -531,6 +544,9 @@ class DevToolsPopupTestHandler : public TestHandler { CefRefPtr main_browser_; CefRefPtr devtools_browser_; + const bool unique_request_context_; + const bool inspect_element_; + TrackCallback got_main_after_created_; TrackCallback got_main_load_end_; TrackCallback got_before_devtools_popup_; @@ -551,3 +567,11 @@ TEST(LifeSpanTest, OnBeforeDevToolsPopup) { handler->ExecuteTest(); ReleaseAndWaitForDestructor(handler); } + +TEST(LifeSpanTest, OnBeforeDevToolsPopupUniqueContextInspectElement) { + CefRefPtr handler = + new DevToolsPopupTestHandler(/*unique_request_context=*/true, + /*inspect_element=*/true); + handler->ExecuteTest(); + ReleaseAndWaitForDestructor(handler); +}