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 7a9ae490f6ec0b07eedf6aea62a58a026dfef693..bc04ea8d07c4b1cd11df626f98330b64dd2f1c7f 100644
--- a/ui/native_theme/native_theme.cc
+++ b/ui/native_theme/native_theme.cc
@@ -253,6 +253,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 e2ca7dd198390ebc923facd690904faeb8300e62..f2f53775abb0ba2954cbb3ec431d84c3e03ad6f8 100644
--- a/ui/native_theme/native_theme.h
+++ b/ui/native_theme/native_theme.h
@@ -392,6 +392,22 @@ class NATIVE_THEME_EXPORT NativeTheme {
       ColorId color_id,
       ColorScheme color_scheme = ColorScheme::kDefault) 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) NotifyObservers();
+  }
+
   // 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
@@ -530,6 +546,8 @@ class NATIVE_THEME_EXPORT NativeTheme {
   bool is_high_contrast_ = false;
   PreferredColorScheme preferred_color_scheme_ = PreferredColorScheme::kLight;
 
+  ThemeSource theme_source_ = ThemeSource::kSystem;
+
   DISALLOW_COPY_AND_ASSIGN(NativeTheme);
 };
 
diff --git a/ui/native_theme/native_theme_win.cc b/ui/native_theme/native_theme_win.cc
index fd075a6f8a1357e7456a97d37af339848635a102..54fa642f614acd221ceaeee0938506e6fea41b1a 100644
--- a/ui/native_theme/native_theme_win.cc
+++ b/ui/native_theme/native_theme_win.cc
@@ -737,6 +737,8 @@ bool NativeThemeWin::ShouldUseDarkColors() const {
   // ...unless --force-dark-mode was specified in which case caveat emptor.
   if (UsesHighContrastColors() && !IsForcedDarkMode())
     return false;
+  if (theme_source() == ThemeSource::kForcedLight) return false;
+  if (theme_source() == ThemeSource::kForcedDark) return true;
   return NativeTheme::ShouldUseDarkColors();
 }
 
