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( | |
|
Satish
2011/11/15 13:03:53
can we rename this to CheckRenderViewTypeAndStartR
Leandro Graciá Gil
2011/11/16 00:42:01
Done.
| |
| 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 | |
| 117 // For privacy reasons, prevent from start recording if the speech input | |
|
Satish
2011/11/15 13:03:53
this comment is vague, suggest describing in detai
Leandro Graciá Gil
2011/11/16 00:42:01
Done.
| |
| 118 // bubble won't be able to show. It will fail for any delegate type other | |
| 119 // than TabContents, like extension popups. | |
| 120 if (render_view_host->delegate()->GetRenderViewType() == | |
| 121 content::VIEW_TYPE_TAB_CONTENTS) { | |
| 122 BrowserThread::PostTask( | |
| 123 BrowserThread::IO, FROM_HERE, | |
| 124 base::Bind(&SpeechInputManager::ProceedStartingRecognition, | |
| 125 base::Unretained(this), params)); | |
| 126 } | |
| 127 } | |
| 128 | |
| 129 void SpeechInputManager::ProceedStartingRecognition( | |
| 130 const SpeechInputParams& params) { | |
| 131 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 132 DCHECK(!HasPendingRequest(params.caller_id)); | |
| 62 | 133 |
| 63 ShowRecognitionRequested( | 134 ShowRecognitionRequested( |
| 64 caller_id, render_process_id, render_view_id, element_rect); | 135 params.caller_id, params.render_process_id, params.render_view_id, |
| 136 params.element_rect); | |
| 65 GetRequestInfo(&can_report_metrics_, &request_info_); | 137 GetRequestInfo(&can_report_metrics_, &request_info_); |
| 66 | 138 |
| 67 SpeechInputRequest* request = &requests_[caller_id]; | 139 SpeechInputRequest* request = &requests_[params.caller_id]; |
| 68 request->delegate = delegate; | 140 request->delegate = params.delegate; |
| 69 request->recognizer = new SpeechRecognizer( | 141 request->recognizer = new SpeechRecognizer( |
| 70 this, caller_id, language, grammar, context_getter, | 142 this, params.caller_id, params.language, params.grammar, |
| 71 speech_input_prefs->filter_profanities(), | 143 params.context_getter, params.speech_input_prefs->filter_profanities(), |
| 72 request_info_, can_report_metrics_ ? origin_url : ""); | 144 request_info_, can_report_metrics_ ? params.origin_url : ""); |
| 73 request->is_active = false; | 145 request->is_active = false; |
| 74 | 146 |
| 75 StartRecognitionForRequest(caller_id); | 147 StartRecognitionForRequest(params.caller_id); |
| 76 } | 148 } |
| 77 | 149 |
| 78 void SpeechInputManager::StartRecognitionForRequest(int caller_id) { | 150 void SpeechInputManager::StartRecognitionForRequest(int caller_id) { |
| 79 DCHECK(HasPendingRequest(caller_id)); | 151 DCHECK(HasPendingRequest(caller_id)); |
| 80 | 152 |
| 81 // If we are currently recording audio for another caller, abort that cleanly. | 153 // If we are currently recording audio for another caller, abort that cleanly. |
| 82 if (recording_caller_id_) | 154 if (recording_caller_id_) |
| 83 CancelRecognitionAndInformDelegate(recording_caller_id_); | 155 CancelRecognitionAndInformDelegate(recording_caller_id_); |
| 84 | 156 |
| 85 if (!AudioManager::GetAudioManager()->HasAudioInputDevices()) { | 157 if (!AudioManager::GetAudioManager()->HasAudioInputDevices()) { |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 112 CancelRecognition(it->first); | 184 CancelRecognition(it->first); |
| 113 // This map will have very few elements so it is simpler to restart. | 185 // This map will have very few elements so it is simpler to restart. |
| 114 it = requests_.begin(); | 186 it = requests_.begin(); |
| 115 } else { | 187 } else { |
| 116 ++it; | 188 ++it; |
| 117 } | 189 } |
| 118 } | 190 } |
| 119 } | 191 } |
| 120 | 192 |
| 121 void SpeechInputManager::StopRecording(int caller_id) { | 193 void SpeechInputManager::StopRecording(int caller_id) { |
| 122 DCHECK(HasPendingRequest(caller_id)); | 194 // No pending requests on extension popups. |
| 195 if (!HasPendingRequest(caller_id)) | |
| 196 return; | |
| 197 | |
| 123 requests_[caller_id].recognizer->StopRecording(); | 198 requests_[caller_id].recognizer->StopRecording(); |
| 124 } | 199 } |
| 125 | 200 |
| 126 void SpeechInputManager::SetRecognitionResult( | 201 void SpeechInputManager::SetRecognitionResult( |
| 127 int caller_id, const SpeechInputResult& result) { | 202 int caller_id, const SpeechInputResult& result) { |
| 128 DCHECK(HasPendingRequest(caller_id)); | 203 DCHECK(HasPendingRequest(caller_id)); |
| 129 GetDelegate(caller_id)->SetRecognitionResult(caller_id, result); | 204 GetDelegate(caller_id)->SetRecognitionResult(caller_id, result); |
| 130 } | 205 } |
| 131 | 206 |
| 132 void SpeechInputManager::DidCompleteRecording(int caller_id) { | 207 void SpeechInputManager::DidCompleteRecording(int caller_id) { |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 200 | 275 |
| 201 SpeechInputManager::SpeechInputRequest::SpeechInputRequest() | 276 SpeechInputManager::SpeechInputRequest::SpeechInputRequest() |
| 202 : delegate(NULL), | 277 : delegate(NULL), |
| 203 is_active(false) { | 278 is_active(false) { |
| 204 } | 279 } |
| 205 | 280 |
| 206 SpeechInputManager::SpeechInputRequest::~SpeechInputRequest() { | 281 SpeechInputManager::SpeechInputRequest::~SpeechInputRequest() { |
| 207 } | 282 } |
| 208 | 283 |
| 209 } // namespace speech_input | 284 } // namespace speech_input |
| OLD | NEW |