From ec7067c55ee18c36f5426b1390bf7ef383c57260 Mon Sep 17 00:00:00 2001 From: Marshall Greenblatt Date: Mon, 15 Feb 2021 16:40:07 -0500 Subject: [PATCH] chrome: Fix callbacks for different Profile types (see issue #2969) - Only install network intercepts for Profiles that have an associated CefBrowserContext. For incognito windows the CefBrowserContext is associated with the OffTheRecordProfileImpl's original Profile. - cefsimple: Return the default CefClient instance for browser windows created via the Chrome UI, and allow Chrome to show error pages. --- .../chrome_content_browser_client_cef.cc | 7 +++++++ .../resource_request_handler_wrapper.cc | 4 +++- tests/cefsimple/simple_app.cc | 8 ++++++-- tests/cefsimple/simple_app.h | 6 +++--- tests/cefsimple/simple_handler.cc | 19 +++++++++++++++++++ tests/cefsimple/simple_handler.h | 3 +++ 6 files changed, 41 insertions(+), 6 deletions(-) diff --git a/libcef/browser/chrome/chrome_content_browser_client_cef.cc b/libcef/browser/chrome/chrome_content_browser_client_cef.cc index 89284e7fb..25242c587 100644 --- a/libcef/browser/chrome/chrome_content_browser_client_cef.cc +++ b/libcef/browser/chrome/chrome_content_browser_client_cef.cc @@ -170,6 +170,13 @@ bool ChromeContentBrowserClientCef::WillCreateURLLoaderFactory( return use_proxy; } + // Don't intercept requests for Profiles that were not created by CEF. + // For example, the User Manager profile created via + // profiles::CreateSystemProfileForUserManager. + auto profile = Profile::FromBrowserContext(browser_context); + if (!CefBrowserContext::FromBrowserContext(profile->GetOriginalProfile())) + return false; + auto request_handler = net_service::CreateInterceptedRequestHandler( browser_context, frame, render_process_id, type == URLLoaderFactoryType::kNavigation, diff --git a/libcef/browser/net_service/resource_request_handler_wrapper.cc b/libcef/browser/net_service/resource_request_handler_wrapper.cc index 99a57af58..2a7717101 100644 --- a/libcef/browser/net_service/resource_request_handler_wrapper.cc +++ b/libcef/browser/net_service/resource_request_handler_wrapper.cc @@ -260,8 +260,10 @@ class InterceptedRequestHandlerWrapper : public InterceptedRequestHandler { CEF_REQUIRE_UIT(); browser_context_ = browser_context; + + auto profile = Profile::FromBrowserContext(browser_context); auto cef_browser_context = - CefBrowserContext::FromBrowserContext(browser_context); + CefBrowserContext::FromBrowserContext(profile->GetOriginalProfile()); iothread_state_ = cef_browser_context->iothread_state(); DCHECK(iothread_state_); cookieable_schemes_ = cef_browser_context->GetCookieableSchemes(); diff --git a/tests/cefsimple/simple_app.cc b/tests/cefsimple/simple_app.cc index 30b00ab9e..1ff857e2d 100644 --- a/tests/cefsimple/simple_app.cc +++ b/tests/cefsimple/simple_app.cc @@ -85,8 +85,7 @@ void SimpleApp::OnContextInitialized() { CefRefPtr command_line = CefCommandLine::GetGlobalCommandLine(); - const bool enable_chrome_runtime = - command_line->HasSwitch("enable-chrome-runtime"); + const bool enable_chrome_runtime = SimpleHandler::IsChromeRuntimeEnabled(); #if defined(OS_WIN) || defined(OS_LINUX) // Create the browser using the Views framework if "--use-views" is specified @@ -135,3 +134,8 @@ void SimpleApp::OnContextInitialized() { nullptr, nullptr); } } + +CefRefPtr SimpleApp::GetDefaultClient() { + // Called when a new browser window is created via the Chrome runtime UI. + return SimpleHandler::GetInstance(); +} diff --git a/tests/cefsimple/simple_app.h b/tests/cefsimple/simple_app.h index 8ed996960..ab32391cc 100644 --- a/tests/cefsimple/simple_app.h +++ b/tests/cefsimple/simple_app.h @@ -13,13 +13,13 @@ class SimpleApp : public CefApp, public CefBrowserProcessHandler { SimpleApp(); // CefApp methods: - virtual CefRefPtr GetBrowserProcessHandler() - OVERRIDE { + CefRefPtr GetBrowserProcessHandler() OVERRIDE { return this; } // CefBrowserProcessHandler methods: - virtual void OnContextInitialized() OVERRIDE; + void OnContextInitialized() OVERRIDE; + CefRefPtr GetDefaultClient() OVERRIDE; private: // Include the default reference counting implementation. diff --git a/tests/cefsimple/simple_handler.cc b/tests/cefsimple/simple_handler.cc index fafedc85d..1a8b1922e 100644 --- a/tests/cefsimple/simple_handler.cc +++ b/tests/cefsimple/simple_handler.cc @@ -47,6 +47,10 @@ void SimpleHandler::OnTitleChange(CefRefPtr browser, const CefString& title) { CEF_REQUIRE_UI_THREAD(); + // Allow Chrome to handle the title change. + if (IsChromeRuntimeEnabled()) + return; + if (use_views_) { // Set the title of the window using the Views framework. CefRefPtr browser_view = @@ -110,6 +114,10 @@ void SimpleHandler::OnLoadError(CefRefPtr browser, const CefString& failedUrl) { CEF_REQUIRE_UI_THREAD(); + // Allow Chrome to show the error page. + if (IsChromeRuntimeEnabled()) + return; + // Don't display an error for downloaded files. if (errorCode == ERR_ABORTED) return; @@ -139,3 +147,14 @@ void SimpleHandler::CloseAllBrowsers(bool force_close) { for (; it != browser_list_.end(); ++it) (*it)->GetHost()->CloseBrowser(force_close); } + +// static +bool SimpleHandler::IsChromeRuntimeEnabled() { + static int value = -1; + if (value == -1) { + CefRefPtr command_line = + CefCommandLine::GetGlobalCommandLine(); + value = command_line->HasSwitch("enable-chrome-runtime") ? 1 : 0; + } + return value == 1; +} diff --git a/tests/cefsimple/simple_handler.h b/tests/cefsimple/simple_handler.h index 10194f06d..4ca57339a 100644 --- a/tests/cefsimple/simple_handler.h +++ b/tests/cefsimple/simple_handler.h @@ -50,6 +50,9 @@ class SimpleHandler : public CefClient, bool IsClosing() const { return is_closing_; } + // Returns true if the Chrome runtime is enabled. + static bool IsChromeRuntimeEnabled(); + private: // Platform-specific implementation. void PlatformTitleChange(CefRefPtr browser,