caption_util.cc 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. // Copyright 2020 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 "components/live_caption/caption_util.h"
  5. #include <stddef.h>
  6. #include "base/command_line.h"
  7. #include "base/cpu.h"
  8. #include "base/feature_list.h"
  9. #include "base/metrics/histogram_functions.h"
  10. #include "base/strings/string_number_conversions.h"
  11. #include "base/strings/stringprintf.h"
  12. #include "build/build_config.h"
  13. #include "build/chromeos_buildflags.h"
  14. #include "components/live_caption/pref_names.h"
  15. #include "components/prefs/pref_service.h"
  16. #include "media/base/media_switches.h"
  17. #include "ui/base/ui_base_switches.h"
  18. #include "ui/native_theme/native_theme.h"
  19. #if BUILDFLAG(IS_CHROMEOS_ASH)
  20. #include "ash/constants/ash_features.h"
  21. #endif
  22. #if BUILDFLAG(IS_CHROMEOS_LACROS)
  23. #include "chromeos/startup/browser_params_proxy.h"
  24. #endif
  25. namespace {
  26. // Returns whether the style is default or not. If the user has changed any of
  27. // the captions settings from the default value, that is an interesting metric
  28. // to observe.
  29. bool IsDefaultStyle(absl::optional<ui::CaptionStyle> style) {
  30. return (style.has_value() && style->text_size.empty() &&
  31. style->font_family.empty() && style->text_color.empty() &&
  32. style->background_color.empty() && style->text_shadow.empty());
  33. }
  34. // Adds !important to all captions styles. They should always override any
  35. // styles added by the video author or by a user stylesheet. This is because in
  36. // Chrome, there is an option to turn off captions styles, so any time the
  37. // captions are on, the styles should take priority.
  38. std::string AddCSSImportant(std::string css_string) {
  39. return css_string.empty() ? "" : css_string + " !important";
  40. }
  41. // Constructs the CaptionStyle struct from the caption-related preferences.
  42. absl::optional<ui::CaptionStyle> GetCaptionStyleFromPrefs(PrefService* prefs) {
  43. if (!prefs) {
  44. return absl::nullopt;
  45. }
  46. ui::CaptionStyle style;
  47. style.text_size =
  48. AddCSSImportant(prefs->GetString(prefs::kAccessibilityCaptionsTextSize));
  49. style.font_family =
  50. AddCSSImportant(prefs->GetString(prefs::kAccessibilityCaptionsTextFont));
  51. if (!prefs->GetString(prefs::kAccessibilityCaptionsTextColor).empty()) {
  52. std::string text_color = base::StringPrintf(
  53. "rgba(%s,%s)",
  54. prefs->GetString(prefs::kAccessibilityCaptionsTextColor).c_str(),
  55. base::NumberToString(
  56. prefs->GetInteger(prefs::kAccessibilityCaptionsTextOpacity) / 100.0)
  57. .c_str());
  58. style.text_color = AddCSSImportant(text_color);
  59. }
  60. if (!prefs->GetString(prefs::kAccessibilityCaptionsBackgroundColor).empty()) {
  61. std::string background_color = base::StringPrintf(
  62. "rgba(%s,%s)",
  63. prefs->GetString(prefs::kAccessibilityCaptionsBackgroundColor).c_str(),
  64. base::NumberToString(
  65. prefs->GetInteger(prefs::kAccessibilityCaptionsBackgroundOpacity) /
  66. 100.0)
  67. .c_str());
  68. style.background_color = AddCSSImportant(background_color);
  69. }
  70. style.text_shadow = AddCSSImportant(
  71. prefs->GetString(prefs::kAccessibilityCaptionsTextShadow));
  72. return style;
  73. }
  74. } // namespace
  75. namespace captions {
  76. absl::optional<ui::CaptionStyle> GetCaptionStyleFromUserSettings(
  77. PrefService* prefs,
  78. bool record_metrics) {
  79. // Apply native CaptionStyle parameters.
  80. absl::optional<ui::CaptionStyle> style;
  81. // Apply native CaptionStyle parameters.
  82. if (base::CommandLine::ForCurrentProcess()->HasSwitch(
  83. ::switches::kForceCaptionStyle)) {
  84. style = ui::CaptionStyle::FromSpec(
  85. base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
  86. switches::kForceCaptionStyle));
  87. }
  88. // Apply system caption style.
  89. if (!style) {
  90. ui::NativeTheme* native_theme = ui::NativeTheme::GetInstanceForWeb();
  91. style = native_theme->GetSystemCaptionStyle();
  92. if (record_metrics && style.has_value()) {
  93. base::UmaHistogramBoolean(
  94. "Accessibility.CaptionSettingsLoadedFromSystemSettings",
  95. !IsDefaultStyle(style));
  96. }
  97. }
  98. // Apply caption style from preferences if system caption style is undefined.
  99. if (!style) {
  100. style = GetCaptionStyleFromPrefs(prefs);
  101. if (record_metrics && style.has_value()) {
  102. base::UmaHistogramBoolean("Accessibility.CaptionSettingsLoadedFromPrefs",
  103. !IsDefaultStyle(style));
  104. }
  105. }
  106. return style;
  107. }
  108. bool IsLiveCaptionFeatureSupported() {
  109. if (!base::FeatureList::IsEnabled(media::kLiveCaption))
  110. return false;
  111. // Some Chrome OS devices do not support on-device speech.
  112. #if BUILDFLAG(IS_CHROMEOS_ASH)
  113. if (!base::FeatureList::IsEnabled(ash::features::kOnDeviceSpeechRecognition))
  114. return false;
  115. #elif BUILDFLAG(IS_CHROMEOS_LACROS)
  116. if (!chromeos::BrowserParamsProxy::Get()->IsOndeviceSpeechSupported())
  117. return false;
  118. #endif
  119. #if BUILDFLAG(IS_LINUX)
  120. // Check if the CPU has the required instruction set to run the Speech
  121. // On-Device API (SODA) library.
  122. static bool has_sse41 = base::CPU().has_sse41();
  123. if (!has_sse41)
  124. return false;
  125. #endif
  126. #if BUILDFLAG(IS_WIN) && defined(ARCH_CPU_ARM64)
  127. // The Speech On-Device API (SODA) component does not support Windows on
  128. // arm64.
  129. return false;
  130. #else
  131. return true;
  132. #endif
  133. }
  134. } // namespace captions