Commit d14781f8 authored by Rune Lillesveen's avatar Rune Lillesveen Committed by Commit Bot

Send color-scheme background to browser side widget.

Take color-scheme meta tags into account and set the content background
color for the page to the dark background before we start committing
frames for the document. This way we can avoid white flashes navigating
between dark schemed pages if there are e.g. slow render blocking
stylesheets blocking rendering.

Bug: 1103667
Change-Id: I1ed110bf652dd4936bf6669e6b9cd634c0f73b0a
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2438532Reviewed-by: default avatarBo <boliu@chromium.org>
Reviewed-by: default avatarAvi Drissman <avi@chromium.org>
Commit-Queue: Rune Lillesveen <futhark@chromium.org>
Cr-Commit-Position: refs/heads/master@{#814347}
parent cf3da5fd
...@@ -64,8 +64,7 @@ public class AwContentsRenderTest { ...@@ -64,8 +64,7 @@ public class AwContentsRenderTest {
mActivityTestRule.loadUrlSync(mAwContents, mContentsClient.getOnPageFinishedHelper(), mActivityTestRule.loadUrlSync(mAwContents, mContentsClient.getOnPageFinishedHelper(),
ContentUrlConstants.ABOUT_BLANK_DISPLAY_URL); ContentUrlConstants.ABOUT_BLANK_DISPLAY_URL);
Assert.assertEquals( GraphicsTestUtils.pollForBackgroundColor(mAwContents, Color.CYAN);
Color.CYAN, GraphicsTestUtils.sampleBackgroundColorOnUiThread(mAwContents));
setBackgroundColorOnUiThread(Color.YELLOW); setBackgroundColorOnUiThread(Color.YELLOW);
GraphicsTestUtils.pollForBackgroundColor(mAwContents, Color.YELLOW); GraphicsTestUtils.pollForBackgroundColor(mAwContents, Color.YELLOW);
......
...@@ -517,12 +517,16 @@ class CONTENT_EXPORT RenderWidgetHostViewBase : public RenderWidgetHostView { ...@@ -517,12 +517,16 @@ class CONTENT_EXPORT RenderWidgetHostViewBase : public RenderWidgetHostView {
void reset_is_evicted() { is_evicted_ = false; } void reset_is_evicted() { is_evicted_ = false; }
bool is_evicted() { return is_evicted_; } bool is_evicted() { return is_evicted_; }
// SetContentBackgroundColor is called when the renderer wants to update the
// view's background color.
void SetContentBackgroundColor(SkColor color);
base::Optional<SkColor> content_background_color() const {
return content_background_color_;
}
protected: protected:
explicit RenderWidgetHostViewBase(RenderWidgetHost* host); explicit RenderWidgetHostViewBase(RenderWidgetHost* host);
// SetContentBackgroundColor is called when the render wants to update the
// view's background color.
void SetContentBackgroundColor(SkColor color);
void NotifyObserversAboutShutdown(); void NotifyObserversAboutShutdown();
virtual MouseWheelPhaseHandler* GetMouseWheelPhaseHandler(); virtual MouseWheelPhaseHandler* GetMouseWheelPhaseHandler();
......
...@@ -29,6 +29,7 @@ ...@@ -29,6 +29,7 @@
#include "content/public/test/browser_test_utils.h" #include "content/public/test/browser_test_utils.h"
#include "content/public/test/content_browser_test.h" #include "content/public/test/content_browser_test.h"
#include "content/public/test/content_browser_test_utils.h" #include "content/public/test/content_browser_test_utils.h"
#include "content/public/test/slow_http_response.h"
#include "content/public/test/test_utils.h" #include "content/public/test/test_utils.h"
#include "content/shell/browser/shell.h" #include "content/shell/browser/shell.h"
#include "content/test/did_commit_navigation_interceptor.h" #include "content/test/did_commit_navigation_interceptor.h"
...@@ -293,6 +294,55 @@ IN_PROC_BROWSER_TEST_F(NoCompositingRenderWidgetHostViewBrowserTest, ...@@ -293,6 +294,55 @@ IN_PROC_BROWSER_TEST_F(NoCompositingRenderWidgetHostViewBrowserTest,
} }
#endif // !defined(OS_MAC) #endif // !defined(OS_MAC)
namespace {
std::unique_ptr<net::test_server::HttpResponse> HandleSlowStyleSheet(
const net::test_server::HttpRequest& request) {
auto response = std::make_unique<SlowHttpResponse>(request.relative_url);
if (!response->IsHandledUrl())
return nullptr;
return std::move(response);
}
class DOMContentLoadedObserver : public WebContentsObserver {
public:
explicit DOMContentLoadedObserver(WebContents* web_contents)
: WebContentsObserver(web_contents) {}
bool Wait() {
run_loop_.Run();
return dom_content_loaded_ && !did_paint_;
}
private:
// WebContentsObserver:
void DOMContentLoaded(RenderFrameHost* render_frame_host) override {
dom_content_loaded_ = true;
run_loop_.Quit();
}
void DidFirstVisuallyNonEmptyPaint() override { did_paint_ = true; }
base::RunLoop run_loop_;
bool did_paint_{false};
bool dom_content_loaded_{false};
};
} // namespace
IN_PROC_BROWSER_TEST_F(NoCompositingRenderWidgetHostViewBrowserTest,
ColorSchemeMetaBackground) {
embedded_test_server()->RegisterRequestHandler(
base::BindRepeating(&HandleSlowStyleSheet));
ASSERT_TRUE(embedded_test_server()->Start());
DOMContentLoadedObserver observer(shell()->web_contents());
shell()->LoadURL(
embedded_test_server()->GetURL("/dark_color_scheme_meta_slow.html"));
EXPECT_TRUE(observer.Wait());
auto bg_color = GetRenderWidgetHostView()->content_background_color();
ASSERT_TRUE(bg_color.has_value());
EXPECT_EQ(SkColorSetRGB(18, 18, 18), bg_color.value());
}
IN_PROC_BROWSER_TEST_F(RenderWidgetHostViewBrowserTestBase, IN_PROC_BROWSER_TEST_F(RenderWidgetHostViewBrowserTestBase,
CompositorWorksWhenReusingRenderer) { CompositorWorksWhenReusingRenderer) {
ASSERT_TRUE(embedded_test_server()->Start()); ASSERT_TRUE(embedded_test_server()->Start());
......
...@@ -5482,12 +5482,28 @@ void WebContentsImpl::OnThemeColorChanged(RenderViewHostImpl* source) { ...@@ -5482,12 +5482,28 @@ void WebContentsImpl::OnThemeColorChanged(RenderViewHostImpl* source) {
} }
void WebContentsImpl::OnBackgroundColorChanged(RenderViewHostImpl* source) { void WebContentsImpl::OnBackgroundColorChanged(RenderViewHostImpl* source) {
if (source->did_first_visually_non_empty_paint() && if (source->did_first_visually_non_empty_paint()) {
last_sent_background_color_ != source->background_color()) { if (last_sent_background_color_ != source->background_color()) {
observers_.ForEachObserver([&](WebContentsObserver* observer) { observers_.ForEachObserver([&](WebContentsObserver* observer) {
observer->OnBackgroundColorChanged(); observer->OnBackgroundColorChanged();
}); });
last_sent_background_color_ = source->background_color(); last_sent_background_color_ = source->background_color();
}
return;
}
if (source->background_color().has_value()) {
// <meta name="color-scheme" content="dark"> may pass the dark canvas
// background before the first paint in order to avoid flashing the white
// background in between loading documents. If we perform a navigation
// within the same renderer process, we keep the content background from the
// previous page while rendering is blocked in the new page, but for cross
// process navigations we would paint the default background (typically
// white) while the rendering is blocked.
if (auto* view = GetRenderWidgetHostView()) {
static_cast<RenderWidgetHostViewBase*>(view)->SetContentBackgroundColor(
source->background_color().value());
}
} }
} }
......
<!doctype html>
<meta name="color-scheme" content="dark">
<link rel="stylesheet" href="/slow-response">
...@@ -1869,6 +1869,18 @@ void LocalFrameView::SetUseColorAdjustBackground(UseColorAdjustBackground use, ...@@ -1869,6 +1869,18 @@ void LocalFrameView::SetUseColorAdjustBackground(UseColorAdjustBackground use,
return; return;
use_color_adjust_background_ = use; use_color_adjust_background_ = use;
if (GetFrame().IsMainFrame() && ShouldUseColorAdjustBackground()) {
// Pass the dark color-scheme background to the browser process to paint a
// dark background in the browser tab while rendering is blocked in order to
// avoid flashing the white background in between loading documents. If we
// perform a navigation within the same renderer process, we keep the
// content background from the previous page while rendering is blocked in
// the new page, but for cross process navigations we would paint the
// default background (typically white) while the rendering is blocked.
GetFrame().DidChangeBackgroundColor(SkColor(BaseBackgroundColor()));
}
if (auto* layout_view = GetLayoutView()) if (auto* layout_view = GetLayoutView())
layout_view->SetBackgroundNeedsFullPaintInvalidation(); layout_view->SetBackgroundNeedsFullPaintInvalidation();
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment