File: caption_bubble_controller_views.h

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (136 lines) | stat: -rw-r--r-- 5,430 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef COMPONENTS_LIVE_CAPTION_VIEWS_CAPTION_BUBBLE_CONTROLLER_VIEWS_H_
#define COMPONENTS_LIVE_CAPTION_VIEWS_CAPTION_BUBBLE_CONTROLLER_VIEWS_H_

#include <memory>
#include <set>
#include <string>
#include <unordered_map>

#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "components/live_caption/caption_bubble_controller.h"
#include "components/live_caption/views/caption_bubble.h"
#include "components/soda/soda_installer.h"
#include "media/mojo/mojom/speech_recognition.mojom.h"

namespace views {
class Widget;
}

namespace captions {

class CaptionBubble;
class CaptionBubbleModel;
class CaptionBubbleSettings;
class CaptionBubbleSessionObserver;
class TranslationViewWrapperBase;

///////////////////////////////////////////////////////////////////////////////
// Caption Bubble Controller for Views
//
//  The implementation of the caption bubble controller for Views.
//
class CaptionBubbleControllerViews : public CaptionBubbleController,
                                     public speech::SodaInstaller::Observer {
 public:
  CaptionBubbleControllerViews(
      CaptionBubbleSettings* caption_bubble_settings,
      const std::string& application_locale,
      std::unique_ptr<TranslationViewWrapperBase> translation_view_wrapper);
  ~CaptionBubbleControllerViews() override;
  CaptionBubbleControllerViews(const CaptionBubbleControllerViews&) = delete;
  CaptionBubbleControllerViews& operator=(const CaptionBubbleControllerViews&) =
      delete;

  // Called when a transcription is received from the service. Returns whether
  // the transcription result was set on the caption bubble successfully.
  // Transcriptions will halt if this returns false.
  bool OnTranscription(content::WebContents* web_contents,
                       CaptionBubbleContext* caption_bubble_context,
                       const media::SpeechRecognitionResult& result) override;

  // Called when the speech service has an error.
  void OnError(
      CaptionBubbleContext* caption_bubble_context,
      CaptionBubbleErrorType error_type,
      OnErrorClickedCallback error_clicked_callback,
      OnDoNotShowAgainClickedCallback error_silenced_callback) override;

  // Called when the audio stream has ended.
  void OnAudioStreamEnd(content::WebContents* web_contents,
                        CaptionBubbleContext* caption_bubble_context) override;

  // Called when the caption style changes.
  void UpdateCaptionStyle(
      std::optional<ui::CaptionStyle> caption_style) override;

  bool IsWidgetVisibleForTesting() override;
  bool IsGenericErrorMessageVisibleForTesting() override;
  std::string GetBubbleLabelTextForTesting() override;
  void OnLanguageIdentificationEvent(
      content::WebContents* web_contents,
      CaptionBubbleContext* caption_bubble_context,
      const media::mojom::LanguageIdentificationEventPtr& event) override;
  void CloseActiveModelForTesting() override;
  views::Widget* GetCaptionWidgetForTesting();
  CaptionBubble* GetCaptionBubbleForTesting();

 private:
  friend class CaptionBubbleControllerViewsTest;
  friend class LiveCaptionUnavailabilityNotifierTest;

  // SodaInstaller::Observer overrides:
  void OnSodaInstalled(speech::LanguageCode language_code) override;
  void OnSodaInstallError(speech::LanguageCode language_code,
                          speech::SodaInstaller::ErrorCode error_code) override;
  void OnSodaProgress(speech::LanguageCode language_code,
                      int progress) override;

  // A callback passed to the CaptionBubble which is called when the
  // CaptionBubble is destroyed.
  void OnCaptionBubbleDestroyed();

  // Sets the active CaptionBubbleModel to the one corresponding to the given
  // media player id, and creates a new CaptionBubbleModel if one does not
  // already exist.
  void SetActiveModel(CaptionBubbleContext* caption_bubble_context);

  // Called when the user closes the caption bubble.
  void OnSessionEnded(const std::string& session_id);

  // Called on a cross-origin navigation or reload.
  void OnSessionReset(const std::string& session_id);

  raw_ptr<CaptionBubble, DanglingUntriaged> caption_bubble_;
  raw_ptr<views::Widget, DanglingUntriaged> caption_widget_;

  // A pointer to the currently active CaptionBubbleModel.
  raw_ptr<CaptionBubbleModel, DanglingUntriaged> active_model_ = nullptr;

  // A map of media player ids and their corresponding CaptionBubbleModel. New
  // entries are added to this map when a previously unseen media player id is
  // received.
  std::unordered_map<CaptionBubbleContext*, std::unique_ptr<CaptionBubbleModel>>
      caption_bubble_models_;

  // A collection of closed session identifiers that should not display
  // captions. Identifiers are removed from this collection when a user
  // refreshes the page or navigates away.
  std::set<std::string> closed_sessions_;

  // Mapping of unique session identifiers to the observer that observes the
  // sessions.
  std::unordered_map<std::string, std::unique_ptr<CaptionBubbleSessionObserver>>
      caption_bubble_session_observers_;

  std::string application_locale_;

  base::WeakPtrFactory<CaptionBubbleControllerViews> weak_factory_{this};
};
}  // namespace captions

#endif  // COMPONENTS_LIVE_CAPTION_VIEWS_CAPTION_BUBBLE_CONTROLLER_VIEWS_H_