From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Samuel Attard <sattard@slack-corp.com>
Date: Mon, 26 Aug 2019 14:32:41 -0700
Subject: feat: add set_theme_source to allow apps to override chromiums
 internal theme choice

This patch is required as Chromium doesn't currently let folks using
//ui override the theme choice in NativeTheme.  It defaults to
respecting the OS theme choice and some apps don't always want to do
that.  With this patch we can override the theme value that Chromium
uses internally for things like menus and devtools.

We can remove this patch once it has in some shape been upstreamed.

diff --git a/ui/native_theme/native_theme.cc b/ui/native_theme/native_theme.cc
index 9d108fe375fa68811c5aba9b2d3b5622b7293b49..3080f77e125e1792f37b402ceeb3dbf730af45eb 100644
--- a/ui/native_theme/native_theme.cc
+++ b/ui/native_theme/native_theme.cc
@@ -209,6 +209,8 @@ NativeTheme::NativeTheme(bool should_use_dark_colors,
 NativeTheme::~NativeTheme() = default;
 
 bool NativeTheme::ShouldUseDarkColors() const {
+  if (theme_source() == ThemeSource::kForcedLight) return false;
+  if (theme_source() == ThemeSource::kForcedDark) return true;
   return should_use_dark_colors_;
 }
 
diff --git a/ui/native_theme/native_theme.h b/ui/native_theme/native_theme.h
index 0ba23d2d8ea58024b48d1dd3b0a411bc53d6e26e..128cb008aeef4a75ef1569a37a308e295408f77c 100644
--- a/ui/native_theme/native_theme.h
+++ b/ui/native_theme/native_theme.h
@@ -449,6 +449,23 @@ class NATIVE_THEME_EXPORT NativeTheme {
       scoped_refptr<ColorProviderKey::ThemeInitializerSupplier> custom_theme,
       bool use_custom_frame = true) const;
 
+
+  enum ThemeSource {
+    kSystem,
+    kForcedDark,
+    kForcedLight,
+  };
+
+  ThemeSource theme_source() const {
+    return theme_source_;
+  }
+
+  void set_theme_source(ThemeSource theme_source) {
+    bool original = ShouldUseDarkColors();
+    theme_source_ = theme_source;
+    if (ShouldUseDarkColors() != original) NotifyOnNativeThemeUpdated();
+  }
+
   // Returns a shared instance of the native theme that should be used for web
   // rendering. Do not use it in a normal application context (i.e. browser).
   // The returned object should not be deleted by the caller. This function is
@@ -677,6 +694,7 @@ class NATIVE_THEME_EXPORT NativeTheme {
   PreferredColorScheme preferred_color_scheme_ = PreferredColorScheme::kLight;
   PreferredContrast preferred_contrast_ = PreferredContrast::kNoPreference;
   std::optional<base::TimeDelta> caret_blink_interval_;
+  ThemeSource theme_source_ = ThemeSource::kSystem;
 
   SEQUENCE_CHECKER(sequence_checker_);
 };
diff --git a/ui/native_theme/native_theme_win.cc b/ui/native_theme/native_theme_win.cc
index c1523bbdbf16e1df1bf158b7b053dce09116f2fc..06831ec767f6ba81e76f52322fc2d81a45c2c275 100644
--- a/ui/native_theme/native_theme_win.cc
+++ b/ui/native_theme/native_theme_win.cc
@@ -682,6 +682,8 @@ bool NativeThemeWin::ShouldUseDarkColors() const {
   // ...unless --force-dark-mode was specified in which case caveat emptor.
   if (InForcedColorsMode() && !IsForcedDarkMode())
     return false;
+  if (theme_source() == ThemeSource::kForcedLight) return false;
+  if (theme_source() == ThemeSource::kForcedDark) return true;
   return NativeTheme::ShouldUseDarkColors();
 }
 
