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 "content/browser/browser_main_loop.h" | 8 #include "content/browser/browser_main_loop.h" |
9 #include "content/browser/renderer_host/media/media_stream_manager.h" | 9 #include "content/browser/renderer_host/media/media_stream_manager.h" |
10 #include "content/browser/renderer_host/media/media_stream_ui_proxy.h" | 10 #include "content/browser/renderer_host/media/media_stream_ui_proxy.h" |
11 #include "content/browser/speech/google_one_shot_remote_engine.h" | 11 #include "content/browser/speech/google_one_shot_remote_engine.h" |
12 #include "content/browser/speech/google_streaming_remote_engine.h" | 12 #include "content/browser/speech/google_streaming_remote_engine.h" |
13 #include "content/browser/speech/speech_recognition_engine.h" | 13 #include "content/browser/speech/speech_recognition_engine.h" |
14 #include "content/browser/speech/speech_recognizer_impl.h" | 14 #include "content/browser/speech/speech_recognizer_impl.h" |
15 #include "content/public/browser/browser_thread.h" | 15 #include "content/public/browser/browser_thread.h" |
16 #include "content/public/browser/content_browser_client.h" | 16 #include "content/public/browser/content_browser_client.h" |
| 17 #include "content/public/browser/render_frame_host.h" |
| 18 #include "content/public/browser/render_process_host.h" |
17 #include "content/public/browser/resource_context.h" | 19 #include "content/public/browser/resource_context.h" |
18 #include "content/public/browser/speech_recognition_event_listener.h" | 20 #include "content/public/browser/speech_recognition_event_listener.h" |
19 #include "content/public/browser/speech_recognition_manager_delegate.h" | 21 #include "content/public/browser/speech_recognition_manager_delegate.h" |
20 #include "content/public/browser/speech_recognition_session_config.h" | 22 #include "content/public/browser/speech_recognition_session_config.h" |
21 #include "content/public/browser/speech_recognition_session_context.h" | 23 #include "content/public/browser/speech_recognition_session_context.h" |
22 #include "content/public/common/speech_recognition_error.h" | 24 #include "content/public/common/speech_recognition_error.h" |
23 #include "content/public/common/speech_recognition_result.h" | 25 #include "content/public/common/speech_recognition_result.h" |
24 #include "media/audio/audio_manager.h" | 26 #include "media/audio/audio_manager.h" |
25 #include "media/audio/audio_manager_base.h" | 27 #include "media/audio/audio_manager_base.h" |
26 | 28 |
(...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
188 SessionsTable::iterator iter = sessions_.find(session_id); | 190 SessionsTable::iterator iter = sessions_.find(session_id); |
189 DCHECK(iter != sessions_.end()); | 191 DCHECK(iter != sessions_.end()); |
190 Session* session = iter->second; | 192 Session* session = iter->second; |
191 | 193 |
192 if (session->abort_requested) | 194 if (session->abort_requested) |
193 return; | 195 return; |
194 | 196 |
195 if (ask_user) { | 197 if (ask_user) { |
196 SpeechRecognitionSessionContext& context = session->context; | 198 SpeechRecognitionSessionContext& context = session->context; |
197 context.label = media_stream_manager_->MakeMediaAccessRequest( | 199 context.label = media_stream_manager_->MakeMediaAccessRequest( |
198 context.render_process_id, | 200 context.render_frame_host->GetProcess()->GetID(), |
199 context.render_frame_id, | 201 context.render_frame_host->GetRoutingID(), |
200 context.request_id, | 202 context.request_id, |
201 StreamOptions(true, false), | 203 StreamOptions(true, false), |
202 GURL(context.context_name), | 204 GURL(context.context_name), |
203 base::Bind( | 205 base::Bind( |
204 &SpeechRecognitionManagerImpl::MediaRequestPermissionCallback, | 206 &SpeechRecognitionManagerImpl::MediaRequestPermissionCallback, |
205 weak_factory_.GetWeakPtr(), session_id)); | 207 weak_factory_.GetWeakPtr(), session_id)); |
206 return; | 208 return; |
207 } | 209 } |
208 | 210 |
209 if (is_allowed) { | 211 if (is_allowed) { |
(...skipping 214 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
424 listener->OnRecognitionEnd(session_id); | 426 listener->OnRecognitionEnd(session_id); |
425 base::MessageLoop::current()->PostTask( | 427 base::MessageLoop::current()->PostTask( |
426 FROM_HERE, | 428 FROM_HERE, |
427 base::Bind(&SpeechRecognitionManagerImpl::DispatchEvent, | 429 base::Bind(&SpeechRecognitionManagerImpl::DispatchEvent, |
428 weak_factory_.GetWeakPtr(), | 430 weak_factory_.GetWeakPtr(), |
429 session_id, | 431 session_id, |
430 EVENT_RECOGNITION_ENDED)); | 432 EVENT_RECOGNITION_ENDED)); |
431 } | 433 } |
432 | 434 |
433 int SpeechRecognitionManagerImpl::GetSession( | 435 int SpeechRecognitionManagerImpl::GetSession( |
434 int render_process_id, int render_view_id, int request_id) const { | 436 RenderFrameHost* render_frame_host, int request_id) const { |
435 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 437 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
436 SessionsTable::const_iterator iter; | 438 SessionsTable::const_iterator iter; |
437 for(iter = sessions_.begin(); iter != sessions_.end(); ++iter) { | 439 for(iter = sessions_.begin(); iter != sessions_.end(); ++iter) { |
438 const int session_id = iter->first; | 440 const int session_id = iter->first; |
439 const SpeechRecognitionSessionContext& context = iter->second->context; | 441 const SpeechRecognitionSessionContext& context = iter->second->context; |
440 if (context.render_process_id == render_process_id && | 442 if (context.render_frame_host == render_frame_host && |
441 context.render_view_id == render_view_id && | |
442 context.request_id == request_id) { | 443 context.request_id == request_id) { |
443 return session_id; | 444 return session_id; |
444 } | 445 } |
445 } | 446 } |
446 return kSessionIDInvalid; | 447 return kSessionIDInvalid; |
447 } | 448 } |
448 | 449 |
449 SpeechRecognitionSessionContext | 450 SpeechRecognitionSessionContext |
450 SpeechRecognitionManagerImpl::GetSessionContext(int session_id) const { | 451 SpeechRecognitionManagerImpl::GetSessionContext(int session_id) const { |
451 return GetSession(session_id)->context; | 452 return GetSession(session_id)->context; |
452 } | 453 } |
453 | 454 |
454 void SpeechRecognitionManagerImpl::AbortAllSessionsForRenderProcess( | 455 void SpeechRecognitionManagerImpl::AbortAllSessionsForRenderFrame( |
455 int render_process_id) { | 456 RenderFrameHost* render_frame_host) { |
456 // This method gracefully destroys sessions for the listener. However, since | 457 DCHECK(render_frame_host); |
457 // the listener itself is likely to be destroyed after this call, we avoid | 458 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
458 // dispatching further events to it, marking the |listener_is_active| flag. | 459 |
459 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
460 for (SessionsTable::iterator it = sessions_.begin(); it != sessions_.end(); | 460 for (SessionsTable::iterator it = sessions_.begin(); it != sessions_.end(); |
461 ++it) { | 461 ++it) { |
462 Session* session = it->second; | 462 Session* session = it->second; |
463 if (session->context.render_process_id == render_process_id) { | 463 if (session->context.render_frame_host == render_frame_host) |
464 AbortSession(session->id); | 464 AbortSession(session->id); |
465 session->listener_is_active = false; | |
466 } | |
467 } | 465 } |
468 } | 466 } |
469 | 467 |
470 void SpeechRecognitionManagerImpl::AbortAllSessionsForRenderView( | |
471 int render_process_id, | |
472 int render_view_id) { | |
473 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
474 for (SessionsTable::iterator it = sessions_.begin(); it != sessions_.end(); | |
475 ++it) { | |
476 Session* session = it->second; | |
477 if (session->context.render_process_id == render_process_id && | |
478 session->context.render_view_id == render_view_id) { | |
479 AbortSession(session->id); | |
480 } | |
481 } | |
482 } | |
483 | |
484 // ----------------------- Core FSM implementation --------------------------- | 468 // ----------------------- Core FSM implementation --------------------------- |
485 void SpeechRecognitionManagerImpl::DispatchEvent(int session_id, | 469 void SpeechRecognitionManagerImpl::DispatchEvent(int session_id, |
486 FSMEvent event) { | 470 FSMEvent event) { |
487 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 471 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
488 | 472 |
489 // There are some corner cases in which the session might be deleted (due to | 473 // There are some corner cases in which the session might be deleted (due to |
490 // an EndRecognition event) between a request (e.g. Abort) and its dispatch. | 474 // an EndRecognition event) between a request (e.g. Abort) and its dispatch. |
491 if (!SessionExists(session_id)) | 475 if (!SessionExists(session_id)) |
492 return; | 476 return; |
493 | 477 |
(...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
691 SpeechRecognitionManagerImpl::Session::Session() | 675 SpeechRecognitionManagerImpl::Session::Session() |
692 : id(kSessionIDInvalid), | 676 : id(kSessionIDInvalid), |
693 abort_requested(false), | 677 abort_requested(false), |
694 listener_is_active(true) { | 678 listener_is_active(true) { |
695 } | 679 } |
696 | 680 |
697 SpeechRecognitionManagerImpl::Session::~Session() { | 681 SpeechRecognitionManagerImpl::Session::~Session() { |
698 } | 682 } |
699 | 683 |
700 } // namespace content | 684 } // namespace content |
OLD | NEW |