aw_feature_entries.cc 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. // Copyright 2021 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "android_webview/browser/aw_feature_entries.h"
  5. #include <set>
  6. #include "android_webview/common/aw_switches.h"
  7. #include "base/command_line.h"
  8. #include "base/metrics/field_trial.h"
  9. #include "components/flags_ui/feature_entry.h"
  10. #include "components/flags_ui/feature_entry_macros.h"
  11. #include "components/flags_ui/flags_state.h"
  12. #include "components/variations/variations_associated_data.h"
  13. #include "third_party/blink/public/common/features.h"
  14. namespace android_webview {
  15. namespace aw_feature_entries {
  16. namespace {
  17. constexpr flags_ui::FeatureEntry::FeatureParam
  18. kForceDark_SelectiveImageInversion[] = {
  19. {"inversion_method", "cielab_based"},
  20. {"image_behavior", "selective"},
  21. {"foreground_lightness_threshold", "150"},
  22. {"background_lightness_threshold", "205"}};
  23. // Not like Chrome, WebView only provides a switch in dev ui and uses the
  24. // preferred variation if it is turned on.
  25. constexpr flags_ui::FeatureEntry::FeatureVariation kForceDarkVariations[] = {
  26. {"with selective image inversion", kForceDark_SelectiveImageInversion,
  27. std::size(kForceDark_SelectiveImageInversion), nullptr}};
  28. // Not for display, set the descriptions to empty.
  29. constexpr flags_ui::FeatureEntry kForceDark = {
  30. "enable-force-dark", "", "", flags_ui::kOsWebView,
  31. FEATURE_WITH_PARAMS_VALUE_TYPE(blink::features::kForceWebContentsDarkMode,
  32. kForceDarkVariations,
  33. "ForceDarkVariations")};
  34. constexpr flags_ui::FeatureEntry kWebViewFeatureEntries[] = {
  35. kForceDark,
  36. };
  37. } // namespace
  38. namespace internal {
  39. std::string ToEnabledEntry(const flags_ui::FeatureEntry& entry,
  40. int enabled_variation_index) {
  41. CHECK(entry.type == flags_ui::FeatureEntry::FEATURE_WITH_PARAMS_VALUE);
  42. // Index 0 is "Default" and 1 is "Enabled" inside FeatureEntry.
  43. return entry.NameForOption(enabled_variation_index + 2);
  44. }
  45. } // namespace internal
  46. std::vector<std::string> RegisterEnabledFeatureEntries(
  47. base::FeatureList* feature_list) {
  48. std::set<std::string> enabled_entries;
  49. if (base::CommandLine::ForCurrentProcess()->HasSwitch(
  50. switches::kWebViewSelectiveImageInversionDarkening)) {
  51. enabled_entries.insert(internal::ToEnabledEntry(kForceDark, 0));
  52. }
  53. return flags_ui::FlagsState::RegisterEnabledFeatureVariationParameters(
  54. kWebViewFeatureEntries, enabled_entries, /*trial_name=*/"webview_dev_ui",
  55. feature_list);
  56. }
  57. } // namespace aw_feature_entries
  58. } // namespace android_webview