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;
}
// 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,

View File

@ -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();

View File

@ -85,8 +85,7 @@ void SimpleApp::OnContextInitialized() {
CefRefPtr<CefCommandLine> 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<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();
// CefApp methods:
virtual CefRefPtr<CefBrowserProcessHandler> GetBrowserProcessHandler()
OVERRIDE {
CefRefPtr<CefBrowserProcessHandler> GetBrowserProcessHandler() OVERRIDE {
return this;
}
// CefBrowserProcessHandler methods:
virtual void OnContextInitialized() OVERRIDE;
void OnContextInitialized() OVERRIDE;
CefRefPtr<CefClient> GetDefaultClient() OVERRIDE;
private:
// Include the default reference counting implementation.

View File

@ -47,6 +47,10 @@ void SimpleHandler::OnTitleChange(CefRefPtr<CefBrowser> 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<CefBrowserView> browser_view =
@ -110,6 +114,10 @@ void SimpleHandler::OnLoadError(CefRefPtr<CefBrowser> 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<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_; }
// Returns true if the Chrome runtime is enabled.
static bool IsChromeRuntimeEnabled();
private:
// Platform-specific implementation.
void PlatformTitleChange(CefRefPtr<CefBrowser> browser,