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 "base/memory/singleton.h" | |
9 #include "content/browser/browser_main_loop.h" | 8 #include "content/browser/browser_main_loop.h" |
10 #include "content/browser/speech/google_one_shot_remote_engine.h" | 9 #include "content/browser/speech/google_one_shot_remote_engine.h" |
11 #include "content/browser/speech/speech_recognition_engine.h" | 10 #include "content/browser/speech/speech_recognition_engine.h" |
12 #include "content/browser/speech/speech_recognizer_impl.h" | 11 #include "content/browser/speech/speech_recognizer_impl.h" |
13 #include "content/public/browser/browser_thread.h" | 12 #include "content/public/browser/browser_thread.h" |
14 #include "content/public/browser/content_browser_client.h" | 13 #include "content/public/browser/content_browser_client.h" |
15 #include "content/public/browser/resource_context.h" | 14 #include "content/public/browser/resource_context.h" |
16 #include "content/public/browser/speech_recognition_event_listener.h" | 15 #include "content/public/browser/speech_recognition_event_listener.h" |
17 #include "content/public/browser/speech_recognition_manager_delegate.h" | 16 #include "content/public/browser/speech_recognition_manager_delegate.h" |
18 #include "content/public/browser/speech_recognition_session_config.h" | 17 #include "content/public/browser/speech_recognition_session_config.h" |
(...skipping 13 matching lines...) Expand all Loading... |
32 using content::SpeechRecognitionSessionConfig; | 31 using content::SpeechRecognitionSessionConfig; |
33 | 32 |
34 namespace content { | 33 namespace content { |
35 const int SpeechRecognitionManager::kSessionIDInvalid = 0; | 34 const int SpeechRecognitionManager::kSessionIDInvalid = 0; |
36 | 35 |
37 SpeechRecognitionManager* SpeechRecognitionManager::GetInstance() { | 36 SpeechRecognitionManager* SpeechRecognitionManager::GetInstance() { |
38 return speech::SpeechRecognitionManagerImpl::GetInstance(); | 37 return speech::SpeechRecognitionManagerImpl::GetInstance(); |
39 } | 38 } |
40 } // namespace content | 39 } // namespace content |
41 | 40 |
| 41 namespace { |
| 42 speech::SpeechRecognitionManagerImpl* g_speech_recognition_manager_impl; |
| 43 } // namespace |
| 44 |
42 namespace speech { | 45 namespace speech { |
43 | 46 |
44 SpeechRecognitionManagerImpl* SpeechRecognitionManagerImpl::GetInstance() { | 47 SpeechRecognitionManagerImpl* SpeechRecognitionManagerImpl::GetInstance() { |
45 return Singleton<SpeechRecognitionManagerImpl, | 48 DCHECK(g_speech_recognition_manager_impl); |
46 LeakySingletonTraits<SpeechRecognitionManagerImpl> >::get(); | 49 return g_speech_recognition_manager_impl; |
47 } | 50 } |
48 | 51 |
49 SpeechRecognitionManagerImpl::SpeechRecognitionManagerImpl() | 52 SpeechRecognitionManagerImpl::SpeechRecognitionManagerImpl() |
50 : session_id_capturing_audio_(kSessionIDInvalid), | 53 : session_id_capturing_audio_(kSessionIDInvalid), |
51 last_session_id_(kSessionIDInvalid), | 54 last_session_id_(kSessionIDInvalid), |
52 is_dispatching_event_(false) { | 55 is_dispatching_event_(false), |
53 delegate_ = content::GetContentClient()->browser()-> | 56 delegate_(content::GetContentClient()->browser()-> |
54 GetSpeechRecognitionManagerDelegate(); | 57 GetSpeechRecognitionManagerDelegate()) { |
| 58 DCHECK(!g_speech_recognition_manager_impl); |
| 59 g_speech_recognition_manager_impl = this; |
55 } | 60 } |
56 | 61 |
57 SpeechRecognitionManagerImpl::~SpeechRecognitionManagerImpl() { | 62 SpeechRecognitionManagerImpl::~SpeechRecognitionManagerImpl() { |
| 63 DCHECK(g_speech_recognition_manager_impl); |
| 64 g_speech_recognition_manager_impl = NULL; |
58 // Recognition sessions will be aborted by the corresponding destructors. | 65 // Recognition sessions will be aborted by the corresponding destructors. |
59 sessions_.clear(); | 66 sessions_.clear(); |
60 } | 67 } |
61 | 68 |
62 int SpeechRecognitionManagerImpl::CreateSession( | 69 int SpeechRecognitionManagerImpl::CreateSession( |
63 const SpeechRecognitionSessionConfig& config) { | 70 const SpeechRecognitionSessionConfig& config) { |
64 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 71 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
65 | 72 |
66 const int session_id = GetNextSessionID(); | 73 const int session_id = GetNextSessionID(); |
67 DCHECK(!SessionExists(session_id)); | 74 DCHECK(!SessionExists(session_id)); |
68 // Set-up the new session. | 75 // Set-up the new session. |
69 Session& session = sessions_[session_id]; | 76 Session& session = sessions_[session_id]; |
70 session.id = session_id; | 77 session.id = session_id; |
71 session.config = config; | 78 session.config = config; |
72 session.context = config.initial_context; | 79 session.context = config.initial_context; |
73 | 80 |
74 std::string hardware_info; | 81 std::string hardware_info; |
75 bool can_report_metrics = false; | 82 bool can_report_metrics = false; |
76 if (delegate_) | 83 if (delegate_.get()) |
77 delegate_->GetDiagnosticInformation(&can_report_metrics, &hardware_info); | 84 delegate_->GetDiagnosticInformation(&can_report_metrics, &hardware_info); |
78 | 85 |
79 GoogleOneShotRemoteEngineConfig remote_engine_config; | 86 GoogleOneShotRemoteEngineConfig remote_engine_config; |
80 remote_engine_config.language = config.language; | 87 remote_engine_config.language = config.language; |
81 remote_engine_config.grammar = config.grammar; | 88 remote_engine_config.grammar = config.grammar; |
82 remote_engine_config.audio_sample_rate = | 89 remote_engine_config.audio_sample_rate = |
83 SpeechRecognizerImpl::kAudioSampleRate; | 90 SpeechRecognizerImpl::kAudioSampleRate; |
84 remote_engine_config.audio_num_bits_per_sample = | 91 remote_engine_config.audio_num_bits_per_sample = |
85 SpeechRecognizerImpl::kNumBitsPerAudioSample; | 92 SpeechRecognizerImpl::kNumBitsPerAudioSample; |
86 remote_engine_config.filter_profanities = config.filter_profanities; | 93 remote_engine_config.filter_profanities = config.filter_profanities; |
(...skipping 14 matching lines...) Expand all Loading... |
101 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 108 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
102 if (!SessionExists(session_id)) | 109 if (!SessionExists(session_id)) |
103 return; | 110 return; |
104 | 111 |
105 // If there is another active session, abort that. | 112 // If there is another active session, abort that. |
106 if (session_id_capturing_audio_ != kSessionIDInvalid && | 113 if (session_id_capturing_audio_ != kSessionIDInvalid && |
107 session_id_capturing_audio_ != session_id) { | 114 session_id_capturing_audio_ != session_id) { |
108 AbortSession(session_id_capturing_audio_); | 115 AbortSession(session_id_capturing_audio_); |
109 } | 116 } |
110 | 117 |
111 if (delegate_) | 118 if (delegate_.get()) |
112 delegate_->CheckRecognitionIsAllowed( | 119 delegate_->CheckRecognitionIsAllowed( |
113 session_id, | 120 session_id, |
114 base::Bind(&SpeechRecognitionManagerImpl::RecognitionAllowedCallback, | 121 base::Bind(&SpeechRecognitionManagerImpl::RecognitionAllowedCallback, |
115 Unretained(this))); | 122 Unretained(this))); |
116 } | 123 } |
117 | 124 |
118 void SpeechRecognitionManagerImpl::RecognitionAllowedCallback(int session_id, | 125 void SpeechRecognitionManagerImpl::RecognitionAllowedCallback(int session_id, |
119 bool is_allowed) { | 126 bool is_allowed) { |
120 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 127 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
121 DCHECK(SessionExists(session_id)); | 128 DCHECK(SessionExists(session_id)); |
(...skipping 343 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
465 } | 472 } |
466 | 473 |
467 SpeechRecognitionEventListener* SpeechRecognitionManagerImpl::GetListener( | 474 SpeechRecognitionEventListener* SpeechRecognitionManagerImpl::GetListener( |
468 int session_id) const { | 475 int session_id) const { |
469 const Session& session = GetSession(session_id); | 476 const Session& session = GetSession(session_id); |
470 return session.listener_is_active ? session.config.event_listener : NULL; | 477 return session.listener_is_active ? session.config.event_listener : NULL; |
471 } | 478 } |
472 | 479 |
473 SpeechRecognitionEventListener* | 480 SpeechRecognitionEventListener* |
474 SpeechRecognitionManagerImpl::GetDelegateListener() const { | 481 SpeechRecognitionManagerImpl::GetDelegateListener() const { |
475 return delegate_ ? delegate_->GetEventListener() : NULL; | 482 return delegate_.get() ? delegate_->GetEventListener() : NULL; |
476 } | 483 } |
477 | 484 |
478 const SpeechRecognitionSessionConfig& | 485 const SpeechRecognitionSessionConfig& |
479 SpeechRecognitionManagerImpl::GetSessionConfig(int session_id) const { | 486 SpeechRecognitionManagerImpl::GetSessionConfig(int session_id) const { |
480 return GetSession(session_id).config; | 487 return GetSession(session_id).config; |
481 } | 488 } |
482 | 489 |
483 bool SpeechRecognitionManagerImpl::HasAudioInputDevices() { | 490 bool SpeechRecognitionManagerImpl::HasAudioInputDevices() { |
484 return BrowserMainLoop::GetAudioManager()->HasAudioInputDevices(); | 491 return BrowserMainLoop::GetAudioManager()->HasAudioInputDevices(); |
485 } | 492 } |
(...skipping 25 matching lines...) Expand all Loading... |
511 | 518 |
512 SpeechRecognitionManagerImpl::Session::Session() | 519 SpeechRecognitionManagerImpl::Session::Session() |
513 : id(kSessionIDInvalid), | 520 : id(kSessionIDInvalid), |
514 listener_is_active(true) { | 521 listener_is_active(true) { |
515 } | 522 } |
516 | 523 |
517 SpeechRecognitionManagerImpl::Session::~Session() { | 524 SpeechRecognitionManagerImpl::Session::~Session() { |
518 } | 525 } |
519 | 526 |
520 } // namespace speech | 527 } // namespace speech |
OLD | NEW |