Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(27)

Side by Side Diff: chrome/browser/extensions/extension_tts_api_controller.h

Issue 9808024: Move TTS extension API to chrome/browser/speech/ (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix chromeos compile error, rebase Created 8 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2011 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
5 #ifndef CHROME_BROWSER_EXTENSIONS_EXTENSION_TTS_API_CONTROLLER_H_
6 #define CHROME_BROWSER_EXTENSIONS_EXTENSION_TTS_API_CONTROLLER_H_
7
8 #include <queue>
9 #include <set>
10 #include <string>
11
12 #include "base/memory/scoped_ptr.h"
13 #include "base/memory/singleton.h"
14 #include "googleurl/src/gurl.h"
15
16 class Extension;
17 class ExtensionTtsPlatformImpl;
18 class Profile;
19
20 namespace base {
21 class ListValue;
22 class Value;
23 }
24
25 // Events sent back from the TTS engine indicating the progress.
26 enum TtsEventType {
27 TTS_EVENT_START,
28 TTS_EVENT_END,
29 TTS_EVENT_WORD,
30 TTS_EVENT_SENTENCE,
31 TTS_EVENT_MARKER,
32 TTS_EVENT_INTERRUPTED,
33 TTS_EVENT_CANCELLED,
34 TTS_EVENT_ERROR
35 };
36
37
38 // The continuous parameters that apply to a given utterance.
39 struct UtteranceContinuousParameters {
40 UtteranceContinuousParameters();
41
42 double rate;
43 double pitch;
44 double volume;
45 };
46
47
48 // One speech utterance.
49 class Utterance {
50 public:
51 // Construct an utterance given a profile and a completion task to call
52 // when the utterance is done speaking. Before speaking this utterance,
53 // its other parameters like text, rate, pitch, etc. should all be set.
54 explicit Utterance(Profile* profile);
55 ~Utterance();
56
57 // Sends an event to the delegate. If the event type is TTS_EVENT_END
58 // or TTS_EVENT_ERROR, deletes the utterance. If |char_index| is -1,
59 // uses the last good value.
60 void OnTtsEvent(TtsEventType event_type,
61 int char_index,
62 const std::string& error_message);
63
64 // Finish an utterance without sending an event to the delegate.
65 void Finish();
66
67 // Getters and setters for the text to speak and other speech options.
68 void set_text(const std::string& text) { text_ = text; }
69 const std::string& text() const { return text_; }
70
71 void set_options(const base::Value* options);
72 const base::Value* options() const { return options_.get(); }
73
74 void set_src_extension_id(const std::string& src_extension_id) {
75 src_extension_id_ = src_extension_id;
76 }
77 const std::string& src_extension_id() { return src_extension_id_; }
78
79 void set_src_id(int src_id) { src_id_ = src_id; }
80 int src_id() { return src_id_; }
81
82 void set_src_url(const GURL& src_url) { src_url_ = src_url; }
83 const GURL& src_url() { return src_url_; }
84
85 void set_voice_name(const std::string& voice_name) {
86 voice_name_ = voice_name;
87 }
88 const std::string& voice_name() const { return voice_name_; }
89
90 void set_lang(const std::string& lang) {
91 lang_ = lang;
92 }
93 const std::string& lang() const { return lang_; }
94
95 void set_gender(const std::string& gender) {
96 gender_ = gender;
97 }
98 const std::string& gender() const { return gender_; }
99
100 void set_continuous_parameters(const UtteranceContinuousParameters& params) {
101 continuous_parameters_ = params;
102 }
103 const UtteranceContinuousParameters& continuous_parameters() {
104 return continuous_parameters_;
105 }
106
107 void set_can_enqueue(bool can_enqueue) { can_enqueue_ = can_enqueue; }
108 bool can_enqueue() const { return can_enqueue_; }
109
110 void set_required_event_types(const std::set<std::string>& types) {
111 required_event_types_ = types;
112 }
113 const std::set<std::string>& required_event_types() const {
114 return required_event_types_;
115 }
116
117 void set_desired_event_types(const std::set<std::string>& types) {
118 desired_event_types_ = types;
119 }
120 const std::set<std::string>& desired_event_types() const {
121 return desired_event_types_;
122 }
123
124 const std::string& extension_id() const { return extension_id_; }
125 void set_extension_id(const std::string& extension_id) {
126 extension_id_ = extension_id;
127 }
128
129 // Getters and setters for internal state.
130 Profile* profile() const { return profile_; }
131 int id() const { return id_; }
132 bool finished() const { return finished_; }
133
134 private:
135 // The profile that initiated this utterance.
136 Profile* profile_;
137
138 // The extension ID of the extension providing TTS for this utterance, or
139 // empty if native TTS is being used.
140 std::string extension_id_;
141
142 // The unique ID of this utterance, used to associate callback functions
143 // with utterances.
144 int id_;
145
146 // The id of the next utterance, so we can associate requests with
147 // responses.
148 static int next_utterance_id_;
149
150 // The text to speak.
151 std::string text_;
152
153 // The full options arg passed to tts.speak, which may include fields
154 // other than the ones we explicitly parse, below.
155 scoped_ptr<base::Value> options_;
156
157 // The extension ID of the extension that called speak() and should
158 // receive events.
159 std::string src_extension_id_;
160
161 // The source extension's ID of this utterance, so that it can associate
162 // events with the appropriate callback.
163 int src_id_;
164
165 // The URL of the page where the source extension called speak.
166 GURL src_url_;
167
168 // The parsed options.
169 std::string voice_name_;
170 std::string lang_;
171 std::string gender_;
172 UtteranceContinuousParameters continuous_parameters_;
173 bool can_enqueue_;
174 std::set<std::string> required_event_types_;
175 std::set<std::string> desired_event_types_;
176
177 // The index of the current char being spoken.
178 int char_index_;
179
180 // True if this utterance received an event indicating it's done.
181 bool finished_;
182 };
183
184
185 // Singleton class that manages text-to-speech for the TTS and TTS engine
186 // extension APIs, maintaining a queue of pending utterances and keeping
187 // track of all state.
188 class ExtensionTtsController {
189 public:
190 // Get the single instance of this class.
191 static ExtensionTtsController* GetInstance();
192
193 // Returns true if we're currently speaking an utterance.
194 bool IsSpeaking() const;
195
196 // Speak the given utterance. If the utterance's can_enqueue flag is true
197 // and another utterance is in progress, adds it to the end of the queue.
198 // Otherwise, interrupts any current utterance and speaks this one
199 // immediately.
200 void SpeakOrEnqueue(Utterance* utterance);
201
202 // Stop all utterances and flush the queue.
203 void Stop();
204
205 // Handle events received from the speech engine. Events are forwarded to
206 // the callback function, and in addition, completion and error events
207 // trigger finishing the current utterance and starting the next one, if
208 // any.
209 void OnTtsEvent(int utterance_id,
210 TtsEventType event_type,
211 int char_index,
212 const std::string& error_message);
213
214 // Return a list of all available voices, including the native voice,
215 // if supported, and all voices registered by extensions.
216 base::ListValue* GetVoices(Profile* profile);
217
218 // For unit testing.
219 void SetPlatformImpl(ExtensionTtsPlatformImpl* platform_impl);
220 int QueueSize();
221
222 protected:
223 ExtensionTtsController();
224 virtual ~ExtensionTtsController();
225
226 private:
227 // Get the platform TTS implementation (or injected mock).
228 ExtensionTtsPlatformImpl* GetPlatformImpl();
229
230 // Start speaking the given utterance. Will either take ownership of
231 // |utterance| or delete it if there's an error. Returns true on success.
232 void SpeakNow(Utterance* utterance);
233
234 // Clear the utterance queue. If send_events is true, will send
235 // TTS_EVENT_CANCELLED events on each one.
236 void ClearUtteranceQueue(bool send_events);
237
238 // Finalize and delete the current utterance.
239 void FinishCurrentUtterance();
240
241 // Start speaking the next utterance in the queue.
242 void SpeakNextUtterance();
243
244 friend struct DefaultSingletonTraits<ExtensionTtsController>;
245
246 // The current utterance being spoken.
247 Utterance* current_utterance_;
248
249 // A queue of utterances to speak after the current one finishes.
250 std::queue<Utterance*> utterance_queue_;
251
252 // A pointer to the platform implementation of text-to-speech, for
253 // dependency injection.
254 ExtensionTtsPlatformImpl* platform_impl_;
255
256 DISALLOW_COPY_AND_ASSIGN(ExtensionTtsController);
257 };
258
259 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_TTS_API_CONTROLLER_H_
OLDNEW
« no previous file with comments | « chrome/browser/extensions/extension_tts_api_constants.cc ('k') | chrome/browser/extensions/extension_tts_api_controller.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698