OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/browser/speech/speech_recognition_manager_impl.h" | 5 #include "content/browser/speech/speech_recognition_manager_impl.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "content/browser/browser_main_loop.h" | 8 #include "content/browser/browser_main_loop.h" |
9 #include "content/browser/renderer_host/render_view_host_impl.h" | 9 #include "content/browser/renderer_host/render_view_host_impl.h" |
10 #include "content/browser/speech/input_tag_speech_dispatcher_host.h" | 10 #include "content/browser/speech/input_tag_speech_dispatcher_host.h" |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
66 content::SpeechRecognitionPreferences* recognition_prefs; | 66 content::SpeechRecognitionPreferences* recognition_prefs; |
67 }; | 67 }; |
68 | 68 |
69 SpeechRecognitionManagerImpl* SpeechRecognitionManagerImpl::GetInstance() { | 69 SpeechRecognitionManagerImpl* SpeechRecognitionManagerImpl::GetInstance() { |
70 return Singleton<SpeechRecognitionManagerImpl>::get(); | 70 return Singleton<SpeechRecognitionManagerImpl>::get(); |
71 } | 71 } |
72 | 72 |
73 SpeechRecognitionManagerImpl::SpeechRecognitionManagerImpl() | 73 SpeechRecognitionManagerImpl::SpeechRecognitionManagerImpl() |
74 : can_report_metrics_(false), | 74 : can_report_metrics_(false), |
75 recording_caller_id_(0) { | 75 recording_caller_id_(0) { |
76 delegate_ = content::GetContentClient()->browser()-> | 76 delegate_.reset(content::GetContentClient()->browser()-> |
77 GetSpeechRecognitionManagerDelegate(); | 77 GetSpeechRecognitionManagerDelegate()); |
78 } | 78 } |
79 | 79 |
80 SpeechRecognitionManagerImpl::~SpeechRecognitionManagerImpl() { | 80 SpeechRecognitionManagerImpl::~SpeechRecognitionManagerImpl() { |
81 while (requests_.begin() != requests_.end()) | 81 while (requests_.begin() != requests_.end()) |
82 CancelRecognition(requests_.begin()->first); | 82 CancelRecognition(requests_.begin()->first); |
83 } | 83 } |
84 | 84 |
85 bool SpeechRecognitionManagerImpl::HasAudioInputDevices() { | 85 bool SpeechRecognitionManagerImpl::HasAudioInputDevices() { |
86 return BrowserMainLoop::GetAudioManager()->HasAudioInputDevices(); | 86 return BrowserMainLoop::GetAudioManager()->HasAudioInputDevices(); |
87 } | 87 } |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
165 base::Bind(&SpeechRecognitionManagerImpl::ProceedStartingRecognition, | 165 base::Bind(&SpeechRecognitionManagerImpl::ProceedStartingRecognition, |
166 base::Unretained(this), params)); | 166 base::Unretained(this), params)); |
167 } | 167 } |
168 } | 168 } |
169 | 169 |
170 void SpeechRecognitionManagerImpl::ProceedStartingRecognition( | 170 void SpeechRecognitionManagerImpl::ProceedStartingRecognition( |
171 const SpeechRecognitionParams& params) { | 171 const SpeechRecognitionParams& params) { |
172 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 172 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
173 DCHECK(!HasPendingRequest(params.caller_id)); | 173 DCHECK(!HasPendingRequest(params.caller_id)); |
174 | 174 |
175 if (delegate_) { | 175 if (delegate_.get()) { |
176 delegate_->ShowRecognitionRequested( | 176 delegate_->ShowRecognitionRequested( |
177 params.caller_id, params.render_process_id, params.render_view_id, | 177 params.caller_id, params.render_process_id, params.render_view_id, |
178 params.element_rect); | 178 params.element_rect); |
179 delegate_->GetRequestInfo(&can_report_metrics_, &request_info_); | 179 delegate_->GetRequestInfo(&can_report_metrics_, &request_info_); |
180 } | 180 } |
181 | 181 |
182 Request* request = &requests_[params.caller_id]; | 182 Request* request = &requests_[params.caller_id]; |
183 request->delegate = params.delegate; | 183 request->delegate = params.delegate; |
184 request->recognizer = content::SpeechRecognizer::Create( | 184 request->recognizer = content::SpeechRecognizer::Create( |
185 this, params.caller_id, params.language, params.grammar, | 185 this, params.caller_id, params.language, params.grammar, |
(...skipping 13 matching lines...) Expand all Loading... |
199 | 199 |
200 // We should not currently be recording for the caller. | 200 // We should not currently be recording for the caller. |
201 CHECK(recording_caller_id_ != caller_id); | 201 CHECK(recording_caller_id_ != caller_id); |
202 | 202 |
203 // If we are currently recording audio for another caller, abort that cleanly. | 203 // If we are currently recording audio for another caller, abort that cleanly. |
204 if (recording_caller_id_) | 204 if (recording_caller_id_) |
205 CancelRecognitionAndInformDelegate(recording_caller_id_); | 205 CancelRecognitionAndInformDelegate(recording_caller_id_); |
206 recording_caller_id_ = caller_id; | 206 recording_caller_id_ = caller_id; |
207 requests_[caller_id].is_active = true; | 207 requests_[caller_id].is_active = true; |
208 requests_[caller_id].recognizer->StartRecognition(); | 208 requests_[caller_id].recognizer->StartRecognition(); |
209 if (delegate_) | 209 if (delegate_.get()) |
210 delegate_->ShowWarmUp(caller_id); | 210 delegate_->ShowWarmUp(caller_id); |
211 } | 211 } |
212 | 212 |
213 void SpeechRecognitionManagerImpl::CancelRecognitionForRequest(int caller_id) { | 213 void SpeechRecognitionManagerImpl::CancelRecognitionForRequest(int caller_id) { |
214 // Ignore if the caller id was not in our active recognizers list because the | 214 // Ignore if the caller id was not in our active recognizers list because the |
215 // user might have clicked more than once, or recognition could have been | 215 // user might have clicked more than once, or recognition could have been |
216 // ended due to other reasons before the user click was processed. | 216 // ended due to other reasons before the user click was processed. |
217 if (!HasPendingRequest(caller_id)) | 217 if (!HasPendingRequest(caller_id)) |
218 return; | 218 return; |
219 | 219 |
(...skipping 13 matching lines...) Expand all Loading... |
233 CancelRecognitionAndInformDelegate(caller_id); | 233 CancelRecognitionAndInformDelegate(caller_id); |
234 } | 234 } |
235 | 235 |
236 void SpeechRecognitionManagerImpl::CancelRecognition(int caller_id) { | 236 void SpeechRecognitionManagerImpl::CancelRecognition(int caller_id) { |
237 DCHECK(HasPendingRequest(caller_id)); | 237 DCHECK(HasPendingRequest(caller_id)); |
238 if (requests_[caller_id].is_active) | 238 if (requests_[caller_id].is_active) |
239 requests_[caller_id].recognizer->AbortRecognition(); | 239 requests_[caller_id].recognizer->AbortRecognition(); |
240 requests_.erase(caller_id); | 240 requests_.erase(caller_id); |
241 if (recording_caller_id_ == caller_id) | 241 if (recording_caller_id_ == caller_id) |
242 recording_caller_id_ = 0; | 242 recording_caller_id_ = 0; |
243 if (delegate_) | 243 if (delegate_.get()) |
244 delegate_->DoClose(caller_id); | 244 delegate_->DoClose(caller_id); |
245 } | 245 } |
246 | 246 |
247 void SpeechRecognitionManagerImpl::CancelAllRequestsWithDelegate( | 247 void SpeechRecognitionManagerImpl::CancelAllRequestsWithDelegate( |
248 InputTagSpeechDispatcherHost* delegate) { | 248 InputTagSpeechDispatcherHost* delegate) { |
249 SpeechRecognizerMap::iterator it = requests_.begin(); | 249 SpeechRecognizerMap::iterator it = requests_.begin(); |
250 while (it != requests_.end()) { | 250 while (it != requests_.end()) { |
251 if (it->second.delegate == delegate) { | 251 if (it->second.delegate == delegate) { |
252 CancelRecognition(it->first); | 252 CancelRecognition(it->first); |
253 // This map will have very few elements so it is simpler to restart. | 253 // This map will have very few elements so it is simpler to restart. |
(...skipping 22 matching lines...) Expand all Loading... |
276 | 276 |
277 void SpeechRecognitionManagerImpl::OnAudioEnd(int caller_id) { | 277 void SpeechRecognitionManagerImpl::OnAudioEnd(int caller_id) { |
278 if (recording_caller_id_ != caller_id) | 278 if (recording_caller_id_ != caller_id) |
279 return; | 279 return; |
280 DCHECK_EQ(recording_caller_id_, caller_id); | 280 DCHECK_EQ(recording_caller_id_, caller_id); |
281 DCHECK(HasPendingRequest(caller_id)); | 281 DCHECK(HasPendingRequest(caller_id)); |
282 if (!requests_[caller_id].is_active) | 282 if (!requests_[caller_id].is_active) |
283 return; | 283 return; |
284 recording_caller_id_ = 0; | 284 recording_caller_id_ = 0; |
285 GetDelegate(caller_id)->DidCompleteRecording(caller_id); | 285 GetDelegate(caller_id)->DidCompleteRecording(caller_id); |
286 if (delegate_) | 286 if (delegate_.get()) |
287 delegate_->ShowRecognizing(caller_id); | 287 delegate_->ShowRecognizing(caller_id); |
288 } | 288 } |
289 | 289 |
290 void SpeechRecognitionManagerImpl::OnRecognitionEnd(int caller_id) { | 290 void SpeechRecognitionManagerImpl::OnRecognitionEnd(int caller_id) { |
291 if (!HasPendingRequest(caller_id) || !requests_[caller_id].is_active) | 291 if (!HasPendingRequest(caller_id) || !requests_[caller_id].is_active) |
292 return; | 292 return; |
293 GetDelegate(caller_id)->DidCompleteRecognition(caller_id); | 293 GetDelegate(caller_id)->DidCompleteRecognition(caller_id); |
294 requests_.erase(caller_id); | 294 requests_.erase(caller_id); |
295 if (delegate_) | 295 if (delegate_.get()) |
296 delegate_->DoClose(caller_id); | 296 delegate_->DoClose(caller_id); |
297 } | 297 } |
298 | 298 |
299 void SpeechRecognitionManagerImpl::OnSoundStart(int caller_id) { | 299 void SpeechRecognitionManagerImpl::OnSoundStart(int caller_id) { |
300 } | 300 } |
301 | 301 |
302 void SpeechRecognitionManagerImpl::OnSoundEnd(int caller_id) { | 302 void SpeechRecognitionManagerImpl::OnSoundEnd(int caller_id) { |
303 } | 303 } |
304 | 304 |
305 void SpeechRecognitionManagerImpl::OnRecognitionError( | 305 void SpeechRecognitionManagerImpl::OnRecognitionError( |
306 int caller_id, const content::SpeechRecognitionError& error) { | 306 int caller_id, const content::SpeechRecognitionError& error) { |
307 DCHECK(HasPendingRequest(caller_id)); | 307 DCHECK(HasPendingRequest(caller_id)); |
308 if (caller_id == recording_caller_id_) | 308 if (caller_id == recording_caller_id_) |
309 recording_caller_id_ = 0; | 309 recording_caller_id_ = 0; |
310 requests_[caller_id].is_active = false; | 310 requests_[caller_id].is_active = false; |
311 if (delegate_) { | 311 if (delegate_.get()) { |
312 if (error.code == content::SPEECH_RECOGNITION_ERROR_AUDIO && | 312 if (error.code == content::SPEECH_RECOGNITION_ERROR_AUDIO && |
313 error.details == content::SPEECH_AUDIO_ERROR_DETAILS_NO_MIC) { | 313 error.details == content::SPEECH_AUDIO_ERROR_DETAILS_NO_MIC) { |
314 delegate_->ShowMicError(caller_id, | 314 delegate_->ShowMicError(caller_id, |
315 SpeechRecognitionManagerDelegate::MIC_ERROR_NO_DEVICE_AVAILABLE); | 315 SpeechRecognitionManagerDelegate::MIC_ERROR_NO_DEVICE_AVAILABLE); |
316 } else if (error.code == content::SPEECH_RECOGNITION_ERROR_AUDIO && | 316 } else if (error.code == content::SPEECH_RECOGNITION_ERROR_AUDIO && |
317 error.details == content::SPEECH_AUDIO_ERROR_DETAILS_IN_USE) { | 317 error.details == content::SPEECH_AUDIO_ERROR_DETAILS_IN_USE) { |
318 delegate_->ShowMicError( | 318 delegate_->ShowMicError( |
319 caller_id, SpeechRecognitionManagerDelegate::MIC_ERROR_DEVICE_IN_USE); | 319 caller_id, SpeechRecognitionManagerDelegate::MIC_ERROR_DEVICE_IN_USE); |
320 } else { | 320 } else { |
321 delegate_->ShowRecognizerError(caller_id, error.code); | 321 delegate_->ShowRecognizerError(caller_id, error.code); |
322 } | 322 } |
323 } | 323 } |
324 } | 324 } |
325 | 325 |
326 void SpeechRecognitionManagerImpl::OnAudioStart(int caller_id) { | 326 void SpeechRecognitionManagerImpl::OnAudioStart(int caller_id) { |
327 DCHECK(HasPendingRequest(caller_id)); | 327 DCHECK(HasPendingRequest(caller_id)); |
328 DCHECK_EQ(recording_caller_id_, caller_id); | 328 DCHECK_EQ(recording_caller_id_, caller_id); |
329 if (delegate_) | 329 if (delegate_.get()) |
330 delegate_->ShowRecording(caller_id); | 330 delegate_->ShowRecording(caller_id); |
331 } | 331 } |
332 | 332 |
333 void SpeechRecognitionManagerImpl::OnRecognitionStart(int caller_id) { | 333 void SpeechRecognitionManagerImpl::OnRecognitionStart(int caller_id) { |
334 } | 334 } |
335 | 335 |
336 void SpeechRecognitionManagerImpl::OnEnvironmentEstimationComplete( | 336 void SpeechRecognitionManagerImpl::OnEnvironmentEstimationComplete( |
337 int caller_id) { | 337 int caller_id) { |
338 DCHECK(HasPendingRequest(caller_id)); | 338 DCHECK(HasPendingRequest(caller_id)); |
339 DCHECK_EQ(recording_caller_id_, caller_id); | 339 DCHECK_EQ(recording_caller_id_, caller_id); |
340 } | 340 } |
341 | 341 |
342 void SpeechRecognitionManagerImpl::OnAudioLevelsChange( | 342 void SpeechRecognitionManagerImpl::OnAudioLevelsChange( |
343 int caller_id, float volume, float noise_volume) { | 343 int caller_id, float volume, float noise_volume) { |
344 DCHECK(HasPendingRequest(caller_id)); | 344 DCHECK(HasPendingRequest(caller_id)); |
345 DCHECK_EQ(recording_caller_id_, caller_id); | 345 DCHECK_EQ(recording_caller_id_, caller_id); |
346 if (delegate_) | 346 if (delegate_.get()) |
347 delegate_->ShowInputVolume(caller_id, volume, noise_volume); | 347 delegate_->ShowInputVolume(caller_id, volume, noise_volume); |
348 } | 348 } |
349 | 349 |
350 void SpeechRecognitionManagerImpl::CancelRecognitionAndInformDelegate( | 350 void SpeechRecognitionManagerImpl::CancelRecognitionAndInformDelegate( |
351 int caller_id) { | 351 int caller_id) { |
352 InputTagSpeechDispatcherHost* cur_delegate = GetDelegate(caller_id); | 352 InputTagSpeechDispatcherHost* cur_delegate = GetDelegate(caller_id); |
353 CancelRecognition(caller_id); | 353 CancelRecognition(caller_id); |
354 cur_delegate->DidCompleteRecording(caller_id); | 354 cur_delegate->DidCompleteRecording(caller_id); |
355 cur_delegate->DidCompleteRecognition(caller_id); | 355 cur_delegate->DidCompleteRecognition(caller_id); |
356 } | 356 } |
357 | 357 |
358 SpeechRecognitionManagerImpl::Request::Request() | 358 SpeechRecognitionManagerImpl::Request::Request() |
359 : delegate(NULL), | 359 : is_active(false) { |
360 is_active(false) { | |
361 } | 360 } |
362 | 361 |
363 SpeechRecognitionManagerImpl::Request::~Request() { | 362 SpeechRecognitionManagerImpl::Request::~Request() { |
364 } | 363 } |
365 | 364 |
366 } // namespace speech | 365 } // namespace speech |
OLD | NEW |