blob: 178abf4610c286c751047ff46ec252089f2946bb [file] [log] [blame]
[email protected]64d09222012-05-25 10:10:341// 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"
[email protected]74ebfb12013-06-07 20:48:008#include "base/strings/utf_string_conversions.h"
[email protected]64d09222012-05-25 10:10:349#include "content/common/speech_recognition_messages.h"
10#include "content/renderer/render_view_impl.h"
[email protected]5c30b5e02013-05-30 03:46:0811#include "third_party/WebKit/public/platform/WebString.h"
12#include "third_party/WebKit/public/platform/WebVector.h"
[email protected]2255a9332013-06-17 05:12:3113#include "third_party/WebKit/public/web/WebSpeechGrammar.h"
14#include "third_party/WebKit/public/web/WebSpeechRecognitionParams.h"
15#include "third_party/WebKit/public/web/WebSpeechRecognitionResult.h"
16#include "third_party/WebKit/public/web/WebSpeechRecognizerClient.h"
[email protected]64d09222012-05-25 10:10:3417
[email protected]180ef242013-11-07 06:50:4618using blink::WebVector;
19using blink::WebString;
20using blink::WebSpeechGrammar;
21using blink::WebSpeechRecognitionHandle;
22using blink::WebSpeechRecognitionResult;
23using blink::WebSpeechRecognitionParams;
24using blink::WebSpeechRecognizerClient;
[email protected]64d09222012-05-25 10:10:3425
[email protected]e9ff79c2012-10-19 21:31:2626namespace content {
27
[email protected]64d09222012-05-25 10:10:3428SpeechRecognitionDispatcher::SpeechRecognitionDispatcher(
29 RenderViewImpl* render_view)
[email protected]e9ff79c2012-10-19 21:31:2630 : RenderViewObserver(render_view),
[email protected]64d09222012-05-25 10:10:3431 recognizer_client_(NULL),
32 next_id_(1) {
33}
34
35SpeechRecognitionDispatcher::~SpeechRecognitionDispatcher() {
36}
37
[email protected]e976c3c52014-07-24 17:41:5538void SpeechRecognitionDispatcher::AbortAllRecognitions() {
39 Send(new SpeechRecognitionHostMsg_AbortAllRequests(
40 routing_id()));
41}
42
[email protected]64d09222012-05-25 10:10:3443bool SpeechRecognitionDispatcher::OnMessageReceived(
44 const IPC::Message& message) {
45 bool handled = true;
46 IPC_BEGIN_MESSAGE_MAP(SpeechRecognitionDispatcher, message)
47 IPC_MESSAGE_HANDLER(SpeechRecognitionMsg_Started, OnRecognitionStarted)
48 IPC_MESSAGE_HANDLER(SpeechRecognitionMsg_AudioStarted, OnAudioStarted)
49 IPC_MESSAGE_HANDLER(SpeechRecognitionMsg_SoundStarted, OnSoundStarted)
50 IPC_MESSAGE_HANDLER(SpeechRecognitionMsg_SoundEnded, OnSoundEnded)
51 IPC_MESSAGE_HANDLER(SpeechRecognitionMsg_AudioEnded, OnAudioEnded)
52 IPC_MESSAGE_HANDLER(SpeechRecognitionMsg_ErrorOccurred, OnErrorOccurred)
53 IPC_MESSAGE_HANDLER(SpeechRecognitionMsg_Ended, OnRecognitionEnded)
[email protected]fc88c1e2012-12-04 09:54:3654 IPC_MESSAGE_HANDLER(SpeechRecognitionMsg_ResultRetrieved,
55 OnResultsRetrieved)
[email protected]64d09222012-05-25 10:10:3456 IPC_MESSAGE_UNHANDLED(handled = false)
57 IPC_END_MESSAGE_MAP()
58 return handled;
59}
60
61void SpeechRecognitionDispatcher::start(
62 const WebSpeechRecognitionHandle& handle,
63 const WebSpeechRecognitionParams& params,
64 WebSpeechRecognizerClient* recognizer_client) {
[email protected]64d09222012-05-25 10:10:3465 DCHECK(!recognizer_client_ || recognizer_client_ == recognizer_client);
66 recognizer_client_ = recognizer_client;
67
68 SpeechRecognitionHostMsg_StartRequest_Params msg_params;
69 for (size_t i = 0; i < params.grammars().size(); ++i) {
70 const WebSpeechGrammar& grammar = params.grammars()[i];
71 msg_params.grammars.push_back(
[email protected]e9ff79c2012-10-19 21:31:2672 SpeechRecognitionGrammar(grammar.src().spec(), grammar.weight()));
[email protected]64d09222012-05-25 10:10:3473 }
[email protected]32956122013-12-25 07:29:2474 msg_params.language = base::UTF16ToUTF8(params.language());
[email protected]2b0f67f2012-06-27 17:48:5475 msg_params.max_hypotheses = static_cast<uint32>(params.maxAlternatives());
[email protected]60a63042012-10-04 19:48:4376 msg_params.continuous = params.continuous();
77 msg_params.interim_results = params.interimResults();
[email protected]831a2592012-06-15 16:07:5478 msg_params.origin_url = params.origin().toString().utf8();
[email protected]64d09222012-05-25 10:10:3479 msg_params.render_view_id = routing_id();
[email protected]c766aa92012-06-22 16:57:1480 msg_params.request_id = GetOrCreateIDForHandle(handle);
81 // The handle mapping will be removed in |OnRecognitionEnd|.
[email protected]64d09222012-05-25 10:10:3482 Send(new SpeechRecognitionHostMsg_StartRequest(msg_params));
83}
84
85void SpeechRecognitionDispatcher::stop(
86 const WebSpeechRecognitionHandle& handle,
87 WebSpeechRecognizerClient* recognizer_client) {
[email protected]c766aa92012-06-22 16:57:1488 // Ignore a |stop| issued without a matching |start|.
89 if (recognizer_client_ != recognizer_client || !HandleExists(handle))
90 return;
91 Send(new SpeechRecognitionHostMsg_StopCaptureRequest(
92 routing_id(), GetOrCreateIDForHandle(handle)));
[email protected]64d09222012-05-25 10:10:3493}
94
95void SpeechRecognitionDispatcher::abort(
96 const WebSpeechRecognitionHandle& handle,
97 WebSpeechRecognizerClient* recognizer_client) {
[email protected]c766aa92012-06-22 16:57:1498 // Ignore an |abort| issued without a matching |start|.
99 if (recognizer_client_ != recognizer_client || !HandleExists(handle))
100 return;
101 Send(new SpeechRecognitionHostMsg_AbortRequest(
102 routing_id(), GetOrCreateIDForHandle(handle)));
[email protected]64d09222012-05-25 10:10:34103}
104
105void SpeechRecognitionDispatcher::OnRecognitionStarted(int request_id) {
106 recognizer_client_->didStart(GetHandleFromID(request_id));
107}
108
109void SpeechRecognitionDispatcher::OnAudioStarted(int request_id) {
110 recognizer_client_->didStartAudio(GetHandleFromID(request_id));
111}
112
113void SpeechRecognitionDispatcher::OnSoundStarted(int request_id) {
114 recognizer_client_->didStartSound(GetHandleFromID(request_id));
115}
116
117void SpeechRecognitionDispatcher::OnSoundEnded(int request_id) {
118 recognizer_client_->didEndSound(GetHandleFromID(request_id));
119}
120
121void SpeechRecognitionDispatcher::OnAudioEnded(int request_id) {
122 recognizer_client_->didEndAudio(GetHandleFromID(request_id));
123}
124
[email protected]9c4ca672012-08-02 11:32:45125static WebSpeechRecognizerClient::ErrorCode WebKitErrorCode(
[email protected]e9ff79c2012-10-19 21:31:26126 SpeechRecognitionErrorCode e) {
[email protected]9c4ca672012-08-02 11:32:45127 switch (e) {
[email protected]e9ff79c2012-10-19 21:31:26128 case SPEECH_RECOGNITION_ERROR_NONE:
[email protected]9c4ca672012-08-02 11:32:45129 NOTREACHED();
130 return WebSpeechRecognizerClient::OtherError;
[email protected]e9ff79c2012-10-19 21:31:26131 case SPEECH_RECOGNITION_ERROR_ABORTED:
[email protected]9c4ca672012-08-02 11:32:45132 return WebSpeechRecognizerClient::AbortedError;
[email protected]e9ff79c2012-10-19 21:31:26133 case SPEECH_RECOGNITION_ERROR_AUDIO:
[email protected]9c4ca672012-08-02 11:32:45134 return WebSpeechRecognizerClient::AudioCaptureError;
[email protected]e9ff79c2012-10-19 21:31:26135 case SPEECH_RECOGNITION_ERROR_NETWORK:
[email protected]9c4ca672012-08-02 11:32:45136 return WebSpeechRecognizerClient::NetworkError;
[email protected]e9ff79c2012-10-19 21:31:26137 case SPEECH_RECOGNITION_ERROR_NOT_ALLOWED:
[email protected]2af35c502012-09-13 20:14:43138 return WebSpeechRecognizerClient::NotAllowedError;
[email protected]e9ff79c2012-10-19 21:31:26139 case SPEECH_RECOGNITION_ERROR_NO_SPEECH:
[email protected]9c4ca672012-08-02 11:32:45140 return WebSpeechRecognizerClient::NoSpeechError;
[email protected]e9ff79c2012-10-19 21:31:26141 case SPEECH_RECOGNITION_ERROR_NO_MATCH:
[email protected]9c4ca672012-08-02 11:32:45142 NOTREACHED();
143 return WebSpeechRecognizerClient::OtherError;
[email protected]e9ff79c2012-10-19 21:31:26144 case SPEECH_RECOGNITION_ERROR_BAD_GRAMMAR:
[email protected]9c4ca672012-08-02 11:32:45145 return WebSpeechRecognizerClient::BadGrammarError;
146 }
147 NOTREACHED();
148 return WebSpeechRecognizerClient::OtherError;
149}
150
[email protected]64d09222012-05-25 10:10:34151void SpeechRecognitionDispatcher::OnErrorOccurred(
152 int request_id, const SpeechRecognitionError& error) {
[email protected]e9ff79c2012-10-19 21:31:26153 if (error.code == SPEECH_RECOGNITION_ERROR_NO_MATCH) {
[email protected]64d09222012-05-25 10:10:34154 recognizer_client_->didReceiveNoMatch(GetHandleFromID(request_id),
155 WebSpeechRecognitionResult());
156 } else {
[email protected]0a8d4275e2013-01-04 22:21:26157 recognizer_client_->didReceiveError(
158 GetHandleFromID(request_id),
159 WebString(), // TODO(primiano): message?
160 WebKitErrorCode(error.code));
[email protected]64d09222012-05-25 10:10:34161 }
162}
163
164void SpeechRecognitionDispatcher::OnRecognitionEnded(int request_id) {
[email protected]fc88c1e2012-12-04 09:54:36165 // TODO(tommi): It is possible that the handle isn't found in the array if
166 // the user just refreshed the page. It seems that we then get a notification
167 // for the previously loaded instance of the page.
168 HandleMap::iterator iter = handle_map_.find(request_id);
169 if (iter == handle_map_.end()) {
170 DLOG(ERROR) << "OnRecognitionEnded called for a handle that doesn't exist";
171 } else {
172 WebSpeechRecognitionHandle handle = iter->second;
173 // Note: we need to erase the handle from the map *before* calling didEnd.
174 // didEnd may call back synchronously to start a new recognition session,
175 // and we don't want to delete the handle from the map after that happens.
176 handle_map_.erase(request_id);
177 recognizer_client_->didEnd(handle);
178 }
[email protected]64d09222012-05-25 10:10:34179}
180
[email protected]fc88c1e2012-12-04 09:54:36181void SpeechRecognitionDispatcher::OnResultsRetrieved(
182 int request_id, const SpeechRecognitionResults& results) {
183 size_t provisional_count = 0;
184 SpeechRecognitionResults::const_iterator it = results.begin();
185 for (; it != results.end(); ++it) {
186 if (it->is_provisional)
187 ++provisional_count;
[email protected]64d09222012-05-25 10:10:34188 }
[email protected]fc88c1e2012-12-04 09:54:36189
190 WebVector<WebSpeechRecognitionResult> provisional(provisional_count);
191 WebVector<WebSpeechRecognitionResult> final(
192 results.size() - provisional_count);
193
194 int provisional_index = 0, final_index = 0;
195 for (it = results.begin(); it != results.end(); ++it) {
196 const SpeechRecognitionResult& result = (*it);
197 WebSpeechRecognitionResult* webkit_result = result.is_provisional ?
198 &provisional[provisional_index++] : &final[final_index++];
199
200 const size_t num_hypotheses = result.hypotheses.size();
201 WebVector<WebString> transcripts(num_hypotheses);
202 WebVector<float> confidences(num_hypotheses);
203 for (size_t i = 0; i < num_hypotheses; ++i) {
204 transcripts[i] = result.hypotheses[i].utterance;
205 confidences[i] = static_cast<float>(result.hypotheses[i].confidence);
206 }
207 webkit_result->assign(transcripts, confidences, !result.is_provisional);
208 }
209
210 recognizer_client_->didReceiveResults(
211 GetHandleFromID(request_id), final, provisional);
[email protected]64d09222012-05-25 10:10:34212}
213
[email protected]c766aa92012-06-22 16:57:14214
215int SpeechRecognitionDispatcher::GetOrCreateIDForHandle(
[email protected]64d09222012-05-25 10:10:34216 const WebSpeechRecognitionHandle& handle) {
217 // Search first for an existing mapping.
218 for (HandleMap::iterator iter = handle_map_.begin();
219 iter != handle_map_.end();
220 ++iter) {
221 if (iter->second.equals(handle))
222 return iter->first;
223 }
224 // If no existing mapping found, create a new one.
225 const int new_id = next_id_;
226 handle_map_[new_id] = handle;
227 ++next_id_;
228 return new_id;
229}
230
[email protected]c766aa92012-06-22 16:57:14231bool SpeechRecognitionDispatcher::HandleExists(
232 const WebSpeechRecognitionHandle& handle) {
233 for (HandleMap::iterator iter = handle_map_.begin();
234 iter != handle_map_.end();
235 ++iter) {
236 if (iter->second.equals(handle))
237 return true;
238 }
239 return false;
240}
241
[email protected]64d09222012-05-25 10:10:34242const WebSpeechRecognitionHandle& SpeechRecognitionDispatcher::GetHandleFromID(
243 int request_id) {
244 HandleMap::iterator iter = handle_map_.find(request_id);
245 DCHECK(iter != handle_map_.end());
246 return iter->second;
247}
[email protected]e9ff79c2012-10-19 21:31:26248
249} // namespace content