modifier-split: Update modifier split flag to allow disabling for tests

Fixed: b/332786859
Change-Id: Ic7ead281f82c43881c0ed8b3f22660715aed12d0
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5422328
Reviewed-by: Danny Wang <wangdanny@google.com>
Commit-Queue: David Padlipsky <dpad@google.com>
Cr-Commit-Position: refs/heads/main@{#1283367}
diff --git a/ash/constants/ash_features.cc b/ash/constants/ash_features.cc
index cee65e1..f4ecb5d 100644
--- a/ash/constants/ash_features.cc
+++ b/ash/constants/ash_features.cc
@@ -4041,26 +4041,8 @@
 }
 
 bool IsModifierSplitEnabled() {
-  static std::optional<bool> enabled;
-  if (enabled) {
-    return *enabled;
-  }
-
-  if (!base::FeatureList::IsEnabled(kModifierSplit)) {
-    enabled = false;
-    return false;
-  }
-
-  const std::string debug_key_hash = base::SHA1HashString(
-      base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
-          ash::switches::kModifierSplitFeatureKey));
-
-  const std::string hash =
-      "\xFC\xEF\x09\x7D\x01\x39\x86\x6A\x57\x08\x7C\x22\x5F\x1C\xEF\x8A\x3B"
-      "\x7E\x10\x99";
-  enabled = debug_key_hash == hash;
-
-  return *enabled;
+  return base::FeatureList::IsEnabled(kModifierSplit) &&
+         switches::IsModifierSplitSecretKeyMatched();
 }
 
 bool IsSplitKeyboardRefactorEnabled() {
diff --git a/ash/constants/ash_switches.cc b/ash/constants/ash_switches.cc
index e5f7b76b..2dbd07715 100644
--- a/ash/constants/ash_switches.cc
+++ b/ash/constants/ash_switches.cc
@@ -44,6 +44,14 @@
 // Whether checking the mahi secret key is ignored.
 bool g_ignore_mahi_secret_key = false;
 
+// The hash value for the secret key of the mahi feature.
+constexpr char kModifierSplitHashKey[] =
+    "\xFC\xEF\x09\x7D\x01\x39\x86\x6A\x57\x08\x7C\x22\x5F\x1C\xEF\x8A\x3B\x7E"
+    "\x10\x99";
+
+// Whether checking the mahi secret key is ignored.
+bool g_ignore_modifier_split_secret_key = false;
+
 }  // namespace
 
 // Please keep the order of these switches synchronized with the header file
@@ -1413,4 +1421,30 @@
   return {&g_ignore_mahi_secret_key, true};
 }
 
+bool IsModifierSplitSecretKeyMatched() {
+  if (g_ignore_modifier_split_secret_key) {
+    return true;
+  }
+
+  // Commandline looks like:
+  //  out/Default/chrome --user-data-dir=/tmp/tmp123
+  //  --modifier-split-feature-key="INSERT KEY HERE"
+  //  --enable-features=ModifierSplit
+  const std::string provided_key_hash = base::SHA1HashString(
+      base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
+          kModifierSplitFeatureKey));
+
+  bool modifier_split_key_matched =
+      (provided_key_hash == kModifierSplitHashKey);
+  if (!modifier_split_key_matched) {
+    LOG(ERROR) << "Provided secret key does not match with the expected one.";
+  }
+
+  return modifier_split_key_matched;
+}
+
+base::AutoReset<bool> SetIgnoreModifierSplitSecretKeyForTest() {
+  return {&g_ignore_modifier_split_secret_key, true};
+}
+
 }  // namespace ash::switches
diff --git a/ash/constants/ash_switches.h b/ash/constants/ash_switches.h
index bafd18b..3ad477c5 100644
--- a/ash/constants/ash_switches.h
+++ b/ash/constants/ash_switches.h
@@ -515,6 +515,12 @@
 COMPONENT_EXPORT(ASH_CONSTANTS)
 base::AutoReset<bool> SetIgnoreMahiSecretKeyForTest();
 
+COMPONENT_EXPORT(ASH_CONSTANTS)
+bool IsModifierSplitSecretKeyMatched();
+
+COMPONENT_EXPORT(ASH_CONSTANTS)
+base::AutoReset<bool> SetIgnoreModifierSplitSecretKeyForTest();
+
 }  // namespace ash::switches
 
 #endif  // ASH_CONSTANTS_ASH_SWITCHES_H_