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/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.js_handle_id = params.js_handle_id; | |
Satish
2012/05/20 22:57:46
hmm I see my comment in the header file about the
Primiano Tucci (use gerrit)
2012/05/21 13:38:25
Ok, renamed here and everywhere else to 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 namespace { | |
Satish
2012/05/20 22:57:46
It is customary to have the anonymous namespace at
hans
2012/05/21 09:08:41
I asked for the function to be moved here so it wo
Primiano Tucci (use gerrit)
2012/05/21 13:38:25
Done.
| |
99 bool IsSameContext(int render_process_id, | |
100 int render_view_id, | |
101 int js_handle_id, | |
102 const SpeechRecognitionSessionContext& context) { | |
103 return context.render_process_id == render_process_id && | |
104 context.render_view_id == render_view_id && | |
105 context.js_handle_id == js_handle_id; | |
106 } | |
107 } // namespace | |
108 | |
109 void SpeechRecognitionDispatcherHost::OnAbortRequest(int render_view_id, | |
110 int js_handle_id) { | |
111 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
112 int session_id = manager()->LookupSessionByContext( | |
Satish
2012/05/20 22:57:46
In an earlier CL we discussed about replacing the
Primiano Tucci (use gerrit)
2012/05/21 13:38:25
Done.
| |
113 base::Bind(&IsSameContext, | |
114 render_process_id_, | |
115 render_view_id, | |
116 js_handle_id)); | |
117 if (session_id != content::SpeechRecognitionManager::kSessionIDInvalid) | |
118 manager()->AbortSession(session_id); | |
119 } | |
120 | |
121 void SpeechRecognitionDispatcherHost::OnStopCaptureRequest( | |
122 int render_view_id, int js_handle_id) { | |
123 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
124 int session_id = manager()->LookupSessionByContext( | |
125 base::Bind(&IsSameContext, | |
126 render_process_id_, | |
127 render_view_id, | |
128 js_handle_id)); | |
129 if (session_id != content::SpeechRecognitionManager::kSessionIDInvalid) | |
130 manager()->StopAudioCaptureForSession(session_id); | |
131 } | |
132 | |
133 // -------- SpeechRecognitionEventListener interface implementation ----------- | |
134 | |
135 void SpeechRecognitionDispatcherHost::OnRecognitionStart(int session_id) { | |
136 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
137 const SpeechRecognitionSessionContext& context = | |
138 manager()->GetSessionContext(session_id); | |
139 Send(new SpeechRecognitionMsg_Started(context.render_view_id, | |
140 context.js_handle_id)); | |
141 } | |
142 | |
143 void SpeechRecognitionDispatcherHost::OnAudioStart(int session_id) { | |
144 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
145 const SpeechRecognitionSessionContext& context = | |
146 manager()->GetSessionContext(session_id); | |
147 Send(new SpeechRecognitionMsg_AudioStarted(context.render_view_id, | |
148 context.js_handle_id)); | |
149 } | |
150 | |
151 void SpeechRecognitionDispatcherHost::OnSoundStart(int session_id) { | |
152 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
153 const SpeechRecognitionSessionContext& context = | |
154 manager()->GetSessionContext(session_id); | |
155 Send(new SpeechRecognitionMsg_SoundStarted(context.render_view_id, | |
156 context.js_handle_id)); | |
157 } | |
158 | |
159 void SpeechRecognitionDispatcherHost::OnSoundEnd(int session_id) { | |
160 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
161 const SpeechRecognitionSessionContext& context = | |
162 manager()->GetSessionContext(session_id); | |
163 Send(new SpeechRecognitionMsg_SoundEnded(context.render_view_id, | |
164 context.js_handle_id)); | |
165 } | |
166 | |
167 void SpeechRecognitionDispatcherHost::OnAudioEnd(int session_id) { | |
168 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
169 const SpeechRecognitionSessionContext& context = | |
170 manager()->GetSessionContext(session_id); | |
171 Send(new SpeechRecognitionMsg_AudioEnded(context.render_view_id, | |
172 context.js_handle_id)); | |
173 } | |
174 | |
175 void SpeechRecognitionDispatcherHost::OnRecognitionEnd(int session_id) { | |
176 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
177 const SpeechRecognitionSessionContext& context = | |
178 manager()->GetSessionContext(session_id); | |
179 Send(new SpeechRecognitionMsg_Ended(context.render_view_id, | |
180 context.js_handle_id)); | |
181 } | |
182 | |
183 void SpeechRecognitionDispatcherHost::OnRecognitionResult( | |
184 int session_id, const content::SpeechRecognitionResult& result) { | |
185 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
186 const SpeechRecognitionSessionContext& context = | |
187 manager()->GetSessionContext(session_id); | |
188 Send(new SpeechRecognitionMsg_ResultRetrieved(context.render_view_id, | |
189 context.js_handle_id, | |
190 result)); | |
191 } | |
192 | |
193 void SpeechRecognitionDispatcherHost::OnRecognitionError( | |
194 int session_id, const content::SpeechRecognitionError& error) { | |
195 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
196 const SpeechRecognitionSessionContext& context = | |
197 manager()->GetSessionContext(session_id); | |
198 Send(new SpeechRecognitionMsg_ErrorOccurred(context.render_view_id, | |
199 context.js_handle_id, | |
200 error)); | |
201 } | |
202 | |
203 // The events below are currently not used by speech JS APIs implementation. | |
204 void SpeechRecognitionDispatcherHost::OnAudioLevelsChange( | |
205 int session_id, float volume, float noise_volume) {} | |
206 void SpeechRecognitionDispatcherHost::OnEnvironmentEstimationComplete( | |
207 int session_id) {} | |
208 | |
209 } // namespace speech | |
OLD | NEW |