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 "content/renderer/speech_recognition_dispatcher.h" | 5 #include "content/renderer/speech_recognition_dispatcher.h" |
6 | 6 |
7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
8 #include "base/utf_string_conversions.h" | 8 #include "base/utf_string_conversions.h" |
9 #include "content/common/speech_recognition_messages.h" | 9 #include "content/common/speech_recognition_messages.h" |
10 #include "content/renderer/render_view_impl.h" | 10 #include "content/renderer/render_view_impl.h" |
(...skipping 28 matching lines...) Expand all Loading... |
39 const IPC::Message& message) { | 39 const IPC::Message& message) { |
40 bool handled = true; | 40 bool handled = true; |
41 IPC_BEGIN_MESSAGE_MAP(SpeechRecognitionDispatcher, message) | 41 IPC_BEGIN_MESSAGE_MAP(SpeechRecognitionDispatcher, message) |
42 IPC_MESSAGE_HANDLER(SpeechRecognitionMsg_Started, OnRecognitionStarted) | 42 IPC_MESSAGE_HANDLER(SpeechRecognitionMsg_Started, OnRecognitionStarted) |
43 IPC_MESSAGE_HANDLER(SpeechRecognitionMsg_AudioStarted, OnAudioStarted) | 43 IPC_MESSAGE_HANDLER(SpeechRecognitionMsg_AudioStarted, OnAudioStarted) |
44 IPC_MESSAGE_HANDLER(SpeechRecognitionMsg_SoundStarted, OnSoundStarted) | 44 IPC_MESSAGE_HANDLER(SpeechRecognitionMsg_SoundStarted, OnSoundStarted) |
45 IPC_MESSAGE_HANDLER(SpeechRecognitionMsg_SoundEnded, OnSoundEnded) | 45 IPC_MESSAGE_HANDLER(SpeechRecognitionMsg_SoundEnded, OnSoundEnded) |
46 IPC_MESSAGE_HANDLER(SpeechRecognitionMsg_AudioEnded, OnAudioEnded) | 46 IPC_MESSAGE_HANDLER(SpeechRecognitionMsg_AudioEnded, OnAudioEnded) |
47 IPC_MESSAGE_HANDLER(SpeechRecognitionMsg_ErrorOccurred, OnErrorOccurred) | 47 IPC_MESSAGE_HANDLER(SpeechRecognitionMsg_ErrorOccurred, OnErrorOccurred) |
48 IPC_MESSAGE_HANDLER(SpeechRecognitionMsg_Ended, OnRecognitionEnded) | 48 IPC_MESSAGE_HANDLER(SpeechRecognitionMsg_Ended, OnRecognitionEnded) |
49 IPC_MESSAGE_HANDLER(SpeechRecognitionMsg_ResultRetrieved, | 49 IPC_MESSAGE_HANDLER(SpeechRecognitionMsg_ResultRetrieved, OnResultRetrieved) |
50 OnResultsRetrieved) | |
51 IPC_MESSAGE_UNHANDLED(handled = false) | 50 IPC_MESSAGE_UNHANDLED(handled = false) |
52 IPC_END_MESSAGE_MAP() | 51 IPC_END_MESSAGE_MAP() |
53 return handled; | 52 return handled; |
54 } | 53 } |
55 | 54 |
56 void SpeechRecognitionDispatcher::start( | 55 void SpeechRecognitionDispatcher::start( |
57 const WebSpeechRecognitionHandle& handle, | 56 const WebSpeechRecognitionHandle& handle, |
58 const WebSpeechRecognitionParams& params, | 57 const WebSpeechRecognitionParams& params, |
59 WebSpeechRecognizerClient* recognizer_client) { | 58 WebSpeechRecognizerClient* recognizer_client) { |
60 DCHECK(!recognizer_client_ || recognizer_client_ == recognizer_client); | 59 DCHECK(!recognizer_client_ || recognizer_client_ == recognizer_client); |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
149 recognizer_client_->didReceiveNoMatch(GetHandleFromID(request_id), | 148 recognizer_client_->didReceiveNoMatch(GetHandleFromID(request_id), |
150 WebSpeechRecognitionResult()); | 149 WebSpeechRecognitionResult()); |
151 } else { | 150 } else { |
152 recognizer_client_->didReceiveError(GetHandleFromID(request_id), | 151 recognizer_client_->didReceiveError(GetHandleFromID(request_id), |
153 WebString(), // TODO(primiano): message? | 152 WebString(), // TODO(primiano): message? |
154 WebKitErrorCode(error.code)); | 153 WebKitErrorCode(error.code)); |
155 } | 154 } |
156 } | 155 } |
157 | 156 |
158 void SpeechRecognitionDispatcher::OnRecognitionEnded(int request_id) { | 157 void SpeechRecognitionDispatcher::OnRecognitionEnded(int request_id) { |
159 // TODO(tommi): It is possible that the handle isn't found in the array if | 158 WebSpeechRecognitionHandle handle = GetHandleFromID(request_id); |
160 // the user just refreshed the page. It seems that we then get a notification | 159 // Note: we need to erase the handle from the map *before* calling didEnd. |
161 // for the previously loaded instance of the page. | 160 // didEnd may call back synchronously to start a new recognition session, |
162 HandleMap::iterator iter = handle_map_.find(request_id); | 161 // and we don't want to delete the handle from the map after that happens. |
163 if (iter == handle_map_.end()) { | 162 handle_map_.erase(request_id); |
164 DLOG(ERROR) << "OnRecognitionEnded called for a handle that doesn't exist"; | 163 recognizer_client_->didEnd(handle); |
165 } else { | |
166 WebSpeechRecognitionHandle handle = iter->second; | |
167 // Note: we need to erase the handle from the map *before* calling didEnd. | |
168 // didEnd may call back synchronously to start a new recognition session, | |
169 // and we don't want to delete the handle from the map after that happens. | |
170 handle_map_.erase(request_id); | |
171 recognizer_client_->didEnd(handle); | |
172 } | |
173 } | 164 } |
174 | 165 |
175 void SpeechRecognitionDispatcher::OnResultsRetrieved( | 166 void SpeechRecognitionDispatcher::OnResultRetrieved( |
176 int request_id, const SpeechRecognitionResults& results) { | 167 int request_id, const SpeechRecognitionResult& result) { |
177 size_t provisional_count = 0; | 168 const size_t num_hypotheses = result.hypotheses.size(); |
178 SpeechRecognitionResults::const_iterator it = results.begin(); | 169 WebSpeechRecognitionResult webkit_result; |
179 for (; it != results.end(); ++it) { | 170 WebVector<WebString> transcripts(num_hypotheses); |
180 if (it->is_provisional) | 171 WebVector<float> confidences(num_hypotheses); |
181 ++provisional_count; | 172 for (size_t i = 0; i < num_hypotheses; ++i) { |
| 173 transcripts[i] = result.hypotheses[i].utterance; |
| 174 confidences[i] = static_cast<float>(result.hypotheses[i].confidence); |
182 } | 175 } |
183 | 176 webkit_result.assign(transcripts, confidences, !result.is_provisional); |
184 WebVector<WebSpeechRecognitionResult> provisional(provisional_count); | 177 // TODO(primiano): Handle history, currently empty. |
185 WebVector<WebSpeechRecognitionResult> final( | 178 WebVector<WebSpeechRecognitionResult> empty_history; |
186 results.size() - provisional_count); | 179 recognizer_client_->didReceiveResult(GetHandleFromID(request_id), |
187 | 180 webkit_result, |
188 int provisional_index = 0, final_index = 0; | 181 0, // result_index |
189 for (it = results.begin(); it != results.end(); ++it) { | 182 empty_history); |
190 const SpeechRecognitionResult& result = (*it); | |
191 WebSpeechRecognitionResult* webkit_result = result.is_provisional ? | |
192 &provisional[provisional_index++] : &final[final_index++]; | |
193 | |
194 const size_t num_hypotheses = result.hypotheses.size(); | |
195 WebVector<WebString> transcripts(num_hypotheses); | |
196 WebVector<float> confidences(num_hypotheses); | |
197 for (size_t i = 0; i < num_hypotheses; ++i) { | |
198 transcripts[i] = result.hypotheses[i].utterance; | |
199 confidences[i] = static_cast<float>(result.hypotheses[i].confidence); | |
200 } | |
201 webkit_result->assign(transcripts, confidences, !result.is_provisional); | |
202 } | |
203 | |
204 recognizer_client_->didReceiveResults( | |
205 GetHandleFromID(request_id), final, provisional); | |
206 } | 183 } |
207 | 184 |
208 | 185 |
209 int SpeechRecognitionDispatcher::GetOrCreateIDForHandle( | 186 int SpeechRecognitionDispatcher::GetOrCreateIDForHandle( |
210 const WebSpeechRecognitionHandle& handle) { | 187 const WebSpeechRecognitionHandle& handle) { |
211 // Search first for an existing mapping. | 188 // Search first for an existing mapping. |
212 for (HandleMap::iterator iter = handle_map_.begin(); | 189 for (HandleMap::iterator iter = handle_map_.begin(); |
213 iter != handle_map_.end(); | 190 iter != handle_map_.end(); |
214 ++iter) { | 191 ++iter) { |
215 if (iter->second.equals(handle)) | 192 if (iter->second.equals(handle)) |
(...skipping 18 matching lines...) Expand all Loading... |
234 } | 211 } |
235 | 212 |
236 const WebSpeechRecognitionHandle& SpeechRecognitionDispatcher::GetHandleFromID( | 213 const WebSpeechRecognitionHandle& SpeechRecognitionDispatcher::GetHandleFromID( |
237 int request_id) { | 214 int request_id) { |
238 HandleMap::iterator iter = handle_map_.find(request_id); | 215 HandleMap::iterator iter = handle_map_.find(request_id); |
239 DCHECK(iter != handle_map_.end()); | 216 DCHECK(iter != handle_map_.end()); |
240 return iter->second; | 217 return iter->second; |
241 } | 218 } |
242 | 219 |
243 } // namespace content | 220 } // namespace content |
OLD | NEW |