From cf1074cf49fa6afe32e5985166751eaee8e3fcf6 Mon Sep 17 00:00:00 2001 From: Marshall Greenblatt Date: Sat, 17 Apr 2021 21:12:54 -0400 Subject: [PATCH] chrome: Support configuration of renderer prefs (see issue #2969) --- .../browser/alloy/alloy_browser_host_impl.cc | 6 - .../browser/alloy/alloy_browser_host_impl.h | 5 +- libcef/browser/browser_host_base.cc | 11 + libcef/browser/browser_host_base.h | 4 + .../chrome_content_browser_client_cef.cc | 34 ++++ .../chrome_content_browser_client_cef.h | 2 + libcef/browser/prefs/renderer_prefs.cc | 190 +++++++++--------- libcef/browser/prefs/renderer_prefs.h | 9 + 8 files changed, 154 insertions(+), 107 deletions(-) diff --git a/libcef/browser/alloy/alloy_browser_host_impl.cc b/libcef/browser/alloy/alloy_browser_host_impl.cc index e8778dba3..1f9eda540 100644 --- a/libcef/browser/alloy/alloy_browser_host_impl.cc +++ b/libcef/browser/alloy/alloy_browser_host_impl.cc @@ -812,12 +812,6 @@ bool AlloyBrowserHostImpl::MaybeAllowNavigation( return true; } -SkColor AlloyBrowserHostImpl::GetBackgroundColor() const { - // Don't use |platform_delegate_| because it's not thread-safe. - return CefContext::Get()->GetBackgroundColor( - &settings_, is_windowless_ ? STATE_ENABLED : STATE_DISABLED); -} - extensions::ExtensionHost* AlloyBrowserHostImpl::GetExtensionHost() const { CEF_REQUIRE_UIT(); DCHECK(platform_delegate_); diff --git a/libcef/browser/alloy/alloy_browser_host_impl.h b/libcef/browser/alloy/alloy_browser_host_impl.h index 89d8773b3..c14710683 100644 --- a/libcef/browser/alloy/alloy_browser_host_impl.h +++ b/libcef/browser/alloy/alloy_browser_host_impl.h @@ -151,7 +151,7 @@ class AlloyBrowserHostImpl : public CefBrowserHostBase, bool IsBackgroundHost() override; // Returns true if windowless rendering is enabled. - bool IsWindowless() const; + bool IsWindowless() const override; // Returns true if this browser supports picture-in-picture. bool IsPictureInPictureSupported() const; @@ -182,9 +182,6 @@ class AlloyBrowserHostImpl : public CefBrowserHostBase, content::RenderWidgetHostImpl* source_rwh); void UpdateDragCursor(ui::mojom::DragOperation operation); - // Thread safe accessors. - SkColor GetBackgroundColor() const; - // Accessors that must be called on the UI thread. extensions::ExtensionHost* GetExtensionHost() const; diff --git a/libcef/browser/browser_host_base.cc b/libcef/browser/browser_host_base.cc index 5670d731e..20760ddff 100644 --- a/libcef/browser/browser_host_base.cc +++ b/libcef/browser/browser_host_base.cc @@ -6,6 +6,7 @@ #include "libcef/browser/browser_info_manager.h" #include "libcef/browser/browser_platform_delegate.h" +#include "libcef/browser/context.h" #include "libcef/browser/image_impl.h" #include "libcef/browser/navigation_entry_impl.h" #include "libcef/browser/thread_util.h" @@ -826,6 +827,16 @@ int CefBrowserHostBase::browser_id() const { return browser_info_->browser_id(); } +SkColor CefBrowserHostBase::GetBackgroundColor() const { + // Don't use |platform_delegate_| because it's not thread-safe. + return CefContext::Get()->GetBackgroundColor( + &settings_, IsWindowless() ? STATE_ENABLED : STATE_DISABLED); +} + +bool CefBrowserHostBase::IsWindowless() const { + return false; +} + content::WebContents* CefBrowserHostBase::GetWebContents() const { CEF_REQUIRE_UIT(); return contents_delegate_->web_contents(); diff --git a/libcef/browser/browser_host_base.h b/libcef/browser/browser_host_base.h index 76d0db138..3b8530a78 100644 --- a/libcef/browser/browser_host_base.h +++ b/libcef/browser/browser_host_base.h @@ -246,6 +246,10 @@ class CefBrowserHostBase : public CefBrowserHost, return request_context_; } bool is_views_hosted() const { return is_views_hosted_; } + SkColor GetBackgroundColor() const; + + // Returns true if windowless rendering is enabled. + virtual bool IsWindowless() const; // Accessors that must be called on the UI thread. content::WebContents* GetWebContents() const; diff --git a/libcef/browser/chrome/chrome_content_browser_client_cef.cc b/libcef/browser/chrome/chrome_content_browser_client_cef.cc index c2aa900f8..d7d657f99 100644 --- a/libcef/browser/chrome/chrome_content_browser_client_cef.cc +++ b/libcef/browser/chrome/chrome_content_browser_client_cef.cc @@ -7,13 +7,16 @@ #include "libcef/browser/browser_info_manager.h" #include "libcef/browser/browser_message_filter.h" +#include "libcef/browser/chrome/chrome_browser_host_impl.h" #include "libcef/browser/chrome/chrome_browser_main_extra_parts_cef.h" +#include "libcef/browser/context.h" #include "libcef/browser/net/chrome_scheme_handler.h" #include "libcef/browser/net/throttle_handler.h" #include "libcef/browser/net_service/cookie_manager_impl.h" #include "libcef/browser/net_service/login_delegate.h" #include "libcef/browser/net_service/proxy_url_loader_factory.h" #include "libcef/browser/net_service/resource_request_handler_wrapper.h" +#include "libcef/browser/prefs/renderer_prefs.h" #include "libcef/common/app_manager.h" #include "libcef/common/cef_switches.h" #include "libcef/common/command_line_impl.h" @@ -25,7 +28,11 @@ #include "chrome/common/chrome_switches.h" #include "content/public/browser/navigation_throttle.h" #include "content/public/browser/render_process_host.h" +#include "content/public/browser/render_view_host.h" +#include "content/public/browser/render_widget_host.h" +#include "content/public/browser/render_widget_host_view.h" #include "content/public/common/content_switches.h" +#include "third_party/blink/public/common/web_preferences/web_preferences.h" #include "third_party/blink/public/mojom/loader/resource_load_info.mojom-shared.h" namespace { @@ -148,6 +155,33 @@ bool ChromeContentBrowserClientCef::CanCreateWindow( user_gesture, opener_suppressed, no_javascript_access); } +void ChromeContentBrowserClientCef::OverrideWebkitPrefs( + content::WebContents* web_contents, + blink::web_pref::WebPreferences* prefs) { + renderer_prefs::SetDefaultPrefs(*prefs); + + ChromeContentBrowserClient::OverrideWebkitPrefs(web_contents, prefs); + + auto browser = ChromeBrowserHostImpl::GetBrowserForContents(web_contents); + if (browser) { + renderer_prefs::SetCefPrefs(browser->settings(), *prefs); + + // Set the background color for the WebView. + prefs->base_background_color = browser->GetBackgroundColor(); + } else { + // We don't know for sure that the browser will be windowless but assume + // that the global windowless state is likely to be accurate. + prefs->base_background_color = + CefContext::Get()->GetBackgroundColor(nullptr, STATE_DEFAULT); + } + + auto rvh = web_contents->GetRenderViewHost(); + if (rvh->GetWidget()->GetView()) { + rvh->GetWidget()->GetView()->SetBackgroundColor( + prefs->base_background_color); + } +} + bool ChromeContentBrowserClientCef::WillCreateURLLoaderFactory( content::BrowserContext* browser_context, content::RenderFrameHost* frame, diff --git a/libcef/browser/chrome/chrome_content_browser_client_cef.h b/libcef/browser/chrome/chrome_content_browser_client_cef.h index 8b4b7f5f0..2c2e28ee2 100644 --- a/libcef/browser/chrome/chrome_content_browser_client_cef.h +++ b/libcef/browser/chrome/chrome_content_browser_client_cef.h @@ -40,6 +40,8 @@ class ChromeContentBrowserClientCef : public ChromeContentBrowserClient { bool user_gesture, bool opener_suppressed, bool* no_javascript_access) override; + void OverrideWebkitPrefs(content::WebContents* web_contents, + blink::web_pref::WebPreferences* prefs) override; bool WillCreateURLLoaderFactory( content::BrowserContext* browser_context, content::RenderFrameHost* frame, diff --git a/libcef/browser/prefs/renderer_prefs.cc b/libcef/browser/prefs/renderer_prefs.cc index 01bac713f..5bba21fb1 100644 --- a/libcef/browser/prefs/renderer_prefs.cc +++ b/libcef/browser/prefs/renderer_prefs.cc @@ -47,27 +47,6 @@ namespace renderer_prefs { namespace { -// Set default values based on CEF command-line flags for preferences that are -// not available via the PrefService. Chromium command-line flags should not -// exist for these preferences. -void SetDefaultPrefs(blink::web_pref::WebPreferences& web) { - const base::CommandLine* command_line = - base::CommandLine::ForCurrentProcess(); - - web.javascript_enabled = - !command_line->HasSwitch(switches::kDisableJavascript); - web.allow_scripts_to_close_windows = - !command_line->HasSwitch(switches::kDisableJavascriptCloseWindows); - web.javascript_can_access_clipboard = - !command_line->HasSwitch(switches::kDisableJavascriptAccessClipboard); - web.allow_universal_access_from_file_urls = - command_line->HasSwitch(switches::kAllowUniversalAccessFromFileUrls); - web.shrinks_standalone_images_to_fit = - command_line->HasSwitch(switches::kImageShrinkStandaloneToFit); - web.text_areas_are_resizable = - !command_line->HasSwitch(switches::kDisableTextAreaResize); -} - // Chrome preferences. // Should match ChromeContentBrowserClient::OverrideWebkitPrefs. void SetChromePrefs(Profile* profile, blink::web_pref::WebPreferences& web) { @@ -194,82 +173,6 @@ void SetExtensionPrefs(content::RenderViewHost* rvh, extension_webkit_preferences::SetPreferences(extension, view_type, &web); } -// Helper macro for setting a WebPreferences variable based on the value of a -// CefBrowserSettings variable. -#define SET_STATE(cef_var, web_var) \ - if (cef_var == STATE_ENABLED) \ - web_var = true; \ - else if (cef_var == STATE_DISABLED) \ - web_var = false; - -// Set preferences based on CefBrowserSettings. -void SetCefPrefs(const CefBrowserSettings& cef, - blink::web_pref::WebPreferences& web) { - if (cef.standard_font_family.length > 0) { - web.standard_font_family_map[blink::web_pref::kCommonScript] = - CefString(&cef.standard_font_family); - } - if (cef.fixed_font_family.length > 0) { - web.fixed_font_family_map[blink::web_pref::kCommonScript] = - CefString(&cef.fixed_font_family); - } - if (cef.serif_font_family.length > 0) { - web.serif_font_family_map[blink::web_pref::kCommonScript] = - CefString(&cef.serif_font_family); - } - if (cef.sans_serif_font_family.length > 0) { - web.sans_serif_font_family_map[blink::web_pref::kCommonScript] = - CefString(&cef.sans_serif_font_family); - } - if (cef.cursive_font_family.length > 0) { - web.cursive_font_family_map[blink::web_pref::kCommonScript] = - CefString(&cef.cursive_font_family); - } - if (cef.fantasy_font_family.length > 0) { - web.fantasy_font_family_map[blink::web_pref::kCommonScript] = - CefString(&cef.fantasy_font_family); - } - - if (cef.default_font_size > 0) - web.default_font_size = cef.default_font_size; - if (cef.default_fixed_font_size > 0) - web.default_fixed_font_size = cef.default_fixed_font_size; - if (cef.minimum_font_size > 0) - web.minimum_font_size = cef.minimum_font_size; - if (cef.minimum_logical_font_size > 0) - web.minimum_logical_font_size = cef.minimum_logical_font_size; - - if (cef.default_encoding.length > 0) - web.default_encoding = CefString(&cef.default_encoding); - - SET_STATE(cef.remote_fonts, web.remote_fonts_enabled); - SET_STATE(cef.javascript, web.javascript_enabled); - SET_STATE(cef.javascript_close_windows, web.allow_scripts_to_close_windows); - SET_STATE(cef.javascript_access_clipboard, - web.javascript_can_access_clipboard); - SET_STATE(cef.javascript_dom_paste, web.dom_paste_enabled); - SET_STATE(cef.plugins, web.plugins_enabled); - SET_STATE(cef.universal_access_from_file_urls, - web.allow_universal_access_from_file_urls); - SET_STATE(cef.file_access_from_file_urls, - web.allow_file_access_from_file_urls); - SET_STATE(cef.image_loading, web.loads_images_automatically); - SET_STATE(cef.image_shrink_standalone_to_fit, - web.shrinks_standalone_images_to_fit); - SET_STATE(cef.text_area_resize, web.text_areas_are_resizable); - SET_STATE(cef.tab_to_links, web.tabs_to_links); - SET_STATE(cef.local_storage, web.local_storage_enabled); - SET_STATE(cef.databases, web.databases_enabled); - SET_STATE(cef.application_cache, web.application_cache_enabled); - - // Never explicitly enable GPU-related functions in this method because the - // GPU blacklist is not being checked here. - if (cef.webgl == STATE_DISABLED) { - web.webgl1_enabled = false; - web.webgl2_enabled = false; - } -} - void SetString(CommandLinePrefStore* prefs, const std::string& key, const std::string& value) { @@ -347,6 +250,99 @@ void SetCommandLinePrefDefaults(CommandLinePrefStore* prefs) { SetBool(prefs, prefs::kWebKitPluginsEnabled, false); } +void SetDefaultPrefs(blink::web_pref::WebPreferences& web) { + const base::CommandLine* command_line = + base::CommandLine::ForCurrentProcess(); + + web.javascript_enabled = + !command_line->HasSwitch(switches::kDisableJavascript); + web.allow_scripts_to_close_windows = + !command_line->HasSwitch(switches::kDisableJavascriptCloseWindows); + web.javascript_can_access_clipboard = + !command_line->HasSwitch(switches::kDisableJavascriptAccessClipboard); + web.allow_universal_access_from_file_urls = + command_line->HasSwitch(switches::kAllowUniversalAccessFromFileUrls); + web.shrinks_standalone_images_to_fit = + command_line->HasSwitch(switches::kImageShrinkStandaloneToFit); + web.text_areas_are_resizable = + !command_line->HasSwitch(switches::kDisableTextAreaResize); +} + +// Helper macro for setting a WebPreferences variable based on the value of a +// CefBrowserSettings variable. +#define SET_STATE(cef_var, web_var) \ + if (cef_var == STATE_ENABLED) \ + web_var = true; \ + else if (cef_var == STATE_DISABLED) \ + web_var = false; + +void SetCefPrefs(const CefBrowserSettings& cef, + blink::web_pref::WebPreferences& web) { + if (cef.standard_font_family.length > 0) { + web.standard_font_family_map[blink::web_pref::kCommonScript] = + CefString(&cef.standard_font_family); + } + if (cef.fixed_font_family.length > 0) { + web.fixed_font_family_map[blink::web_pref::kCommonScript] = + CefString(&cef.fixed_font_family); + } + if (cef.serif_font_family.length > 0) { + web.serif_font_family_map[blink::web_pref::kCommonScript] = + CefString(&cef.serif_font_family); + } + if (cef.sans_serif_font_family.length > 0) { + web.sans_serif_font_family_map[blink::web_pref::kCommonScript] = + CefString(&cef.sans_serif_font_family); + } + if (cef.cursive_font_family.length > 0) { + web.cursive_font_family_map[blink::web_pref::kCommonScript] = + CefString(&cef.cursive_font_family); + } + if (cef.fantasy_font_family.length > 0) { + web.fantasy_font_family_map[blink::web_pref::kCommonScript] = + CefString(&cef.fantasy_font_family); + } + + if (cef.default_font_size > 0) + web.default_font_size = cef.default_font_size; + if (cef.default_fixed_font_size > 0) + web.default_fixed_font_size = cef.default_fixed_font_size; + if (cef.minimum_font_size > 0) + web.minimum_font_size = cef.minimum_font_size; + if (cef.minimum_logical_font_size > 0) + web.minimum_logical_font_size = cef.minimum_logical_font_size; + + if (cef.default_encoding.length > 0) + web.default_encoding = CefString(&cef.default_encoding); + + SET_STATE(cef.remote_fonts, web.remote_fonts_enabled); + SET_STATE(cef.javascript, web.javascript_enabled); + SET_STATE(cef.javascript_close_windows, web.allow_scripts_to_close_windows); + SET_STATE(cef.javascript_access_clipboard, + web.javascript_can_access_clipboard); + SET_STATE(cef.javascript_dom_paste, web.dom_paste_enabled); + SET_STATE(cef.plugins, web.plugins_enabled); + SET_STATE(cef.universal_access_from_file_urls, + web.allow_universal_access_from_file_urls); + SET_STATE(cef.file_access_from_file_urls, + web.allow_file_access_from_file_urls); + SET_STATE(cef.image_loading, web.loads_images_automatically); + SET_STATE(cef.image_shrink_standalone_to_fit, + web.shrinks_standalone_images_to_fit); + SET_STATE(cef.text_area_resize, web.text_areas_are_resizable); + SET_STATE(cef.tab_to_links, web.tabs_to_links); + SET_STATE(cef.local_storage, web.local_storage_enabled); + SET_STATE(cef.databases, web.databases_enabled); + SET_STATE(cef.application_cache, web.application_cache_enabled); + + // Never explicitly enable GPU-related functions in this method because the + // GPU blacklist is not being checked here. + if (cef.webgl == STATE_DISABLED) { + web.webgl1_enabled = false; + web.webgl2_enabled = false; + } +} + void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry, const std::string& locale) { PrefsTabHelper::RegisterProfilePrefs(registry, locale); diff --git a/libcef/browser/prefs/renderer_prefs.h b/libcef/browser/prefs/renderer_prefs.h index 0d8ead4f8..00ab48d22 100644 --- a/libcef/browser/prefs/renderer_prefs.h +++ b/libcef/browser/prefs/renderer_prefs.h @@ -36,6 +36,15 @@ void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry, // for these preferences. void SetCommandLinePrefDefaults(CommandLinePrefStore* prefs); +// Set default values based on CEF command-line flags for preferences that are +// not available via the PrefService. Chromium command-line flags should not +// exist for these preferences. +void SetDefaultPrefs(blink::web_pref::WebPreferences& web); + +// Set preferences based on CefBrowserSettings. +void SetCefPrefs(const CefBrowserSettings& cef, + blink::web_pref::WebPreferences& web); + // Populate WebPreferences based on a combination of command-line values, // PrefService and CefBrowserSettings. void PopulateWebPreferences(content::RenderViewHost* rvh,