| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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 "base/location.h" |
| 9 #include "base/single_thread_task_runner.h" |
| 10 #include "base/thread_task_runner_handle.h" |
| 8 #include "content/browser/browser_main_loop.h" | 11 #include "content/browser/browser_main_loop.h" |
| 9 #include "content/browser/renderer_host/media/media_stream_manager.h" | 12 #include "content/browser/renderer_host/media/media_stream_manager.h" |
| 10 #include "content/browser/renderer_host/media/media_stream_ui_proxy.h" | 13 #include "content/browser/renderer_host/media/media_stream_ui_proxy.h" |
| 11 #include "content/browser/speech/google_one_shot_remote_engine.h" | 14 #include "content/browser/speech/google_one_shot_remote_engine.h" |
| 12 #include "content/browser/speech/google_streaming_remote_engine.h" | 15 #include "content/browser/speech/google_streaming_remote_engine.h" |
| 13 #include "content/browser/speech/speech_recognition_engine.h" | 16 #include "content/browser/speech/speech_recognition_engine.h" |
| 14 #include "content/browser/speech/speech_recognizer_impl.h" | 17 #include "content/browser/speech/speech_recognizer_impl.h" |
| 15 #include "content/public/browser/browser_thread.h" | 18 #include "content/public/browser/browser_thread.h" |
| 16 #include "content/public/browser/content_browser_client.h" | 19 #include "content/public/browser/content_browser_client.h" |
| 17 #include "content/public/browser/resource_context.h" | 20 #include "content/public/browser/resource_context.h" |
| (...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 203 context.request_id, | 206 context.request_id, |
| 204 StreamOptions(true, false), | 207 StreamOptions(true, false), |
| 205 GURL(context.context_name), | 208 GURL(context.context_name), |
| 206 base::Bind( | 209 base::Bind( |
| 207 &SpeechRecognitionManagerImpl::MediaRequestPermissionCallback, | 210 &SpeechRecognitionManagerImpl::MediaRequestPermissionCallback, |
| 208 weak_factory_.GetWeakPtr(), session_id)); | 211 weak_factory_.GetWeakPtr(), session_id)); |
| 209 return; | 212 return; |
| 210 } | 213 } |
| 211 | 214 |
| 212 if (is_allowed) { | 215 if (is_allowed) { |
| 213 base::MessageLoop::current()->PostTask( | 216 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 214 FROM_HERE, | 217 FROM_HERE, |
| 215 base::Bind(&SpeechRecognitionManagerImpl::DispatchEvent, | 218 base::Bind(&SpeechRecognitionManagerImpl::DispatchEvent, |
| 216 weak_factory_.GetWeakPtr(), | 219 weak_factory_.GetWeakPtr(), session_id, EVENT_START)); |
| 217 session_id, | |
| 218 EVENT_START)); | |
| 219 } else { | 220 } else { |
| 220 OnRecognitionError(session_id, SpeechRecognitionError( | 221 OnRecognitionError(session_id, SpeechRecognitionError( |
| 221 SPEECH_RECOGNITION_ERROR_NOT_ALLOWED)); | 222 SPEECH_RECOGNITION_ERROR_NOT_ALLOWED)); |
| 222 base::MessageLoop::current()->PostTask( | 223 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 223 FROM_HERE, | 224 FROM_HERE, |
| 224 base::Bind(&SpeechRecognitionManagerImpl::DispatchEvent, | 225 base::Bind(&SpeechRecognitionManagerImpl::DispatchEvent, |
| 225 weak_factory_.GetWeakPtr(), | 226 weak_factory_.GetWeakPtr(), session_id, EVENT_ABORT)); |
| 226 session_id, | |
| 227 EVENT_ABORT)); | |
| 228 } | 227 } |
| 229 } | 228 } |
| 230 | 229 |
| 231 void SpeechRecognitionManagerImpl::MediaRequestPermissionCallback( | 230 void SpeechRecognitionManagerImpl::MediaRequestPermissionCallback( |
| 232 int session_id, | 231 int session_id, |
| 233 const MediaStreamDevices& devices, | 232 const MediaStreamDevices& devices, |
| 234 scoped_ptr<MediaStreamUIProxy> stream_ui) { | 233 scoped_ptr<MediaStreamUIProxy> stream_ui) { |
| 235 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 234 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 236 | 235 |
| 237 SessionsTable::iterator iter = sessions_.find(session_id); | 236 SessionsTable::iterator iter = sessions_.find(session_id); |
| (...skipping 22 matching lines...) Expand all Loading... |
| 260 return; | 259 return; |
| 261 | 260 |
| 262 SessionsTable::iterator iter = sessions_.find(session_id); | 261 SessionsTable::iterator iter = sessions_.find(session_id); |
| 263 iter->second->ui.reset(); | 262 iter->second->ui.reset(); |
| 264 | 263 |
| 265 if (iter->second->abort_requested) | 264 if (iter->second->abort_requested) |
| 266 return; | 265 return; |
| 267 | 266 |
| 268 iter->second->abort_requested = true; | 267 iter->second->abort_requested = true; |
| 269 | 268 |
| 270 base::MessageLoop::current()->PostTask( | 269 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 271 FROM_HERE, | 270 FROM_HERE, |
| 272 base::Bind(&SpeechRecognitionManagerImpl::DispatchEvent, | 271 base::Bind(&SpeechRecognitionManagerImpl::DispatchEvent, |
| 273 weak_factory_.GetWeakPtr(), | 272 weak_factory_.GetWeakPtr(), session_id, EVENT_ABORT)); |
| 274 session_id, | |
| 275 EVENT_ABORT)); | |
| 276 } | 273 } |
| 277 | 274 |
| 278 void SpeechRecognitionManagerImpl::StopAudioCaptureForSession(int session_id) { | 275 void SpeechRecognitionManagerImpl::StopAudioCaptureForSession(int session_id) { |
| 279 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 276 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 280 if (!SessionExists(session_id)) | 277 if (!SessionExists(session_id)) |
| 281 return; | 278 return; |
| 282 | 279 |
| 283 SessionsTable::iterator iter = sessions_.find(session_id); | 280 SessionsTable::iterator iter = sessions_.find(session_id); |
| 284 iter->second->ui.reset(); | 281 iter->second->ui.reset(); |
| 285 | 282 |
| 286 base::MessageLoop::current()->PostTask( | 283 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 287 FROM_HERE, | 284 FROM_HERE, |
| 288 base::Bind(&SpeechRecognitionManagerImpl::DispatchEvent, | 285 base::Bind(&SpeechRecognitionManagerImpl::DispatchEvent, |
| 289 weak_factory_.GetWeakPtr(), | 286 weak_factory_.GetWeakPtr(), session_id, EVENT_STOP_CAPTURE)); |
| 290 session_id, | |
| 291 EVENT_STOP_CAPTURE)); | |
| 292 } | 287 } |
| 293 | 288 |
| 294 // Here begins the SpeechRecognitionEventListener interface implementation, | 289 // Here begins the SpeechRecognitionEventListener interface implementation, |
| 295 // which will simply relay the events to the proper listener registered for the | 290 // which will simply relay the events to the proper listener registered for the |
| 296 // particular session and to the catch-all listener provided by the delegate | 291 // particular session and to the catch-all listener provided by the delegate |
| 297 // (if any). | 292 // (if any). |
| 298 | 293 |
| 299 void SpeechRecognitionManagerImpl::OnRecognitionStart(int session_id) { | 294 void SpeechRecognitionManagerImpl::OnRecognitionStart(int session_id) { |
| 300 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 295 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 301 if (!SessionExists(session_id)) | 296 if (!SessionExists(session_id)) |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 365 | 360 |
| 366 void SpeechRecognitionManagerImpl::OnAudioEnd(int session_id) { | 361 void SpeechRecognitionManagerImpl::OnAudioEnd(int session_id) { |
| 367 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 362 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 368 if (!SessionExists(session_id)) | 363 if (!SessionExists(session_id)) |
| 369 return; | 364 return; |
| 370 | 365 |
| 371 if (SpeechRecognitionEventListener* delegate_listener = GetDelegateListener()) | 366 if (SpeechRecognitionEventListener* delegate_listener = GetDelegateListener()) |
| 372 delegate_listener->OnAudioEnd(session_id); | 367 delegate_listener->OnAudioEnd(session_id); |
| 373 if (SpeechRecognitionEventListener* listener = GetListener(session_id)) | 368 if (SpeechRecognitionEventListener* listener = GetListener(session_id)) |
| 374 listener->OnAudioEnd(session_id); | 369 listener->OnAudioEnd(session_id); |
| 375 base::MessageLoop::current()->PostTask( | 370 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 376 FROM_HERE, | 371 FROM_HERE, |
| 377 base::Bind(&SpeechRecognitionManagerImpl::DispatchEvent, | 372 base::Bind(&SpeechRecognitionManagerImpl::DispatchEvent, |
| 378 weak_factory_.GetWeakPtr(), | 373 weak_factory_.GetWeakPtr(), session_id, EVENT_AUDIO_ENDED)); |
| 379 session_id, | |
| 380 EVENT_AUDIO_ENDED)); | |
| 381 } | 374 } |
| 382 | 375 |
| 383 void SpeechRecognitionManagerImpl::OnRecognitionResults( | 376 void SpeechRecognitionManagerImpl::OnRecognitionResults( |
| 384 int session_id, const SpeechRecognitionResults& results) { | 377 int session_id, const SpeechRecognitionResults& results) { |
| 385 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 378 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 386 if (!SessionExists(session_id)) | 379 if (!SessionExists(session_id)) |
| 387 return; | 380 return; |
| 388 | 381 |
| 389 if (SpeechRecognitionEventListener* delegate_listener = GetDelegateListener()) | 382 if (SpeechRecognitionEventListener* delegate_listener = GetDelegateListener()) |
| 390 delegate_listener->OnRecognitionResults(session_id, results); | 383 delegate_listener->OnRecognitionResults(session_id, results); |
| (...skipping 27 matching lines...) Expand all Loading... |
| 418 | 411 |
| 419 void SpeechRecognitionManagerImpl::OnRecognitionEnd(int session_id) { | 412 void SpeechRecognitionManagerImpl::OnRecognitionEnd(int session_id) { |
| 420 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 413 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 421 if (!SessionExists(session_id)) | 414 if (!SessionExists(session_id)) |
| 422 return; | 415 return; |
| 423 | 416 |
| 424 if (SpeechRecognitionEventListener* delegate_listener = GetDelegateListener()) | 417 if (SpeechRecognitionEventListener* delegate_listener = GetDelegateListener()) |
| 425 delegate_listener->OnRecognitionEnd(session_id); | 418 delegate_listener->OnRecognitionEnd(session_id); |
| 426 if (SpeechRecognitionEventListener* listener = GetListener(session_id)) | 419 if (SpeechRecognitionEventListener* listener = GetListener(session_id)) |
| 427 listener->OnRecognitionEnd(session_id); | 420 listener->OnRecognitionEnd(session_id); |
| 428 base::MessageLoop::current()->PostTask( | 421 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 429 FROM_HERE, | 422 FROM_HERE, base::Bind(&SpeechRecognitionManagerImpl::DispatchEvent, |
| 430 base::Bind(&SpeechRecognitionManagerImpl::DispatchEvent, | 423 weak_factory_.GetWeakPtr(), session_id, |
| 431 weak_factory_.GetWeakPtr(), | 424 EVENT_RECOGNITION_ENDED)); |
| 432 session_id, | |
| 433 EVENT_RECOGNITION_ENDED)); | |
| 434 } | 425 } |
| 435 | 426 |
| 436 int SpeechRecognitionManagerImpl::GetSession( | 427 int SpeechRecognitionManagerImpl::GetSession( |
| 437 int render_process_id, int render_view_id, int request_id) const { | 428 int render_process_id, int render_view_id, int request_id) const { |
| 438 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 429 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 439 SessionsTable::const_iterator iter; | 430 SessionsTable::const_iterator iter; |
| 440 for (iter = sessions_.begin(); iter != sessions_.end(); ++iter) { | 431 for (iter = sessions_.begin(); iter != sessions_.end(); ++iter) { |
| 441 const int session_id = iter->first; | 432 const int session_id = iter->first; |
| 442 const SpeechRecognitionSessionContext& context = iter->second->context; | 433 const SpeechRecognitionSessionContext& context = iter->second->context; |
| 443 if (context.render_process_id == render_process_id && | 434 if (context.render_process_id == render_process_id && |
| (...skipping 250 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 694 SpeechRecognitionManagerImpl::Session::Session() | 685 SpeechRecognitionManagerImpl::Session::Session() |
| 695 : id(kSessionIDInvalid), | 686 : id(kSessionIDInvalid), |
| 696 abort_requested(false), | 687 abort_requested(false), |
| 697 listener_is_active(true) { | 688 listener_is_active(true) { |
| 698 } | 689 } |
| 699 | 690 |
| 700 SpeechRecognitionManagerImpl::Session::~Session() { | 691 SpeechRecognitionManagerImpl::Session::~Session() { |
| 701 } | 692 } |
| 702 | 693 |
| 703 } // namespace content | 694 } // namespace content |
| OLD | NEW |