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