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 "chrome/browser/speech/chrome_speech_input_manager_delegate.h" | 5 #include "chrome/browser/speech/chrome_speech_recognition_manager_delegate.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/synchronization/lock.h" | 10 #include "base/synchronization/lock.h" |
11 #include "base/threading/thread_restrictions.h" | 11 #include "base/threading/thread_restrictions.h" |
12 #include "base/utf_string_conversions.h" | 12 #include "base/utf_string_conversions.h" |
13 #include "chrome/browser/browser_process.h" | 13 #include "chrome/browser/browser_process.h" |
14 #include "chrome/browser/prefs/pref_service.h" | 14 #include "chrome/browser/prefs/pref_service.h" |
15 #include "chrome/browser/profiles/profile_manager.h" | 15 #include "chrome/browser/profiles/profile_manager.h" |
16 #include "chrome/browser/tab_contents/tab_util.h" | 16 #include "chrome/browser/tab_contents/tab_util.h" |
17 #include "chrome/common/pref_names.h" | 17 #include "chrome/common/pref_names.h" |
18 #include "content/public/browser/browser_thread.h" | 18 #include "content/public/browser/browser_thread.h" |
19 #include "content/public/browser/resource_context.h" | 19 #include "content/public/browser/resource_context.h" |
20 #include "content/public/browser/speech_input_manager.h" | 20 #include "content/public/browser/speech_recognition_manager.h" |
21 #include "content/public/common/speech_input_result.h" | 21 #include "content/public/common/speech_recognition_result.h" |
22 #include "grit/generated_resources.h" | 22 #include "grit/generated_resources.h" |
23 #include "ui/base/l10n/l10n_util.h" | 23 #include "ui/base/l10n/l10n_util.h" |
24 | 24 |
25 #if defined(OS_WIN) | 25 #if defined(OS_WIN) |
26 #include "chrome/installer/util/wmi.h" | 26 #include "chrome/installer/util/wmi.h" |
27 #endif | 27 #endif |
28 | 28 |
29 using content::BrowserThread; | 29 using content::BrowserThread; |
30 using content::SpeechInputManager; | 30 using content::SpeechRecognitionManager; |
31 | 31 |
32 namespace speech_input { | 32 namespace speech { |
33 | 33 |
34 // Asynchronously fetches the PC and audio hardware/driver info if | 34 // Asynchronously fetches the PC and audio hardware/driver info if |
35 // the user has opted into UMA. This information is sent with speech input | 35 // the user has opted into UMA. This information is sent with speech input |
36 // requests to the server for identifying and improving quality issues with | 36 // requests to the server for identifying and improving quality issues with |
37 // specific device configurations. | 37 // specific device configurations. |
38 class ChromeSpeechInputManagerDelegate::OptionalRequestInfo | 38 class ChromeSpeechRecognitionManagerDelegate::OptionalRequestInfo |
39 : public base::RefCountedThreadSafe<OptionalRequestInfo> { | 39 : public base::RefCountedThreadSafe<OptionalRequestInfo> { |
40 public: | 40 public: |
41 OptionalRequestInfo() : can_report_metrics_(false) { | 41 OptionalRequestInfo() : can_report_metrics_(false) { |
42 } | 42 } |
43 | 43 |
44 void Refresh() { | 44 void Refresh() { |
45 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 45 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
46 // UMA opt-in can be checked only from the UI thread, so switch to that. | 46 // UMA opt-in can be checked only from the UI thread, so switch to that. |
47 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 47 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
48 base::Bind(&OptionalRequestInfo::CheckUMAAndGetHardwareInfo, this)); | 48 base::Bind(&OptionalRequestInfo::CheckUMAAndGetHardwareInfo, this)); |
49 } | 49 } |
50 | 50 |
51 void CheckUMAAndGetHardwareInfo() { | 51 void CheckUMAAndGetHardwareInfo() { |
52 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 52 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
53 if (g_browser_process->local_state()->GetBoolean( | 53 if (g_browser_process->local_state()->GetBoolean( |
54 prefs::kMetricsReportingEnabled)) { | 54 prefs::kMetricsReportingEnabled)) { |
55 // Access potentially slow OS calls from the FILE thread. | 55 // Access potentially slow OS calls from the FILE thread. |
56 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, | 56 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
57 base::Bind(&OptionalRequestInfo::GetHardwareInfo, this)); | 57 base::Bind(&OptionalRequestInfo::GetHardwareInfo, this)); |
58 } | 58 } |
59 } | 59 } |
60 | 60 |
61 void GetHardwareInfo() { | 61 void GetHardwareInfo() { |
62 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 62 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
63 base::AutoLock lock(lock_); | 63 base::AutoLock lock(lock_); |
64 can_report_metrics_ = true; | 64 can_report_metrics_ = true; |
65 string16 device_model = | 65 string16 device_model = |
66 SpeechInputManager::GetInstance()->GetAudioInputDeviceModel(); | 66 SpeechRecognitionManager::GetInstance()->GetAudioInputDeviceModel(); |
67 #if defined(OS_WIN) | 67 #if defined(OS_WIN) |
68 value_ = UTF16ToUTF8( | 68 value_ = UTF16ToUTF8( |
69 installer::WMIComputerSystem::GetModel() + L"|" + device_model); | 69 installer::WMIComputerSystem::GetModel() + L"|" + device_model); |
70 #else // defined(OS_WIN) | 70 #else // defined(OS_WIN) |
71 value_ = UTF16ToUTF8(device_model); | 71 value_ = UTF16ToUTF8(device_model); |
72 #endif // defined(OS_WIN) | 72 #endif // defined(OS_WIN) |
73 } | 73 } |
74 | 74 |
75 std::string value() { | 75 std::string value() { |
76 base::AutoLock lock(lock_); | 76 base::AutoLock lock(lock_); |
(...skipping 10 matching lines...) Expand all Loading... |
87 | 87 |
88 ~OptionalRequestInfo() {} | 88 ~OptionalRequestInfo() {} |
89 | 89 |
90 base::Lock lock_; | 90 base::Lock lock_; |
91 std::string value_; | 91 std::string value_; |
92 bool can_report_metrics_; | 92 bool can_report_metrics_; |
93 | 93 |
94 DISALLOW_COPY_AND_ASSIGN(OptionalRequestInfo); | 94 DISALLOW_COPY_AND_ASSIGN(OptionalRequestInfo); |
95 }; | 95 }; |
96 | 96 |
97 ChromeSpeechInputManagerDelegate::ChromeSpeechInputManagerDelegate() | 97 ChromeSpeechRecognitionManagerDelegate::ChromeSpeechRecognitionManagerDelegate() |
98 : bubble_controller_(new SpeechInputBubbleController( | 98 : bubble_controller_(new SpeechRecognitionBubbleController( |
99 ALLOW_THIS_IN_INITIALIZER_LIST(this))) { | 99 ALLOW_THIS_IN_INITIALIZER_LIST(this))) { |
100 } | 100 } |
101 | 101 |
102 ChromeSpeechInputManagerDelegate::~ChromeSpeechInputManagerDelegate() { | 102 ChromeSpeechRecognitionManagerDelegate:: |
| 103 ~ChromeSpeechRecognitionManagerDelegate() { |
103 } | 104 } |
104 | 105 |
105 void ChromeSpeechInputManagerDelegate::ShowRecognitionRequested( | 106 void ChromeSpeechRecognitionManagerDelegate::ShowRecognitionRequested( |
106 int caller_id, | 107 int caller_id, |
107 int render_process_id, | 108 int render_process_id, |
108 int render_view_id, | 109 int render_view_id, |
109 const gfx::Rect& element_rect) { | 110 const gfx::Rect& element_rect) { |
110 bubble_controller_->CreateBubble(caller_id, render_process_id, | 111 bubble_controller_->CreateBubble(caller_id, render_process_id, |
111 render_view_id, element_rect); | 112 render_view_id, element_rect); |
112 } | 113 } |
113 | 114 |
114 void ChromeSpeechInputManagerDelegate::GetRequestInfo( | 115 void ChromeSpeechRecognitionManagerDelegate::GetRequestInfo( |
115 bool* can_report_metrics, | 116 bool* can_report_metrics, |
116 std::string* request_info) { | 117 std::string* request_info) { |
117 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 118 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
118 if (!optional_request_info_.get()) { | 119 if (!optional_request_info_.get()) { |
119 optional_request_info_ = new OptionalRequestInfo(); | 120 optional_request_info_ = new OptionalRequestInfo(); |
120 // Since hardware info is optional with speech input requests, we start an | 121 // Since hardware info is optional with speech input requests, we start an |
121 // asynchronous fetch here and move on with recording audio. This first | 122 // asynchronous fetch here and move on with recording audio. This first |
122 // speech input request would send an empty string for hardware info and | 123 // speech input request would send an empty string for hardware info and |
123 // subsequent requests may have the hardware info available if the fetch | 124 // subsequent requests may have the hardware info available if the fetch |
124 // completed before them. This way we don't end up stalling the user with | 125 // completed before them. This way we don't end up stalling the user with |
125 // a long wait and disk seeks when they click on a UI element and start | 126 // a long wait and disk seeks when they click on a UI element and start |
126 // speaking. | 127 // speaking. |
127 optional_request_info_->Refresh(); | 128 optional_request_info_->Refresh(); |
128 } | 129 } |
129 *can_report_metrics = optional_request_info_->can_report_metrics(); | 130 *can_report_metrics = optional_request_info_->can_report_metrics(); |
130 *request_info = optional_request_info_->value(); | 131 *request_info = optional_request_info_->value(); |
131 } | 132 } |
132 | 133 |
133 void ChromeSpeechInputManagerDelegate::ShowWarmUp(int caller_id) { | 134 void ChromeSpeechRecognitionManagerDelegate::ShowWarmUp(int caller_id) { |
134 bubble_controller_->SetBubbleWarmUpMode(caller_id); | 135 bubble_controller_->SetBubbleWarmUpMode(caller_id); |
135 } | 136 } |
136 | 137 |
137 void ChromeSpeechInputManagerDelegate::ShowRecognizing(int caller_id) { | 138 void ChromeSpeechRecognitionManagerDelegate::ShowRecognizing(int caller_id) { |
138 bubble_controller_->SetBubbleRecognizingMode(caller_id); | 139 bubble_controller_->SetBubbleRecognizingMode(caller_id); |
139 } | 140 } |
140 | 141 |
141 void ChromeSpeechInputManagerDelegate::ShowRecording(int caller_id) { | 142 void ChromeSpeechRecognitionManagerDelegate::ShowRecording(int caller_id) { |
142 bubble_controller_->SetBubbleRecordingMode(caller_id); | 143 bubble_controller_->SetBubbleRecordingMode(caller_id); |
143 } | 144 } |
144 | 145 |
145 void ChromeSpeechInputManagerDelegate::ShowInputVolume( | 146 void ChromeSpeechRecognitionManagerDelegate::ShowInputVolume( |
146 int caller_id, float volume, float noise_volume) { | 147 int caller_id, float volume, float noise_volume) { |
147 bubble_controller_->SetBubbleInputVolume(caller_id, volume, noise_volume); | 148 bubble_controller_->SetBubbleInputVolume(caller_id, volume, noise_volume); |
148 } | 149 } |
149 | 150 |
150 void ChromeSpeechInputManagerDelegate::ShowMicError(int caller_id, | 151 void ChromeSpeechRecognitionManagerDelegate::ShowMicError(int caller_id, |
151 MicError error) { | 152 MicError error) { |
152 switch (error) { | 153 switch (error) { |
153 case MIC_ERROR_NO_DEVICE_AVAILABLE: | 154 case MIC_ERROR_NO_DEVICE_AVAILABLE: |
154 bubble_controller_->SetBubbleMessage( | 155 bubble_controller_->SetBubbleMessage( |
155 caller_id, l10n_util::GetStringUTF16(IDS_SPEECH_INPUT_NO_MIC)); | 156 caller_id, l10n_util::GetStringUTF16(IDS_SPEECH_INPUT_NO_MIC)); |
156 break; | 157 break; |
157 | 158 |
158 case MIC_ERROR_DEVICE_IN_USE: | 159 case MIC_ERROR_DEVICE_IN_USE: |
159 bubble_controller_->SetBubbleMessage( | 160 bubble_controller_->SetBubbleMessage( |
160 caller_id, l10n_util::GetStringUTF16(IDS_SPEECH_INPUT_MIC_IN_USE)); | 161 caller_id, l10n_util::GetStringUTF16(IDS_SPEECH_INPUT_MIC_IN_USE)); |
161 break; | 162 break; |
162 | 163 |
163 default: | 164 default: |
164 NOTREACHED(); | 165 NOTREACHED(); |
165 } | 166 } |
166 } | 167 } |
167 | 168 |
168 void ChromeSpeechInputManagerDelegate::ShowRecognizerError( | 169 void ChromeSpeechRecognitionManagerDelegate::ShowRecognizerError( |
169 int caller_id, content::SpeechInputError error) { | 170 int caller_id, content::SpeechRecognitionErrorCode error) { |
170 struct ErrorMessageMapEntry { | 171 struct ErrorMessageMapEntry { |
171 content::SpeechInputError error; | 172 content::SpeechRecognitionErrorCode error; |
172 int message_id; | 173 int message_id; |
173 }; | 174 }; |
174 ErrorMessageMapEntry error_message_map[] = { | 175 ErrorMessageMapEntry error_message_map[] = { |
175 { | 176 { |
176 content::SPEECH_INPUT_ERROR_AUDIO, IDS_SPEECH_INPUT_MIC_ERROR | 177 content::SPEECH_RECOGNITION_ERROR_AUDIO, IDS_SPEECH_INPUT_MIC_ERROR |
177 }, { | 178 }, { |
178 content::SPEECH_INPUT_ERROR_NO_SPEECH, IDS_SPEECH_INPUT_NO_SPEECH | 179 content::SPEECH_RECOGNITION_ERROR_NO_SPEECH, IDS_SPEECH_INPUT_NO_SPEECH |
179 }, { | 180 }, { |
180 content::SPEECH_INPUT_ERROR_NO_MATCH, IDS_SPEECH_INPUT_NO_RESULTS | 181 content::SPEECH_RECOGNITION_ERROR_NO_MATCH, IDS_SPEECH_INPUT_NO_RESULTS |
181 }, { | 182 }, { |
182 content::SPEECH_INPUT_ERROR_NETWORK, IDS_SPEECH_INPUT_NET_ERROR | 183 content::SPEECH_RECOGNITION_ERROR_NETWORK, IDS_SPEECH_INPUT_NET_ERROR |
183 } | 184 } |
184 }; | 185 }; |
185 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(error_message_map); ++i) { | 186 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(error_message_map); ++i) { |
186 if (error_message_map[i].error == error) { | 187 if (error_message_map[i].error == error) { |
187 bubble_controller_->SetBubbleMessage( | 188 bubble_controller_->SetBubbleMessage( |
188 caller_id, | 189 caller_id, |
189 l10n_util::GetStringUTF16(error_message_map[i].message_id)); | 190 l10n_util::GetStringUTF16(error_message_map[i].message_id)); |
190 return; | 191 return; |
191 } | 192 } |
192 } | 193 } |
193 | 194 |
194 NOTREACHED() << "unknown error " << error; | 195 NOTREACHED() << "unknown error " << error; |
195 } | 196 } |
196 | 197 |
197 void ChromeSpeechInputManagerDelegate::DoClose(int caller_id) { | 198 void ChromeSpeechRecognitionManagerDelegate::DoClose(int caller_id) { |
198 bubble_controller_->CloseBubble(caller_id); | 199 bubble_controller_->CloseBubble(caller_id); |
199 } | 200 } |
200 | 201 |
201 void ChromeSpeechInputManagerDelegate::InfoBubbleButtonClicked( | 202 void ChromeSpeechRecognitionManagerDelegate::InfoBubbleButtonClicked( |
202 int caller_id, SpeechInputBubble::Button button) { | 203 int caller_id, SpeechRecognitionBubble::Button button) { |
203 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 204 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
204 | 205 |
205 if (button == SpeechInputBubble::BUTTON_CANCEL) { | 206 if (button == SpeechRecognitionBubble::BUTTON_CANCEL) { |
206 SpeechInputManager::GetInstance()->CancelRecognitionForRequest(caller_id); | 207 SpeechRecognitionManager::GetInstance()->CancelRecognitionForRequest( |
207 } else if (button == SpeechInputBubble::BUTTON_TRY_AGAIN) { | 208 caller_id); |
208 SpeechInputManager::GetInstance()->StartRecognitionForRequest(caller_id); | 209 } else if (button == SpeechRecognitionBubble::BUTTON_TRY_AGAIN) { |
| 210 SpeechRecognitionManager::GetInstance()->StartRecognitionForRequest( |
| 211 caller_id); |
209 } | 212 } |
210 } | 213 } |
211 | 214 |
212 void ChromeSpeechInputManagerDelegate::InfoBubbleFocusChanged(int caller_id) { | 215 void ChromeSpeechRecognitionManagerDelegate::InfoBubbleFocusChanged( |
| 216 int caller_id) { |
213 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 217 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
214 | 218 SpeechRecognitionManager::GetInstance()->FocusLostForRequest(caller_id); |
215 SpeechInputManager::GetInstance()->FocusLostForRequest(caller_id); | |
216 } | 219 } |
217 | 220 |
218 } // namespace speech_input | 221 } // namespace speech |
OLD | NEW |