caption_bubble_controller_views.cc 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. // Copyright (c) 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/views/caption_bubble_controller_views.h"
  5. #include <memory>
  6. #include <string>
  7. #include "base/bind.h"
  8. #include "components/live_caption/caption_bubble_context.h"
  9. #include "components/live_caption/live_caption_controller.h"
  10. #include "components/live_caption/views/caption_bubble.h"
  11. #include "components/live_caption/views/caption_bubble_model.h"
  12. #include "components/prefs/pref_service.h"
  13. namespace captions {
  14. // Static
  15. std::unique_ptr<CaptionBubbleController> CaptionBubbleController::Create(
  16. PrefService* profile_prefs) {
  17. return std::make_unique<CaptionBubbleControllerViews>(profile_prefs);
  18. }
  19. CaptionBubbleControllerViews::CaptionBubbleControllerViews(
  20. PrefService* profile_prefs) {
  21. caption_bubble_ = new CaptionBubble(
  22. profile_prefs,
  23. base::BindOnce(&CaptionBubbleControllerViews::OnCaptionBubbleDestroyed,
  24. base::Unretained(this)));
  25. caption_widget_ =
  26. views::BubbleDialogDelegateView::CreateBubble(caption_bubble_);
  27. caption_bubble_->SetCaptionBubbleStyle();
  28. }
  29. CaptionBubbleControllerViews::~CaptionBubbleControllerViews() {
  30. if (caption_widget_)
  31. caption_widget_->CloseNow();
  32. }
  33. void CaptionBubbleControllerViews::OnCaptionBubbleDestroyed() {
  34. caption_bubble_ = nullptr;
  35. caption_widget_ = nullptr;
  36. }
  37. bool CaptionBubbleControllerViews::OnTranscription(
  38. CaptionBubbleContext* caption_bubble_context,
  39. const media::SpeechRecognitionResult& result) {
  40. if (!caption_bubble_)
  41. return false;
  42. SetActiveModel(caption_bubble_context);
  43. if (active_model_->IsClosed())
  44. return false;
  45. // If the caption bubble has no activity and it receives a final
  46. // transcription, don't set text. The speech service sends a final
  47. // transcription after several seconds of no audio. This prevents the bubble
  48. // reappearing with a final transcription after it had disappeared due to no
  49. // activity.
  50. if (!caption_bubble_->HasActivity() && result.is_final)
  51. return true;
  52. active_model_->SetPartialText(result.transcription);
  53. if (result.is_final)
  54. active_model_->CommitPartialText();
  55. return true;
  56. }
  57. void CaptionBubbleControllerViews::OnError(
  58. CaptionBubbleContext* caption_bubble_context,
  59. CaptionBubbleErrorType error_type,
  60. OnErrorClickedCallback error_clicked_callback,
  61. OnDoNotShowAgainClickedCallback error_silenced_callback) {
  62. if (!caption_bubble_)
  63. return;
  64. SetActiveModel(caption_bubble_context);
  65. if (active_model_->IsClosed())
  66. return;
  67. active_model_->OnError(error_type, std::move(error_clicked_callback),
  68. std::move(error_silenced_callback));
  69. }
  70. void CaptionBubbleControllerViews::OnAudioStreamEnd(
  71. CaptionBubbleContext* caption_bubble_context) {
  72. if (!caption_bubble_)
  73. return;
  74. CaptionBubbleModel* caption_bubble_model =
  75. caption_bubble_models_[caption_bubble_context].get();
  76. if (active_model_ == caption_bubble_model) {
  77. active_model_ = nullptr;
  78. caption_bubble_->SetModel(nullptr);
  79. }
  80. caption_bubble_models_.erase(caption_bubble_context);
  81. }
  82. void CaptionBubbleControllerViews::UpdateCaptionStyle(
  83. absl::optional<ui::CaptionStyle> caption_style) {
  84. caption_bubble_->UpdateCaptionStyle(caption_style);
  85. }
  86. void CaptionBubbleControllerViews::SetActiveModel(
  87. CaptionBubbleContext* caption_bubble_context) {
  88. if (!caption_bubble_models_.count(caption_bubble_context)) {
  89. caption_bubble_models_.emplace(
  90. caption_bubble_context,
  91. std::make_unique<CaptionBubbleModel>(caption_bubble_context));
  92. }
  93. CaptionBubbleModel* caption_bubble_model =
  94. caption_bubble_models_[caption_bubble_context].get();
  95. if (active_model_ != caption_bubble_model) {
  96. active_model_ = caption_bubble_model;
  97. caption_bubble_->SetModel(active_model_);
  98. }
  99. }
  100. bool CaptionBubbleControllerViews::IsWidgetVisibleForTesting() {
  101. return caption_widget_ && caption_widget_->IsVisible();
  102. }
  103. std::string CaptionBubbleControllerViews::GetBubbleLabelTextForTesting() {
  104. return caption_bubble_
  105. ? base::UTF16ToUTF8(
  106. caption_bubble_->GetLabelForTesting()->GetText()) // IN-TEST
  107. : "";
  108. }
  109. } // namespace captions