Chromium Code Reviews| 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 std::string src_extension_id_; | |
|
David Tseng
2014/10/02 22:49:38
nit: migrate the [modified] comment as well?
dmazzoni
2014/10/21 18:50:55
Done.
| |
| 93 }; | 98 }; |
| 94 | 99 |
| 100 TtsExtensionEventHandler::TtsExtensionEventHandler( | |
| 101 const std::string& src_extension_id) | |
| 102 : src_extension_id_(src_extension_id) { | |
| 103 } | |
| 104 | |
| 95 void TtsExtensionEventHandler::OnTtsEvent(Utterance* utterance, | 105 void TtsExtensionEventHandler::OnTtsEvent(Utterance* utterance, |
| 96 TtsEventType event_type, | 106 TtsEventType event_type, |
| 97 int char_index, | 107 int char_index, |
| 98 const std::string& error_message) { | 108 const std::string& error_message) { |
| 99 if (utterance->src_id() < 0) { | 109 if (utterance->src_id() < 0) { |
| 100 if (utterance->finished()) | 110 if (utterance->finished()) |
| 101 delete this; | 111 delete this; |
| 102 return; | 112 return; |
| 103 } | 113 } |
| 104 | 114 |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 123 details->SetBoolean(constants::kIsFinalEventKey, utterance->finished()); | 133 details->SetBoolean(constants::kIsFinalEventKey, utterance->finished()); |
| 124 | 134 |
| 125 scoped_ptr<base::ListValue> arguments(new base::ListValue()); | 135 scoped_ptr<base::ListValue> arguments(new base::ListValue()); |
| 126 arguments->Set(0, details.release()); | 136 arguments->Set(0, details.release()); |
| 127 | 137 |
| 128 scoped_ptr<extensions::Event> event( | 138 scoped_ptr<extensions::Event> event( |
| 129 new extensions::Event(events::kOnEvent, arguments.Pass())); | 139 new extensions::Event(events::kOnEvent, arguments.Pass())); |
| 130 event->restrict_to_browser_context = utterance->browser_context(); | 140 event->restrict_to_browser_context = utterance->browser_context(); |
| 131 event->event_url = utterance->src_url(); | 141 event->event_url = utterance->src_url(); |
| 132 extensions::EventRouter::Get(utterance->browser_context()) | 142 extensions::EventRouter::Get(utterance->browser_context()) |
| 133 ->DispatchEventToExtension(utterance->src_extension_id(), event.Pass()); | 143 ->DispatchEventToExtension(src_extension_id_, event.Pass()); |
| 134 | 144 |
| 135 if (utterance->finished()) | 145 if (utterance->finished()) |
| 136 delete this; | 146 delete this; |
| 137 } | 147 } |
| 138 | 148 |
| 139 bool TtsSpeakFunction::RunAsync() { | 149 bool TtsSpeakFunction::RunAsync() { |
| 140 std::string text; | 150 std::string text; |
| 141 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &text)); | 151 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &text)); |
| 142 if (text.size() > 32768) { | 152 if (text.size() > 32768) { |
| 143 error_ = constants::kErrorUtteranceTooLong; | 153 error_ = constants::kErrorUtteranceTooLong; |
| (...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 260 SendResponse(true); | 270 SendResponse(true); |
| 261 | 271 |
| 262 UtteranceContinuousParameters continuous_params; | 272 UtteranceContinuousParameters continuous_params; |
| 263 continuous_params.rate = rate; | 273 continuous_params.rate = rate; |
| 264 continuous_params.pitch = pitch; | 274 continuous_params.pitch = pitch; |
| 265 continuous_params.volume = volume; | 275 continuous_params.volume = volume; |
| 266 | 276 |
| 267 Utterance* utterance = new Utterance(GetProfile()); | 277 Utterance* utterance = new Utterance(GetProfile()); |
| 268 utterance->set_text(text); | 278 utterance->set_text(text); |
| 269 utterance->set_voice_name(voice_name); | 279 utterance->set_voice_name(voice_name); |
| 270 utterance->set_src_extension_id(extension_id()); | |
| 271 utterance->set_src_id(src_id); | 280 utterance->set_src_id(src_id); |
| 272 utterance->set_src_url(source_url()); | 281 utterance->set_src_url(source_url()); |
| 273 utterance->set_lang(lang); | 282 utterance->set_lang(lang); |
| 274 utterance->set_gender(gender); | 283 utterance->set_gender(gender); |
| 275 utterance->set_continuous_parameters(continuous_params); | 284 utterance->set_continuous_parameters(continuous_params); |
| 276 utterance->set_can_enqueue(can_enqueue); | 285 utterance->set_can_enqueue(can_enqueue); |
| 277 utterance->set_required_event_types(required_event_types); | 286 utterance->set_required_event_types(required_event_types); |
| 278 utterance->set_desired_event_types(desired_event_types); | 287 utterance->set_desired_event_types(desired_event_types); |
| 279 utterance->set_extension_id(voice_extension_id); | 288 utterance->set_extension_id(voice_extension_id); |
| 280 utterance->set_options(options.get()); | 289 utterance->set_options(options.get()); |
| 281 utterance->set_event_delegate( | 290 utterance->set_event_delegate( |
| 282 (new TtsExtensionEventHandler())->AsWeakPtr()); | 291 (new TtsExtensionEventHandler(extension_id()))->AsWeakPtr()); |
| 283 | 292 |
| 284 TtsController* controller = TtsController::GetInstance(); | 293 TtsController* controller = TtsController::GetInstance(); |
| 285 controller->SpeakOrEnqueue(utterance); | 294 controller->SpeakOrEnqueue(utterance); |
| 286 return true; | 295 return true; |
| 287 } | 296 } |
| 288 | 297 |
| 289 bool TtsStopSpeakingFunction::RunSync() { | 298 bool TtsStopSpeakingFunction::RunSync() { |
| 290 TtsController::GetInstance()->Stop(); | 299 TtsController::GetInstance()->Stop(); |
| 291 return true; | 300 return true; |
| 292 } | 301 } |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 360 } | 369 } |
| 361 | 370 |
| 362 static base::LazyInstance<BrowserContextKeyedAPIFactory<TtsAPI> > g_factory = | 371 static base::LazyInstance<BrowserContextKeyedAPIFactory<TtsAPI> > g_factory = |
| 363 LAZY_INSTANCE_INITIALIZER; | 372 LAZY_INSTANCE_INITIALIZER; |
| 364 | 373 |
| 365 BrowserContextKeyedAPIFactory<TtsAPI>* TtsAPI::GetFactoryInstance() { | 374 BrowserContextKeyedAPIFactory<TtsAPI>* TtsAPI::GetFactoryInstance() { |
| 366 return g_factory.Pointer(); | 375 return g_factory.Pointer(); |
| 367 } | 376 } |
| 368 | 377 |
| 369 } // namespace extensions | 378 } // namespace extensions |
| OLD | NEW |