File: tts_engine_extension_api.h

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (137 lines) | stat: -rw-r--r-- 5,679 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
137
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_SPEECH_EXTENSION_API_TTS_ENGINE_EXTENSION_API_H_
#define CHROME_BROWSER_SPEECH_EXTENSION_API_TTS_ENGINE_EXTENSION_API_H_

#include <vector>

#include "base/values.h"
#include "content/public/browser/tts_controller.h"
#include "extensions/browser/extension_function.h"

namespace content {
class BrowserContext;
}

namespace tts_engine_events {
extern const char kOnSpeak[];
extern const char kOnSpeakWithAudioStream[];
extern const char kOnStop[];
extern const char kOnPause[];
extern const char kOnResume[];
extern const char kOnInstallLanguageRequest[];
extern const char kOnLanguageStatusRequest[];
extern const char kOnUninstallLanguageRequest[];

// Specifying where events sent to the TTS Engine originated
enum class TtsClientSource {
  CHROMEFEATURE,  // The event originated from a feature within Chrome
  EXTENSION       // The event originated from an extension
};
}  // namespace tts_engine_events

// TtsEngineDelegate implementation used by TtsController.
class TtsExtensionEngine : public content::TtsEngineDelegate {
 public:
  static TtsExtensionEngine* GetInstance();

  TtsExtensionEngine();
  ~TtsExtensionEngine() override;

  // Sends audio buffer for playback in tts service. See
  // chromeos/services/tts/public/mojom for more details.
  virtual void SendAudioBuffer(int utterance_id,
                               const std::vector<float>& audio_buffer,
                               int char_index,
                               bool is_last_buffer) {}

  // Overridden from TtsEngineDelegate:
  void GetVoices(content::BrowserContext* browser_context,
                 const GURL& source_url,
                 std::vector<content::VoiceData>* out_voices) override;
  void Speak(content::TtsUtterance* utterance,
             const content::VoiceData& voice) override;
  void Stop(content::TtsUtterance* utterance) override;
  void Pause(content::TtsUtterance* utterance) override;
  void Resume(content::TtsUtterance* utterance) override;
  void UninstallLanguageRequest(content::BrowserContext* browser_context,
                                const std::string& lang,
                                const std::string& client_id,
                                int source,
                                bool uninstall_immediately) override;
  void InstallLanguageRequest(content::BrowserContext* browser_context,
                              const std::string& lang,
                              const std::string& client_id,
                              int source) override;
  void LanguageStatusRequest(content::BrowserContext* browser_context,
                             const std::string& lang,
                             const std::string& client_id,
                             int source) override;
  void LoadBuiltInTtsEngine(content::BrowserContext* browser_context) override;
  bool IsBuiltInTtsEngineInitialized(
      content::BrowserContext* browser_context) override;

  // Stops the given speech engine loaded in |browser_context|.
  void Stop(content::BrowserContext* browser_context,
            const std::string& engine_id);
  // Pauses the given speech engine loaded in |browser_context|.
  void Pause(content::BrowserContext* browser_context,
             const std::string& engine_id);
  // Resumes the given speech engine loaded in |browser_context|.
  void Resume(content::BrowserContext* browser_context,
              const std::string& engine_id);

  void DisableBuiltInTTSEngineForTesting() {
    disable_built_in_tts_engine_for_testing_ = true;
  }

 protected:
  base::Value::List BuildSpeakArgs(content::TtsUtterance* utterance,
                                   const content::VoiceData& voice);
  base::Value::List BuildLanguagePackArgs(
      const std::string& lang,
      const std::string& client_id,
      tts_engine_events::TtsClientSource source);

  bool disable_built_in_tts_engine_for_testing_ = false;
};

// Function that allows tts engines to update its list of supported voices at
// runtime.
class ExtensionTtsEngineUpdateVoicesFunction : public ExtensionFunction {
 private:
  ~ExtensionTtsEngineUpdateVoicesFunction() override = default;
  ResponseAction Run() override;
  DECLARE_EXTENSION_FUNCTION("ttsEngine.updateVoices", TTSENGINE_UPDATEVOICES)
};

// Hidden/internal extension function used to allow TTS engine extensions
// to send events back to the client that's calling tts.speak().
class ExtensionTtsEngineSendTtsEventFunction : public ExtensionFunction {
 private:
  ~ExtensionTtsEngineSendTtsEventFunction() override = default;
  ResponseAction Run() override;
  DECLARE_EXTENSION_FUNCTION("ttsEngine.sendTtsEvent", TTSENGINE_SENDTTSEVENT)
};

// Hidden/internal extension function used to allow TTS engine extensions
// to send audio back to the client that's calling tts.speak().
class ExtensionTtsEngineSendTtsAudioFunction : public ExtensionFunction {
 private:
  ~ExtensionTtsEngineSendTtsAudioFunction() override = default;
  ResponseAction Run() override;
  DECLARE_EXTENSION_FUNCTION("ttsEngine.sendTtsAudio", TTSENGINE_SENDTTSAUDIO)
};

// Function that allows tts engines to update the status of voice for a language
class ExtensionTtsEngineUpdateLanguageFunction : public ExtensionFunction {
 private:
  ~ExtensionTtsEngineUpdateLanguageFunction() override = default;
  ResponseAction Run() override;
  DECLARE_EXTENSION_FUNCTION("ttsEngine.updateLanguage",
                             TTSENGINE_UPDATELANGUAGE)
};

#endif  // CHROME_BROWSER_SPEECH_EXTENSION_API_TTS_ENGINE_EXTENSION_API_H_