Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "content/renderer/speech_recognition_dispatcher.h" | |
| 6 | |
| 7 #include "base/basictypes.h" | |
| 8 #include "base/utf_string_conversions.h" | |
| 9 #include "content/common/speech_recognition_messages.h" | |
| 10 #include "content/renderer/render_view_impl.h" | |
| 11 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebString.h" | |
| 12 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebVector.h" | |
| 13 #include "third_party/WebKit/Source/WebKit/chromium/public/WebSpeechGrammar.h" | |
| 14 #include "third_party/WebKit/Source/WebKit/chromium/public/WebSpeechRecognitionP arams.h" | |
| 15 #include "third_party/WebKit/Source/WebKit/chromium/public/WebSpeechRecognitionR esult.h" | |
| 16 #include "third_party/WebKit/Source/WebKit/chromium/public/WebSpeechRecognizer.h " | |
|
jam
2012/05/23 15:41:24
nit: this is already in your header, so take out
Primiano Tucci (use gerrit)
2012/05/23 17:11:56
Done.
| |
| 17 #include "third_party/WebKit/Source/WebKit/chromium/public/WebSpeechRecognizerCl ient.h" | |
| 18 | |
| 19 using content::SpeechRecognitionError; | |
| 20 using content::SpeechRecognitionResult; | |
| 21 using WebKit::WebVector; | |
| 22 using WebKit::WebString; | |
| 23 using WebKit::WebSpeechGrammar; | |
| 24 using WebKit::WebSpeechRecognitionHandle; | |
| 25 using WebKit::WebSpeechRecognitionResult; | |
| 26 using WebKit::WebSpeechRecognitionParams; | |
| 27 using WebKit::WebSpeechRecognizerClient; | |
| 28 | |
| 29 SpeechRecognitionDispatcher::SpeechRecognitionDispatcher( | |
| 30 RenderViewImpl* render_view) | |
| 31 : content::RenderViewObserver(render_view), | |
| 32 recognizer_client_(NULL), | |
| 33 next_id_(1) { | |
| 34 } | |
| 35 | |
| 36 SpeechRecognitionDispatcher::~SpeechRecognitionDispatcher() { | |
| 37 } | |
| 38 | |
| 39 bool SpeechRecognitionDispatcher::OnMessageReceived( | |
| 40 const IPC::Message& message) { | |
| 41 bool handled = true; | |
| 42 IPC_BEGIN_MESSAGE_MAP(SpeechRecognitionDispatcher, message) | |
| 43 IPC_MESSAGE_HANDLER(SpeechRecognitionMsg_Started, OnRecognitionStarted) | |
| 44 IPC_MESSAGE_HANDLER(SpeechRecognitionMsg_AudioStarted, OnAudioStarted) | |
| 45 IPC_MESSAGE_HANDLER(SpeechRecognitionMsg_SoundStarted, OnSoundStarted) | |
| 46 IPC_MESSAGE_HANDLER(SpeechRecognitionMsg_SoundEnded, OnSoundEnded) | |
| 47 IPC_MESSAGE_HANDLER(SpeechRecognitionMsg_AudioEnded, OnAudioEnded) | |
| 48 IPC_MESSAGE_HANDLER(SpeechRecognitionMsg_ErrorOccurred, OnErrorOccurred) | |
| 49 IPC_MESSAGE_HANDLER(SpeechRecognitionMsg_Ended, OnRecognitionEnded) | |
| 50 IPC_MESSAGE_HANDLER(SpeechRecognitionMsg_ResultRetrieved, OnResultRetrieved) | |
| 51 IPC_MESSAGE_UNHANDLED(handled = false) | |
| 52 IPC_END_MESSAGE_MAP() | |
| 53 return handled; | |
| 54 } | |
| 55 | |
| 56 void SpeechRecognitionDispatcher::start( | |
| 57 const WebSpeechRecognitionHandle& handle, | |
| 58 const WebSpeechRecognitionParams& params, | |
| 59 WebSpeechRecognizerClient* recognizer_client) { | |
| 60 //TODO(primiano) What to do if a start is issued to an already started object? | |
| 61 DCHECK(!recognizer_client_ || recognizer_client_ == recognizer_client); | |
| 62 recognizer_client_ = recognizer_client; | |
| 63 | |
| 64 SpeechRecognitionHostMsg_StartRequest_Params msg_params; | |
| 65 for (size_t i = 0; i < params.grammars().size(); ++i) { | |
| 66 const WebSpeechGrammar& grammar = params.grammars()[i]; | |
| 67 msg_params.grammars.push_back( | |
| 68 content::SpeechRecognitionGrammar(grammar.src().spec(), | |
| 69 grammar.weight())); | |
| 70 } | |
| 71 msg_params.language = UTF16ToUTF8(params.language()); | |
| 72 msg_params.is_one_shot = !params.continuous(); | |
| 73 msg_params.origin_url = ""; // TODO(primiano) we need an origin from WebKit. | |
| 74 msg_params.render_view_id = routing_id(); | |
| 75 msg_params.request_id = GetIDForHandle(handle); | |
| 76 Send(new SpeechRecognitionHostMsg_StartRequest(msg_params)); | |
| 77 } | |
| 78 | |
| 79 void SpeechRecognitionDispatcher::stop( | |
| 80 const WebSpeechRecognitionHandle& handle, | |
| 81 WebSpeechRecognizerClient* recognizer_client) { | |
| 82 DCHECK(recognizer_client_ == recognizer_client); | |
| 83 Send(new SpeechRecognitionHostMsg_StopCaptureRequest(routing_id(), | |
| 84 GetIDForHandle(handle))); | |
| 85 } | |
| 86 | |
| 87 void SpeechRecognitionDispatcher::abort( | |
| 88 const WebSpeechRecognitionHandle& handle, | |
| 89 WebSpeechRecognizerClient* recognizer_client) { | |
| 90 Send(new SpeechRecognitionHostMsg_AbortRequest(routing_id(), | |
| 91 GetIDForHandle(handle))); | |
| 92 } | |
| 93 | |
| 94 void SpeechRecognitionDispatcher::OnRecognitionStarted(int request_id) { | |
| 95 DCHECK(recognizer_client_); | |
|
jam
2012/05/23 15:41:24
nit: all these dchecks aren't necessary. if it's n
Primiano Tucci (use gerrit)
2012/05/23 17:11:56
Done.
| |
| 96 recognizer_client_->didStart(GetHandleFromID(request_id)); | |
| 97 } | |
| 98 | |
| 99 void SpeechRecognitionDispatcher::OnAudioStarted(int request_id) { | |
| 100 DCHECK(recognizer_client_); | |
| 101 recognizer_client_->didStartAudio(GetHandleFromID(request_id)); | |
| 102 } | |
| 103 | |
| 104 void SpeechRecognitionDispatcher::OnSoundStarted(int request_id) { | |
| 105 DCHECK(recognizer_client_); | |
| 106 recognizer_client_->didStartSound(GetHandleFromID(request_id)); | |
| 107 } | |
| 108 | |
| 109 void SpeechRecognitionDispatcher::OnSoundEnded(int request_id) { | |
| 110 DCHECK(recognizer_client_); | |
| 111 recognizer_client_->didEndSound(GetHandleFromID(request_id)); | |
| 112 } | |
| 113 | |
| 114 void SpeechRecognitionDispatcher::OnAudioEnded(int request_id) { | |
| 115 DCHECK(recognizer_client_); | |
| 116 recognizer_client_->didEndAudio(GetHandleFromID(request_id)); | |
| 117 } | |
| 118 | |
| 119 void SpeechRecognitionDispatcher::OnErrorOccurred( | |
| 120 int request_id, const SpeechRecognitionError& error) { | |
| 121 DCHECK(recognizer_client_); | |
| 122 if (error.code == content::SPEECH_RECOGNITION_ERROR_NO_MATCH) { | |
| 123 recognizer_client_->didReceiveNoMatch(GetHandleFromID(request_id), | |
| 124 WebSpeechRecognitionResult()); | |
| 125 } else { | |
| 126 // TODO(primiano) speech_recognition_error.h must be updated with the new | |
| 127 // API specs soon. | |
| 128 WebSpeechRecognizerClient::ErrorCode wk_error_code; | |
| 129 switch (error.code) { | |
| 130 case content::SPEECH_RECOGNITION_ERROR_ABORTED: | |
| 131 wk_error_code = WebSpeechRecognizerClient::AbortedError; | |
| 132 break; | |
| 133 case content::SPEECH_RECOGNITION_ERROR_AUDIO: | |
| 134 wk_error_code = WebSpeechRecognizerClient::AudioCaptureError; | |
| 135 break; | |
| 136 case content::SPEECH_RECOGNITION_ERROR_NETWORK: | |
| 137 wk_error_code = WebSpeechRecognizerClient::NetworkError; | |
| 138 break; | |
| 139 case content::SPEECH_RECOGNITION_ERROR_NO_SPEECH: | |
| 140 wk_error_code = WebSpeechRecognizerClient::NoSpeechError; | |
| 141 break; | |
| 142 case content::SPEECH_RECOGNITION_ERROR_BAD_GRAMMAR: | |
| 143 wk_error_code = WebSpeechRecognizerClient::BadGrammarError; | |
| 144 break; | |
| 145 default: | |
| 146 NOTREACHED(); | |
| 147 wk_error_code = WebSpeechRecognizerClient::OtherError; | |
| 148 } | |
| 149 recognizer_client_->didReceiveError(GetHandleFromID(request_id), | |
| 150 WebString(), // TODO(primiano) message? | |
| 151 wk_error_code); | |
| 152 } | |
| 153 } | |
| 154 | |
| 155 void SpeechRecognitionDispatcher::OnRecognitionEnded(int request_id) { | |
| 156 DCHECK(recognizer_client_); | |
| 157 recognizer_client_->didEnd(GetHandleFromID(request_id)); | |
| 158 handle_map_.erase(request_id); | |
| 159 } | |
| 160 | |
| 161 void SpeechRecognitionDispatcher::OnResultRetrieved( | |
| 162 int request_id, const SpeechRecognitionResult& result) { | |
| 163 DCHECK(recognizer_client_); | |
| 164 | |
| 165 const size_t num_hypotheses = result.hypotheses.size(); | |
| 166 WebSpeechRecognitionResult webkit_result; | |
| 167 WebVector<WebString> transcripts(num_hypotheses); | |
| 168 WebVector<float> confidences(num_hypotheses); | |
| 169 for (size_t i = 0; i < num_hypotheses; ++i) { | |
| 170 transcripts[i] = result.hypotheses[i].utterance; | |
| 171 confidences[i] = static_cast<float>(result.hypotheses[i].confidence); | |
| 172 } | |
| 173 webkit_result.assign(transcripts, confidences, !result.provisional); | |
| 174 // TODO(primiano) Handle history, currently empty. | |
| 175 WebVector<WebSpeechRecognitionResult> empty_history; | |
| 176 recognizer_client_->didReceiveResult(GetHandleFromID(request_id), | |
| 177 webkit_result, | |
| 178 0, // result_index | |
| 179 empty_history); | |
| 180 } | |
| 181 | |
| 182 int SpeechRecognitionDispatcher::GetIDForHandle( | |
| 183 const WebSpeechRecognitionHandle& handle) { | |
| 184 // Search first for an existing mapping. | |
| 185 for (HandleMap::iterator iter = handle_map_.begin(); | |
| 186 iter != handle_map_.end(); | |
| 187 ++iter) { | |
| 188 if (iter->second.equals(handle)) | |
| 189 return iter->first; | |
| 190 } | |
| 191 // If no existing mapping found, create a new one. | |
| 192 const int new_id = next_id_; | |
| 193 handle_map_[new_id] = handle; | |
| 194 ++next_id_; | |
| 195 return new_id; | |
| 196 } | |
| 197 | |
| 198 const WebSpeechRecognitionHandle& SpeechRecognitionDispatcher::GetHandleFromID( | |
| 199 int request_id) { | |
| 200 HandleMap::iterator iter = handle_map_.find(request_id); | |
| 201 DCHECK(iter != handle_map_.end()); | |
| 202 return iter->second; | |
| 203 } | |
| OLD | NEW |