Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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_input_manager.h" | 5 #include "content/browser/speech/speech_input_manager.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "content/browser/renderer_host/render_view_host.h" | |
| 9 #include "content/browser/renderer_host/render_view_host_delegate.h" | |
| 8 #include "content/browser/speech/speech_input_preferences.h" | 10 #include "content/browser/speech/speech_input_preferences.h" |
| 9 #include "content/public/browser/browser_thread.h" | 11 #include "content/public/browser/browser_thread.h" |
| 12 #include "content/public/common/view_types.h" | |
| 10 #include "media/audio/audio_manager.h" | 13 #include "media/audio/audio_manager.h" |
| 11 | 14 |
| 12 using content::BrowserThread; | 15 using content::BrowserThread; |
| 13 | 16 |
| 14 namespace speech_input { | 17 namespace speech_input { |
| 15 | 18 |
| 19 struct SpeechInputManager::SpeechInputParams { | |
| 20 SpeechInputParams(Delegate* delegate, | |
| 21 int caller_id, | |
| 22 int render_process_id, | |
| 23 int render_view_id, | |
| 24 const gfx::Rect& element_rect, | |
| 25 const std::string& language, | |
| 26 const std::string& grammar, | |
| 27 const std::string& origin_url, | |
| 28 net::URLRequestContextGetter* context_getter, | |
| 29 SpeechInputPreferences* speech_input_prefs) | |
| 30 : delegate(delegate), | |
| 31 caller_id(caller_id), | |
| 32 render_process_id(render_process_id), | |
| 33 render_view_id(render_view_id), | |
| 34 element_rect(element_rect), | |
| 35 language(language), | |
| 36 grammar(grammar), | |
| 37 origin_url(origin_url), | |
| 38 context_getter(context_getter), | |
| 39 speech_input_prefs(speech_input_prefs) { | |
| 40 } | |
| 41 | |
| 42 Delegate* delegate; | |
| 43 int caller_id; | |
| 44 int render_process_id; | |
| 45 int render_view_id; | |
| 46 gfx::Rect element_rect; | |
| 47 std::string language; | |
| 48 std::string grammar; | |
| 49 std::string origin_url; | |
| 50 net::URLRequestContextGetter* context_getter; | |
| 51 SpeechInputPreferences* speech_input_prefs; | |
| 52 }; | |
| 53 | |
| 16 SpeechInputManager::SpeechInputManager() | 54 SpeechInputManager::SpeechInputManager() |
| 17 : can_report_metrics_(false), | 55 : can_report_metrics_(false), |
| 18 recording_caller_id_(0) { | 56 recording_caller_id_(0) { |
| 19 } | 57 } |
| 20 | 58 |
| 21 SpeechInputManager::~SpeechInputManager() { | 59 SpeechInputManager::~SpeechInputManager() { |
| 22 while (requests_.begin() != requests_.end()) | 60 while (requests_.begin() != requests_.end()) |
| 23 CancelRecognition(requests_.begin()->first); | 61 CancelRecognition(requests_.begin()->first); |
| 24 } | 62 } |
| 25 | 63 |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 51 SpeechInputManagerDelegate* delegate, | 89 SpeechInputManagerDelegate* delegate, |
| 52 int caller_id, | 90 int caller_id, |
| 53 int render_process_id, | 91 int render_process_id, |
| 54 int render_view_id, | 92 int render_view_id, |
| 55 const gfx::Rect& element_rect, | 93 const gfx::Rect& element_rect, |
| 56 const std::string& language, | 94 const std::string& language, |
| 57 const std::string& grammar, | 95 const std::string& grammar, |
| 58 const std::string& origin_url, | 96 const std::string& origin_url, |
| 59 net::URLRequestContextGetter* context_getter, | 97 net::URLRequestContextGetter* context_getter, |
| 60 SpeechInputPreferences* speech_input_prefs) { | 98 SpeechInputPreferences* speech_input_prefs) { |
| 61 DCHECK(!HasPendingRequest(caller_id)); | 99 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 100 BrowserThread::PostTask( | |
| 101 BrowserThread::UI, FROM_HERE, | |
| 102 base::Bind(&SpeechInputManager::CheckForExtensionPopup, | |
| 103 base::Unretained(this), SpeechInputParams(delegate, caller_id, | |
| 104 render_process_id, render_view_id, element_rect, language, grammar, | |
| 105 origin_url, context_getter, speech_input_prefs))); | |
| 106 } | |
| 107 | |
| 108 void SpeechInputManager::CheckForExtensionPopup( | |
| 109 const SpeechInputParams& params) { | |
| 110 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 111 | |
| 112 RenderViewHost* render_view_host = RenderViewHost::FromID( | |
| 113 params.render_process_id, params.render_view_id); | |
| 114 if (!render_view_host || !render_view_host->delegate()) | |
| 115 return; | |
| 116 if (render_view_host->delegate()->GetRenderViewType() != | |
|
Aaron Boodman
2011/11/14 19:09:18
(whitelist instead of blacklist)++. I suggest addi
Leandro GraciĆ” Gil
2011/11/14 21:15:09
Done.
| |
| 117 content::VIEW_TYPE_TAB_CONTENTS) { | |
| 118 return; | |
| 119 } | |
| 120 | |
| 121 BrowserThread::PostTask( | |
| 122 BrowserThread::IO, FROM_HERE, | |
| 123 base::Bind(&SpeechInputManager::ProceedStartingRecognition, | |
| 124 base::Unretained(this), params)); | |
| 125 } | |
| 126 | |
| 127 void SpeechInputManager::ProceedStartingRecognition( | |
| 128 const SpeechInputParams& params) { | |
| 129 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 130 DCHECK(!HasPendingRequest(params.caller_id)); | |
| 62 | 131 |
| 63 ShowRecognitionRequested( | 132 ShowRecognitionRequested( |
| 64 caller_id, render_process_id, render_view_id, element_rect); | 133 params.caller_id, params.render_process_id, params.render_view_id, |
| 134 params.element_rect); | |
| 65 GetRequestInfo(&can_report_metrics_, &request_info_); | 135 GetRequestInfo(&can_report_metrics_, &request_info_); |
| 66 | 136 |
| 67 SpeechInputRequest* request = &requests_[caller_id]; | 137 SpeechInputRequest* request = &requests_[params.caller_id]; |
| 68 request->delegate = delegate; | 138 request->delegate = params.delegate; |
| 69 request->recognizer = new SpeechRecognizer( | 139 request->recognizer = new SpeechRecognizer( |
| 70 this, caller_id, language, grammar, context_getter, | 140 this, params.caller_id, params.language, params.grammar, |
| 71 speech_input_prefs->filter_profanities(), | 141 params.context_getter, params.speech_input_prefs->filter_profanities(), |
| 72 request_info_, can_report_metrics_ ? origin_url : ""); | 142 request_info_, can_report_metrics_ ? params.origin_url : ""); |
| 73 request->is_active = false; | 143 request->is_active = false; |
| 74 | 144 |
| 75 StartRecognitionForRequest(caller_id); | 145 StartRecognitionForRequest(params.caller_id); |
| 76 } | 146 } |
| 77 | 147 |
| 78 void SpeechInputManager::StartRecognitionForRequest(int caller_id) { | 148 void SpeechInputManager::StartRecognitionForRequest(int caller_id) { |
| 79 DCHECK(HasPendingRequest(caller_id)); | 149 DCHECK(HasPendingRequest(caller_id)); |
| 80 | 150 |
| 81 // If we are currently recording audio for another caller, abort that cleanly. | 151 // If we are currently recording audio for another caller, abort that cleanly. |
| 82 if (recording_caller_id_) | 152 if (recording_caller_id_) |
| 83 CancelRecognitionAndInformDelegate(recording_caller_id_); | 153 CancelRecognitionAndInformDelegate(recording_caller_id_); |
| 84 | 154 |
| 85 if (!AudioManager::GetAudioManager()->HasAudioInputDevices()) { | 155 if (!AudioManager::GetAudioManager()->HasAudioInputDevices()) { |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 112 CancelRecognition(it->first); | 182 CancelRecognition(it->first); |
| 113 // This map will have very few elements so it is simpler to restart. | 183 // This map will have very few elements so it is simpler to restart. |
| 114 it = requests_.begin(); | 184 it = requests_.begin(); |
| 115 } else { | 185 } else { |
| 116 ++it; | 186 ++it; |
| 117 } | 187 } |
| 118 } | 188 } |
| 119 } | 189 } |
| 120 | 190 |
| 121 void SpeechInputManager::StopRecording(int caller_id) { | 191 void SpeechInputManager::StopRecording(int caller_id) { |
| 122 DCHECK(HasPendingRequest(caller_id)); | 192 // No pending requests on extension popups. |
| 193 if (!HasPendingRequest(caller_id)) | |
| 194 return; | |
| 195 | |
| 123 requests_[caller_id].recognizer->StopRecording(); | 196 requests_[caller_id].recognizer->StopRecording(); |
| 124 } | 197 } |
| 125 | 198 |
| 126 void SpeechInputManager::SetRecognitionResult( | 199 void SpeechInputManager::SetRecognitionResult( |
| 127 int caller_id, const SpeechInputResult& result) { | 200 int caller_id, const SpeechInputResult& result) { |
| 128 DCHECK(HasPendingRequest(caller_id)); | 201 DCHECK(HasPendingRequest(caller_id)); |
| 129 GetDelegate(caller_id)->SetRecognitionResult(caller_id, result); | 202 GetDelegate(caller_id)->SetRecognitionResult(caller_id, result); |
| 130 } | 203 } |
| 131 | 204 |
| 132 void SpeechInputManager::DidCompleteRecording(int caller_id) { | 205 void SpeechInputManager::DidCompleteRecording(int caller_id) { |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 200 | 273 |
| 201 SpeechInputManager::SpeechInputRequest::SpeechInputRequest() | 274 SpeechInputManager::SpeechInputRequest::SpeechInputRequest() |
| 202 : delegate(NULL), | 275 : delegate(NULL), |
| 203 is_active(false) { | 276 is_active(false) { |
| 204 } | 277 } |
| 205 | 278 |
| 206 SpeechInputManager::SpeechInputRequest::~SpeechInputRequest() { | 279 SpeechInputManager::SpeechInputRequest::~SpeechInputRequest() { |
| 207 } | 280 } |
| 208 | 281 |
| 209 } // namespace speech_input | 282 } // namespace speech_input |
| OLD | NEW |