0

Move blink::WebView::SetBaseBackgroundColor to content

There are various embedders that directly call
WebView::SetBaseBackgroundColor for various purpose, which involves
running their own IPC on top of content and has to deal with various
lifetime complexities. Remove WebView::SetBaseBackgroundColor and add
it to WebContents, and move embedders to call WebContents instead.

Bug: 1138800, 1204793
Change-Id: I41f485ef653af8d47f73c76c0b3658b3dc9ceb37
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2695928
Reviewed-by: Devlin <rdevlin.cronin@chromium.org>
Reviewed-by: danakj <danakj@chromium.org>
Reviewed-by: Lei Zhang <thestig@chromium.org>
Reviewed-by: Tommy Li <tommycli@chromium.org>
Reviewed-by: Alex Moshchuk <alexmos@chromium.org>
Reviewed-by: Frank Liberato <liberato@chromium.org>
Reviewed-by: Wez <wez@chromium.org>
Reviewed-by: Daniel Cheng <dcheng@chromium.org>
Reviewed-by: Sean Topping <seantopping@chromium.org>
Commit-Queue: Bo <boliu@chromium.org>
Cr-Commit-Position: refs/heads/master@{#891867}
This commit is contained in:
Bo Liu
2021-06-12 01:40:41 +00:00
committed by Chromium LUCI CQ
parent cf56136560
commit b5e79d9bb9
34 changed files with 140 additions and 152 deletions

@ -1131,7 +1131,7 @@ void AwContents::SetBackgroundColor(JNIEnv* env,
const JavaParamRef<jobject>& obj,
jint color) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
render_view_host_ext_->SetBackgroundColor(color);
web_contents_->SetPageBaseBackgroundColor(color);
}
void AwContents::ZoomBy(JNIEnv* env,

@ -56,7 +56,6 @@ AwRenderViewHostExt::AwRenderViewHostExt(AwRenderViewHostExtClient* client,
content::WebContents* contents)
: content::WebContentsObserver(contents),
client_(client),
background_color_(SK_ColorWHITE),
has_new_hit_test_data_(false),
frame_host_receivers_(contents, this) {
DCHECK(client_);
@ -124,15 +123,6 @@ void AwRenderViewHostExt::SetInitialPageScale(double page_scale_factor) {
local_main_frame_remote_->SetInitialPageScale(page_scale_factor);
}
void AwRenderViewHostExt::SetBackgroundColor(SkColor c) {
if (background_color_ == c)
return;
background_color_ = c;
if (local_main_frame_remote_) {
local_main_frame_remote_->SetBackgroundColor(background_color_);
}
}
void AwRenderViewHostExt::SetWillSuppressErrorPage(bool suppress) {
will_suppress_error_page_ = suppress;
}
@ -149,8 +139,6 @@ void AwRenderViewHostExt::ResetLocalMainFrameRemote(
local_main_frame_remote_.reset();
frame_host->GetRemoteAssociatedInterfaces()->GetInterface(
local_main_frame_remote_.BindNewEndpointAndPassReceiver());
local_main_frame_remote_->SetBackgroundColor(background_color_);
}
void AwRenderViewHostExt::RenderFrameCreated(

@ -70,7 +70,6 @@ class AwRenderViewHostExt : public content::WebContentsObserver,
// Sets the initial page scale. This overrides initial scale set by
// the meta viewport tag.
void SetInitialPageScale(double page_scale_factor);
void SetBackgroundColor(SkColor c);
void SetWillSuppressErrorPage(bool suppress);
void SmoothScroll(int target_x, int target_y, base::TimeDelta duration);
@ -103,8 +102,6 @@ class AwRenderViewHostExt : public content::WebContentsObserver,
AwRenderViewHostExtClient* client_;
SkColor background_color_;
// Authoritative copy of hit test data on the browser side. This is updated
// as a result of DoHitTest called explicitly or when the FocusedNodeChanged
// is called in AwRenderViewExt.

@ -79,9 +79,6 @@ struct HitTestData {
// things that webview needs from the main frame.
interface LocalMainFrame {
// Sets the base background color for this view.
SetBackgroundColor(skia.mojom.SkColor color);
// Sets the initial page scale. This overrides initial scale set by
// the meta viewport tag.
SetInitialPageScale(double page_scale_factor);

@ -286,14 +286,6 @@ void AwRenderFrameExt::HitTest(const gfx::PointF& touch_center,
GetFrameHost()->UpdateHitTestData(std::move(data));
}
void AwRenderFrameExt::SetBackgroundColor(SkColor c) {
blink::WebView* webview = GetWebView();
if (!webview)
return;
webview->SetBaseBackgroundColor(c);
}
void AwRenderFrameExt::SetInitialPageScale(double page_scale_factor) {
blink::WebView* webview = GetWebView();
if (!webview)

@ -48,7 +48,6 @@ class AwRenderFrameExt : public content::RenderFrameObserver,
void OnDestruct() override;
// mojom::LocalMainFrame overrides:
void SetBackgroundColor(SkColor c) override;
void SetInitialPageScale(double page_scale_factor) override;
void SetTextZoomFactor(float zoom_factor) override;
void HitTest(const gfx::PointF& touch_center,

@ -170,6 +170,9 @@ CastWebContentsImpl::CastWebContentsImpl(content::WebContents* web_contents,
web_contents_->GetMutableRendererPrefs()
->webrtc_allow_legacy_tls_protocols = true;
}
web_contents_->SetPageBaseBackgroundColor(chromecast::GetSwitchValueColor(
switches::kCastAppBackgroundColor, SK_ColorBLACK));
}
CastWebContentsImpl::~CastWebContentsImpl() {

@ -163,8 +163,6 @@ void CastContentRendererClient::RenderThreadStarted() {
}
void CastContentRendererClient::WebViewCreated(blink::WebView* webview) {
webview->SetBaseBackgroundColor(chromecast::GetSwitchValueColor(
switches::kCastAppBackgroundColor, SK_ColorBLACK));
// Disable application cache as Chromecast doesn't support off-line
// application running.
webview->GetSettings()->SetOfflineWebApplicationCacheEnabled(false);

@ -274,7 +274,8 @@ WebViewPlugin::WebViewHelper::WebViewHelper(
/*widgets_never_composited=*/false,
/*opener=*/nullptr, mojo::NullAssociatedReceiver(),
*agent_group_scheduler_,
/*session_storage_namespace_id=*/base::EmptyString());
/*session_storage_namespace_id=*/base::EmptyString(),
/*page_base_background_color=*/absl::nullopt);
// ApplyWebPreferences before making a WebLocalFrame so that the frame sees a
// consistent view of our preferences.
blink::WebView::ApplyWebPreferences(parent_web_preferences, web_view_);

@ -672,7 +672,8 @@ void PrintRenderFrameHelper::PrintHeaderAndFooter(
/*compositing_enabled=*/false, /*widgets_never_composited=*/false,
/*opener=*/nullptr, mojo::NullAssociatedReceiver(),
*source_frame.GetAgentGroupScheduler(),
/*session_storage_namespace_id=*/base::EmptyString());
/*session_storage_namespace_id=*/base::EmptyString(),
/*page_base_background_color=*/absl::nullopt);
web_view->GetSettings()->SetJavaScriptEnabled(true);
class HeaderAndFooterClient final : public blink::WebLocalFrameClient {
@ -951,7 +952,8 @@ void PrepareFrameAndViewForPrint::CopySelection(
/*widgets_never_composited=*/false,
/*opener=*/nullptr, mojo::NullAssociatedReceiver(),
agent_group_scheduler_,
/*session_storage_namespace_id=*/base::EmptyString());
/*session_storage_namespace_id=*/base::EmptyString(),
/*page_base_background_color=*/absl::nullopt);
blink::WebView::ApplyWebPreferences(prefs, web_view);
blink::WebLocalFrame* main_frame = blink::WebLocalFrame::CreateMainFrame(
web_view, this, nullptr, blink::LocalFrameToken(), nullptr);

@ -120,6 +120,10 @@ class MockPageBroadcast : public blink::mojom::PageBroadcast {
SetHistoryOffsetAndLength,
(int32_t offset, int32_t length),
(override));
MOCK_METHOD(void,
SetPageBaseBackgroundColor,
(absl::optional<SkColor> color),
(override));
mojo::PendingAssociatedRemote<blink::mojom::PageBroadcast> GetRemote() {
return receiver_.BindNewEndpointAndPassDedicatedRemote();

@ -39,4 +39,8 @@ bool RenderViewHostDelegate::IsPortal() {
return false;
}
absl::optional<SkColor> RenderViewHostDelegate::GetBaseBackgroundColor() {
return absl::nullopt;
}
} // namespace content

@ -13,6 +13,8 @@
#include "content/common/content_export.h"
#include "mojo/public/cpp/bindings/pending_remote.h"
#include "net/base/load_states.h"
#include "third_party/abseil-cpp/absl/types/optional.h"
#include "third_party/skia/include/core/SkColor.h"
namespace blink {
namespace web_pref {
@ -143,6 +145,11 @@ class CONTENT_EXPORT RenderViewHostDelegate {
// changed.
virtual void OnBackgroundColorChanged(RenderViewHostImpl* source) {}
// Called on RenderView creation to get the initial base background color
// for this RenderView. Nullopt means a color is not set, and the blink
// default color should be used.
virtual absl::optional<SkColor> GetBaseBackgroundColor();
protected:
virtual ~RenderViewHostDelegate() {}
};

@ -476,6 +476,7 @@ bool RenderViewHostImpl::CreateRenderView(
params->hidden = frame_tree_->delegate()->IsHidden();
params->never_composited = delegate_->IsNeverComposited();
params->window_was_created_with_opener = window_was_created_with_opener;
params->base_background_color = delegate_->GetBaseBackgroundColor();
bool is_portal = delegate_->IsPortal();
bool is_guest_view = delegate_->IsGuest();

@ -1502,6 +1502,21 @@ absl::optional<SkColor> WebContentsImpl::GetBackgroundColor() {
return GetRenderViewHost()->background_color();
}
void WebContentsImpl::SetPageBaseBackgroundColor(
absl::optional<SkColor> color) {
if (page_base_background_color_ == color)
return;
page_base_background_color_ = color;
ExecutePageBroadcastMethod(base::BindRepeating(
[](absl::optional<SkColor> color, RenderViewHostImpl* rvh) {
// Null `broadcast` can happen before view is created on the renderer
// side, in which case this color will be sent in CreateView.
if (auto& broadcast = rvh->GetAssociatedPageBroadcast())
broadcast->SetPageBaseBackgroundColor(color);
},
page_base_background_color_));
}
void WebContentsImpl::SetAccessibilityMode(ui::AXMode mode) {
OPTIONAL_TRACE_EVENT2("content", "WebContentsImpl::SetAccessibilityMode",
"mode", mode.ToString(), "previous_mode",
@ -5858,6 +5873,10 @@ void WebContentsImpl::RecomputeWebPreferencesSlow() {
OnWebPreferencesChanged();
}
absl::optional<SkColor> WebContentsImpl::GetBaseBackgroundColor() {
return page_base_background_color_;
}
void WebContentsImpl::PrintCrossProcessSubframe(
const gfx::Rect& rect,
int document_cookie,

@ -364,6 +364,7 @@ class CONTENT_EXPORT WebContentsImpl : public WebContents,
void ClosePage() override;
absl::optional<SkColor> GetThemeColor() override;
absl::optional<SkColor> GetBackgroundColor() override;
void SetPageBaseBackgroundColor(absl::optional<SkColor> color) override;
WebUI* GetWebUI() override;
WebUI* GetCommittedWebUI() override;
void SetUserAgentOverride(const blink::UserAgentOverride& ua_override,
@ -858,6 +859,7 @@ class CONTENT_EXPORT WebContentsImpl : public WebContents,
void OnThemeColorChanged(RenderViewHostImpl* source) override;
void OnBackgroundColorChanged(RenderViewHostImpl* source) override;
void RecomputeWebPreferencesSlow() override;
absl::optional<SkColor> GetBaseBackgroundColor() override;
// NavigatorDelegate ---------------------------------------------------------
@ -2208,6 +2210,12 @@ class CONTENT_EXPORT WebContentsImpl : public WebContents,
// information to apps that capture it.
blink::mojom::CaptureHandleConfig capture_handle_config_;
// Background color of the page set by the embedder to be passed to all
// renderers attached to this WebContents, for use in the main frame.
// It is used when the page has not loaded enough to know a background
// color or if the page does not set a background color.
absl::optional<SkColor> page_base_background_color_;
base::WeakPtrFactory<WebContentsImpl> loading_weak_factory_{this};
base::WeakPtrFactory<WebContentsImpl> weak_factory_{this};

@ -17,6 +17,7 @@ import "mojo/public/mojom/base/string16.mojom";
import "mojo/public/mojom/base/time.mojom";
import "mojo/public/mojom/base/unguessable_token.mojom";
import "mojo/public/mojom/base/values.mojom";
import "skia/public/mojom/skcolor.mojom";
import "services/network/public/mojom/content_security_policy.mojom";
import "services/network/public/mojom/url_loader.mojom";
import "services/network/public/mojom/url_loader_factory.mojom";
@ -127,6 +128,9 @@ struct CreateViewParams {
// Endpoint for any messages that are broadcast to all views in a WebContents.
pending_associated_receiver<blink.mojom.PageBroadcast> blink_page_broadcast;
// Base background color of this view. Only used by a local main frame.
skia.mojom.SkColor? base_background_color;
};
// A union to distinguish between parameters specific to local main frame

@ -413,6 +413,19 @@ class WebContents : public PageNavigator,
// any.
virtual absl::optional<SkColor> GetBackgroundColor() = 0;
// Sets the renderer-side default background color of the page. This is used
// when the page has not loaded enough to know a background color or if the
// page does not set a background color.
// Pass in nullopt to reset back to the default.
// Note there are situations where the base background color is not used, such
// as fullscreen.
// Note currently this is sent directly to the renderer, so does not interact
// directly with `RenderWidgetHostView::SetBackgroundColor`. There is pending
// refactor to remove `RenderWidgetHostView::SetBackgroundColor` and merge its
// functionality here, which will be more consistent and simpler to
// understand.
virtual void SetPageBaseBackgroundColor(absl::optional<SkColor> color) = 0;
// Returns the committed WebUI if one exists, otherwise the pending one.
virtual WebUI* GetWebUI() = 0;
virtual WebUI* GetCommittedWebUI() = 0;

@ -120,7 +120,7 @@ void RenderViewImpl::Initialize(
opener_frame ? opener_frame->View() : nullptr,
std::move(params->blink_page_broadcast),
agent_scheduling_group_.agent_group_scheduler(),
params->session_storage_namespace_id);
params->session_storage_namespace_id, params->base_background_color);
g_view_map.Get().insert(std::make_pair(GetWebView(), this));
g_routing_id_view_map.Get().insert(std::make_pair(GetRoutingID(), this));

@ -27,7 +27,8 @@ ScopedWebFrame::ScopedWebFrame()
/*opener=*/nullptr,
mojo::NullAssociatedReceiver(),
*agent_group_scheduler_,
/*session_storage_namespace_id=*/base::EmptyString())),
/*session_storage_namespace_id=*/base::EmptyString(),
/*page_base_background_color=*/absl::nullopt)),
frame_(blink::WebLocalFrame::CreateMainFrame(view_,
&frame_client_,
nullptr,

@ -293,6 +293,7 @@ FrameImpl::FrameImpl(std::unique_ptr<content::WebContents> web_contents,
WebContentsToFrameImplMap()[web_contents_.get()] = this;
web_contents_->SetDelegate(this);
web_contents_->SetPageBaseBackgroundColor(SK_AlphaTRANSPARENT);
Observe(web_contents_.get());
binding_.set_error_handler([this](zx_status_t status) {

@ -137,13 +137,6 @@ void WebEngineContentRendererClient::RenderThreadStarted() {
void WebEngineContentRendererClient::RenderFrameCreated(
content::RenderFrame* render_frame) {
// If this is a top-level frame then it should have a transparent background.
// Both the RenderView and WebView should be guaranteed to be non-null, since
// the |render_frame| was only just created.
if (render_frame->IsMainFrame()) {
render_frame->GetWebView()->SetBaseBackgroundColor(SK_AlphaTRANSPARENT);
}
// Add WebEngine services to the new RenderFrame.
// The objects' lifetimes are bound to the RenderFrame's lifetime.
new on_load_script_injector::OnLoadScriptInjector(render_frame);

@ -326,7 +326,8 @@ class WebMediaPlayerImplTest
/*opener=*/nullptr,
mojo::NullAssociatedReceiver(),
*agent_group_scheduler,
/*session_storage_namespace_id=*/base::EmptyString())),
/*session_storage_namespace_id=*/base::EmptyString(),
/*page_base_background_color=*/absl::nullopt)),
web_local_frame_(
blink::WebLocalFrame::CreateMainFrame(web_view_,
&web_frame_client_,

@ -4,6 +4,7 @@
module blink.mojom;
import "mojo/public/mojom/base/time.mojom";
import "skia/public/mojom/skcolor.mojom";
import "third_party/blink/public/mojom/page/page_visibility_state.mojom";
import "third_party/blink/public/mojom/webpreferences/web_preferences.mojom";
import "third_party/blink/public/mojom/renderer_preferences.mojom";
@ -73,4 +74,7 @@ interface PageBroadcast {
// Set history offset and length.
SetHistoryOffsetAndLength(int32 offset, int32 length);
// Sent to whole page, but should only be used by the main frame.
SetPageBaseBackgroundColor(skia.mojom.SkColor? color);
};

@ -13,6 +13,7 @@ import "services/viz/public/mojom/compositing/compositor_frame_sink.mojom";
import "services/viz/public/mojom/compositing/local_surface_id.mojom";
import "services/viz/public/mojom/hit_test/input_target_client.mojom";
import "skia/public/mojom/bitmap.mojom";
import "skia/public/mojom/skcolor.mojom";
import "third_party/blink/public/mojom/frame/intrinsic_sizing_info.mojom";
import "third_party/blink/public/mojom/frame/viewport_intersection_state.mojom";
import "third_party/blink/public/mojom/input/input_handler.mojom";

@ -114,6 +114,9 @@ class WebView {
// to inform blink it is in the foreground or background. Whereas this bit
// refers to user-visibility and whether the tab needs to produce pixels to
// put on the screen at some point or not.
// |page_base_background_color| initial base background color used by the main
// frame. Set on create to avoid races. Passing in nullopt indicates the
// default base background color should be used.
// TODO(yuzus): Remove |is_hidden| and start using |PageVisibilityState|.
BLINK_EXPORT static WebView* Create(
WebViewClient*,
@ -125,7 +128,8 @@ class WebView {
CrossVariantMojoAssociatedReceiver<mojom::PageBroadcastInterfaceBase>
page_handle,
scheduler::WebAgentGroupScheduler& agent_group_scheduler,
const SessionStorageNamespaceId& session_storage_namespace_id);
const SessionStorageNamespaceId& session_storage_namespace_id,
absl::optional<SkColor> page_base_background_color);
// Destroys the WebView.
virtual void Close() = 0;
@ -353,11 +357,6 @@ class WebView {
// Custom colors -------------------------------------------------------
// Sets the default background color when the page has not loaded enough to
// know a background colour. This can be overridden by the methods below as
// well.
virtual void SetBaseBackgroundColor(SkColor) {}
virtual void SetDeviceColorSpaceForTesting(
const gfx::ColorSpace& color_space) = 0;

@ -464,14 +464,16 @@ WebView* WebView::Create(
CrossVariantMojoAssociatedReceiver<mojom::PageBroadcastInterfaceBase>
page_handle,
scheduler::WebAgentGroupScheduler& agent_group_scheduler,
const SessionStorageNamespaceId& session_storage_namespace_id) {
const SessionStorageNamespaceId& session_storage_namespace_id,
absl::optional<SkColor> page_base_background_color) {
return WebViewImpl::Create(
client,
is_hidden ? mojom::blink::PageVisibilityState::kHidden
: mojom::blink::PageVisibilityState::kVisible,
is_inside_portal, compositing_enabled, widgets_never_composited,
static_cast<WebViewImpl*>(opener), std::move(page_handle),
agent_group_scheduler, session_storage_namespace_id);
agent_group_scheduler, session_storage_namespace_id,
std::move(page_base_background_color));
}
WebViewImpl* WebViewImpl::Create(
@ -483,13 +485,15 @@ WebViewImpl* WebViewImpl::Create(
WebViewImpl* opener,
mojo::PendingAssociatedReceiver<mojom::blink::PageBroadcast> page_handle,
blink::scheduler::WebAgentGroupScheduler& agent_group_scheduler,
const SessionStorageNamespaceId& session_storage_namespace_id) {
const SessionStorageNamespaceId& session_storage_namespace_id,
absl::optional<SkColor> page_base_background_color) {
// Take a self-reference for WebViewImpl that is released by calling Close(),
// then return a raw pointer to the caller.
auto web_view = base::AdoptRef(
new WebViewImpl(client, visibility, is_inside_portal, compositing_enabled,
widgets_never_composited, opener, std::move(page_handle),
agent_group_scheduler, session_storage_namespace_id));
agent_group_scheduler, session_storage_namespace_id,
std::move(page_base_background_color)));
web_view->AddRef();
return web_view.get();
}
@ -553,7 +557,8 @@ WebViewImpl::WebViewImpl(
WebViewImpl* opener,
mojo::PendingAssociatedReceiver<mojom::blink::PageBroadcast> page_handle,
blink::scheduler::WebAgentGroupScheduler& agent_group_scheduler,
const SessionStorageNamespaceId& session_storage_namespace_id)
const SessionStorageNamespaceId& session_storage_namespace_id,
absl::optional<SkColor> page_base_background_color)
: widgets_never_composited_(widgets_never_composited),
web_view_client_(client),
chrome_client_(MakeGarbageCollected<ChromeClientImpl>(this)),
@ -561,6 +566,8 @@ WebViewImpl::WebViewImpl(
maximum_zoom_level_(PageZoomFactorToZoomLevel(kMaximumPageZoomFactor)),
does_composite_(does_composite),
fullscreen_controller_(std::make_unique<FullscreenController>(this)),
page_base_background_color_(
page_base_background_color.value_or(SK_ColorWHITE)),
receiver_(this,
std::move(page_handle),
agent_group_scheduler.DefaultTaskRunner()),
@ -3042,14 +3049,17 @@ Color WebViewImpl::BaseBackgroundColor() const {
return SK_ColorTRANSPARENT;
if (base_background_color_override_for_inspector_)
return base_background_color_override_for_inspector_.value();
return base_background_color_;
// Use the page background color if this is the WebView of the main frame.
if (MainFrameImpl())
return page_base_background_color_;
return Color::kWhite;
}
void WebViewImpl::SetBaseBackgroundColor(SkColor color) {
if (base_background_color_ == color)
void WebViewImpl::SetPageBaseBackgroundColor(absl::optional<SkColor> color) {
SkColor new_color = color.value_or(SK_ColorWHITE);
if (page_base_background_color_ == new_color)
return;
base_background_color_ = color;
page_base_background_color_ = new_color;
UpdateBaseBackgroundColor();
}

@ -120,7 +120,8 @@ class CORE_EXPORT WebViewImpl final : public WebView,
WebViewImpl* opener,
mojo::PendingAssociatedReceiver<mojom::blink::PageBroadcast> page_handle,
scheduler::WebAgentGroupScheduler& agent_group_scheduler,
const SessionStorageNamespaceId& session_storage_namespace_id);
const SessionStorageNamespaceId& session_storage_namespace_id,
absl::optional<SkColor> page_base_background_color);
// All calls to Create() should be balanced with a call to Close(). This
// synchronously destroys the WebViewImpl.
@ -198,7 +199,6 @@ class CORE_EXPORT WebViewImpl final : public WebView,
WebPagePopupImpl* GetPagePopup() const override { return page_popup_.get(); }
void SetPageFrozen(bool frozen) override;
WebFrameWidget* MainFrameWidget() override;
void SetBaseBackgroundColor(SkColor) override;
void SetDeviceColorSpaceForTesting(
const gfx::ColorSpace& color_space) override;
void PaintContent(cc::PaintCanvas*, const gfx::Rect&) override;
@ -284,6 +284,7 @@ class CORE_EXPORT WebViewImpl final : public WebView,
const RendererPreferences& preferences) override;
void SetHistoryOffsetAndLength(int32_t history_offset,
int32_t history_length) override;
void SetPageBaseBackgroundColor(absl::optional<SkColor> color) override;
void DispatchPageshow(base::TimeTicks navigation_start);
void DispatchPagehide(mojom::blink::PagehideDispatch pagehide_dispatch);
@ -637,7 +638,8 @@ class CORE_EXPORT WebViewImpl final : public WebView,
WebViewImpl* opener,
mojo::PendingAssociatedReceiver<mojom::blink::PageBroadcast> page_handle,
scheduler::WebAgentGroupScheduler& agent_group_scheduler,
const SessionStorageNamespaceId& session_storage_namespace_id);
const SessionStorageNamespaceId& session_storage_namespace_id,
absl::optional<SkColor> page_base_background_color);
~WebViewImpl() override;
void ConfigureAutoResizeMode();
@ -825,7 +827,7 @@ class CORE_EXPORT WebViewImpl final : public WebView,
absl::optional<SkColor> background_color_override_for_fullscreen_controller_;
bool override_base_background_color_to_transparent_ = false;
absl::optional<SkColor> base_background_color_override_for_inspector_;
SkColor base_background_color_ = Color::kWhite;
SkColor page_base_background_color_; // Only applies to main frame.
float zoom_factor_override_ = 0.f;

@ -433,7 +433,7 @@ TEST_F(WebViewTest, SetBaseBackgroundColor) {
WebViewImpl* web_view = web_view_helper_.Initialize();
EXPECT_EQ(SK_ColorWHITE, web_view->BackgroundColor());
web_view->SetBaseBackgroundColor(SK_ColorBLUE);
web_view->SetPageBaseBackgroundColor(SK_ColorBLUE);
EXPECT_EQ(SK_ColorBLUE, web_view->BackgroundColor());
WebURL base_url = url_test_helpers::ToKURL("http://example.com/");
@ -453,11 +453,11 @@ TEST_F(WebViewTest, SetBaseBackgroundColor) {
// Expected: red (50% alpha) blended atop base of SK_ColorBLUE.
EXPECT_EQ(0xFF80007F, web_view->BackgroundColor());
web_view->SetBaseBackgroundColor(kTranslucentPutty);
web_view->SetPageBaseBackgroundColor(kTranslucentPutty);
// Expected: red (50% alpha) blended atop kTranslucentPutty. Note the alpha.
EXPECT_EQ(0xBFE93A31, web_view->BackgroundColor());
web_view->SetBaseBackgroundColor(SK_ColorTRANSPARENT);
web_view->SetPageBaseBackgroundColor(SK_ColorTRANSPARENT);
frame_test_helpers::LoadHTMLString(web_view->MainFrameImpl(),
"<html><head><style>body "
"{background-color:transparent}</style></"
@ -494,13 +494,15 @@ TEST_F(WebViewTest, SetBaseBackgroundColorBeforeMainFrame) {
/*widgets_never_composited=*/false,
/*opener=*/nullptr, mojo::NullAssociatedReceiver(),
web_view_helper_.GetAgentGroupScheduler(),
/*session_storage_namespace_id=*/base::EmptyString()));
/*session_storage_namespace_id=*/base::EmptyString(),
/*page_base_background_color=*/absl::nullopt));
EXPECT_NE(SK_ColorBLUE, web_view->BackgroundColor());
// WebView does not have a frame yet, but we should still be able to set the
// background color.
web_view->SetBaseBackgroundColor(SK_ColorBLUE);
EXPECT_EQ(SK_ColorBLUE, web_view->BackgroundColor());
// WebView does not have a frame yet; while it's possible to set the page
// background color, it won't have any effect until a local main frame is
// attached.
web_view->SetPageBaseBackgroundColor(SK_ColorBLUE);
EXPECT_NE(SK_ColorBLUE, web_view->BackgroundColor());
frame_test_helpers::TestWebFrameClient web_frame_client;
WebLocalFrame* frame = WebLocalFrame::CreateMainFrame(
@ -528,7 +530,7 @@ TEST_F(WebViewTest, SetBaseBackgroundColorAndBlendWithExistingContent) {
WebViewImpl* web_view = web_view_helper_.Initialize();
// Set WebView background to green with alpha.
web_view->SetBaseBackgroundColor(kAlphaGreen);
web_view->SetPageBaseBackgroundColor(kAlphaGreen);
web_view->GetSettings()->SetShouldClearDocumentBackground(false);
web_view->MainFrameViewWidget()->Resize(gfx::Size(kWidth, kHeight));
UpdateAllLifecyclePhases();
@ -569,7 +571,7 @@ TEST_F(WebViewTest, SetBaseBackgroundColorWithColorScheme) {
ColorSchemeHelper color_scheme_helper(*(web_view->GetPage()));
color_scheme_helper.SetPreferredColorScheme(
mojom::blink::PreferredColorScheme::kLight);
web_view->SetBaseBackgroundColor(SK_ColorBLUE);
web_view->SetPageBaseBackgroundColor(SK_ColorBLUE);
WebURL base_url = url_test_helpers::ToKURL("http://example.com/");
frame_test_helpers::LoadHTMLString(
@ -586,9 +588,9 @@ TEST_F(WebViewTest, SetBaseBackgroundColorWithColorScheme) {
EXPECT_EQ(Color(0x12, 0x12, 0x12), frame_view->BaseBackgroundColor());
// Don't let dark color-scheme override a transparent background.
web_view->SetBaseBackgroundColor(SK_ColorTRANSPARENT);
web_view->SetPageBaseBackgroundColor(SK_ColorTRANSPARENT);
EXPECT_EQ(Color::kTransparent, frame_view->BaseBackgroundColor());
web_view->SetBaseBackgroundColor(SK_ColorBLUE);
web_view->SetPageBaseBackgroundColor(SK_ColorBLUE);
EXPECT_EQ(Color(0x12, 0x12, 0x12), frame_view->BaseBackgroundColor());
color_scheme_helper.SetForcedColors(*(web_view->GetPage()),
@ -2740,7 +2742,8 @@ TEST_F(WebViewTest, ClientTapHandlingNullWebViewClient) {
/*widgets_never_composited=*/false,
/*opener=*/nullptr, mojo::NullAssociatedReceiver(),
web_view_helper_.GetAgentGroupScheduler(),
/*session_storage_namespace_id=*/base::EmptyString()));
/*session_storage_namespace_id=*/base::EmptyString(),
/*page_base_background_color=*/absl::nullopt));
frame_test_helpers::TestWebFrameClient web_frame_client;
WebLocalFrame* local_frame = WebLocalFrame::CreateMainFrame(
web_view, &web_frame_client, nullptr, LocalFrameToken(), nullptr);

@ -620,7 +620,8 @@ void WebViewHelper::InitializeWebView(TestWebViewClient* web_view_client,
/*widgets_never_composited=*/false,
/*opener=*/opener, mojo::NullAssociatedReceiver(),
*agent_group_scheduler_,
/*session_storage_namespace_id=*/base::EmptyString()));
/*session_storage_namespace_id=*/base::EmptyString(),
/*page_base_background_color=*/absl::nullopt));
// This property must be set at initialization time, it is not supported to be
// changed afterward, and does nothing.
web_view_->GetSettings()->SetViewportEnabled(viewport_enabled_);

@ -449,7 +449,7 @@ TEST_P(ScrollbarsTest, TransparentBackgroundUsesDarkOverlayColorTheme) {
ENABLE_OVERLAY_SCROLLBARS(true);
WebView().MainFrameViewWidget()->Resize(gfx::Size(800, 600));
WebView().SetBaseBackgroundColor(SK_ColorTRANSPARENT);
WebView().SetPageBaseBackgroundColor(SK_ColorTRANSPARENT);
SimRequest request("https://example.com/test.html", "text/html");
LoadURL("https://example.com/test.html");
request.Complete(R"HTML(

@ -26,7 +26,7 @@ class AutoscrollControllerTest : public SimTest {
TEST_F(AutoscrollControllerTest,
CrashWhenLayoutStopAnimationBeforeScheduleAnimation) {
WebView().MainFrameViewWidget()->Resize(gfx::Size(800, 600));
WebView().SetBaseBackgroundColor(SK_ColorTRANSPARENT);
WebView().SetPageBaseBackgroundColor(SK_ColorTRANSPARENT);
SimRequest request("https://example.com/test.html", "text/html");
LoadURL("https://example.com/test.html");
request.Complete(R"HTML(

@ -148,7 +148,8 @@ class MAYBE_WebRtcAudioRendererTest : public testing::Test {
/*opener=*/nullptr,
mojo::NullAssociatedReceiver(),
*agent_group_scheduler_,
/*session_storage_namespace_id=*/base::EmptyString())),
/*session_storage_namespace_id=*/base::EmptyString(),
/*page_base_background_color=*/absl::nullopt)),
web_local_frame_(
blink::WebLocalFrame::CreateMainFrame(web_view_,
&web_local_frame_client_,

@ -24,6 +24,7 @@
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_contents_observer.h"
#include "content/public/browser/web_contents_user_data.h"
#include "third_party/abseil-cpp/absl/types/optional.h"
#include "ui/android/resources/resource_manager.h"
#include "ui/android/view_android.h"
#include "ui/android/window_android.h"
@ -36,67 +37,6 @@ using base::android::ScopedJavaLocalRef;
namespace weblayer {
namespace {
// Setting the page's various background color is convoluted and brittle and
// buggy. This is code inspired from chromeos views and should be considered
// temporary until content API can be fixed to be more robust. This is
// effectively only passing 1 bit of information, whether the background color
// is fully transparent or not, as the actual color isn't used by anything.
class WebContentsSetBackgroundColor
: public content::WebContentsObserver,
public content::WebContentsUserData<WebContentsSetBackgroundColor> {
public:
static void Set(content::WebContents* web_contents,
SkColor background_color) {
if (auto* set = FromWebContents(web_contents)) {
set->SetBackgroundColor(background_color);
return;
}
// SupportsUserData::Data takes ownership over the
// WebContentsSetBackgroundColor instance and will destroy it when the
// WebContents instance is destroyed.
web_contents->SetUserData(
UserDataKey(), base::WrapUnique(new WebContentsSetBackgroundColor(
web_contents, background_color)));
}
~WebContentsSetBackgroundColor() override = default;
private:
friend class content::WebContentsUserData<WebContentsSetBackgroundColor>;
WebContentsSetBackgroundColor(content::WebContents* web_contents,
SkColor color)
: content::WebContentsObserver(web_contents), color_(color) {}
// content::WebContentsObserver:
void RenderViewHostChanged(content::RenderViewHost* old_host,
content::RenderViewHost* new_host) override {
new_host->GetWidget()->GetView()->SetBackgroundColor(color_);
}
void SetBackgroundColor(SkColor background_color) {
if (color_ == background_color)
return;
color_ = background_color;
web_contents()
->GetRenderViewHost()
->GetWidget()
->GetView()
->SetBackgroundColor(color_);
}
SkColor color_;
WEB_CONTENTS_USER_DATA_KEY_DECL();
};
WEB_CONTENTS_USER_DATA_KEY_IMPL(WebContentsSetBackgroundColor)
} // namespace
ContentViewRenderView::ContentViewRenderView(JNIEnv* env,
jobject obj,
gfx::NativeWindow root_window)
@ -136,11 +76,8 @@ void ContentViewRenderView::SetCurrentWebContents(
InitCompositor();
content::WebContents* web_contents =
content::WebContents::FromJavaWebContents(jweb_contents);
if (web_contents_) {
WebContentsSetBackgroundColor::Set(
web_contents_,
Java_ContentViewRenderView_getBackgroundColor(env, java_obj_));
}
if (web_contents_)
web_contents_->SetPageBaseBackgroundColor(absl::nullopt);
if (web_contents_layer_)
web_contents_layer_->RemoveFromParent();
@ -271,12 +208,9 @@ void ContentViewRenderView::InitCompositor() {
void ContentViewRenderView::UpdateWebContentsBaseBackgroundColor() {
if (!web_contents_)
return;
JNIEnv* env = base::android::AttachCurrentThread();
WebContentsSetBackgroundColor::Set(
web_contents_,
use_transparent_background_
? SK_ColorTRANSPARENT
: Java_ContentViewRenderView_getBackgroundColor(env, java_obj_));
web_contents_->SetPageBaseBackgroundColor(
use_transparent_background_ ? absl::make_optional(SK_ColorTRANSPARENT)
: absl::nullopt);
}
} // namespace weblayer