Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(315)

Side by Side Diff: content/renderer/speech_recognition_dispatcher.cc

Issue 10273006: Introduced SpeechRecognitionDispatcher(Host) classes, handling dispatch of IPC messages for continu… (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fixed according to Hans review. Created 8 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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 recognizer_client_(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* recognizer_client) {
61 //TODO(primiano) What to do if a start is issued to an already started object?
62 DCHECK(!recognizer_client_ || recognizer_client_ == recognizer_client);
63 recognizer_client_ = recognizer_client;
64
65 SpeechRecognitionHostMsg_StartRequest_Params msg_params;
66 for (size_t i = 0; i < params.grammars().size(); ++i) {
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.
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(
81 const WebSpeechRecognitionHandle& handle,
82 WebSpeechRecognizerClient* recognizer_client) {
83 DCHECK(recognizer_client_ == recognizer_client);
84 Send(new SpeechRecognitionHostMsg_StopCaptureRequest(routing_id(),
85 GetIDForHandle(handle)));
86 }
87
88 void SpeechRecognitionDispatcher::abort(
89 const WebSpeechRecognitionHandle& handle,
90 WebSpeechRecognizerClient* recognizer_client) {
91 Send(new SpeechRecognitionHostMsg_AbortRequest(routing_id(),
92 GetIDForHandle(handle)));
93 }
94
95 void SpeechRecognitionDispatcher::OnRecognitionStarted(int js_handle_id) {
96 DCHECK(recognizer_client_);
97 recognizer_client_->didStart(GetHandleFromID(js_handle_id));
98 }
99
100 void SpeechRecognitionDispatcher::OnAudioStarted(int js_handle_id) {
101 DCHECK(recognizer_client_);
102 recognizer_client_->didStartAudio(GetHandleFromID(js_handle_id));
103 }
104
105 void SpeechRecognitionDispatcher::OnSoundStarted(int js_handle_id) {
106 DCHECK(recognizer_client_);
107 recognizer_client_->didStartSound(GetHandleFromID(js_handle_id));
108 }
109
110 void SpeechRecognitionDispatcher::OnSoundEnded(int js_handle_id) {
111 DCHECK(recognizer_client_);
112 recognizer_client_->didEndSound(GetHandleFromID(js_handle_id));
113 }
114
115 void SpeechRecognitionDispatcher::OnAudioEnded(int js_handle_id) {
116 DCHECK(recognizer_client_);
117 recognizer_client_->didEndAudio(GetHandleFromID(js_handle_id));
118 }
119
120 void SpeechRecognitionDispatcher::OnErrorOccurred(
121 int js_handle_id, const SpeechRecognitionError& error) {
122 DCHECK(recognizer_client_);
123 if (error.code == content::SPEECH_RECOGNITION_ERROR_NO_MATCH) {
124 recognizer_client_->didReceiveNoMatch(GetHandleFromID(js_handle_id),
125 WebSpeechRecognitionResult());
126 } else {
127 recognizer_client_->didReceiveError(GetHandleFromID(js_handle_id),
128 WebString(), // TODO(primiano) message?
129 error.code);
130 }
131 }
132
133 void SpeechRecognitionDispatcher::OnRecognitionEnded(int js_handle_id) {
134 DCHECK(recognizer_client_);
135 recognizer_client_->didEnd(GetHandleFromID(js_handle_id));
136 handle_map_.erase(js_handle_id);
137 }
138
139 void SpeechRecognitionDispatcher::OnResultRetrieved(
140 int js_handle_id, const SpeechRecognitionResult& result) {
141 DCHECK(recognizer_client_);
142
143 const size_t num_hypotheses = result.hypotheses.size();
144 WebSpeechRecognitionResult webkit_result;
145 WebVector<WebString> transcripts(num_hypotheses);
146 WebVector<float> confidences(num_hypotheses);
147 for (size_t i = 0; i < num_hypotheses; ++i) {
148 transcripts[i] = result.hypotheses[i].utterance;
149 confidences[i] = static_cast<float>(result.hypotheses[i].confidence);
150 }
151 webkit_result.assign(transcripts, confidences, !result.provisional);
152 // TODO(primiano) Handle history, currently empty.
153 WebVector<WebSpeechRecognitionResult> empty_history;
154 recognizer_client_->didReceiveResult(GetHandleFromID(js_handle_id),
155 webkit_result,
156 0, // result_index
157 empty_history);
158 }
159
160 int SpeechRecognitionDispatcher::GetIDForHandle(
161 const WebSpeechRecognitionHandle& handle) {
162 // Search first for an existing mapping.
163 for (HandleMap::iterator iter = handle_map_.begin();
164 iter != handle_map_.end();
165 ++iter) {
166 if (iter->second.equals(handle))
167 return iter->first;
168 }
169 // If no existing mapping found, create a new one.
170 ++last_mapping_id_;
171 handle_map_[last_mapping_id_] = handle;
172 return last_mapping_id_;
173 }
174
175 const WebSpeechRecognitionHandle& SpeechRecognitionDispatcher::GetHandleFromID(
176 int js_handle_id) {
177 HandleMap::iterator iter = handle_map_.find(js_handle_id);
178 DCHECK(iter != handle_map_.end());
179 return iter->second;
180 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698