summaryrefslogtreecommitdiffstats
path: root/content/renderer
diff options
context:
space:
mode:
Diffstat (limited to 'content/renderer')
-rw-r--r--content/renderer/input_tag_speech_dispatcher.cc38
-rw-r--r--content/renderer/input_tag_speech_dispatcher.h5
-rw-r--r--content/renderer/speech_recognition_dispatcher.cc73
-rw-r--r--content/renderer/speech_recognition_dispatcher.h4
4 files changed, 75 insertions, 45 deletions
diff --git a/content/renderer/input_tag_speech_dispatcher.cc b/content/renderer/input_tag_speech_dispatcher.cc
index 7caa3b0..49f8fbf 100644
--- a/content/renderer/input_tag_speech_dispatcher.cc
+++ b/content/renderer/input_tag_speech_dispatcher.cc
@@ -38,8 +38,8 @@ bool InputTagSpeechDispatcher::OnMessageReceived(
const IPC::Message& message) {
bool handled = true;
IPC_BEGIN_MESSAGE_MAP(InputTagSpeechDispatcher, message)
- IPC_MESSAGE_HANDLER(InputTagSpeechMsg_SetRecognitionResult,
- OnSpeechRecognitionResult)
+ IPC_MESSAGE_HANDLER(InputTagSpeechMsg_SetRecognitionResults,
+ OnSpeechRecognitionResults)
IPC_MESSAGE_HANDLER(InputTagSpeechMsg_RecordingComplete,
OnSpeechRecordingComplete)
IPC_MESSAGE_HANDLER(InputTagSpeechMsg_RecognitionComplete,
@@ -57,7 +57,7 @@ bool InputTagSpeechDispatcher::startRecognition(
const WebKit::WebString& language,
const WebKit::WebString& grammar,
const WebKit::WebSecurityOrigin& origin) {
- VLOG(1) << "InputTagSpeechDispatcher::startRecognition enter";
+ DVLOG(1) << "InputTagSpeechDispatcher::startRecognition enter";
InputTagSpeechHostMsg_StartRecognition_Params params;
params.grammar = UTF16ToUTF8(grammar);
@@ -68,50 +68,54 @@ bool InputTagSpeechDispatcher::startRecognition(
params.element_rect = element_rect;
Send(new InputTagSpeechHostMsg_StartRecognition(params));
- VLOG(1) << "InputTagSpeechDispatcher::startRecognition exit";
+ DVLOG(1) << "InputTagSpeechDispatcher::startRecognition exit";
return true;
}
void InputTagSpeechDispatcher::cancelRecognition(int request_id) {
- VLOG(1) << "InputTagSpeechDispatcher::cancelRecognition enter";
+ DVLOG(1) << "InputTagSpeechDispatcher::cancelRecognition enter";
Send(new InputTagSpeechHostMsg_CancelRecognition(routing_id(), request_id));
- VLOG(1) << "InputTagSpeechDispatcher::cancelRecognition exit";
+ DVLOG(1) << "InputTagSpeechDispatcher::cancelRecognition exit";
}
void InputTagSpeechDispatcher::stopRecording(int request_id) {
- VLOG(1) << "InputTagSpeechDispatcher::stopRecording enter";
+ DVLOG(1) << "InputTagSpeechDispatcher::stopRecording enter";
Send(new InputTagSpeechHostMsg_StopRecording(routing_id(),
request_id));
- VLOG(1) << "InputTagSpeechDispatcher::stopRecording exit";
+ DVLOG(1) << "InputTagSpeechDispatcher::stopRecording exit";
}
-void InputTagSpeechDispatcher::OnSpeechRecognitionResult(
+void InputTagSpeechDispatcher::OnSpeechRecognitionResults(
int request_id,
- const SpeechRecognitionResult& result) {
- VLOG(1) << "InputTagSpeechDispatcher::OnSpeechRecognitionResult enter";
+ const SpeechRecognitionResults& results) {
+ DVLOG(1) << "InputTagSpeechDispatcher::OnSpeechRecognitionResults enter";
+ DCHECK_EQ(results.size(), 1U);
+
+ const SpeechRecognitionResult& result = results[0];
WebKit::WebSpeechInputResultArray webkit_result(result.hypotheses.size());
for (size_t i = 0; i < result.hypotheses.size(); ++i) {
webkit_result[i].assign(result.hypotheses[i].utterance,
result.hypotheses[i].confidence);
}
listener_->setRecognitionResult(request_id, webkit_result);
- VLOG(1) << "InputTagSpeechDispatcher::OnSpeechRecognitionResult exit";
+
+ DVLOG(1) << "InputTagSpeechDispatcher::OnSpeechRecognitionResults exit";
}
void InputTagSpeechDispatcher::OnSpeechRecordingComplete(int request_id) {
- VLOG(1) << "InputTagSpeechDispatcher::OnSpeechRecordingComplete enter";
+ DVLOG(1) << "InputTagSpeechDispatcher::OnSpeechRecordingComplete enter";
listener_->didCompleteRecording(request_id);
- VLOG(1) << "InputTagSpeechDispatcher::OnSpeechRecordingComplete exit";
+ DVLOG(1) << "InputTagSpeechDispatcher::OnSpeechRecordingComplete exit";
}
void InputTagSpeechDispatcher::OnSpeechRecognitionComplete(int request_id) {
- VLOG(1) << "InputTagSpeechDispatcher::OnSpeechRecognitionComplete enter";
+ DVLOG(1) << "InputTagSpeechDispatcher::OnSpeechRecognitionComplete enter";
listener_->didCompleteRecognition(request_id);
- VLOG(1) << "InputTagSpeechDispatcher::OnSpeechRecognitionComplete exit";
+ DVLOG(1) << "InputTagSpeechDispatcher::OnSpeechRecognitionComplete exit";
}
void InputTagSpeechDispatcher::OnSpeechRecognitionToggleSpeechInput() {
- VLOG(1) <<"InputTagSpeechDispatcher::OnSpeechRecognitionToggleSpeechInput";
+ DVLOG(1) <<"InputTagSpeechDispatcher::OnSpeechRecognitionToggleSpeechInput";
WebView* web_view = render_view()->GetWebView();
diff --git a/content/renderer/input_tag_speech_dispatcher.h b/content/renderer/input_tag_speech_dispatcher.h
index eb89e11..caefd26 100644
--- a/content/renderer/input_tag_speech_dispatcher.h
+++ b/content/renderer/input_tag_speech_dispatcher.h
@@ -6,6 +6,7 @@
#define CONTENT_RENDERER_INPUT_TAG_SPEECH_DISPATCHER_H_
#include "base/basictypes.h"
+#include "content/public/common/speech_recognition_result.h"
#include "content/public/renderer/render_view_observer.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebSpeechInputController.h"
@@ -39,8 +40,8 @@ class InputTagSpeechDispatcher : public RenderViewObserver,
virtual void cancelRecognition(int request_id);
virtual void stopRecording(int request_id);
- void OnSpeechRecognitionResult(
- int request_id, const SpeechRecognitionResult& result);
+ void OnSpeechRecognitionResults(
+ int request_id, const SpeechRecognitionResults& results);
void OnSpeechRecordingComplete(int request_id);
void OnSpeechRecognitionComplete(int request_id);
void OnSpeechRecognitionToggleSpeechInput();
diff --git a/content/renderer/speech_recognition_dispatcher.cc b/content/renderer/speech_recognition_dispatcher.cc
index f86b141..2304f5d 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);
}
diff --git a/content/renderer/speech_recognition_dispatcher.h b/content/renderer/speech_recognition_dispatcher.h
index 6218b3f..e9dcb2b 100644
--- a/content/renderer/speech_recognition_dispatcher.h
+++ b/content/renderer/speech_recognition_dispatcher.h
@@ -8,6 +8,7 @@
#include <map>
#include "base/basictypes.h"
+#include "content/public/common/speech_recognition_result.h"
#include "content/public/renderer/render_view_observer.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebVector.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebSpeechRecognitionHandle.h"
@@ -47,7 +48,8 @@ class SpeechRecognitionDispatcher : public RenderViewObserver,
void OnAudioEnded(int request_id);
void OnErrorOccurred(int request_id, const SpeechRecognitionError& error);
void OnRecognitionEnded(int request_id);
- void OnResultRetrieved(int request_id, const SpeechRecognitionResult& result);
+ void OnResultsRetrieved(int request_id,
+ const SpeechRecognitionResults& result);
int GetOrCreateIDForHandle(const WebKit::WebSpeechRecognitionHandle& handle);
bool HandleExists(const WebKit::WebSpeechRecognitionHandle& handle);