Commit f0a7e117 authored by Dan Beam's avatar Dan Beam Committed by Commit Bot

Make @media (prefers-color-scheme:) respect features::kWebUIDarkMode

while on chrome:// URLs

This allows us to use this mechanism in CSS:

  @media (prefers-color-scheme: dark) {
  }

to vary styles for Chrome web UI in dark mode while also disabling via
feature on certain platforms.

This unblocks us from migrating to the @media query (from the existing
[dark] attribute syntax) and later removing a bit of JS/C++ custom code
in favor of simply using the web platform.

R=futhark@chromium.org
BUG=965811

Change-Id: I79f4582ce21661d7ba2f6a33f3f054f5ff62d0db
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1626024
Auto-Submit: Dan Beam <dbeam@chromium.org>
Reviewed-by: default avatarScott Violet <sky@chromium.org>
Commit-Queue: Dan Beam <dbeam@chromium.org>
Cr-Commit-Position: refs/heads/master@{#667245}
parent c0831019
......@@ -155,6 +155,7 @@
#include "chrome/browser/ui/prefs/pref_watcher.h"
#include "chrome/browser/ui/sync/sync_promo_ui.h"
#include "chrome/browser/ui/tab_contents/chrome_web_contents_view_delegate.h"
#include "chrome/browser/ui/ui_features.h"
#include "chrome/browser/ui/webui/chrome_web_ui_controller_factory.h"
#include "chrome/browser/ui/webui/log_web_ui_url.h"
#include "chrome/browser/usb/usb_tab_helper.h"
......@@ -3425,7 +3426,7 @@ void ChromeContentBrowserClient::OverrideWebkitPrefs(
}
}
auto* native_theme = ui::NativeTheme::GetInstanceForWeb();
auto* native_theme = GetWebTheme();
#if !defined(OS_ANDROID)
if (IsAutoplayAllowedByPolicy(contents, prefs)) {
// If autoplay is allowed by policy then force the no user gesture required
......@@ -3450,6 +3451,15 @@ void ChromeContentBrowserClient::OverrideWebkitPrefs(
web_prefs->translate_service_available = TranslateService::IsAvailable(prefs);
// Force a light preferred color scheme on chrome:// pages if kWebUIDarkMode
// is disabled until all UI is correctly themed and OSes support dark mode.
// Note: the WebUI CSS explicitly uses light (instead of not dark), which is
// why we don't reset back to no-preference. https://crbug.com/965811
if (contents && contents->GetURL().SchemeIs(content::kChromeUIScheme) &&
!base::FeatureList::IsEnabled(features::kWebUIDarkMode)) {
web_prefs->preferred_color_scheme = blink::PreferredColorScheme::kLight;
}
// Apply native CaptionStyle parameters.
// TODO(ellyjones): Support more native caption styling.
ui::CaptionStyle style = native_theme->GetSystemCaptionStyle();
......@@ -5301,6 +5311,10 @@ bool ChromeContentBrowserClient::HandleWebUIReverse(
url->host() == chrome::kChromeUISettingsHost;
}
const ui::NativeTheme* ChromeContentBrowserClient::GetWebTheme() const {
return ui::NativeTheme::GetInstanceForWeb();
}
// static
void ChromeContentBrowserClient::SetDefaultQuotaSettingsForTesting(
const storage::QuotaSettings* settings) {
......
......@@ -65,6 +65,14 @@ class SafeBrowsingService;
class UrlCheckerDelegate;
}
namespace ui {
class NativeTheme;
}
namespace url {
class Origin;
}
namespace user_prefs {
class PrefRegistrySyncable;
}
......@@ -73,10 +81,6 @@ namespace version_info {
enum class Channel;
}
namespace url {
class Origin;
}
class ChromeHidDelegate;
class ChromeSerialDelegate;
......@@ -637,6 +641,7 @@ class ChromeContentBrowserClient : public content::ContentBrowserClient {
static bool HandleWebUI(GURL* url, content::BrowserContext* browser_context);
static bool HandleWebUIReverse(GURL* url,
content::BrowserContext* browser_context);
virtual const ui::NativeTheme* GetWebTheme() const; // For testing.
private:
friend class DisableWebRtcEncryptionFlagTest;
......
......@@ -4,6 +4,7 @@
#include "chrome/browser/chrome_content_browser_client.h"
#include <memory>
#include <vector>
#include "base/base_switches.h"
......@@ -23,6 +24,7 @@
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/search/instant_test_base.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/browser/ui/ui_features.h"
#include "chrome/common/chrome_features.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/pref_names.h"
......@@ -36,8 +38,10 @@
#include "content/public/browser/navigation_entry.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/browser/render_view_host.h"
#include "content/public/browser/site_isolation_policy.h"
#include "content/public/browser/web_contents.h"
#include "content/public/common/content_client.h"
#include "content/public/common/content_features.h"
#include "content/public/common/content_switches.h"
#include "content/public/test/browser_test_utils.h"
......@@ -49,6 +53,8 @@
#include "net/test/embedded_test_server/http_request.h"
#include "net/test/embedded_test_server/http_response.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "ui/native_theme/native_theme.h"
#include "ui/native_theme/test_native_theme.h"
#include "url/gurl.h"
#include "url/origin.h"
......@@ -545,22 +551,57 @@ IN_PROC_BROWSER_TEST_F(OpenWindowFromNTPBrowserTest,
class PrefersColorSchemeTest : public testing::WithParamInterface<bool>,
public InProcessBrowserTest {
protected:
PrefersColorSchemeTest() = default;
PrefersColorSchemeTest() : theme_client_(&test_theme_) {}
~PrefersColorSchemeTest() {
CHECK_EQ(&theme_client_, SetBrowserClientForTesting(original_client_));
}
const char* ExpectedColorScheme() const {
return ui::NativeTheme::GetInstanceForNativeUi()->SystemDarkModeEnabled()
? "dark"
: "light";
return GetParam() ? "dark" : "light";
}
void SetUpCommandLine(base::CommandLine* command_line) override {
InProcessBrowserTest::SetUpCommandLine(command_line);
command_line->AppendSwitchASCII("enable-blink-features",
command_line->AppendSwitchASCII(switches::kEnableBlinkFeatures,
"MediaQueryPrefersColorScheme");
if (GetParam())
command_line->AppendSwitch("force-dark-mode");
}
void SetUpOnMainThread() override {
InProcessBrowserTest::SetUpOnMainThread();
original_client_ = SetBrowserClientForTesting(&theme_client_);
}
protected:
ui::TestNativeTheme test_theme_;
private:
content::ContentBrowserClient* original_client_ = nullptr;
class ChromeContentBrowserClientWithWebTheme
: public ChromeContentBrowserClient {
public:
explicit ChromeContentBrowserClientWithWebTheme(
const ui::NativeTheme* theme)
: theme_(theme) {}
protected:
const ui::NativeTheme* GetWebTheme() const override { return theme_; }
private:
const ui::NativeTheme* const theme_;
};
ChromeContentBrowserClientWithWebTheme theme_client_;
};
IN_PROC_BROWSER_TEST_P(PrefersColorSchemeTest, PrefersColorScheme) {
test_theme_.SetDarkMode(GetParam());
browser()
->tab_strip_model()
->GetActiveWebContents()
->GetRenderViewHost()
->OnWebkitPreferencesChanged();
ui_test_utils::NavigateToURL(
browser(),
ui_test_utils::GetTestUrl(
......@@ -571,6 +612,23 @@ IN_PROC_BROWSER_TEST_P(PrefersColorSchemeTest, PrefersColorScheme) {
EXPECT_EQ(base::ASCIIToUTF16(ExpectedColorScheme()), tab_title);
}
IN_PROC_BROWSER_TEST_P(PrefersColorSchemeTest, WebUIFeatureOverrides) {
test_theme_.SetDarkMode(true);
base::test::ScopedFeatureList features;
features.InitWithFeatureState(features::kWebUIDarkMode, GetParam());
ui_test_utils::NavigateToURL(browser(), GURL(chrome::kChromeUIDownloadsURL));
bool in_dark_mode; // A default shouldn't matter because of the ASSERT().
ASSERT_TRUE(ExecuteScriptAndExtractBool(
browser()->tab_strip_model()->GetActiveWebContents(),
"window.domAutomationController.send("
" window.matchMedia('(prefers-color-scheme: dark)').matches)",
&in_dark_mode));
EXPECT_EQ(in_dark_mode, GetParam());
}
INSTANTIATE_TEST_SUITE_P(All, PrefersColorSchemeTest, testing::Bool());
class ProtocolHandlerTest : public InProcessBrowserTest {
......
......@@ -429,7 +429,7 @@ TEST(ChromeContentBrowserClientTest, GetMetricSuffixForURL) {
GURL("https://www.google.com/search?notaquery=nope")));
}
TEST(ChromeContentBrowserClient, UserAgentStringFrozen) {
TEST(ChromeContentBrowserClientTest, UserAgentStringFrozen) {
base::test::ScopedFeatureList scoped_feature_list;
scoped_feature_list.InitAndEnableFeature(blink::features::kFreezeUserAgent);
......@@ -468,7 +468,7 @@ TEST(ChromeContentBrowserClient, UserAgentStringFrozen) {
#endif
}
TEST(ChromeContentBrowserClient, UserAgentStringOrdering) {
TEST(ChromeContentBrowserClientTest, UserAgentStringOrdering) {
#if defined(OS_ANDROID)
const char* const kArguments[] = {"chrome"};
base::test::ScopedCommandLine scoped_command_line;
......@@ -488,7 +488,7 @@ TEST(ChromeContentBrowserClient, UserAgentStringOrdering) {
#endif
}
TEST(ChromeContentBrowserClient, UserAgentMetadata) {
TEST(ChromeContentBrowserClientTest, UserAgentMetadata) {
ChromeContentBrowserClient content_browser_client;
auto metadata = content_browser_client.GetUserAgentMetadata();
......
......@@ -21,6 +21,9 @@ per-file browser_instant_controller*=file://chrome/browser/search/OWNERS
per-file avatar_*=file://components/signin/OWNERS
per-file signin_*=file://components/signin/OWNERS
per-file ui_features.cc=*
per-file ui_features.h=*
per-file settings_window_manager*chromeos*=file://chrome/browser/ui/ash/OWNERS
# COMPONENT: UI>Browser
......@@ -39,7 +39,6 @@ const char kTabHoverCardsFeatureParameterName[] = "setting";
const base::Feature kTabHoverCardImages{"TabHoverCardImages",
base::FEATURE_DISABLED_BY_DEFAULT};
#if !defined(OS_ANDROID)
// Whether to enable "dark mode" enhancements in Mac Mojave or Windows 10 for
// UIs implemented with web technologies.
const base::Feature kWebUIDarkMode {
......@@ -50,6 +49,5 @@ const base::Feature kWebUIDarkMode {
base::FEATURE_DISABLED_BY_DEFAULT
#endif // defined(OS_MACOSX) || defined(OS_WIN)
};
#endif // !defined(OS_ANDROID)
} // namespace features
......@@ -31,9 +31,7 @@ extern const char kTabHoverCardsFeatureParameterName[];
extern const base::Feature kTabHoverCardImages;
#if !defined(OS_ANDROID)
extern const base::Feature kWebUIDarkMode;
#endif
} // namespace features
......
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