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

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: Satish 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/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/common/speech_recognition_messages.h"
11 #include "content/public/browser/speech_recognition_manager.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::SpeechRecognitionManager;
19 using content::SpeechRecognitionSessionConfig;
20 using content::SpeechRecognitionSessionContext;
21
22 namespace speech {
23 SpeechRecognitionManager* SpeechRecognitionDispatcherHost::manager_for_tests_;
24
25 void SpeechRecognitionDispatcherHost::SetManagerForTests(
26 SpeechRecognitionManager* manager) {
27 manager_for_tests_ = manager;
28 }
29
30 SpeechRecognitionDispatcherHost::SpeechRecognitionDispatcherHost(
31 int render_process_id,
32 net::URLRequestContextGetter* context_getter,
33 content::SpeechRecognitionPreferences* recognition_preferences)
34 : render_process_id_(render_process_id),
35 context_getter_(context_getter),
36 recognition_preferences_(recognition_preferences) {
37 // Do not add any non-trivial initialization here, instead do it lazily when
38 // required (e.g. see the method |manager()|) or add an Init() method.
39 }
40
41 SpeechRecognitionDispatcherHost::~SpeechRecognitionDispatcherHost() {
42 if (SpeechRecognitionManager* sr_manager = manager())
43 sr_manager->AbortAllSessionsForListener(this);
44 }
45
46 SpeechRecognitionManager* SpeechRecognitionDispatcherHost::manager() {
47 if (manager_for_tests_)
48 return manager_for_tests_;
49
50 const CommandLine& command_line = *CommandLine::ForCurrentProcess();
51 if (command_line.HasSwitch(switches::kEnableScriptedSpeech))
52 return SpeechRecognitionManager::GetInstance();
53
54 return NULL;
55 }
56
57 bool SpeechRecognitionDispatcherHost::OnMessageReceived(
58 const IPC::Message& message, bool* message_was_ok) {
59 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
60 bool handled = true;
61 IPC_BEGIN_MESSAGE_MAP_EX(SpeechRecognitionDispatcherHost, message,
62 *message_was_ok)
63 IPC_MESSAGE_HANDLER(SpeechRecognitionHostMsg_StartRequest,
64 OnStartRequest)
65 IPC_MESSAGE_HANDLER(SpeechRecognitionHostMsg_AbortRequest,
66 OnAbortRequest)
67 IPC_MESSAGE_HANDLER(SpeechRecognitionHostMsg_StopCaptureRequest,
68 OnStopCaptureRequest)
69 IPC_MESSAGE_UNHANDLED(handled = false)
70 IPC_END_MESSAGE_MAP()
71 return handled;
72 }
73
74 void SpeechRecognitionDispatcherHost::OnStartRequest(
75 const SpeechRecognitionHostMsg_StartRequest_Params& params) {
76 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
77
78 SpeechRecognitionSessionContext context;
79 context.render_process_id = render_process_id_;
80 context.render_view_id = params.render_view_id;
81 context.request_id = params.request_id;
82
83 SpeechRecognitionSessionConfig config;
84 config.is_one_shot = params.is_one_shot;
85 config.language = params.language;
86 config.grammars = params.grammars;
87 config.origin_url = params.origin_url;
88 config.initial_context = context;
89 config.url_request_context_getter = context_getter_.get();
90 config.filter_profanities = recognition_preferences_->FilterProfanities();
91 config.event_listener = this;
92
93 int session_id = manager()->CreateSession(config);
94 DCHECK_NE(session_id, content::SpeechRecognitionManager::kSessionIDInvalid);
95 manager()->StartSession(session_id);
96 }
97
98 void SpeechRecognitionDispatcherHost::OnAbortRequest(int render_view_id,
99 int request_id) {
100 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
101 int session_id = manager()->LookupSessionByContext(render_process_id_,
102 render_view_id,
103 request_id);
104 if (session_id != content::SpeechRecognitionManager::kSessionIDInvalid)
105 manager()->AbortSession(session_id);
106 }
107
108 void SpeechRecognitionDispatcherHost::OnStopCaptureRequest(
109 int render_view_id, int request_id) {
110 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
111 int session_id = manager()->LookupSessionByContext(render_process_id_,
112 render_view_id,
113 request_id);
114 if (session_id != content::SpeechRecognitionManager::kSessionIDInvalid)
115 manager()->StopAudioCaptureForSession(session_id);
116 }
117
118 // -------- SpeechRecognitionEventListener interface implementation -----------
119
120 void SpeechRecognitionDispatcherHost::OnRecognitionStart(int session_id) {
121 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
122 const SpeechRecognitionSessionContext& context =
123 manager()->GetSessionContext(session_id);
124 Send(new SpeechRecognitionMsg_Started(context.render_view_id,
125 context.request_id));
126 }
127
128 void SpeechRecognitionDispatcherHost::OnAudioStart(int session_id) {
129 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
130 const SpeechRecognitionSessionContext& context =
131 manager()->GetSessionContext(session_id);
132 Send(new SpeechRecognitionMsg_AudioStarted(context.render_view_id,
133 context.request_id));
134 }
135
136 void SpeechRecognitionDispatcherHost::OnSoundStart(int session_id) {
137 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
138 const SpeechRecognitionSessionContext& context =
139 manager()->GetSessionContext(session_id);
140 Send(new SpeechRecognitionMsg_SoundStarted(context.render_view_id,
141 context.request_id));
142 }
143
144 void SpeechRecognitionDispatcherHost::OnSoundEnd(int session_id) {
145 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
146 const SpeechRecognitionSessionContext& context =
147 manager()->GetSessionContext(session_id);
148 Send(new SpeechRecognitionMsg_SoundEnded(context.render_view_id,
149 context.request_id));
150 }
151
152 void SpeechRecognitionDispatcherHost::OnAudioEnd(int session_id) {
153 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
154 const SpeechRecognitionSessionContext& context =
155 manager()->GetSessionContext(session_id);
156 Send(new SpeechRecognitionMsg_AudioEnded(context.render_view_id,
157 context.request_id));
158 }
159
160 void SpeechRecognitionDispatcherHost::OnRecognitionEnd(int session_id) {
161 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
162 const SpeechRecognitionSessionContext& context =
163 manager()->GetSessionContext(session_id);
164 Send(new SpeechRecognitionMsg_Ended(context.render_view_id,
165 context.request_id));
166 }
167
168 void SpeechRecognitionDispatcherHost::OnRecognitionResult(
169 int session_id, const content::SpeechRecognitionResult& result) {
170 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
171 const SpeechRecognitionSessionContext& context =
172 manager()->GetSessionContext(session_id);
173 Send(new SpeechRecognitionMsg_ResultRetrieved(context.render_view_id,
174 context.request_id,
175 result));
176 }
177
178 void SpeechRecognitionDispatcherHost::OnRecognitionError(
179 int session_id, const content::SpeechRecognitionError& error) {
180 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
181 const SpeechRecognitionSessionContext& context =
182 manager()->GetSessionContext(session_id);
183 Send(new SpeechRecognitionMsg_ErrorOccurred(context.render_view_id,
184 context.request_id,
185 error));
186 }
187
188 // The events below are currently not used by speech JS APIs implementation.
189 void SpeechRecognitionDispatcherHost::OnAudioLevelsChange(
190 int session_id, float volume, float noise_volume) {}
191 void SpeechRecognitionDispatcherHost::OnEnvironmentEstimationComplete(
192 int session_id) {}
193
194 } // namespace speech
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698