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::CheckRenderViewTypeAndStartRecognition, | |
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::CheckRenderViewTypeAndStartRecognition( | |
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 host delegates other than TabContents we can't reliably show a popup, | |
118 // including the speech input bubble. In these cases for privacy reasons we | |
119 // don't want to start recording if the user can't be properly notified. | |
120 // An example of this is trying to show the speech input bubble within an | |
121 // extension popup: http://crbug.com/92083. In these situations the speech | |
122 // input extension API should be used instead. | |
123 | |
Satish
2011/11/16 13:08:56
could remove this extra newline
Leandro GraciĆ” Gil
2011/11/16 14:21:09
Done.
| |
124 if (render_view_host->delegate()->GetRenderViewType() == | |
125 content::VIEW_TYPE_TAB_CONTENTS) { | |
126 BrowserThread::PostTask( | |
127 BrowserThread::IO, FROM_HERE, | |
128 base::Bind(&SpeechInputManager::ProceedStartingRecognition, | |
129 base::Unretained(this), params)); | |
130 } | |
131 } | |
132 | |
133 void SpeechInputManager::ProceedStartingRecognition( | |
134 const SpeechInputParams& params) { | |
135 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
136 DCHECK(!HasPendingRequest(params.caller_id)); | |
62 | 137 |
63 ShowRecognitionRequested( | 138 ShowRecognitionRequested( |
64 caller_id, render_process_id, render_view_id, element_rect); | 139 params.caller_id, params.render_process_id, params.render_view_id, |
140 params.element_rect); | |
65 GetRequestInfo(&can_report_metrics_, &request_info_); | 141 GetRequestInfo(&can_report_metrics_, &request_info_); |
66 | 142 |
67 SpeechInputRequest* request = &requests_[caller_id]; | 143 SpeechInputRequest* request = &requests_[params.caller_id]; |
68 request->delegate = delegate; | 144 request->delegate = params.delegate; |
69 request->recognizer = new SpeechRecognizer( | 145 request->recognizer = new SpeechRecognizer( |
70 this, caller_id, language, grammar, context_getter, | 146 this, params.caller_id, params.language, params.grammar, |
71 speech_input_prefs->filter_profanities(), | 147 params.context_getter, params.speech_input_prefs->filter_profanities(), |
72 request_info_, can_report_metrics_ ? origin_url : ""); | 148 request_info_, can_report_metrics_ ? params.origin_url : ""); |
73 request->is_active = false; | 149 request->is_active = false; |
74 | 150 |
75 StartRecognitionForRequest(caller_id); | 151 StartRecognitionForRequest(params.caller_id); |
76 } | 152 } |
77 | 153 |
78 void SpeechInputManager::StartRecognitionForRequest(int caller_id) { | 154 void SpeechInputManager::StartRecognitionForRequest(int caller_id) { |
79 DCHECK(HasPendingRequest(caller_id)); | 155 DCHECK(HasPendingRequest(caller_id)); |
80 | 156 |
81 // If we are currently recording audio for another caller, abort that cleanly. | 157 // If we are currently recording audio for another caller, abort that cleanly. |
82 if (recording_caller_id_) | 158 if (recording_caller_id_) |
83 CancelRecognitionAndInformDelegate(recording_caller_id_); | 159 CancelRecognitionAndInformDelegate(recording_caller_id_); |
84 | 160 |
85 if (!AudioManager::GetAudioManager()->HasAudioInputDevices()) { | 161 if (!AudioManager::GetAudioManager()->HasAudioInputDevices()) { |
(...skipping 26 matching lines...) Expand all Loading... | |
112 CancelRecognition(it->first); | 188 CancelRecognition(it->first); |
113 // This map will have very few elements so it is simpler to restart. | 189 // This map will have very few elements so it is simpler to restart. |
114 it = requests_.begin(); | 190 it = requests_.begin(); |
115 } else { | 191 } else { |
116 ++it; | 192 ++it; |
117 } | 193 } |
118 } | 194 } |
119 } | 195 } |
120 | 196 |
121 void SpeechInputManager::StopRecording(int caller_id) { | 197 void SpeechInputManager::StopRecording(int caller_id) { |
122 DCHECK(HasPendingRequest(caller_id)); | 198 // No pending requests on extension popups. |
199 if (!HasPendingRequest(caller_id)) | |
200 return; | |
201 | |
123 requests_[caller_id].recognizer->StopRecording(); | 202 requests_[caller_id].recognizer->StopRecording(); |
124 } | 203 } |
125 | 204 |
126 void SpeechInputManager::SetRecognitionResult( | 205 void SpeechInputManager::SetRecognitionResult( |
127 int caller_id, const SpeechInputResult& result) { | 206 int caller_id, const SpeechInputResult& result) { |
128 DCHECK(HasPendingRequest(caller_id)); | 207 DCHECK(HasPendingRequest(caller_id)); |
129 GetDelegate(caller_id)->SetRecognitionResult(caller_id, result); | 208 GetDelegate(caller_id)->SetRecognitionResult(caller_id, result); |
130 } | 209 } |
131 | 210 |
132 void SpeechInputManager::DidCompleteRecording(int caller_id) { | 211 void SpeechInputManager::DidCompleteRecording(int caller_id) { |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
200 | 279 |
201 SpeechInputManager::SpeechInputRequest::SpeechInputRequest() | 280 SpeechInputManager::SpeechInputRequest::SpeechInputRequest() |
202 : delegate(NULL), | 281 : delegate(NULL), |
203 is_active(false) { | 282 is_active(false) { |
204 } | 283 } |
205 | 284 |
206 SpeechInputManager::SpeechInputRequest::~SpeechInputRequest() { | 285 SpeechInputManager::SpeechInputRequest::~SpeechInputRequest() { |
207 } | 286 } |
208 | 287 |
209 } // namespace speech_input | 288 } // namespace speech_input |
OLD | NEW |