diff --git a/lib/browser/api/web-contents.ts b/lib/browser/api/web-contents.ts index 25ac5ae18c1e..01fd54aa3b9d 100644 --- a/lib/browser/api/web-contents.ts +++ b/lib/browser/api/web-contents.ts @@ -588,6 +588,7 @@ WebContents.prototype._init = function () { // it's technically a BrowserWindowConstructorOptions option because // we need to access it in the renderer at init time. backgroundColor: windowOpenOverriddenOptions.backgroundColor, + transparent: windowOpenOverriddenOptions.transparent, ...windowOpenOverriddenOptions.webPreferences } : undefined; this._setNextChildWebPreferences( diff --git a/patches/chromium/.patches b/patches/chromium/.patches index 350412698dd1..f5f957d3ee50 100644 --- a/patches/chromium/.patches +++ b/patches/chromium/.patches @@ -108,3 +108,4 @@ add_trustedauthclient_to_urlloaderfactory.patch fix_route_mouse_event_navigations_through_the_web_contents_delegate.patch disable_unload_metrics.patch fix_add_check_for_sandbox_then_result.patch +moves_background_color_setter_of_webview_to_blinks_webprefs_logic.patch diff --git a/patches/chromium/moves_background_color_setter_of_webview_to_blinks_webprefs_logic.patch b/patches/chromium/moves_background_color_setter_of_webview_to_blinks_webprefs_logic.patch new file mode 100644 index 000000000000..25eb6020d391 --- /dev/null +++ b/patches/chromium/moves_background_color_setter_of_webview_to_blinks_webprefs_logic.patch @@ -0,0 +1,40 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: Samuel Attard +Date: Mon, 8 Mar 2021 16:27:39 -0800 +Subject: moves background_color setter of WebView to blinks webprefs logic + +background_color can be updated at runtime, as such we need to apply the +new background color to the WebView in the ApplyPreferences method. +There is no current way to attach an observer to these prefs so patching +is our only option. + +Ideally we could add an embedder observer pattern here but that can be +done in future work. + +diff --git a/third_party/blink/renderer/core/exported/web_view_impl.cc b/third_party/blink/renderer/core/exported/web_view_impl.cc +index fb994344ea53e17df84f176ad7d8722793b72cfd..fb3d42378d1ec2611c3c2055f96380e3a9873847 100644 +--- a/third_party/blink/renderer/core/exported/web_view_impl.cc ++++ b/third_party/blink/renderer/core/exported/web_view_impl.cc +@@ -154,6 +154,7 @@ + #include "third_party/blink/renderer/core/timing/dom_window_performance.h" + #include "third_party/blink/renderer/core/timing/window_performance.h" + #include "third_party/blink/renderer/platform/fonts/font_cache.h" ++#include "third_party/blink/renderer/platform/graphics/color.h" + #include "third_party/blink/renderer/platform/graphics/image.h" + #include "third_party/blink/renderer/platform/graphics/paint/cull_rect.h" + #include "third_party/blink/renderer/platform/graphics/paint/paint_record_builder.h" +@@ -1763,6 +1764,14 @@ void WebView::ApplyWebPreferences(const web_pref::WebPreferences& prefs, + + RuntimeEnabledFeatures::SetTranslateServiceEnabled( + prefs.translate_service_available); ++ ++ SkColor color = SK_ColorTRANSPARENT; ++ if (!prefs.guest_instance_id) { // not inside electron tag, which is always transparent. ++ Color blink_color; ++ if (blink_color.SetFromString(WebString::FromASCII(prefs.background_color))) ++ color = static_cast(blink_color); ++ } ++ web_view->SetBaseBackgroundColor(color); + } + + void WebViewImpl::ThemeChanged() { diff --git a/shell/browser/api/electron_api_web_contents.cc b/shell/browser/api/electron_api_web_contents.cc index 5d8395996c3e..aa9cb0fdd4a2 100644 --- a/shell/browser/api/electron_api_web_contents.cc +++ b/shell/browser/api/electron_api_web_contents.cc @@ -1057,6 +1057,16 @@ void WebContents::AddNewContents( v8::HandleScope handle_scope(isolate); auto api_web_contents = CreateAndTake(isolate, std::move(new_contents), Type::kBrowserWindow); + + // We call RenderFrameCreated here as at this point the empty "about:blank" + // render frame has already been created. If the window never navigates again + // RenderFrameCreated won't be called and certain prefs like + // "kBackgroundColor" will not be applied. + auto* frame = api_web_contents->MainFrame(); + if (frame) { + api_web_contents->HandleNewRenderFrame(frame); + } + if (Emit("-add-new-contents", api_web_contents, disposition, user_gesture, initial_rect.x(), initial_rect.y(), initial_rect.width(), initial_rect.height(), tracker->url, tracker->frame_name, @@ -1346,7 +1356,7 @@ void WebContents::BeforeUnloadFired(bool proceed, // there are two virtual functions named BeforeUnloadFired. } -void WebContents::RenderFrameCreated( +void WebContents::HandleNewRenderFrame( content::RenderFrameHost* render_frame_host) { auto* rwhv = render_frame_host->GetView(); if (!rwhv) @@ -1375,6 +1385,11 @@ void WebContents::RenderFrameCreated( WebFrameMain::RenderFrameCreated(render_frame_host); } +void WebContents::RenderFrameCreated( + content::RenderFrameHost* render_frame_host) { + HandleNewRenderFrame(render_frame_host); +} + void WebContents::RenderViewDeleted(content::RenderViewHost* render_view_host) { // This event is necessary for tracking any states with respect to // intermediate render view hosts aka speculative render view hosts. Currently diff --git a/shell/browser/api/electron_api_web_contents.h b/shell/browser/api/electron_api_web_contents.h index 9be0701cb981..1613ae2a7d5c 100644 --- a/shell/browser/api/electron_api_web_contents.h +++ b/shell/browser/api/electron_api_web_contents.h @@ -196,6 +196,7 @@ class WebContents : public gin::Wrappable, void IncrementCapturerCount(gin::Arguments* args); void DecrementCapturerCount(gin::Arguments* args); bool IsBeingCaptured(); + void HandleNewRenderFrame(content::RenderFrameHost* render_frame_host); #if BUILDFLAG(ENABLE_PRINTING) void OnGetDefaultPrinter(base::Value print_settings, diff --git a/shell/renderer/renderer_client_base.cc b/shell/renderer/renderer_client_base.cc index 9189c3f878a9..e1fde902b1d6 100644 --- a/shell/renderer/renderer_client_base.cc +++ b/shell/renderer/renderer_client_base.cc @@ -253,22 +253,6 @@ void RendererClientBase::RenderFrameCreated( // DidCreateDocumentElement event. new ElectronApiServiceImpl(render_frame, this); - content::RenderView* render_view = render_frame->GetRenderView(); - if (render_frame->IsMainFrame() && render_view) { - blink::WebView* webview = render_view->GetWebView(); - if (webview) { - auto prefs = render_frame->GetBlinkPreferences(); - if (prefs.guest_instance_id) { // webview. - webview->SetBaseBackgroundColor(SK_ColorTRANSPARENT); - } else { // normal window. - std::string name = prefs.background_color; - SkColor color = - name.empty() ? SK_ColorTRANSPARENT : ParseHexColor(name); - webview->SetBaseBackgroundColor(color); - } - } - } - #if BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS) auto* dispatcher = extensions_renderer_client_->GetDispatcher(); // ExtensionFrameHelper destroys itself when the RenderFrame is destroyed.