OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2013 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 #include "chrome/browser/speech/tts_message_filter.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/logging.h" |
| 9 #include "chrome/browser/profiles/profile.h" |
| 10 #include "chrome/browser/speech/tts_controller.h" |
| 11 #include "chrome/browser/speech/tts_message_filter.h" |
| 12 #include "content/public/browser/browser_context.h" |
| 13 #include "content/public/browser/render_process_host.h" |
| 14 |
| 15 using content::BrowserThread; |
| 16 |
| 17 TtsMessageFilter::TtsMessageFilter(int render_process_id, Profile* profile) |
| 18 : render_process_id_(render_process_id), |
| 19 profile_(profile) { |
| 20 } |
| 21 |
| 22 TtsMessageFilter::~TtsMessageFilter() { |
| 23 } |
| 24 |
| 25 void TtsMessageFilter::OverrideThreadForMessage( |
| 26 const IPC::Message& message, BrowserThread::ID* thread) { |
| 27 switch (message.type()) { |
| 28 case TtsHostMsg_InitializeVoiceList::ID: |
| 29 case TtsHostMsg_Speak::ID: |
| 30 case TtsHostMsg_Pause::ID: |
| 31 case TtsHostMsg_Resume::ID: |
| 32 case TtsHostMsg_Cancel::ID: |
| 33 *thread = BrowserThread::UI; |
| 34 break; |
| 35 } |
| 36 } |
| 37 |
| 38 bool TtsMessageFilter::OnMessageReceived(const IPC::Message& message, |
| 39 bool* message_was_ok) { |
| 40 bool handled = true; |
| 41 IPC_BEGIN_MESSAGE_MAP_EX(TtsMessageFilter, message, *message_was_ok) |
| 42 IPC_MESSAGE_HANDLER(TtsHostMsg_InitializeVoiceList, OnInitializeVoiceList) |
| 43 IPC_MESSAGE_HANDLER(TtsHostMsg_Speak, OnSpeak) |
| 44 IPC_MESSAGE_HANDLER(TtsHostMsg_Pause, OnPause) |
| 45 IPC_MESSAGE_HANDLER(TtsHostMsg_Resume, OnResume) |
| 46 IPC_MESSAGE_HANDLER(TtsHostMsg_Cancel, OnCancel) |
| 47 IPC_MESSAGE_UNHANDLED(handled = false) |
| 48 IPC_END_MESSAGE_MAP() |
| 49 return handled; |
| 50 } |
| 51 |
| 52 void TtsMessageFilter::OnInitializeVoiceList() { |
| 53 TtsController* tts_controller = TtsController::GetInstance(); |
| 54 std::vector<VoiceData> voices; |
| 55 tts_controller->GetVoices(profile_, &voices); |
| 56 |
| 57 std::vector<TtsVoice> out_voices; |
| 58 out_voices.resize(voices.size()); |
| 59 for (size_t i = 0; i < voices.size(); ++i) { |
| 60 TtsVoice& out_voice = out_voices[i]; |
| 61 out_voice.voice_uri = voices[i].name; |
| 62 out_voice.name = voices[i].name; |
| 63 out_voice.lang = voices[i].lang; |
| 64 out_voice.local_service = true; |
| 65 out_voice.is_default = (i == 0); |
| 66 } |
| 67 Send(new TtsMsg_SetVoiceList(out_voices)); |
| 68 } |
| 69 |
| 70 void TtsMessageFilter::OnSpeak(const TtsUtteranceRequest& request) { |
| 71 scoped_ptr<Utterance> utterance(new Utterance(profile_)); |
| 72 utterance->set_src_id(request.id); |
| 73 utterance->set_text(request.text); |
| 74 utterance->set_lang(request.lang); |
| 75 utterance->set_voice_name(request.voice); |
| 76 utterance->set_can_enqueue(true); |
| 77 |
| 78 UtteranceContinuousParameters params; |
| 79 params.rate = request.rate; |
| 80 params.pitch = request.pitch; |
| 81 params.volume = request.volume; |
| 82 utterance->set_continuous_parameters(params); |
| 83 |
| 84 utterance->set_event_delegate(this); |
| 85 |
| 86 TtsController::GetInstance()->SpeakOrEnqueue(utterance.release()); |
| 87 } |
| 88 |
| 89 void TtsMessageFilter::OnPause() { |
| 90 // TODO(dmazzoni): Not supported by TtsController yet. |
| 91 } |
| 92 |
| 93 void TtsMessageFilter::OnResume() { |
| 94 // TODO(dmazzoni): Not supported by TtsController yet. |
| 95 } |
| 96 |
| 97 void TtsMessageFilter::OnCancel() { |
| 98 TtsController::GetInstance()->Stop(); |
| 99 } |
| 100 |
| 101 void TtsMessageFilter::OnTtsEvent(Utterance* utterance, |
| 102 TtsEventType event_type, |
| 103 int char_index, |
| 104 const std::string& error_message) { |
| 105 switch (event_type) { |
| 106 case TTS_EVENT_START: |
| 107 Send(new TtsMsg_DidStartSpeaking(utterance->src_id())); |
| 108 break; |
| 109 case TTS_EVENT_END: |
| 110 Send(new TtsMsg_DidFinishSpeaking(utterance->src_id())); |
| 111 break; |
| 112 case TTS_EVENT_WORD: |
| 113 Send(new TtsMsg_WordBoundary(utterance->src_id(), char_index)); |
| 114 break; |
| 115 case TTS_EVENT_SENTENCE: |
| 116 Send(new TtsMsg_SentenceBoundary(utterance->src_id(), char_index)); |
| 117 break; |
| 118 case TTS_EVENT_MARKER: |
| 119 Send(new TtsMsg_MarkerEvent(utterance->src_id(), char_index)); |
| 120 break; |
| 121 case TTS_EVENT_INTERRUPTED: |
| 122 Send(new TtsMsg_WasInterrupted(utterance->src_id())); |
| 123 break; |
| 124 case TTS_EVENT_CANCELLED: |
| 125 Send(new TtsMsg_WasCancelled(utterance->src_id())); |
| 126 break; |
| 127 case TTS_EVENT_ERROR: |
| 128 Send(new TtsMsg_SpeakingErrorOccurred( |
| 129 utterance->src_id(), error_message)); |
| 130 break; |
| 131 } |
| 132 } |
OLD | NEW |