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.
This commit is contained in:
Marshall Greenblatt 2021-02-15 16:40:07 -05:00
parent bf3b7b2c62
commit ec7067c55e
6 changed files with 41 additions and 6 deletions

View File

@ -170,6 +170,13 @@ bool ChromeContentBrowserClientCef::WillCreateURLLoaderFactory(
return use_proxy; 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( auto request_handler = net_service::CreateInterceptedRequestHandler(
browser_context, frame, render_process_id, browser_context, frame, render_process_id,
type == URLLoaderFactoryType::kNavigation, type == URLLoaderFactoryType::kNavigation,

View File

@ -260,8 +260,10 @@ class InterceptedRequestHandlerWrapper : public InterceptedRequestHandler {
CEF_REQUIRE_UIT(); CEF_REQUIRE_UIT();
browser_context_ = browser_context; browser_context_ = browser_context;
auto profile = Profile::FromBrowserContext(browser_context);
auto cef_browser_context = auto cef_browser_context =
CefBrowserContext::FromBrowserContext(browser_context); CefBrowserContext::FromBrowserContext(profile->GetOriginalProfile());
iothread_state_ = cef_browser_context->iothread_state(); iothread_state_ = cef_browser_context->iothread_state();
DCHECK(iothread_state_); DCHECK(iothread_state_);
cookieable_schemes_ = cef_browser_context->GetCookieableSchemes(); cookieable_schemes_ = cef_browser_context->GetCookieableSchemes();

View File

@ -85,8 +85,7 @@ void SimpleApp::OnContextInitialized() {
CefRefPtr<CefCommandLine> command_line = CefRefPtr<CefCommandLine> command_line =
CefCommandLine::GetGlobalCommandLine(); CefCommandLine::GetGlobalCommandLine();
const bool enable_chrome_runtime = const bool enable_chrome_runtime = SimpleHandler::IsChromeRuntimeEnabled();
command_line->HasSwitch("enable-chrome-runtime");
#if defined(OS_WIN) || defined(OS_LINUX) #if defined(OS_WIN) || defined(OS_LINUX)
// Create the browser using the Views framework if "--use-views" is specified // Create the browser using the Views framework if "--use-views" is specified
@ -135,3 +134,8 @@ void SimpleApp::OnContextInitialized() {
nullptr, nullptr); nullptr, nullptr);
} }
} }
CefRefPtr<CefClient> SimpleApp::GetDefaultClient() {
// Called when a new browser window is created via the Chrome runtime UI.
return SimpleHandler::GetInstance();
}

View File

@ -13,13 +13,13 @@ class SimpleApp : public CefApp, public CefBrowserProcessHandler {
SimpleApp(); SimpleApp();
// CefApp methods: // CefApp methods:
virtual CefRefPtr<CefBrowserProcessHandler> GetBrowserProcessHandler() CefRefPtr<CefBrowserProcessHandler> GetBrowserProcessHandler() OVERRIDE {
OVERRIDE {
return this; return this;
} }
// CefBrowserProcessHandler methods: // CefBrowserProcessHandler methods:
virtual void OnContextInitialized() OVERRIDE; void OnContextInitialized() OVERRIDE;
CefRefPtr<CefClient> GetDefaultClient() OVERRIDE;
private: private:
// Include the default reference counting implementation. // Include the default reference counting implementation.

View File

@ -47,6 +47,10 @@ void SimpleHandler::OnTitleChange(CefRefPtr<CefBrowser> browser,
const CefString& title) { const CefString& title) {
CEF_REQUIRE_UI_THREAD(); CEF_REQUIRE_UI_THREAD();
// Allow Chrome to handle the title change.
if (IsChromeRuntimeEnabled())
return;
if (use_views_) { if (use_views_) {
// Set the title of the window using the Views framework. // Set the title of the window using the Views framework.
CefRefPtr<CefBrowserView> browser_view = CefRefPtr<CefBrowserView> browser_view =
@ -110,6 +114,10 @@ void SimpleHandler::OnLoadError(CefRefPtr<CefBrowser> browser,
const CefString& failedUrl) { const CefString& failedUrl) {
CEF_REQUIRE_UI_THREAD(); CEF_REQUIRE_UI_THREAD();
// Allow Chrome to show the error page.
if (IsChromeRuntimeEnabled())
return;
// Don't display an error for downloaded files. // Don't display an error for downloaded files.
if (errorCode == ERR_ABORTED) if (errorCode == ERR_ABORTED)
return; return;
@ -139,3 +147,14 @@ void SimpleHandler::CloseAllBrowsers(bool force_close) {
for (; it != browser_list_.end(); ++it) for (; it != browser_list_.end(); ++it)
(*it)->GetHost()->CloseBrowser(force_close); (*it)->GetHost()->CloseBrowser(force_close);
} }
// static
bool SimpleHandler::IsChromeRuntimeEnabled() {
static int value = -1;
if (value == -1) {
CefRefPtr<CefCommandLine> command_line =
CefCommandLine::GetGlobalCommandLine();
value = command_line->HasSwitch("enable-chrome-runtime") ? 1 : 0;
}
return value == 1;
}

View File

@ -50,6 +50,9 @@ class SimpleHandler : public CefClient,
bool IsClosing() const { return is_closing_; } bool IsClosing() const { return is_closing_; }
// Returns true if the Chrome runtime is enabled.
static bool IsChromeRuntimeEnabled();
private: private:
// Platform-specific implementation. // Platform-specific implementation.
void PlatformTitleChange(CefRefPtr<CefBrowser> browser, void PlatformTitleChange(CefRefPtr<CefBrowser> browser,