OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/speech/extension_api/tts_extension_api.h" | 5 #include "chrome/browser/speech/extension_api/tts_extension_api.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/lazy_instance.h" | 9 #include "base/lazy_instance.h" |
10 #include "base/memory/weak_ptr.h" | 10 #include "base/memory/weak_ptr.h" |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
79 } | 79 } |
80 | 80 |
81 namespace extensions { | 81 namespace extensions { |
82 | 82 |
83 // One of these is constructed for each utterance, and deleted | 83 // One of these is constructed for each utterance, and deleted |
84 // when the utterance gets any final event. | 84 // when the utterance gets any final event. |
85 class TtsExtensionEventHandler | 85 class TtsExtensionEventHandler |
86 : public UtteranceEventDelegate, | 86 : public UtteranceEventDelegate, |
87 public base::SupportsWeakPtr<TtsExtensionEventHandler> { | 87 public base::SupportsWeakPtr<TtsExtensionEventHandler> { |
88 public: | 88 public: |
| 89 explicit TtsExtensionEventHandler(const std::string& src_extension_id); |
| 90 |
89 virtual void OnTtsEvent(Utterance* utterance, | 91 virtual void OnTtsEvent(Utterance* utterance, |
90 TtsEventType event_type, | 92 TtsEventType event_type, |
91 int char_index, | 93 int char_index, |
92 const std::string& error_message) override; | 94 const std::string& error_message) override; |
| 95 |
| 96 private: |
| 97 // The extension ID of the extension that called speak() and should |
| 98 // receive events. |
| 99 std::string src_extension_id_; |
93 }; | 100 }; |
94 | 101 |
| 102 TtsExtensionEventHandler::TtsExtensionEventHandler( |
| 103 const std::string& src_extension_id) |
| 104 : src_extension_id_(src_extension_id) { |
| 105 } |
| 106 |
95 void TtsExtensionEventHandler::OnTtsEvent(Utterance* utterance, | 107 void TtsExtensionEventHandler::OnTtsEvent(Utterance* utterance, |
96 TtsEventType event_type, | 108 TtsEventType event_type, |
97 int char_index, | 109 int char_index, |
98 const std::string& error_message) { | 110 const std::string& error_message) { |
99 if (utterance->src_id() < 0) { | 111 if (utterance->src_id() < 0) { |
100 if (utterance->finished()) | 112 if (utterance->finished()) |
101 delete this; | 113 delete this; |
102 return; | 114 return; |
103 } | 115 } |
104 | 116 |
(...skipping 18 matching lines...) Expand all Loading... |
123 details->SetBoolean(constants::kIsFinalEventKey, utterance->finished()); | 135 details->SetBoolean(constants::kIsFinalEventKey, utterance->finished()); |
124 | 136 |
125 scoped_ptr<base::ListValue> arguments(new base::ListValue()); | 137 scoped_ptr<base::ListValue> arguments(new base::ListValue()); |
126 arguments->Set(0, details.release()); | 138 arguments->Set(0, details.release()); |
127 | 139 |
128 scoped_ptr<extensions::Event> event( | 140 scoped_ptr<extensions::Event> event( |
129 new extensions::Event(events::kOnEvent, arguments.Pass())); | 141 new extensions::Event(events::kOnEvent, arguments.Pass())); |
130 event->restrict_to_browser_context = utterance->browser_context(); | 142 event->restrict_to_browser_context = utterance->browser_context(); |
131 event->event_url = utterance->src_url(); | 143 event->event_url = utterance->src_url(); |
132 extensions::EventRouter::Get(utterance->browser_context()) | 144 extensions::EventRouter::Get(utterance->browser_context()) |
133 ->DispatchEventToExtension(utterance->src_extension_id(), event.Pass()); | 145 ->DispatchEventToExtension(src_extension_id_, event.Pass()); |
134 | 146 |
135 if (utterance->finished()) | 147 if (utterance->finished()) |
136 delete this; | 148 delete this; |
137 } | 149 } |
138 | 150 |
139 bool TtsSpeakFunction::RunAsync() { | 151 bool TtsSpeakFunction::RunAsync() { |
140 std::string text; | 152 std::string text; |
141 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &text)); | 153 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &text)); |
142 if (text.size() > 32768) { | 154 if (text.size() > 32768) { |
143 error_ = constants::kErrorUtteranceTooLong; | 155 error_ = constants::kErrorUtteranceTooLong; |
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
260 SendResponse(true); | 272 SendResponse(true); |
261 | 273 |
262 UtteranceContinuousParameters continuous_params; | 274 UtteranceContinuousParameters continuous_params; |
263 continuous_params.rate = rate; | 275 continuous_params.rate = rate; |
264 continuous_params.pitch = pitch; | 276 continuous_params.pitch = pitch; |
265 continuous_params.volume = volume; | 277 continuous_params.volume = volume; |
266 | 278 |
267 Utterance* utterance = new Utterance(GetProfile()); | 279 Utterance* utterance = new Utterance(GetProfile()); |
268 utterance->set_text(text); | 280 utterance->set_text(text); |
269 utterance->set_voice_name(voice_name); | 281 utterance->set_voice_name(voice_name); |
270 utterance->set_src_extension_id(extension_id()); | |
271 utterance->set_src_id(src_id); | 282 utterance->set_src_id(src_id); |
272 utterance->set_src_url(source_url()); | 283 utterance->set_src_url(source_url()); |
273 utterance->set_lang(lang); | 284 utterance->set_lang(lang); |
274 utterance->set_gender(gender); | 285 utterance->set_gender(gender); |
275 utterance->set_continuous_parameters(continuous_params); | 286 utterance->set_continuous_parameters(continuous_params); |
276 utterance->set_can_enqueue(can_enqueue); | 287 utterance->set_can_enqueue(can_enqueue); |
277 utterance->set_required_event_types(required_event_types); | 288 utterance->set_required_event_types(required_event_types); |
278 utterance->set_desired_event_types(desired_event_types); | 289 utterance->set_desired_event_types(desired_event_types); |
279 utterance->set_extension_id(voice_extension_id); | 290 utterance->set_extension_id(voice_extension_id); |
280 utterance->set_options(options.get()); | 291 utterance->set_options(options.get()); |
281 utterance->set_event_delegate( | 292 utterance->set_event_delegate( |
282 (new TtsExtensionEventHandler())->AsWeakPtr()); | 293 (new TtsExtensionEventHandler(extension_id()))->AsWeakPtr()); |
283 | 294 |
284 TtsController* controller = TtsController::GetInstance(); | 295 TtsController* controller = TtsController::GetInstance(); |
285 controller->SpeakOrEnqueue(utterance); | 296 controller->SpeakOrEnqueue(utterance); |
286 return true; | 297 return true; |
287 } | 298 } |
288 | 299 |
289 bool TtsStopSpeakingFunction::RunSync() { | 300 bool TtsStopSpeakingFunction::RunSync() { |
290 TtsController::GetInstance()->Stop(); | 301 TtsController::GetInstance()->Stop(); |
291 return true; | 302 return true; |
292 } | 303 } |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
360 } | 371 } |
361 | 372 |
362 static base::LazyInstance<BrowserContextKeyedAPIFactory<TtsAPI> > g_factory = | 373 static base::LazyInstance<BrowserContextKeyedAPIFactory<TtsAPI> > g_factory = |
363 LAZY_INSTANCE_INITIALIZER; | 374 LAZY_INSTANCE_INITIALIZER; |
364 | 375 |
365 BrowserContextKeyedAPIFactory<TtsAPI>* TtsAPI::GetFactoryInstance() { | 376 BrowserContextKeyedAPIFactory<TtsAPI>* TtsAPI::GetFactoryInstance() { |
366 return g_factory.Pointer(); | 377 return g_factory.Pointer(); |
367 } | 378 } |
368 | 379 |
369 } // namespace extensions | 380 } // namespace extensions |
OLD | NEW |