caption_bubble_model.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. #ifndef COMPONENTS_LIVE_CAPTION_VIEWS_CAPTION_BUBBLE_MODEL_H_
  5. #define COMPONENTS_LIVE_CAPTION_VIEWS_CAPTION_BUBBLE_MODEL_H_
  6. #include <string>
  7. #include "base/callback.h"
  8. #include "base/memory/raw_ptr.h"
  9. namespace captions {
  10. class CaptionBubble;
  11. class CaptionBubbleContext;
  12. // These values are persisted to logs. Entries should not be renumbered and
  13. // numeric values should never be reused.
  14. enum CaptionBubbleErrorType {
  15. kGeneric = 0,
  16. kMediaFoundationRendererUnsupported = 1,
  17. kMaxValue = kMediaFoundationRendererUnsupported
  18. };
  19. using OnErrorClickedCallback = base::RepeatingCallback<void()>;
  20. using OnDoNotShowAgainClickedCallback =
  21. base::RepeatingCallback<void(CaptionBubbleErrorType, bool)>;
  22. ///////////////////////////////////////////////////////////////////////////////
  23. // Caption Bubble Model
  24. //
  25. // A representation of the data a caption bubble needs for a particular media
  26. // stream. The caption bubble controller sets the value of the text. The
  27. // caption bubble observes the model, and when the values change, the observer
  28. // is alerted.
  29. //
  30. // There exists one CaptionBubble and one CaptionBubbleControllerViews per
  31. // profile, but one CaptionBubbleModel per media stream. The CaptionBubbleModel
  32. // is owned by the CaptionBubbleControllerViews. It is created when
  33. // transcriptions from a new media stream are received and exists until the
  34. // audio stream ends for that stream.
  35. //
  36. // Partial text is a speech result that is subject to change. Incoming partial
  37. // texts overlap with the previous partial text.
  38. // Final text is the final transcription from the speech service that no
  39. // longer changes. Incoming partial texts do not overlap with final text.
  40. // When a final result is received from the speech service, the partial text is
  41. // appended to the end of the final text. The caption bubble displays the full
  42. // final + partial text.
  43. //
  44. class CaptionBubbleModel {
  45. public:
  46. explicit CaptionBubbleModel(CaptionBubbleContext* context);
  47. ~CaptionBubbleModel();
  48. CaptionBubbleModel(const CaptionBubbleModel&) = delete;
  49. CaptionBubbleModel& operator=(const CaptionBubbleModel&) = delete;
  50. void SetObserver(CaptionBubble* observer);
  51. void RemoveObserver();
  52. // Set the partial text and alert the observer.
  53. void SetPartialText(const std::string& partial_text);
  54. // Commits the partial text as final text.
  55. void CommitPartialText();
  56. // Set that the bubble has an error and alert the observer.
  57. void OnError(CaptionBubbleErrorType error_type,
  58. OnErrorClickedCallback error_clicked_callback,
  59. OnDoNotShowAgainClickedCallback error_silenced_callback);
  60. // Mark the bubble as closed, clear the partial and final text, and alert the
  61. // observer.
  62. void Close();
  63. // Marks the bubble as open.
  64. void Open();
  65. // Clears the partial and final text and alerts the observer.
  66. void ClearText();
  67. bool IsClosed() const { return is_closed_; }
  68. bool HasError() const { return has_error_; }
  69. CaptionBubbleErrorType ErrorType() const { return error_type_; }
  70. std::string GetFullText() const { return final_text_ + partial_text_; }
  71. CaptionBubbleContext* GetContext() { return context_; }
  72. private:
  73. // Alert the observer that a change has occurred to the model text.
  74. void OnTextChanged();
  75. std::string final_text_;
  76. std::string partial_text_;
  77. // Whether the bubble has been closed by the user.
  78. bool is_closed_ = false;
  79. // Whether an error should be displayed in the bubble.
  80. bool has_error_ = false;
  81. // The most recent error type encountered.
  82. CaptionBubbleErrorType error_type_ = CaptionBubbleErrorType::kGeneric;
  83. // The CaptionBubble observing changes to this model.
  84. raw_ptr<CaptionBubble> observer_ = nullptr;
  85. const raw_ptr<CaptionBubbleContext> context_;
  86. };
  87. } // namespace captions
  88. #endif // COMPONENTS_LIVE_CAPTION_VIEWS_CAPTION_BUBBLE_MODEL_H_