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