OLD | NEW |
(Empty) | |
| 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/WebSpeechRecognitionP
arams.h" |
| 15 #include "third_party/WebKit/Source/WebKit/chromium/public/WebSpeechRecognitionR
esult.h" |
| 16 #include "third_party/WebKit/Source/WebKit/chromium/public/WebSpeechRecognizerCl
ient.h" |
| 17 |
| 18 using content::SpeechRecognitionError; |
| 19 using content::SpeechRecognitionResult; |
| 20 using WebKit::WebVector; |
| 21 using WebKit::WebString; |
| 22 using WebKit::WebSpeechGrammar; |
| 23 using WebKit::WebSpeechRecognitionHandle; |
| 24 using WebKit::WebSpeechRecognitionResult; |
| 25 using WebKit::WebSpeechRecognitionParams; |
| 26 using WebKit::WebSpeechRecognizerClient; |
| 27 |
| 28 SpeechRecognitionDispatcher::SpeechRecognitionDispatcher( |
| 29 RenderViewImpl* render_view) |
| 30 : content::RenderViewObserver(render_view), |
| 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) |
| 49 IPC_MESSAGE_HANDLER(SpeechRecognitionMsg_ResultRetrieved, OnResultRetrieved) |
| 50 IPC_MESSAGE_UNHANDLED(handled = false) |
| 51 IPC_END_MESSAGE_MAP() |
| 52 return handled; |
| 53 } |
| 54 |
| 55 void SpeechRecognitionDispatcher::start( |
| 56 const WebSpeechRecognitionHandle& handle, |
| 57 const WebSpeechRecognitionParams& params, |
| 58 WebSpeechRecognizerClient* recognizer_client) { |
| 59 //TODO(primiano) What to do if a start is issued to an already started object? |
| 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( |
| 67 content::SpeechRecognitionGrammar(grammar.src().spec(), |
| 68 grammar.weight())); |
| 69 } |
| 70 msg_params.language = UTF16ToUTF8(params.language()); |
| 71 msg_params.is_one_shot = !params.continuous(); |
| 72 msg_params.origin_url = ""; // TODO(primiano) we need an origin from WebKit. |
| 73 msg_params.render_view_id = routing_id(); |
| 74 msg_params.request_id = GetIDForHandle(handle); |
| 75 Send(new SpeechRecognitionHostMsg_StartRequest(msg_params)); |
| 76 } |
| 77 |
| 78 void SpeechRecognitionDispatcher::stop( |
| 79 const WebSpeechRecognitionHandle& handle, |
| 80 WebSpeechRecognizerClient* recognizer_client) { |
| 81 DCHECK(recognizer_client_ == recognizer_client); |
| 82 Send(new SpeechRecognitionHostMsg_StopCaptureRequest(routing_id(), |
| 83 GetIDForHandle(handle))); |
| 84 } |
| 85 |
| 86 void SpeechRecognitionDispatcher::abort( |
| 87 const WebSpeechRecognitionHandle& handle, |
| 88 WebSpeechRecognizerClient* recognizer_client) { |
| 89 Send(new SpeechRecognitionHostMsg_AbortRequest(routing_id(), |
| 90 GetIDForHandle(handle))); |
| 91 } |
| 92 |
| 93 void SpeechRecognitionDispatcher::OnRecognitionStarted(int request_id) { |
| 94 recognizer_client_->didStart(GetHandleFromID(request_id)); |
| 95 } |
| 96 |
| 97 void SpeechRecognitionDispatcher::OnAudioStarted(int request_id) { |
| 98 recognizer_client_->didStartAudio(GetHandleFromID(request_id)); |
| 99 } |
| 100 |
| 101 void SpeechRecognitionDispatcher::OnSoundStarted(int request_id) { |
| 102 recognizer_client_->didStartSound(GetHandleFromID(request_id)); |
| 103 } |
| 104 |
| 105 void SpeechRecognitionDispatcher::OnSoundEnded(int request_id) { |
| 106 recognizer_client_->didEndSound(GetHandleFromID(request_id)); |
| 107 } |
| 108 |
| 109 void SpeechRecognitionDispatcher::OnAudioEnded(int request_id) { |
| 110 recognizer_client_->didEndAudio(GetHandleFromID(request_id)); |
| 111 } |
| 112 |
| 113 void SpeechRecognitionDispatcher::OnErrorOccurred( |
| 114 int request_id, const SpeechRecognitionError& error) { |
| 115 if (error.code == content::SPEECH_RECOGNITION_ERROR_NO_MATCH) { |
| 116 recognizer_client_->didReceiveNoMatch(GetHandleFromID(request_id), |
| 117 WebSpeechRecognitionResult()); |
| 118 } else { |
| 119 // TODO(primiano) speech_recognition_error.h must be updated with the new |
| 120 // API specs soon. |
| 121 WebSpeechRecognizerClient::ErrorCode wk_error_code; |
| 122 switch (error.code) { |
| 123 case content::SPEECH_RECOGNITION_ERROR_ABORTED: |
| 124 wk_error_code = WebSpeechRecognizerClient::AbortedError; |
| 125 break; |
| 126 case content::SPEECH_RECOGNITION_ERROR_AUDIO: |
| 127 wk_error_code = WebSpeechRecognizerClient::AudioCaptureError; |
| 128 break; |
| 129 case content::SPEECH_RECOGNITION_ERROR_NETWORK: |
| 130 wk_error_code = WebSpeechRecognizerClient::NetworkError; |
| 131 break; |
| 132 case content::SPEECH_RECOGNITION_ERROR_NO_SPEECH: |
| 133 wk_error_code = WebSpeechRecognizerClient::NoSpeechError; |
| 134 break; |
| 135 case content::SPEECH_RECOGNITION_ERROR_BAD_GRAMMAR: |
| 136 wk_error_code = WebSpeechRecognizerClient::BadGrammarError; |
| 137 break; |
| 138 default: |
| 139 NOTREACHED(); |
| 140 wk_error_code = WebSpeechRecognizerClient::OtherError; |
| 141 } |
| 142 recognizer_client_->didReceiveError(GetHandleFromID(request_id), |
| 143 WebString(), // TODO(primiano) message? |
| 144 wk_error_code); |
| 145 } |
| 146 } |
| 147 |
| 148 void SpeechRecognitionDispatcher::OnRecognitionEnded(int request_id) { |
| 149 recognizer_client_->didEnd(GetHandleFromID(request_id)); |
| 150 handle_map_.erase(request_id); |
| 151 } |
| 152 |
| 153 void SpeechRecognitionDispatcher::OnResultRetrieved( |
| 154 int request_id, const SpeechRecognitionResult& result) { |
| 155 const size_t num_hypotheses = result.hypotheses.size(); |
| 156 WebSpeechRecognitionResult webkit_result; |
| 157 WebVector<WebString> transcripts(num_hypotheses); |
| 158 WebVector<float> confidences(num_hypotheses); |
| 159 for (size_t i = 0; i < num_hypotheses; ++i) { |
| 160 transcripts[i] = result.hypotheses[i].utterance; |
| 161 confidences[i] = static_cast<float>(result.hypotheses[i].confidence); |
| 162 } |
| 163 webkit_result.assign(transcripts, confidences, !result.provisional); |
| 164 // TODO(primiano) Handle history, currently empty. |
| 165 WebVector<WebSpeechRecognitionResult> empty_history; |
| 166 recognizer_client_->didReceiveResult(GetHandleFromID(request_id), |
| 167 webkit_result, |
| 168 0, // result_index |
| 169 empty_history); |
| 170 } |
| 171 |
| 172 int SpeechRecognitionDispatcher::GetIDForHandle( |
| 173 const WebSpeechRecognitionHandle& handle) { |
| 174 // Search first for an existing mapping. |
| 175 for (HandleMap::iterator iter = handle_map_.begin(); |
| 176 iter != handle_map_.end(); |
| 177 ++iter) { |
| 178 if (iter->second.equals(handle)) |
| 179 return iter->first; |
| 180 } |
| 181 // If no existing mapping found, create a new one. |
| 182 const int new_id = next_id_; |
| 183 handle_map_[new_id] = handle; |
| 184 ++next_id_; |
| 185 return new_id; |
| 186 } |
| 187 |
| 188 const WebSpeechRecognitionHandle& SpeechRecognitionDispatcher::GetHandleFromID( |
| 189 int request_id) { |
| 190 HandleMap::iterator iter = handle_map_.find(request_id); |
| 191 DCHECK(iter != handle_map_.end()); |
| 192 return iter->second; |
| 193 } |
OLD | NEW |