| Index: content/browser/speech/speech_recognition_manager_impl.cc
|
| diff --git a/content/browser/speech/speech_recognition_manager_impl.cc b/content/browser/speech/speech_recognition_manager_impl.cc
|
| index 89a10ba177cf64024cce0dc6ae90ce7ba1d68e8c..e59500ff11bcc5fac78a5cf883ea04e0c9b38dac 100644
|
| --- a/content/browser/speech/speech_recognition_manager_impl.cc
|
| +++ b/content/browser/speech/speech_recognition_manager_impl.cc
|
| @@ -7,6 +7,7 @@
|
| #include "base/bind.h"
|
| #include "content/browser/browser_main_loop.h"
|
| #include "content/browser/renderer_host/media/media_stream_manager.h"
|
| +#include "content/browser/renderer_host/media/media_stream_ui_proxy.h"
|
| #include "content/browser/speech/google_one_shot_remote_engine.h"
|
| #include "content/browser/speech/google_streaming_remote_engine.h"
|
| #include "content/browser/speech/speech_recognition_engine.h"
|
| @@ -68,7 +69,14 @@ SpeechRecognitionManagerImpl::SpeechRecognitionManagerImpl()
|
| SpeechRecognitionManagerImpl::~SpeechRecognitionManagerImpl() {
|
| DCHECK(g_speech_recognition_manager_impl);
|
| g_speech_recognition_manager_impl = NULL;
|
| - // Recognition sessions will be aborted by the corresponding destructors.
|
| +
|
| + for (SessionsTable::iterator it = sessions_.begin(); it != sessions_.end();
|
| + ++it) {
|
| + // MediaStreamUIProxy must be deleted on the IO thread.
|
| + BrowserThread::DeleteSoon(BrowserThread::IO, FROM_HERE,
|
| + it->second->ui.release());
|
| + delete it->second;
|
| + }
|
| sessions_.clear();
|
| }
|
|
|
| @@ -79,10 +87,11 @@ int SpeechRecognitionManagerImpl::CreateSession(
|
| const int session_id = GetNextSessionID();
|
| DCHECK(!SessionExists(session_id));
|
| // Set-up the new session.
|
| - Session& session = sessions_[session_id];
|
| - session.id = session_id;
|
| - session.config = config;
|
| - session.context = config.initial_context;
|
| + Session* session = new Session();
|
| + sessions_[session_id] = session;
|
| + session->id = session_id;
|
| + session->config = config;
|
| + session->context = config.initial_context;
|
|
|
| std::string hardware_info;
|
| bool can_report_metrics = false;
|
| @@ -117,7 +126,7 @@ int SpeechRecognitionManagerImpl::CreateSession(
|
| // The legacy api cannot use continuous mode.
|
| DCHECK(!config.is_legacy_api || !config.continuous);
|
|
|
| - session.recognizer = new SpeechRecognizer(
|
| + session->recognizer = new SpeechRecognizer(
|
| this,
|
| session_id,
|
| !config.continuous,
|
| @@ -162,7 +171,7 @@ void SpeechRecognitionManagerImpl::RecognitionAllowedCallback(int session_id,
|
| if (ask_user) {
|
| SessionsTable::iterator iter = sessions_.find(session_id);
|
| DCHECK(iter != sessions_.end());
|
| - SpeechRecognitionSessionContext& context = iter->second.context;
|
| + SpeechRecognitionSessionContext& context = iter->second->context;
|
| context.label =
|
| BrowserMainLoop::GetMediaStreamManager()->MakeMediaAccessRequest(
|
| context.render_process_id,
|
| @@ -171,8 +180,7 @@ void SpeechRecognitionManagerImpl::RecognitionAllowedCallback(int session_id,
|
| GURL(context.context_name),
|
| base::Bind(
|
| &SpeechRecognitionManagerImpl::MediaRequestPermissionCallback,
|
| - weak_factory_.GetWeakPtr()));
|
| -
|
| + weak_factory_.GetWeakPtr(), session_id));
|
| return;
|
| }
|
| #endif // defined(OS_IOS)
|
| @@ -191,26 +199,26 @@ void SpeechRecognitionManagerImpl::RecognitionAllowedCallback(int session_id,
|
| }
|
|
|
| void SpeechRecognitionManagerImpl::MediaRequestPermissionCallback(
|
| - const std::string& label, const MediaStreamDevices& devices) {
|
| + int session_id,
|
| + const MediaStreamDevices& devices,
|
| + scoped_ptr<MediaStreamUIProxy> stream_ui) {
|
| DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
|
| - for (SessionsTable::iterator iter = sessions_.begin();
|
| - iter != sessions_.end(); ++iter) {
|
| - if (iter->second.context.label == label) {
|
| - bool is_allowed = false;
|
| - if (!devices.empty()) {
|
| - // Copy the approved devices array to the context for UI indication.
|
| - iter->second.context.devices = devices;
|
| - is_allowed = true;
|
| - }
|
|
|
| - // Clear the label to indicate the request has been done.
|
| - iter->second.context.label.clear();
|
| + SessionsTable::iterator iter = sessions_.find(session_id);
|
| + if (iter == sessions_.end())
|
| + return;
|
|
|
| - // Notify the recognition about the request result.
|
| - RecognitionAllowedCallback(iter->first, false, is_allowed);
|
| - break;
|
| - }
|
| + bool is_allowed = !devices.empty();
|
| + if (is_allowed) {
|
| + // Copy the approved devices array to the context for UI indication.
|
| + iter->second->context.devices = devices;
|
| }
|
| +
|
| + // Clear the label to indicate the request has been done.
|
| + iter->second->context.label.clear();
|
| +
|
| + // Notify the recognition about the request result.
|
| + RecognitionAllowedCallback(iter->first, false, is_allowed);
|
| }
|
|
|
| void SpeechRecognitionManagerImpl::AbortSession(int session_id) {
|
| @@ -218,12 +226,8 @@ void SpeechRecognitionManagerImpl::AbortSession(int session_id) {
|
| if (!SessionExists(session_id))
|
| return;
|
|
|
| -#if !defined(OS_IOS)
|
| - const SpeechRecognitionSessionContext& context =
|
| - GetSessionContext(session_id);
|
| - if (!context.label.empty())
|
| - BrowserMainLoop::GetMediaStreamManager()->CancelRequest(context.label);
|
| -#endif // !defined(OS_IOS)
|
| + SessionsTable::iterator iter = sessions_.find(session_id);
|
| + iter->second->ui.reset();
|
|
|
| MessageLoop::current()->PostTask(FROM_HERE,
|
| base::Bind(&SpeechRecognitionManagerImpl::DispatchEvent,
|
| @@ -235,12 +239,8 @@ void SpeechRecognitionManagerImpl::StopAudioCaptureForSession(int session_id) {
|
| if (!SessionExists(session_id))
|
| return;
|
|
|
| -#if !defined(OS_IOS)
|
| - const SpeechRecognitionSessionContext& context =
|
| - GetSessionContext(session_id);
|
| - if (!context.label.empty())
|
| - BrowserMainLoop::GetMediaStreamManager()->CancelRequest(context.label);
|
| -#endif // !defined(OS_IOS)
|
| + SessionsTable::iterator iter = sessions_.find(session_id);
|
| + iter->second->ui.reset();
|
|
|
| MessageLoop::current()->PostTask(FROM_HERE,
|
| base::Bind(&SpeechRecognitionManagerImpl::DispatchEvent,
|
| @@ -257,15 +257,11 @@ void SpeechRecognitionManagerImpl::OnRecognitionStart(int session_id) {
|
| if (!SessionExists(session_id))
|
| return;
|
|
|
| -#if !defined(OS_IOS)
|
| - const SpeechRecognitionSessionContext& context =
|
| - GetSessionContext(session_id);
|
| - if (!context.devices.empty()) {
|
| - // Notify the UI the devices are being used.
|
| - BrowserMainLoop::GetMediaStreamManager()->NotifyUIDevicesOpened(
|
| - context.label);
|
| + SessionsTable::iterator iter = sessions_.find(session_id);
|
| + if (iter->second->ui) {
|
| + // Notify the UI that the devices are being used.
|
| + iter->second->ui->OnStarted(base::Closure());
|
| }
|
| -#endif // !defined(OS_IOS)
|
|
|
| DCHECK_EQ(primary_session_id_, session_id);
|
| if (SpeechRecognitionEventListener* delegate_listener = GetDelegateListener())
|
| @@ -376,15 +372,6 @@ void SpeechRecognitionManagerImpl::OnRecognitionEnd(int session_id) {
|
| DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
|
| if (!SessionExists(session_id))
|
| return;
|
| -#if !defined(OS_IOS)
|
| - const SpeechRecognitionSessionContext& context =
|
| - GetSessionContext(session_id);
|
| - if (!context.devices.empty()) {
|
| - // Notify the UI the devices has been closed.
|
| - BrowserMainLoop::GetMediaStreamManager()->NotifyUIDevicesClosed(
|
| - context.label);
|
| - }
|
| -#endif // !defined(OS_IOS)
|
|
|
| if (SpeechRecognitionEventListener* delegate_listener = GetDelegateListener())
|
| delegate_listener->OnRecognitionEnd(session_id);
|
| @@ -403,7 +390,7 @@ int SpeechRecognitionManagerImpl::GetSession(
|
| SessionsTable::const_iterator iter;
|
| for(iter = sessions_.begin(); iter != sessions_.end(); ++iter) {
|
| const int session_id = iter->first;
|
| - const SpeechRecognitionSessionContext& context = iter->second.context;
|
| + const SpeechRecognitionSessionContext& context = iter->second->context;
|
| if (context.render_process_id == render_process_id &&
|
| context.render_view_id == render_view_id &&
|
| context.request_id == request_id) {
|
| @@ -415,7 +402,7 @@ int SpeechRecognitionManagerImpl::GetSession(
|
|
|
| SpeechRecognitionSessionContext
|
| SpeechRecognitionManagerImpl::GetSessionContext(int session_id) const {
|
| - return GetSession(session_id).context;
|
| + return GetSession(session_id)->context;
|
| }
|
|
|
| void SpeechRecognitionManagerImpl::AbortAllSessionsForListener(
|
| @@ -426,10 +413,10 @@ void SpeechRecognitionManagerImpl::AbortAllSessionsForListener(
|
| DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
|
| for (SessionsTable::iterator it = sessions_.begin(); it != sessions_.end();
|
| ++it) {
|
| - Session& session = it->second;
|
| - if (session.config.event_listener == listener) {
|
| - AbortSession(session.id);
|
| - session.listener_is_active = false;
|
| + Session* session = it->second;
|
| + if (session->config.event_listener == listener) {
|
| + AbortSession(session->id);
|
| + session->listener_is_active = false;
|
| }
|
| }
|
| }
|
| @@ -440,10 +427,10 @@ void SpeechRecognitionManagerImpl::AbortAllSessionsForRenderView(
|
| DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
|
| for (SessionsTable::iterator it = sessions_.begin(); it != sessions_.end();
|
| ++it) {
|
| - Session& session = it->second;
|
| - if (session.context.render_process_id == render_process_id &&
|
| - session.context.render_view_id == render_view_id) {
|
| - AbortSession(session.id);
|
| + Session* session = it->second;
|
| + if (session->context.render_process_id == render_process_id &&
|
| + session->context.render_view_id == render_view_id) {
|
| + AbortSession(session->id);
|
| }
|
| }
|
| }
|
| @@ -458,7 +445,7 @@ void SpeechRecognitionManagerImpl::DispatchEvent(int session_id,
|
| if (!SessionExists(session_id))
|
| return;
|
|
|
| - const Session& session = GetSession(session_id);
|
| + Session* session = GetSession(session_id);
|
| FSMState session_state = GetSessionState(session_id);
|
| DCHECK_LE(session_state, SESSION_STATE_MAX_VALUE);
|
| DCHECK_LE(event, EVENT_MAX_VALUE);
|
| @@ -478,7 +465,7 @@ void SpeechRecognitionManagerImpl::DispatchEvent(int session_id,
|
| // session) are always routed to the SpeechRecognitionEventListener(s)
|
| // regardless the choices taken in this FSM.
|
| void SpeechRecognitionManagerImpl::ExecuteTransitionAndGetNextState(
|
| - const Session& session, FSMState session_state, FSMEvent event) {
|
| + Session* session, FSMState session_state, FSMEvent event) {
|
| // Note: since we're not tracking the state of the recognizer object, rather
|
| // we're directly retrieving it (through GetSessionState), we see its events
|
| // (that are AUDIO_ENDED and RECOGNITION_ENDED) after its state evolution
|
| @@ -491,13 +478,13 @@ void SpeechRecognitionManagerImpl::ExecuteTransitionAndGetNextState(
|
| case SESSION_STATE_IDLE:
|
| switch (event) {
|
| case EVENT_START:
|
| - return SessionStart(session);
|
| + return SessionStart(*session);
|
| case EVENT_ABORT:
|
| - return SessionAbort(session);
|
| + return SessionAbort(*session);
|
| case EVENT_RECOGNITION_ENDED:
|
| return SessionDelete(session);
|
| case EVENT_STOP_CAPTURE:
|
| - return SessionStopAudioCapture(session);
|
| + return SessionStopAudioCapture(*session);
|
| case EVENT_AUDIO_ENDED:
|
| return;
|
| }
|
| @@ -505,39 +492,39 @@ void SpeechRecognitionManagerImpl::ExecuteTransitionAndGetNextState(
|
| case SESSION_STATE_CAPTURING_AUDIO:
|
| switch (event) {
|
| case EVENT_STOP_CAPTURE:
|
| - return SessionStopAudioCapture(session);
|
| + return SessionStopAudioCapture(*session);
|
| case EVENT_ABORT:
|
| - return SessionAbort(session);
|
| + return SessionAbort(*session);
|
| case EVENT_START:
|
| return;
|
| case EVENT_AUDIO_ENDED:
|
| case EVENT_RECOGNITION_ENDED:
|
| - return NotFeasible(session, event);
|
| + return NotFeasible(*session, event);
|
| }
|
| break;
|
| case SESSION_STATE_WAITING_FOR_RESULT:
|
| switch (event) {
|
| case EVENT_ABORT:
|
| - return SessionAbort(session);
|
| + return SessionAbort(*session);
|
| case EVENT_AUDIO_ENDED:
|
| - return ResetCapturingSessionId(session);
|
| + return ResetCapturingSessionId(*session);
|
| case EVENT_START:
|
| case EVENT_STOP_CAPTURE:
|
| return;
|
| case EVENT_RECOGNITION_ENDED:
|
| - return NotFeasible(session, event);
|
| + return NotFeasible(*session, event);
|
| }
|
| break;
|
| }
|
| - return NotFeasible(session, event);
|
| + return NotFeasible(*session, event);
|
| }
|
|
|
| SpeechRecognitionManagerImpl::FSMState
|
| SpeechRecognitionManagerImpl::GetSessionState(int session_id) const {
|
| - const Session& session = GetSession(session_id);
|
| - if (!session.recognizer.get() || !session.recognizer->IsActive())
|
| + Session* session = GetSession(session_id);
|
| + if (!session->recognizer.get() || !session->recognizer->IsActive())
|
| return SESSION_STATE_IDLE;
|
| - if (session.recognizer->IsCapturingAudio())
|
| + if (session->recognizer->IsCapturingAudio())
|
| return SESSION_STATE_CAPTURING_AUDIO;
|
| return SESSION_STATE_WAITING_FOR_RESULT;
|
| }
|
| @@ -570,11 +557,12 @@ void SpeechRecognitionManagerImpl::ResetCapturingSessionId(
|
| primary_session_id_ = kSessionIDInvalid;
|
| }
|
|
|
| -void SpeechRecognitionManagerImpl::SessionDelete(const Session& session) {
|
| - DCHECK(session.recognizer == NULL || !session.recognizer->IsActive());
|
| - if (primary_session_id_ == session.id)
|
| +void SpeechRecognitionManagerImpl::SessionDelete(Session* session) {
|
| + DCHECK(session->recognizer == NULL || !session->recognizer->IsActive());
|
| + if (primary_session_id_ == session->id)
|
| primary_session_id_ = kSessionIDInvalid;
|
| - sessions_.erase(session.id);
|
| + sessions_.erase(session->id);
|
| + delete session;
|
| }
|
|
|
| void SpeechRecognitionManagerImpl::NotFeasible(const Session& session,
|
| @@ -596,7 +584,7 @@ bool SpeechRecognitionManagerImpl::SessionExists(int session_id) const {
|
| return sessions_.find(session_id) != sessions_.end();
|
| }
|
|
|
| -const SpeechRecognitionManagerImpl::Session&
|
| +SpeechRecognitionManagerImpl::Session*
|
| SpeechRecognitionManagerImpl::GetSession(int session_id) const {
|
| DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
|
| SessionsTable::const_iterator iter = sessions_.find(session_id);
|
| @@ -606,8 +594,8 @@ SpeechRecognitionManagerImpl::GetSession(int session_id) const {
|
|
|
| SpeechRecognitionEventListener* SpeechRecognitionManagerImpl::GetListener(
|
| int session_id) const {
|
| - const Session& session = GetSession(session_id);
|
| - return session.listener_is_active ? session.config.event_listener : NULL;
|
| + Session* session = GetSession(session_id);
|
| + return session->listener_is_active ? session->config.event_listener : NULL;
|
| }
|
|
|
| SpeechRecognitionEventListener*
|
| @@ -617,7 +605,7 @@ SpeechRecognitionManagerImpl::GetDelegateListener() const {
|
|
|
| const SpeechRecognitionSessionConfig&
|
| SpeechRecognitionManagerImpl::GetSessionConfig(int session_id) const {
|
| - return GetSession(session_id).config;
|
| + return GetSession(session_id)->config;
|
| }
|
|
|
| bool SpeechRecognitionManagerImpl::HasAudioInputDevices() {
|
|
|