[email protected] | 64d0922 | 2012-05-25 10:10:34 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "content/renderer/speech_recognition_dispatcher.h" |
| 6 | |
| 7 | #include "base/basictypes.h" |
| 8 | #include "base/utf_string_conversions.h" |
| 9 | #include "content/common/speech_recognition_messages.h" |
| 10 | #include "content/renderer/render_view_impl.h" |
| 11 | #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebString.h" |
| 12 | #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebVector.h" |
| 13 | #include "third_party/WebKit/Source/WebKit/chromium/public/WebSpeechGrammar.h" |
| 14 | #include "third_party/WebKit/Source/WebKit/chromium/public/WebSpeechRecognitionParams.h" |
| 15 | #include "third_party/WebKit/Source/WebKit/chromium/public/WebSpeechRecognitionResult.h" |
| 16 | #include "third_party/WebKit/Source/WebKit/chromium/public/WebSpeechRecognizerClient.h" |
| 17 | |
[email protected] | 64d0922 | 2012-05-25 10:10:34 | [diff] [blame] | 18 | using WebKit::WebVector; |
| 19 | using WebKit::WebString; |
| 20 | using WebKit::WebSpeechGrammar; |
| 21 | using WebKit::WebSpeechRecognitionHandle; |
| 22 | using WebKit::WebSpeechRecognitionResult; |
| 23 | using WebKit::WebSpeechRecognitionParams; |
| 24 | using WebKit::WebSpeechRecognizerClient; |
| 25 | |
[email protected] | e9ff79c | 2012-10-19 21:31:26 | [diff] [blame] | 26 | namespace content { |
| 27 | |
[email protected] | 64d0922 | 2012-05-25 10:10:34 | [diff] [blame] | 28 | SpeechRecognitionDispatcher::SpeechRecognitionDispatcher( |
| 29 | RenderViewImpl* render_view) |
[email protected] | e9ff79c | 2012-10-19 21:31:26 | [diff] [blame] | 30 | : RenderViewObserver(render_view), |
[email protected] | 64d0922 | 2012-05-25 10:10:34 | [diff] [blame] | 31 | recognizer_client_(NULL), |
| 32 | next_id_(1) { |
| 33 | } |
| 34 | |
| 35 | SpeechRecognitionDispatcher::~SpeechRecognitionDispatcher() { |
| 36 | } |
| 37 | |
| 38 | bool SpeechRecognitionDispatcher::OnMessageReceived( |
| 39 | const IPC::Message& message) { |
| 40 | bool handled = true; |
| 41 | IPC_BEGIN_MESSAGE_MAP(SpeechRecognitionDispatcher, message) |
| 42 | IPC_MESSAGE_HANDLER(SpeechRecognitionMsg_Started, OnRecognitionStarted) |
| 43 | IPC_MESSAGE_HANDLER(SpeechRecognitionMsg_AudioStarted, OnAudioStarted) |
| 44 | IPC_MESSAGE_HANDLER(SpeechRecognitionMsg_SoundStarted, OnSoundStarted) |
| 45 | IPC_MESSAGE_HANDLER(SpeechRecognitionMsg_SoundEnded, OnSoundEnded) |
| 46 | IPC_MESSAGE_HANDLER(SpeechRecognitionMsg_AudioEnded, OnAudioEnded) |
| 47 | IPC_MESSAGE_HANDLER(SpeechRecognitionMsg_ErrorOccurred, OnErrorOccurred) |
| 48 | IPC_MESSAGE_HANDLER(SpeechRecognitionMsg_Ended, OnRecognitionEnded) |
[email protected] | fc88c1e | 2012-12-04 09:54:36 | [diff] [blame^] | 49 | IPC_MESSAGE_HANDLER(SpeechRecognitionMsg_ResultRetrieved, |
| 50 | OnResultsRetrieved) |
[email protected] | 64d0922 | 2012-05-25 10:10:34 | [diff] [blame] | 51 | IPC_MESSAGE_UNHANDLED(handled = false) |
| 52 | IPC_END_MESSAGE_MAP() |
| 53 | return handled; |
| 54 | } |
| 55 | |
| 56 | void SpeechRecognitionDispatcher::start( |
| 57 | const WebSpeechRecognitionHandle& handle, |
| 58 | const WebSpeechRecognitionParams& params, |
| 59 | WebSpeechRecognizerClient* recognizer_client) { |
[email protected] | 64d0922 | 2012-05-25 10:10:34 | [diff] [blame] | 60 | DCHECK(!recognizer_client_ || recognizer_client_ == recognizer_client); |
| 61 | recognizer_client_ = recognizer_client; |
| 62 | |
| 63 | SpeechRecognitionHostMsg_StartRequest_Params msg_params; |
| 64 | for (size_t i = 0; i < params.grammars().size(); ++i) { |
| 65 | const WebSpeechGrammar& grammar = params.grammars()[i]; |
| 66 | msg_params.grammars.push_back( |
[email protected] | e9ff79c | 2012-10-19 21:31:26 | [diff] [blame] | 67 | SpeechRecognitionGrammar(grammar.src().spec(), grammar.weight())); |
[email protected] | 64d0922 | 2012-05-25 10:10:34 | [diff] [blame] | 68 | } |
| 69 | msg_params.language = UTF16ToUTF8(params.language()); |
[email protected] | 2b0f67f | 2012-06-27 17:48:54 | [diff] [blame] | 70 | msg_params.max_hypotheses = static_cast<uint32>(params.maxAlternatives()); |
[email protected] | 60a6304 | 2012-10-04 19:48:43 | [diff] [blame] | 71 | msg_params.continuous = params.continuous(); |
| 72 | msg_params.interim_results = params.interimResults(); |
[email protected] | 831a259 | 2012-06-15 16:07:54 | [diff] [blame] | 73 | msg_params.origin_url = params.origin().toString().utf8(); |
[email protected] | 64d0922 | 2012-05-25 10:10:34 | [diff] [blame] | 74 | msg_params.render_view_id = routing_id(); |
[email protected] | c766aa9 | 2012-06-22 16:57:14 | [diff] [blame] | 75 | msg_params.request_id = GetOrCreateIDForHandle(handle); |
| 76 | // The handle mapping will be removed in |OnRecognitionEnd|. |
[email protected] | 64d0922 | 2012-05-25 10:10:34 | [diff] [blame] | 77 | Send(new SpeechRecognitionHostMsg_StartRequest(msg_params)); |
| 78 | } |
| 79 | |
| 80 | void SpeechRecognitionDispatcher::stop( |
| 81 | const WebSpeechRecognitionHandle& handle, |
| 82 | WebSpeechRecognizerClient* recognizer_client) { |
[email protected] | c766aa9 | 2012-06-22 16:57:14 | [diff] [blame] | 83 | // Ignore a |stop| issued without a matching |start|. |
| 84 | if (recognizer_client_ != recognizer_client || !HandleExists(handle)) |
| 85 | return; |
| 86 | Send(new SpeechRecognitionHostMsg_StopCaptureRequest( |
| 87 | routing_id(), GetOrCreateIDForHandle(handle))); |
[email protected] | 64d0922 | 2012-05-25 10:10:34 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | void SpeechRecognitionDispatcher::abort( |
| 91 | const WebSpeechRecognitionHandle& handle, |
| 92 | WebSpeechRecognizerClient* recognizer_client) { |
[email protected] | c766aa9 | 2012-06-22 16:57:14 | [diff] [blame] | 93 | // Ignore an |abort| issued without a matching |start|. |
| 94 | if (recognizer_client_ != recognizer_client || !HandleExists(handle)) |
| 95 | return; |
| 96 | Send(new SpeechRecognitionHostMsg_AbortRequest( |
| 97 | routing_id(), GetOrCreateIDForHandle(handle))); |
[email protected] | 64d0922 | 2012-05-25 10:10:34 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | void SpeechRecognitionDispatcher::OnRecognitionStarted(int request_id) { |
| 101 | recognizer_client_->didStart(GetHandleFromID(request_id)); |
| 102 | } |
| 103 | |
| 104 | void SpeechRecognitionDispatcher::OnAudioStarted(int request_id) { |
| 105 | recognizer_client_->didStartAudio(GetHandleFromID(request_id)); |
| 106 | } |
| 107 | |
| 108 | void SpeechRecognitionDispatcher::OnSoundStarted(int request_id) { |
| 109 | recognizer_client_->didStartSound(GetHandleFromID(request_id)); |
| 110 | } |
| 111 | |
| 112 | void SpeechRecognitionDispatcher::OnSoundEnded(int request_id) { |
| 113 | recognizer_client_->didEndSound(GetHandleFromID(request_id)); |
| 114 | } |
| 115 | |
| 116 | void SpeechRecognitionDispatcher::OnAudioEnded(int request_id) { |
| 117 | recognizer_client_->didEndAudio(GetHandleFromID(request_id)); |
| 118 | } |
| 119 | |
[email protected] | 9c4ca67 | 2012-08-02 11:32:45 | [diff] [blame] | 120 | static WebSpeechRecognizerClient::ErrorCode WebKitErrorCode( |
[email protected] | e9ff79c | 2012-10-19 21:31:26 | [diff] [blame] | 121 | SpeechRecognitionErrorCode e) { |
[email protected] | 9c4ca67 | 2012-08-02 11:32:45 | [diff] [blame] | 122 | switch (e) { |
[email protected] | e9ff79c | 2012-10-19 21:31:26 | [diff] [blame] | 123 | case SPEECH_RECOGNITION_ERROR_NONE: |
[email protected] | 9c4ca67 | 2012-08-02 11:32:45 | [diff] [blame] | 124 | NOTREACHED(); |
| 125 | return WebSpeechRecognizerClient::OtherError; |
[email protected] | e9ff79c | 2012-10-19 21:31:26 | [diff] [blame] | 126 | case SPEECH_RECOGNITION_ERROR_ABORTED: |
[email protected] | 9c4ca67 | 2012-08-02 11:32:45 | [diff] [blame] | 127 | return WebSpeechRecognizerClient::AbortedError; |
[email protected] | e9ff79c | 2012-10-19 21:31:26 | [diff] [blame] | 128 | case SPEECH_RECOGNITION_ERROR_AUDIO: |
[email protected] | 9c4ca67 | 2012-08-02 11:32:45 | [diff] [blame] | 129 | return WebSpeechRecognizerClient::AudioCaptureError; |
[email protected] | e9ff79c | 2012-10-19 21:31:26 | [diff] [blame] | 130 | case SPEECH_RECOGNITION_ERROR_NETWORK: |
[email protected] | 9c4ca67 | 2012-08-02 11:32:45 | [diff] [blame] | 131 | return WebSpeechRecognizerClient::NetworkError; |
[email protected] | e9ff79c | 2012-10-19 21:31:26 | [diff] [blame] | 132 | case SPEECH_RECOGNITION_ERROR_NOT_ALLOWED: |
[email protected] | 2af35c50 | 2012-09-13 20:14:43 | [diff] [blame] | 133 | return WebSpeechRecognizerClient::NotAllowedError; |
[email protected] | e9ff79c | 2012-10-19 21:31:26 | [diff] [blame] | 134 | case SPEECH_RECOGNITION_ERROR_NO_SPEECH: |
[email protected] | 9c4ca67 | 2012-08-02 11:32:45 | [diff] [blame] | 135 | return WebSpeechRecognizerClient::NoSpeechError; |
[email protected] | e9ff79c | 2012-10-19 21:31:26 | [diff] [blame] | 136 | case SPEECH_RECOGNITION_ERROR_NO_MATCH: |
[email protected] | 9c4ca67 | 2012-08-02 11:32:45 | [diff] [blame] | 137 | NOTREACHED(); |
| 138 | return WebSpeechRecognizerClient::OtherError; |
[email protected] | e9ff79c | 2012-10-19 21:31:26 | [diff] [blame] | 139 | case SPEECH_RECOGNITION_ERROR_BAD_GRAMMAR: |
[email protected] | 9c4ca67 | 2012-08-02 11:32:45 | [diff] [blame] | 140 | return WebSpeechRecognizerClient::BadGrammarError; |
| 141 | } |
| 142 | NOTREACHED(); |
| 143 | return WebSpeechRecognizerClient::OtherError; |
| 144 | } |
| 145 | |
[email protected] | 64d0922 | 2012-05-25 10:10:34 | [diff] [blame] | 146 | void SpeechRecognitionDispatcher::OnErrorOccurred( |
| 147 | int request_id, const SpeechRecognitionError& error) { |
[email protected] | e9ff79c | 2012-10-19 21:31:26 | [diff] [blame] | 148 | if (error.code == SPEECH_RECOGNITION_ERROR_NO_MATCH) { |
[email protected] | 64d0922 | 2012-05-25 10:10:34 | [diff] [blame] | 149 | recognizer_client_->didReceiveNoMatch(GetHandleFromID(request_id), |
| 150 | WebSpeechRecognitionResult()); |
| 151 | } else { |
[email protected] | 64d0922 | 2012-05-25 10:10:34 | [diff] [blame] | 152 | recognizer_client_->didReceiveError(GetHandleFromID(request_id), |
[email protected] | c766aa9 | 2012-06-22 16:57:14 | [diff] [blame] | 153 | WebString(), // TODO(primiano): message? |
[email protected] | 9c4ca67 | 2012-08-02 11:32:45 | [diff] [blame] | 154 | WebKitErrorCode(error.code)); |
[email protected] | 64d0922 | 2012-05-25 10:10:34 | [diff] [blame] | 155 | } |
| 156 | } |
| 157 | |
| 158 | void SpeechRecognitionDispatcher::OnRecognitionEnded(int request_id) { |
[email protected] | fc88c1e | 2012-12-04 09:54:36 | [diff] [blame^] | 159 | // TODO(tommi): It is possible that the handle isn't found in the array if |
| 160 | // the user just refreshed the page. It seems that we then get a notification |
| 161 | // for the previously loaded instance of the page. |
| 162 | HandleMap::iterator iter = handle_map_.find(request_id); |
| 163 | if (iter == handle_map_.end()) { |
| 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 | } |
[email protected] | 64d0922 | 2012-05-25 10:10:34 | [diff] [blame] | 173 | } |
| 174 | |
[email protected] | fc88c1e | 2012-12-04 09:54:36 | [diff] [blame^] | 175 | void SpeechRecognitionDispatcher::OnResultsRetrieved( |
| 176 | int request_id, const SpeechRecognitionResults& results) { |
| 177 | size_t provisional_count = 0; |
| 178 | SpeechRecognitionResults::const_iterator it = results.begin(); |
| 179 | for (; it != results.end(); ++it) { |
| 180 | if (it->is_provisional) |
| 181 | ++provisional_count; |
[email protected] | 64d0922 | 2012-05-25 10:10:34 | [diff] [blame] | 182 | } |
[email protected] | fc88c1e | 2012-12-04 09:54:36 | [diff] [blame^] | 183 | |
| 184 | WebVector<WebSpeechRecognitionResult> provisional(provisional_count); |
| 185 | WebVector<WebSpeechRecognitionResult> final( |
| 186 | results.size() - provisional_count); |
| 187 | |
| 188 | int provisional_index = 0, final_index = 0; |
| 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); |
[email protected] | 64d0922 | 2012-05-25 10:10:34 | [diff] [blame] | 206 | } |
| 207 | |
[email protected] | c766aa9 | 2012-06-22 16:57:14 | [diff] [blame] | 208 | |
| 209 | int SpeechRecognitionDispatcher::GetOrCreateIDForHandle( |
[email protected] | 64d0922 | 2012-05-25 10:10:34 | [diff] [blame] | 210 | const WebSpeechRecognitionHandle& handle) { |
| 211 | // Search first for an existing mapping. |
| 212 | for (HandleMap::iterator iter = handle_map_.begin(); |
| 213 | iter != handle_map_.end(); |
| 214 | ++iter) { |
| 215 | if (iter->second.equals(handle)) |
| 216 | return iter->first; |
| 217 | } |
| 218 | // If no existing mapping found, create a new one. |
| 219 | const int new_id = next_id_; |
| 220 | handle_map_[new_id] = handle; |
| 221 | ++next_id_; |
| 222 | return new_id; |
| 223 | } |
| 224 | |
[email protected] | c766aa9 | 2012-06-22 16:57:14 | [diff] [blame] | 225 | bool SpeechRecognitionDispatcher::HandleExists( |
| 226 | const WebSpeechRecognitionHandle& handle) { |
| 227 | for (HandleMap::iterator iter = handle_map_.begin(); |
| 228 | iter != handle_map_.end(); |
| 229 | ++iter) { |
| 230 | if (iter->second.equals(handle)) |
| 231 | return true; |
| 232 | } |
| 233 | return false; |
| 234 | } |
| 235 | |
[email protected] | 64d0922 | 2012-05-25 10:10:34 | [diff] [blame] | 236 | const WebSpeechRecognitionHandle& SpeechRecognitionDispatcher::GetHandleFromID( |
| 237 | int request_id) { |
| 238 | HandleMap::iterator iter = handle_map_.find(request_id); |
| 239 | DCHECK(iter != handle_map_.end()); |
| 240 | return iter->second; |
| 241 | } |
[email protected] | e9ff79c | 2012-10-19 21:31:26 | [diff] [blame] | 242 | |
| 243 | } // namespace content |