Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 "chrome/browser/speech/speech_input_manager.h" | 5 #include "chrome/browser/speech/speech_input_manager.h" |
| 6 | 6 |
| 7 #include "app/l10n_util.h" | 7 #include "app/l10n_util.h" |
| 8 #include "base/ref_counted.h" | 8 #include "base/ref_counted.h" |
| 9 #include "base/singleton.h" | 9 #include "base/singleton.h" |
| 10 #include "chrome/browser/chrome_thread.h" | 10 #include "chrome/browser/chrome_thread.h" |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 30 int render_view_id, | 30 int render_view_id, |
| 31 const gfx::Rect& element_rect); | 31 const gfx::Rect& element_rect); |
| 32 virtual void CancelRecognition(int caller_id); | 32 virtual void CancelRecognition(int caller_id); |
| 33 virtual void StopRecording(int caller_id); | 33 virtual void StopRecording(int caller_id); |
| 34 | 34 |
| 35 // SpeechRecognizer::Delegate methods. | 35 // SpeechRecognizer::Delegate methods. |
| 36 virtual void SetRecognitionResult(int caller_id, bool error, | 36 virtual void SetRecognitionResult(int caller_id, bool error, |
| 37 const string16& value); | 37 const string16& value); |
| 38 virtual void DidCompleteRecording(int caller_id); | 38 virtual void DidCompleteRecording(int caller_id); |
| 39 virtual void DidCompleteRecognition(int caller_id); | 39 virtual void DidCompleteRecognition(int caller_id); |
| 40 virtual void OnRecognizerError(int caller_id); | 40 virtual void OnRecognizerError(int caller_id, |
| 41 SpeechRecognizer::ErrorCode error); | |
| 42 virtual void DidCompleteEnvironmentEstimation(int caller_id); | |
| 41 | 43 |
| 42 // SpeechInputBubbleController::Delegate methods. | 44 // SpeechInputBubbleController::Delegate methods. |
| 43 virtual void RecognitionCancelled(int caller_id); | 45 virtual void RecognitionCancelled(int caller_id); |
| 44 virtual void SpeechInputFocusChanged(int caller_id); | 46 virtual void SpeechInputFocusChanged(int caller_id); |
| 45 | 47 |
| 46 private: | 48 private: |
| 47 struct SpeechInputRequest { | 49 struct SpeechInputRequest { |
| 48 SpeechInputManagerDelegate* delegate; | 50 SpeechInputManagerDelegate* delegate; |
| 49 scoped_refptr<SpeechRecognizer> recognizer; | 51 scoped_refptr<SpeechRecognizer> recognizer; |
| 50 int render_process_id; | 52 int render_process_id; |
| 51 int render_view_id; | 53 int render_view_id; |
| 54 gfx::Rect element_rect; | |
| 52 }; | 55 }; |
| 53 | 56 |
| 54 // Private constructor to enforce singleton. | 57 // Private constructor to enforce singleton. |
| 55 friend struct DefaultSingletonTraits<SpeechInputManagerImpl>; | 58 friend struct DefaultSingletonTraits<SpeechInputManagerImpl>; |
| 56 SpeechInputManagerImpl(); | 59 SpeechInputManagerImpl(); |
| 57 virtual ~SpeechInputManagerImpl(); | 60 virtual ~SpeechInputManagerImpl(); |
| 58 | 61 |
| 59 bool HasPendingRequest(int caller_id) const; | 62 bool HasPendingRequest(int caller_id) const; |
| 60 SpeechRecognizer* GetRecognizer(int caller_id) const; | 63 SpeechRecognizer* GetRecognizer(int caller_id) const; |
| 61 SpeechInputManagerDelegate* GetDelegate(int caller_id) const; | 64 SpeechInputManagerDelegate* GetDelegate(int caller_id) const; |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 117 if (recording_caller_id_) | 120 if (recording_caller_id_) |
| 118 CancelRecognitionAndInformDelegate(recording_caller_id_); | 121 CancelRecognitionAndInformDelegate(recording_caller_id_); |
| 119 | 122 |
| 120 if (!AudioManager::GetAudioManager()->HasAudioInputDevices()) { | 123 if (!AudioManager::GetAudioManager()->HasAudioInputDevices()) { |
| 121 ShowErrorMessage(render_process_id, render_view_id, | 124 ShowErrorMessage(render_process_id, render_view_id, |
| 122 l10n_util::GetStringUTF16(IDS_SPEECH_INPUT_NO_MIC)); | 125 l10n_util::GetStringUTF16(IDS_SPEECH_INPUT_NO_MIC)); |
| 123 delegate->DidCompleteRecognition(caller_id); | 126 delegate->DidCompleteRecognition(caller_id); |
| 124 return; | 127 return; |
| 125 } | 128 } |
| 126 | 129 |
| 127 bubble_controller_->CreateBubble(caller_id, render_process_id, | |
| 128 render_view_id, element_rect); | |
| 129 | |
| 130 recording_caller_id_ = caller_id; | 130 recording_caller_id_ = caller_id; |
| 131 SpeechInputRequest request; | 131 SpeechInputRequest request; |
| 132 request.delegate = delegate; | 132 request.delegate = delegate; |
| 133 request.recognizer = new SpeechRecognizer(this, caller_id); | 133 request.recognizer = new SpeechRecognizer(this, caller_id); |
| 134 request.render_process_id = render_process_id; | 134 request.render_process_id = render_process_id; |
| 135 request.render_view_id = render_view_id; | 135 request.render_view_id = render_view_id; |
| 136 request.element_rect = element_rect; | |
| 136 requests_[caller_id] = request; | 137 requests_[caller_id] = request; |
| 137 request.recognizer->StartRecording(); | 138 request.recognizer->StartRecording(); |
| 138 } | 139 } |
| 139 | 140 |
| 140 void SpeechInputManagerImpl::CancelRecognition(int caller_id) { | 141 void SpeechInputManagerImpl::CancelRecognition(int caller_id) { |
| 141 DCHECK(HasPendingRequest(caller_id)); | 142 DCHECK(HasPendingRequest(caller_id)); |
| 142 GetRecognizer(caller_id)->CancelRecognition(); | 143 GetRecognizer(caller_id)->CancelRecognition(); |
| 143 requests_.erase(caller_id); | 144 requests_.erase(caller_id); |
| 144 if (recording_caller_id_ == caller_id) | 145 if (recording_caller_id_ == caller_id) |
| 145 recording_caller_id_ = 0; | 146 recording_caller_id_ = 0; |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 166 GetDelegate(caller_id)->DidCompleteRecording(caller_id); | 167 GetDelegate(caller_id)->DidCompleteRecording(caller_id); |
| 167 bubble_controller_->SetBubbleToRecognizingMode(caller_id); | 168 bubble_controller_->SetBubbleToRecognizingMode(caller_id); |
| 168 } | 169 } |
| 169 | 170 |
| 170 void SpeechInputManagerImpl::DidCompleteRecognition(int caller_id) { | 171 void SpeechInputManagerImpl::DidCompleteRecognition(int caller_id) { |
| 171 GetDelegate(caller_id)->DidCompleteRecognition(caller_id); | 172 GetDelegate(caller_id)->DidCompleteRecognition(caller_id); |
| 172 requests_.erase(caller_id); | 173 requests_.erase(caller_id); |
| 173 bubble_controller_->CloseBubble(caller_id); | 174 bubble_controller_->CloseBubble(caller_id); |
| 174 } | 175 } |
| 175 | 176 |
| 176 void SpeechInputManagerImpl::OnRecognizerError(int caller_id) { | 177 void SpeechInputManagerImpl::OnRecognizerError( |
| 178 int caller_id, SpeechRecognizer::ErrorCode error) { | |
| 179 // TODO(satish): Show specific messages for the various error codes and allow | |
| 180 // user to retry without ending the recognition session if possible. | |
| 177 const SpeechInputRequest& request = requests_.find(caller_id)->second; | 181 const SpeechInputRequest& request = requests_.find(caller_id)->second; |
| 178 ShowErrorMessage(request.render_process_id, request.render_view_id, | 182 ShowErrorMessage(request.render_process_id, request.render_view_id, |
| 179 l10n_util::GetStringUTF16(IDS_SPEECH_INPUT_ERROR)); | 183 l10n_util::GetStringUTF16(IDS_SPEECH_INPUT_ERROR)); |
| 180 } | 184 } |
| 181 | 185 |
| 186 void SpeechInputManagerImpl::DidCompleteEnvironmentEstimation(int caller_id) { | |
| 187 DCHECK(HasPendingRequest(caller_id)); | |
| 188 DCHECK(recording_caller_id_ == caller_id); | |
| 189 | |
| 190 // Speech recognizer has gathered enough background audio so we can ask the | |
| 191 // user to start speaking. | |
| 192 const SpeechInputRequest& request = requests_.find(caller_id)->second; | |
|
joth
2010/09/09 10:51:37
maybe easier as const SpeechInputRequest& request
| |
| 193 bubble_controller_->CreateBubble(caller_id, request.render_process_id, | |
| 194 request.render_view_id, | |
| 195 request.element_rect); | |
| 196 } | |
| 197 | |
| 182 void SpeechInputManagerImpl::CancelRecognitionAndInformDelegate(int caller_id) { | 198 void SpeechInputManagerImpl::CancelRecognitionAndInformDelegate(int caller_id) { |
| 183 SpeechInputManagerDelegate* cur_delegate = GetDelegate(caller_id); | 199 SpeechInputManagerDelegate* cur_delegate = GetDelegate(caller_id); |
| 184 CancelRecognition(caller_id); | 200 CancelRecognition(caller_id); |
| 185 cur_delegate->DidCompleteRecording(caller_id); | 201 cur_delegate->DidCompleteRecording(caller_id); |
| 186 cur_delegate->DidCompleteRecognition(caller_id); | 202 cur_delegate->DidCompleteRecognition(caller_id); |
| 187 } | 203 } |
| 188 | 204 |
| 189 void SpeechInputManagerImpl::RecognitionCancelled(int caller_id) { | 205 void SpeechInputManagerImpl::RecognitionCancelled(int caller_id) { |
| 190 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::IO)); | 206 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::IO)); |
| 191 // Ignore if the caller id was not in our active recognizers list because the | 207 // Ignore if the caller id was not in our active recognizers list because the |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 222 int render_process_id, int render_view_id, | 238 int render_process_id, int render_view_id, |
| 223 const string16& message) { | 239 const string16& message) { |
| 224 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); | 240 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); |
| 225 TabContents* tab = tab_util::GetTabContentsByID(render_process_id, | 241 TabContents* tab = tab_util::GetTabContentsByID(render_process_id, |
| 226 render_view_id); | 242 render_view_id); |
| 227 if (tab) // Check in case the tab was closed before we got this request. | 243 if (tab) // Check in case the tab was closed before we got this request. |
| 228 tab->AddInfoBar(new SimpleAlertInfoBarDelegate(tab, message, NULL, true)); | 244 tab->AddInfoBar(new SimpleAlertInfoBarDelegate(tab, message, NULL, true)); |
| 229 } | 245 } |
| 230 | 246 |
| 231 } // namespace speech_input | 247 } // namespace speech_input |
| OLD | NEW |