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/WebSpeechRecognizer.h " | |
17 #include "third_party/WebKit/Source/WebKit/chromium/public/WebSpeechRecognizerCl ient.h" | |
18 | |
19 using content::SpeechRecognitionError; | |
20 using content::SpeechRecognitionResult; | |
21 using WebKit::WebVector; | |
22 using WebKit::WebString; | |
23 using WebKit::WebSpeechGrammar; | |
24 using WebKit::WebSpeechRecognitionHandle; | |
25 using WebKit::WebSpeechRecognitionResult; | |
26 using WebKit::WebSpeechRecognitionParams; | |
27 using WebKit::WebSpeechRecognizerClient; | |
28 | |
29 SpeechRecognitionDispatcher::SpeechRecognitionDispatcher( | |
30 RenderViewImpl* render_view) | |
31 : content::RenderViewObserver(render_view), | |
32 event_proxy_(NULL), | |
33 last_mapping_id_(0) { | |
34 } | |
35 | |
36 SpeechRecognitionDispatcher::~SpeechRecognitionDispatcher() { | |
37 } | |
38 | |
39 bool SpeechRecognitionDispatcher::OnMessageReceived( | |
40 const IPC::Message& message) { | |
41 bool handled = true; | |
42 IPC_BEGIN_MESSAGE_MAP(SpeechRecognitionDispatcher, message) | |
43 IPC_MESSAGE_HANDLER(SpeechRecognitionMsg_Started, OnRecognitionStarted) | |
44 IPC_MESSAGE_HANDLER(SpeechRecognitionMsg_AudioStarted, OnAudioStarted) | |
45 IPC_MESSAGE_HANDLER(SpeechRecognitionMsg_SoundStarted, OnSoundStarted) | |
46 IPC_MESSAGE_HANDLER(SpeechRecognitionMsg_SoundEnded, OnSoundEnded) | |
47 IPC_MESSAGE_HANDLER(SpeechRecognitionMsg_AudioEnded, OnAudioEnded) | |
48 IPC_MESSAGE_HANDLER(SpeechRecognitionMsg_ErrorOccurred,OnErrorOccurred) | |
49 IPC_MESSAGE_HANDLER(SpeechRecognitionMsg_Ended, OnRecognitionEnded) | |
50 IPC_MESSAGE_HANDLER(SpeechRecognitionMsg_ResultRetrieved, | |
51 OnResultRetrieved) | |
52 IPC_MESSAGE_UNHANDLED(handled = false) | |
53 IPC_END_MESSAGE_MAP() | |
54 return handled; | |
55 } | |
56 | |
57 void SpeechRecognitionDispatcher::start( | |
58 const WebSpeechRecognitionHandle& handle, | |
59 const WebSpeechRecognitionParams& params, | |
60 WebSpeechRecognizerClient* event_proxy) { | |
61 //TODO(primiano) What to do if a start is issued to an already started object? | |
62 DCHECK(!event_proxy_ || event_proxy_ == event_proxy); | |
63 event_proxy_ = event_proxy; | |
64 | |
65 SpeechRecognitionHostMsg_StartRequest_Params msg_params; | |
66 for (size_t i=0; i < params.grammars().size(); ++i) { | |
hans
2012/05/11 16:56:32
spaces around the =
Primiano Tucci (use gerrit)
2012/05/14 12:58:22
Done.
| |
67 const WebSpeechGrammar& grammar = params.grammars()[i]; | |
68 msg_params.grammars.push_back( | |
69 content::SpeechRecognitionGrammar(grammar.src().spec(), | |
70 grammar.weight())); | |
71 } | |
72 msg_params.language = UTF16ToUTF8(params.language()); | |
73 msg_params.is_one_shot = !params.continuous(); | |
74 msg_params.origin_url = ""; //TODO (primiano) we need an origin from WebKit. | |
hans
2012/05/11 16:56:32
i don't think we normally put space between TODO a
Primiano Tucci (use gerrit)
2012/05/14 12:58:22
Uh, right. The space was misplaced.
| |
75 msg_params.render_view_id = routing_id(); | |
76 msg_params.js_handle_id = GetIDForHandle(handle); | |
77 Send(new SpeechRecognitionHostMsg_StartRequest(msg_params)); | |
78 } | |
79 | |
80 void SpeechRecognitionDispatcher::stop(const WebSpeechRecognitionHandle& handle, | |
81 WebSpeechRecognizerClient* event_proxy) { | |
82 DCHECK(event_proxy_ == event_proxy); | |
83 Send(new SpeechRecognitionHostMsg_StopCaptureRequest(routing_id(), | |
84 GetIDForHandle(handle))); | |
85 } | |
86 | |
87 void SpeechRecognitionDispatcher::abort( | |
88 const WebSpeechRecognitionHandle& handle, | |
89 WebSpeechRecognizerClient* event_proxy) { | |
90 Send(new SpeechRecognitionHostMsg_AbortRequest(routing_id(), | |
91 GetIDForHandle(handle))); | |
92 } | |
93 | |
94 void SpeechRecognitionDispatcher::OnRecognitionStarted(int js_handle_id) { | |
95 if (!event_proxy_) | |
hans
2012/05/11 16:56:32
can this happen?
Primiano Tucci (use gerrit)
2012/05/14 12:58:22
Hmm, agree. A DCHECK is more suitable.
| |
96 return; | |
97 event_proxy_->didStart(GetHandleFromID(js_handle_id)); | |
98 } | |
99 | |
100 void SpeechRecognitionDispatcher::OnAudioStarted(int js_handle_id) { | |
101 if (!event_proxy_) | |
102 return; | |
103 event_proxy_->didStartAudio(GetHandleFromID(js_handle_id)); | |
104 } | |
105 | |
106 void SpeechRecognitionDispatcher::OnSoundStarted(int js_handle_id) { | |
107 if (!event_proxy_) | |
108 return; | |
109 event_proxy_->didStartSound(GetHandleFromID(js_handle_id)); | |
110 } | |
111 | |
112 void SpeechRecognitionDispatcher::OnSoundEnded(int js_handle_id) { | |
113 if (!event_proxy_) | |
114 return; | |
115 event_proxy_->didEndSound(GetHandleFromID(js_handle_id)); | |
116 } | |
117 | |
118 void SpeechRecognitionDispatcher::OnAudioEnded(int js_handle_id) { | |
119 if (!event_proxy_) | |
120 return; | |
121 event_proxy_->didEndAudio(GetHandleFromID(js_handle_id)); | |
122 } | |
123 | |
124 void SpeechRecognitionDispatcher::OnErrorOccurred( | |
125 int js_handle_id, const SpeechRecognitionError& error) { | |
126 if (!event_proxy_) | |
127 return; | |
128 if (error.code == content::SPEECH_RECOGNITION_ERROR_NO_MATCH) { | |
129 event_proxy_->didReceiveNoMatch(GetHandleFromID(js_handle_id), | |
130 WebSpeechRecognitionResult()); | |
131 } else { | |
132 event_proxy_->didReceiveError(GetHandleFromID(js_handle_id), | |
133 WebString(), // TODO(primiano) error message? | |
hans
2012/05/11 16:56:32
hmm, i guess we should look at the error.details a
Primiano Tucci (use gerrit)
2012/05/14 12:58:22
Right.
| |
134 error.code); | |
135 } | |
136 } | |
137 | |
138 void SpeechRecognitionDispatcher::OnRecognitionEnded(int js_handle_id) { | |
139 if (!event_proxy_) | |
140 return; | |
141 event_proxy_->didEnd(GetHandleFromID(js_handle_id)); | |
142 handle_mappings_.erase(js_handle_id); | |
143 } | |
144 | |
145 void SpeechRecognitionDispatcher::OnResultRetrieved( | |
146 int js_handle_id, const SpeechRecognitionResult& result) { | |
147 if (!event_proxy_) | |
148 return; | |
149 | |
150 const size_t num_hypotheses = result.hypotheses.size(); | |
151 WebSpeechRecognitionResult webkit_result; | |
152 WebVector<WebString> transcripts(num_hypotheses); | |
153 WebVector<float> confidences(num_hypotheses); | |
154 for (size_t i=0; i < num_hypotheses; ++i) { | |
hans
2012/05/11 16:56:32
spaces around =
Primiano Tucci (use gerrit)
2012/05/14 12:58:22
Done.
| |
155 transcripts[i] = result.hypotheses[i].utterance; | |
156 confidences[i] = static_cast<float>(result.hypotheses[i].confidence); | |
157 } | |
158 webkit_result.assign(transcripts, confidences, !result.provisional); | |
159 // TODO(primiano) Handle history, currently empty. | |
160 WebVector<WebSpeechRecognitionResult> empty_history; | |
161 event_proxy_->didReceiveResult(GetHandleFromID(js_handle_id), | |
162 webkit_result, | |
163 0, // result_index | |
hans
2012/05/11 16:56:32
maybe do "const int result_index = 0;" and pass in
Primiano Tucci (use gerrit)
2012/05/14 12:58:22
I really agree with the pattern, but in a similar
hans
2012/05/14 14:04:52
OK. Satish can comment if he has opinions here :)
| |
164 empty_history); | |
165 } | |
166 | |
167 int SpeechRecognitionDispatcher::GetIDForHandle( | |
168 const WebSpeechRecognitionHandle& handle) { | |
169 // Search first for an existing mapping. | |
170 for(HandleMap::iterator iter = handle_mappings_.begin(); | |
hans
2012/05/11 16:56:32
space between for and (
Primiano Tucci (use gerrit)
2012/05/14 12:58:22
Done.
| |
171 iter != handle_mappings_.end(); | |
172 ++iter) { | |
173 if (iter->second.equals(handle)) | |
174 return iter->first; | |
175 } | |
176 // If no existing mapping found, create a new one. | |
177 for (++last_mapping_id_; | |
178 handle_mappings_.find(last_mapping_id_) != handle_mappings_.end(); | |
179 ++last_mapping_id_) {} | |
hans
2012/05/11 16:56:32
why do we need this loop? why not just
handle_map
Primiano Tucci (use gerrit)
2012/05/14 12:58:22
Just me being extremely paranoid about holes after
| |
180 handle_mappings_[last_mapping_id_] = handle; | |
181 return last_mapping_id_; | |
182 } | |
183 | |
184 const WebSpeechRecognitionHandle& SpeechRecognitionDispatcher::GetHandleFromID( | |
185 int js_handle_id) { | |
186 HandleMap::iterator iter = handle_mappings_.find(js_handle_id); | |
187 DCHECK(iter != handle_mappings_.end()); | |
188 return iter->second; | |
189 } | |
OLD | NEW |