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 TtsController::GetInstance()->AddVoicesChangedDelegate(this); 21 } 22 23 void TtsMessageFilter::OverrideThreadForMessage( 24 const IPC::Message& message, BrowserThread::ID* thread) { 25 switch (message.type()) { 26 case TtsHostMsg_InitializeVoiceList::ID: 27 case TtsHostMsg_Speak::ID: 28 case TtsHostMsg_Pause::ID: 29 case TtsHostMsg_Resume::ID: 30 case TtsHostMsg_Cancel::ID: 31 *thread = BrowserThread::UI; 32 break; 33 } 34 } 35 36 bool TtsMessageFilter::OnMessageReceived(const IPC::Message& message, 37 bool* message_was_ok) { 38 bool handled = true; 39 IPC_BEGIN_MESSAGE_MAP_EX(TtsMessageFilter, message, *message_was_ok) 40 IPC_MESSAGE_HANDLER(TtsHostMsg_InitializeVoiceList, OnInitializeVoiceList) 41 IPC_MESSAGE_HANDLER(TtsHostMsg_Speak, OnSpeak) 42 IPC_MESSAGE_HANDLER(TtsHostMsg_Pause, OnPause) 43 IPC_MESSAGE_HANDLER(TtsHostMsg_Resume, OnResume) 44 IPC_MESSAGE_HANDLER(TtsHostMsg_Cancel, OnCancel) 45 IPC_MESSAGE_UNHANDLED(handled = false) 46 IPC_END_MESSAGE_MAP() 47 return handled; 48 } 49 50 void TtsMessageFilter::OnChannelClosing() { 51 TtsController::GetInstance()->RemoveVoicesChangedDelegate(this); 52 } 53 54 void TtsMessageFilter::OnInitializeVoiceList() { 55 TtsController* tts_controller = TtsController::GetInstance(); 56 std::vector<VoiceData> voices; 57 tts_controller->GetVoices(profile_, &voices); 58 59 std::vector<TtsVoice> out_voices; 60 out_voices.resize(voices.size()); 61 for (size_t i = 0; i < voices.size(); ++i) { 62 TtsVoice& out_voice = out_voices[i]; 63 out_voice.voice_uri = voices[i].name; 64 out_voice.name = voices[i].name; 65 out_voice.lang = voices[i].lang; 66 out_voice.local_service = true; 67 out_voice.is_default = (i == 0); 68 } 69 Send(new TtsMsg_SetVoiceList(out_voices)); 70 } 71 72 void TtsMessageFilter::OnSpeak(const TtsUtteranceRequest& request) { 73 scoped_ptr<Utterance> utterance(new Utterance(profile_)); 74 utterance->set_src_id(request.id); 75 utterance->set_text(request.text); 76 utterance->set_lang(request.lang); 77 utterance->set_voice_name(request.voice); 78 utterance->set_can_enqueue(true); 79 80 UtteranceContinuousParameters params; 81 params.rate = request.rate; 82 params.pitch = request.pitch; 83 params.volume = request.volume; 84 utterance->set_continuous_parameters(params); 85 86 utterance->set_event_delegate(this); 87 88 TtsController::GetInstance()->SpeakOrEnqueue(utterance.release()); 89 } 90 91 void TtsMessageFilter::OnPause() { 92 TtsController::GetInstance()->Pause(); 93 } 94 95 void TtsMessageFilter::OnResume() { 96 TtsController::GetInstance()->Resume(); 97 } 98 99 void TtsMessageFilter::OnCancel() { 100 TtsController::GetInstance()->Stop(); 101 } 102 103 void TtsMessageFilter::OnTtsEvent(Utterance* utterance, 104 TtsEventType event_type, 105 int char_index, 106 const std::string& error_message) { 107 switch (event_type) { 108 case TTS_EVENT_START: 109 Send(new TtsMsg_DidStartSpeaking(utterance->src_id())); 110 break; 111 case TTS_EVENT_END: 112 Send(new TtsMsg_DidFinishSpeaking(utterance->src_id())); 113 break; 114 case TTS_EVENT_WORD: 115 Send(new TtsMsg_WordBoundary(utterance->src_id(), char_index)); 116 break; 117 case TTS_EVENT_SENTENCE: 118 Send(new TtsMsg_SentenceBoundary(utterance->src_id(), char_index)); 119 break; 120 case TTS_EVENT_MARKER: 121 Send(new TtsMsg_MarkerEvent(utterance->src_id(), char_index)); 122 break; 123 case TTS_EVENT_INTERRUPTED: 124 Send(new TtsMsg_WasInterrupted(utterance->src_id())); 125 break; 126 case TTS_EVENT_CANCELLED: 127 Send(new TtsMsg_WasCancelled(utterance->src_id())); 128 break; 129 case TTS_EVENT_ERROR: 130 Send(new TtsMsg_SpeakingErrorOccurred( 131 utterance->src_id(), error_message)); 132 break; 133 case TTS_EVENT_PAUSE: 134 Send(new TtsMsg_DidPauseSpeaking(utterance->src_id())); 135 break; 136 case TTS_EVENT_RESUME: 137 Send(new TtsMsg_DidResumeSpeaking(utterance->src_id())); 138 break; 139 } 140 } 141 142 void TtsMessageFilter::OnVoicesChanged() { 143 OnInitializeVoiceList(); 144 } 145 146 TtsMessageFilter::~TtsMessageFilter() { 147 } 148