Index: content/renderer/speech_recognition_dispatcher.cc |
diff --git a/content/renderer/speech_recognition_dispatcher.cc b/content/renderer/speech_recognition_dispatcher.cc |
index f86b141be1cbb5a3b385bc8b0bf675064f0ceb04..2304f5dce979331902a28d98adcfe729326f1c81 100644 |
--- a/content/renderer/speech_recognition_dispatcher.cc |
+++ b/content/renderer/speech_recognition_dispatcher.cc |
@@ -46,7 +46,8 @@ bool SpeechRecognitionDispatcher::OnMessageReceived( |
IPC_MESSAGE_HANDLER(SpeechRecognitionMsg_AudioEnded, OnAudioEnded) |
IPC_MESSAGE_HANDLER(SpeechRecognitionMsg_ErrorOccurred, OnErrorOccurred) |
IPC_MESSAGE_HANDLER(SpeechRecognitionMsg_Ended, OnRecognitionEnded) |
- IPC_MESSAGE_HANDLER(SpeechRecognitionMsg_ResultRetrieved, OnResultRetrieved) |
+ IPC_MESSAGE_HANDLER(SpeechRecognitionMsg_ResultRetrieved, |
+ OnResultsRetrieved) |
IPC_MESSAGE_UNHANDLED(handled = false) |
IPC_END_MESSAGE_MAP() |
return handled; |
@@ -155,31 +156,53 @@ void SpeechRecognitionDispatcher::OnErrorOccurred( |
} |
void SpeechRecognitionDispatcher::OnRecognitionEnded(int request_id) { |
- WebSpeechRecognitionHandle handle = GetHandleFromID(request_id); |
- // Note: we need to erase the handle from the map *before* calling didEnd. |
- // didEnd may call back synchronously to start a new recognition session, |
- // and we don't want to delete the handle from the map after that happens. |
- handle_map_.erase(request_id); |
- recognizer_client_->didEnd(handle); |
-} |
- |
-void SpeechRecognitionDispatcher::OnResultRetrieved( |
- int request_id, const SpeechRecognitionResult& result) { |
- const size_t num_hypotheses = result.hypotheses.size(); |
- WebSpeechRecognitionResult webkit_result; |
- WebVector<WebString> transcripts(num_hypotheses); |
- WebVector<float> confidences(num_hypotheses); |
- for (size_t i = 0; i < num_hypotheses; ++i) { |
- transcripts[i] = result.hypotheses[i].utterance; |
- confidences[i] = static_cast<float>(result.hypotheses[i].confidence); |
+ // TODO(tommi): It is possible that the handle isn't found in the array if |
+ // the user just refreshed the page. It seems that we then get a notification |
+ // for the previously loaded instance of the page. |
+ HandleMap::iterator iter = handle_map_.find(request_id); |
+ if (iter == handle_map_.end()) { |
+ DLOG(ERROR) << "OnRecognitionEnded called for a handle that doesn't exist"; |
+ } else { |
+ WebSpeechRecognitionHandle handle = iter->second; |
+ // Note: we need to erase the handle from the map *before* calling didEnd. |
+ // didEnd may call back synchronously to start a new recognition session, |
+ // and we don't want to delete the handle from the map after that happens. |
+ handle_map_.erase(request_id); |
+ recognizer_client_->didEnd(handle); |
+ } |
+} |
+ |
+void SpeechRecognitionDispatcher::OnResultsRetrieved( |
+ int request_id, const SpeechRecognitionResults& results) { |
+ size_t provisional_count = 0; |
+ SpeechRecognitionResults::const_iterator it = results.begin(); |
+ for (; it != results.end(); ++it) { |
+ if (it->is_provisional) |
+ ++provisional_count; |
+ } |
+ |
+ WebVector<WebSpeechRecognitionResult> provisional(provisional_count); |
+ WebVector<WebSpeechRecognitionResult> final( |
+ results.size() - provisional_count); |
+ |
+ int provisional_index = 0, final_index = 0; |
+ for (it = results.begin(); it != results.end(); ++it) { |
+ const SpeechRecognitionResult& result = (*it); |
+ WebSpeechRecognitionResult* webkit_result = result.is_provisional ? |
+ &provisional[provisional_index++] : &final[final_index++]; |
+ |
+ const size_t num_hypotheses = result.hypotheses.size(); |
+ WebVector<WebString> transcripts(num_hypotheses); |
+ WebVector<float> confidences(num_hypotheses); |
+ for (size_t i = 0; i < num_hypotheses; ++i) { |
+ transcripts[i] = result.hypotheses[i].utterance; |
+ confidences[i] = static_cast<float>(result.hypotheses[i].confidence); |
+ } |
+ webkit_result->assign(transcripts, confidences, !result.is_provisional); |
} |
- webkit_result.assign(transcripts, confidences, !result.is_provisional); |
- // TODO(primiano): Handle history, currently empty. |
- WebVector<WebSpeechRecognitionResult> empty_history; |
- recognizer_client_->didReceiveResult(GetHandleFromID(request_id), |
- webkit_result, |
- 0, // result_index |
- empty_history); |
+ |
+ recognizer_client_->didReceiveResults( |
+ GetHandleFromID(request_id), final, provisional); |
} |