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

Side by Side Diff: content/browser/speech/speech_recognition_dispatcher_host.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: 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/browser/speech/speech_recognition_dispatcher_host.h"
6
7 #include "base/bind.h"
8 #include "base/command_line.h"
9 #include "base/lazy_instance.h"
10 #include "content/browser/speech/speech_recognition_manager_impl.h"
11 #include "content/common/speech_recognition_messages.h"
12 #include "content/public/browser/speech_recognition_preferences.h"
13 #include "content/public/browser/speech_recognition_session_config.h"
14 #include "content/public/browser/speech_recognition_session_context.h"
15 #include "content/public/common/content_switches.h"
16
17 using content::BrowserThread;
18 using content::SpeechRecognitionSessionConfig;
19 using content::SpeechRecognitionSessionContext;
20
21 namespace {
22 bool IsSameContext(int render_process_id,
hans 2012/05/11 16:56:32 i would personally move this function closer to wh
Primiano Tucci (use gerrit) 2012/05/14 12:58:22 Done.
23 int render_view_id,
24 int js_handle_id,
25 const SpeechRecognitionSessionContext& context) {
26 return context.render_process_id == render_process_id &&
27 context.render_view_id == render_view_id &&
28 context.js_handle_id == js_handle_id;
29 }
30 } // namespace
31
32 namespace speech {
33 SpeechRecognitionManagerImpl* SpeechRecognitionDispatcherHost::manager_;
34
35 void SpeechRecognitionDispatcherHost::set_manager(
36 SpeechRecognitionManagerImpl* manager) {
37 manager_ = manager;
38 }
39
40 SpeechRecognitionDispatcherHost::SpeechRecognitionDispatcherHost(
41 int render_process_id,
42 net::URLRequestContextGetter* context_getter,
43 content::SpeechRecognitionPreferences* recognition_preferences)
44 : render_process_id_(render_process_id),
45 may_have_pending_requests_(false),
46 context_getter_(context_getter),
47 recognition_preferences_(recognition_preferences) {
48 // This is initialized by Browser. Do not add any non-trivial
49 // initialization here, instead do it lazily when required (e.g. see the
50 // method |manager()|) or add an Init() method.
51 }
52
53 SpeechRecognitionDispatcherHost::~SpeechRecognitionDispatcherHost() {
54 // If the renderer crashed for some reason or if we didn't receive a proper
55 // Cancel/Stop call for an existing session, cancel such active sessions now.
56 // We first check if this dispatcher received any speech IPC requst so that
hans 2012/05/11 16:56:32 s/requst/request/
Primiano Tucci (use gerrit) 2012/05/14 12:58:22 Done.
57 // we don't end up creating the speech input manager for web pages which don't
58 // use speech input.
59 if (may_have_pending_requests_)
hans 2012/05/11 16:56:32 couldn't we just do "if (manager_) ..." ? That wou
Primiano Tucci (use gerrit) 2012/05/14 12:58:22 2 Observations: 1) Depending on CL1.10 it might be
hans 2012/05/14 14:04:52 Ah, thanks for clarifying.
60 manager()->AbortAllSessionsForListener(this);
61 }
62
63 SpeechRecognitionManagerImpl* SpeechRecognitionDispatcherHost::manager() {
64 if (manager_)
65 return manager_;
66
67 const CommandLine& command_line = *CommandLine::ForCurrentProcess();
68 if (command_line.HasSwitch(switches::kEnableScriptedSpeech))
69 return SpeechRecognitionManagerImpl::GetInstance();
70
71 return NULL;
72 }
73
74 bool SpeechRecognitionDispatcherHost::OnMessageReceived(
75 const IPC::Message& message, bool* message_was_ok) {
76 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
77 bool handled = true;
78 IPC_BEGIN_MESSAGE_MAP_EX(SpeechRecognitionDispatcherHost, message,
79 *message_was_ok)
80 IPC_MESSAGE_HANDLER(SpeechRecognitionHostMsg_StartRequest,
81 OnStartRequest)
82 IPC_MESSAGE_HANDLER(SpeechRecognitionHostMsg_AbortRequest,
83 OnAbortRequest)
84 IPC_MESSAGE_HANDLER(SpeechRecognitionHostMsg_StopCaptureRequest,
85 OnStopCaptureRequest)
86 IPC_MESSAGE_UNHANDLED(handled = false)
87 IPC_END_MESSAGE_MAP()
88 if (handled)
89 may_have_pending_requests_ = true;
90 return handled;
91 }
92
93 void SpeechRecognitionDispatcherHost::OnStartRequest(
94 const SpeechRecognitionHostMsg_StartRequest_Params &params) {
hans 2012/05/11 16:56:32 the & is misplaced
Primiano Tucci (use gerrit) 2012/05/14 12:58:22 I see that is a copy/paste "heritage". Fixed also
95 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
96
97 SpeechRecognitionSessionContext context;
98 context.render_process_id = render_process_id_;
99 context.render_view_id = params.render_view_id;
100 context.js_handle_id = params.js_handle_id;
101
102 SpeechRecognitionSessionConfig config;
103 config.is_one_shot = params.is_one_shot;
104 config.language = params.language;
105 config.grammars = params.grammars;
106 config.origin_url = params.origin_url;
107 config.initial_context = context;
108 config.url_request_context_getter = context_getter_.get();
109 config.filter_profanities = recognition_preferences_->FilterProfanities();
110
111 int session_id = manager()->CreateSession(config, this);
112
113 if (session_id == content::SpeechRecognitionManager::kSessionIDInvalid)
hans 2012/05/11 16:56:32 when does this happen?
Primiano Tucci (use gerrit) 2012/05/14 12:58:22 Hmm I remember that I turned this into a DCHECK in
114 return;
115
116 manager()->StartSession(session_id);
117 }
118
119 void SpeechRecognitionDispatcherHost::OnAbortRequest(int render_view_id,
120 int js_handle_id) {
121 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
hans 2012/05/11 16:56:32 indent +1
Primiano Tucci (use gerrit) 2012/05/14 12:58:22 Done.
122 int session_id = manager()->LookupSessionByContext(
123 base::Bind(&IsSameContext,
124 render_process_id_,
125 render_view_id,
126 js_handle_id));
127 if (session_id != 0)
hans 2012/05/11 16:56:32 should 0 be kSessionIDInvalid?
Primiano Tucci (use gerrit) 2012/05/14 12:58:22 Right.
128 manager()->AbortSession(session_id);
129 }
130
131 void SpeechRecognitionDispatcherHost::OnStopCaptureRequest(
132 int render_view_id, int js_handle_id) {
133 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
134 int session_id = manager()->LookupSessionByContext(
135 base::Bind(&IsSameContext,
136 render_process_id_,
137 render_view_id,
138 js_handle_id));
139 if (session_id != 0)
140 manager()->StopAudioCaptureForSession(session_id);
141 }
142
143 // -------- SpeechRecognitionEventListener interface implementation -----------
144
145 void SpeechRecognitionDispatcherHost::OnRecognitionStart(int session_id) {
146 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
147 const SpeechRecognitionSessionContext& context =
148 manager()->GetSessionContext(session_id);
149 Send(new SpeechRecognitionMsg_Started(context.render_view_id,
150 context.js_handle_id));
151 }
152
153 void SpeechRecognitionDispatcherHost::OnAudioStart(int session_id) {
154 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
155 const SpeechRecognitionSessionContext& context =
156 manager()->GetSessionContext(session_id);
157 Send(new SpeechRecognitionMsg_AudioStarted(context.render_view_id,
158 context.js_handle_id));
159 }
160
161 void SpeechRecognitionDispatcherHost::OnSoundStart(int session_id) {
162 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
163 const SpeechRecognitionSessionContext& context =
164 manager()->GetSessionContext(session_id);
165 Send(new SpeechRecognitionMsg_SoundStarted(context.render_view_id,
166 context.js_handle_id));
167 }
168
169 void SpeechRecognitionDispatcherHost::OnSoundEnd(int session_id) {
170 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
171 const SpeechRecognitionSessionContext& context =
172 manager()->GetSessionContext(session_id);
173 Send(new SpeechRecognitionMsg_SoundEnded(context.render_view_id,
174 context.js_handle_id));
175 }
176
177 void SpeechRecognitionDispatcherHost::OnAudioEnd(int session_id) {
178 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
179 const SpeechRecognitionSessionContext& context =
180 manager()->GetSessionContext(session_id);
181 Send(new SpeechRecognitionMsg_AudioEnded(context.render_view_id,
182 context.js_handle_id));
183 }
184
185 void SpeechRecognitionDispatcherHost::OnRecognitionEnd(int session_id) {
186 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
187 const SpeechRecognitionSessionContext& context =
188 manager()->GetSessionContext(session_id);
189 Send(new SpeechRecognitionMsg_Ended(context.render_view_id,
190 context.js_handle_id));
191 }
192
193 void SpeechRecognitionDispatcherHost::OnRecognitionResult(
194 int session_id, const content::SpeechRecognitionResult& result) {
195 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
196 const SpeechRecognitionSessionContext& context =
197 manager()->GetSessionContext(session_id);
198 Send(new SpeechRecognitionMsg_ResultRetrieved(context.render_view_id,
199 context.js_handle_id,
200 result));
201 }
202
203 void SpeechRecognitionDispatcherHost::OnRecognitionError(
204 int session_id, const content::SpeechRecognitionError& error) {
205 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
206 const SpeechRecognitionSessionContext& context =
207 manager()->GetSessionContext(session_id);
208 Send(new SpeechRecognitionMsg_ErrorOccurred(context.render_view_id,
209 context.js_handle_id,
210 error));
211 }
212
213 // The events below are currently not used by speech JS APIs implementation.
214 void SpeechRecognitionDispatcherHost::OnAudioLevelsChange(
215 int session_id, float volume, float noise_volume) {}
216 void SpeechRecognitionDispatcherHost::OnEnvironmentEstimationComplete(
217 int session_id) {}
218
219 } // namespace speech
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698