Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(97)

Side by Side Diff: chrome/browser/ui/app_list/start_page_service.cc

Issue 676593003: Implement native speech recognition for the launcher. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Refactor and address review comments. Created 6 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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/ui/app_list/start_page_service.h" 5 #include "chrome/browser/ui/app_list/start_page_service.h"
6 6
7 #include <string> 7 #include <string>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/command_line.h" 10 #include "base/command_line.h"
11 #include "base/memory/singleton.h" 11 #include "base/memory/singleton.h"
12 #include "base/metrics/user_metrics.h" 12 #include "base/metrics/user_metrics.h"
13 #include "base/prefs/pref_service.h" 13 #include "base/prefs/pref_service.h"
14 #include "chrome/browser/browser_process.h"
14 #include "chrome/browser/chrome_notification_types.h" 15 #include "chrome/browser/chrome_notification_types.h"
15 #include "chrome/browser/media/media_stream_infobar_delegate.h" 16 #include "chrome/browser/media/media_stream_infobar_delegate.h"
16 #include "chrome/browser/profiles/profile.h" 17 #include "chrome/browser/profiles/profile.h"
17 #include "chrome/browser/search/hotword_service.h" 18 #include "chrome/browser/search/hotword_service.h"
18 #include "chrome/browser/search/hotword_service_factory.h" 19 #include "chrome/browser/search/hotword_service_factory.h"
19 #include "chrome/browser/ui/app_list/recommended_apps.h" 20 #include "chrome/browser/ui/app_list/recommended_apps.h"
21 #include "chrome/browser/ui/app_list/speech_recognizer.h"
20 #include "chrome/browser/ui/app_list/start_page_observer.h" 22 #include "chrome/browser/ui/app_list/start_page_observer.h"
21 #include "chrome/browser/ui/app_list/start_page_service_factory.h" 23 #include "chrome/browser/ui/app_list/start_page_service_factory.h"
22 #include "chrome/common/chrome_switches.h" 24 #include "chrome/common/chrome_switches.h"
23 #include "chrome/common/pref_names.h" 25 #include "chrome/common/pref_names.h"
24 #include "chrome/common/url_constants.h" 26 #include "chrome/common/url_constants.h"
25 #include "content/public/browser/notification_details.h" 27 #include "content/public/browser/notification_details.h"
26 #include "content/public/browser/notification_observer.h" 28 #include "content/public/browser/notification_observer.h"
27 #include "content/public/browser/notification_registrar.h" 29 #include "content/public/browser/notification_registrar.h"
28 #include "content/public/browser/notification_service.h" 30 #include "content/public/browser/notification_service.h"
29 #include "content/public/browser/notification_source.h" 31 #include "content/public/browser/notification_source.h"
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after
129 observers_.AddObserver(observer); 131 observers_.AddObserver(observer);
130 } 132 }
131 133
132 void StartPageService::RemoveObserver(StartPageObserver* observer) { 134 void StartPageService::RemoveObserver(StartPageObserver* observer) {
133 observers_.RemoveObserver(observer); 135 observers_.RemoveObserver(observer);
134 } 136 }
135 137
136 void StartPageService::AppListShown() { 138 void StartPageService::AppListShown() {
137 if (!contents_) { 139 if (!contents_) {
138 LoadContents(); 140 LoadContents();
139 } else if (contents_->GetWebUI()) { 141 } else if (contents_->GetWebUI() &&
142 !HotwordService::IsExperimentalHotwordingEnabled()) {
Matt Giuca 2014/11/03 02:35:52 Why did you add this?
Anand Mistry (off Chromium) 2014/11/03 06:51:56 New hotwording doesn't use the web speech api, whi
140 // If experimental hotwording is enabled, don't enable hotwording in the 143 // If experimental hotwording is enabled, don't enable hotwording in the
141 // start page, since the hotword extension is taking care of this. 144 // start page, since the hotword extension is taking care of this.
142 bool hotword_enabled = HotwordEnabled() && 145 bool hotword_enabled = HotwordEnabled() &&
143 !HotwordService::IsExperimentalHotwordingEnabled(); 146 !HotwordService::IsExperimentalHotwordingEnabled();
Matt Giuca 2014/11/03 02:35:52 The second clause can be removed as it is now alwa
Anand Mistry (off Chromium) 2014/11/03 06:51:56 Done.
144 contents_->GetWebUI()->CallJavascriptFunction( 147 contents_->GetWebUI()->CallJavascriptFunction(
145 "appList.startPage.onAppListShown", 148 "appList.startPage.onAppListShown",
146 base::FundamentalValue(hotword_enabled)); 149 base::FundamentalValue(hotword_enabled));
147 } 150 }
148 } 151 }
149 152
150 void StartPageService::AppListHidden() { 153 void StartPageService::AppListHidden() {
151 if (contents_->GetWebUI()) { 154 if (contents_->GetWebUI()) {
152 contents_->GetWebUI()->CallJavascriptFunction( 155 contents_->GetWebUI()->CallJavascriptFunction(
153 "appList.startPage.onAppListHidden"); 156 "appList.startPage.onAppListHidden");
154 } 157 }
155 if (!app_list::switches::IsExperimentalAppListEnabled()) 158 if (!app_list::switches::IsExperimentalAppListEnabled())
156 UnloadContents(); 159 UnloadContents();
160
161 if (HotwordService::IsExperimentalHotwordingEnabled() &&
162 speech_recognizer_.get()) {
163 speech_recognizer_->Stop();
164 }
157 } 165 }
158 166
159 void StartPageService::ToggleSpeechRecognition() { 167 void StartPageService::ToggleSpeechRecognition() {
160 DCHECK(contents_); 168 DCHECK(contents_);
161 speech_button_toggled_manually_ = true; 169 speech_button_toggled_manually_ = true;
162 if (!contents_->GetWebUI()) 170 if (!contents_->GetWebUI())
163 return; 171 return;
164 172
165 if (webui_finished_loading_) { 173 if (!webui_finished_loading_) {
166 contents_->GetWebUI()->CallJavascriptFunction(
167 "appList.startPage.toggleSpeechRecognition");
168 } else {
169 pending_webui_callbacks_.push_back( 174 pending_webui_callbacks_.push_back(
170 base::Bind(&StartPageService::ToggleSpeechRecognition, 175 base::Bind(&StartPageService::ToggleSpeechRecognition,
171 base::Unretained(this))); 176 base::Unretained(this)));
177 return;
172 } 178 }
179
180 if (HotwordService::IsExperimentalHotwordingEnabled()) {
181 if (!speech_recognizer_.get()) {
182 std::string profile_locale;
183 #if defined(OS_CHROMEOS)
184 profile_locale = profile_->GetPrefs()->GetString(
185 prefs::kApplicationLocale);
186 #endif
187 if (profile_locale.empty())
188 profile_locale = g_browser_process->GetApplicationLocale();
189
190 speech_recognizer_ = new SpeechRecognizer(this,
191 profile_->GetRequestContext(),
192 profile_locale);
193 }
194
195 speech_recognizer_->Start();
196 return;
197 }
198
199 contents_->GetWebUI()->CallJavascriptFunction(
200 "appList.startPage.toggleSpeechRecognition");
173 } 201 }
174 202
175 bool StartPageService::HotwordEnabled() { 203 bool StartPageService::HotwordEnabled() {
176 if (HotwordService::IsExperimentalHotwordingEnabled()) { 204 if (HotwordService::IsExperimentalHotwordingEnabled()) {
177 return HotwordServiceFactory::IsServiceAvailable(profile_) && 205 return HotwordServiceFactory::IsServiceAvailable(profile_) &&
178 profile_->GetPrefs()->GetBoolean(prefs::kHotwordSearchEnabled); 206 profile_->GetPrefs()->GetBoolean(prefs::kHotwordSearchEnabled);
179 } 207 }
180 #if defined(OS_CHROMEOS) 208 #if defined(OS_CHROMEOS)
181 return HotwordServiceFactory::IsServiceAvailable(profile_) && 209 return HotwordServiceFactory::IsServiceAvailable(profile_) &&
182 profile_->GetPrefs()->GetBoolean(prefs::kHotwordSearchEnabled); 210 profile_->GetPrefs()->GetBoolean(prefs::kHotwordSearchEnabled);
(...skipping 20 matching lines...) Expand all
203 const base::string16& query, bool is_final) { 231 const base::string16& query, bool is_final) {
204 if (is_final) { 232 if (is_final) {
205 speech_result_obtained_ = true; 233 speech_result_obtained_ = true;
206 RecordAction(UserMetricsAction("AppList_SearchedBySpeech")); 234 RecordAction(UserMetricsAction("AppList_SearchedBySpeech"));
207 } 235 }
208 FOR_EACH_OBSERVER(StartPageObserver, 236 FOR_EACH_OBSERVER(StartPageObserver,
209 observers_, 237 observers_,
210 OnSpeechResult(query, is_final)); 238 OnSpeechResult(query, is_final));
211 } 239 }
212 240
213 void StartPageService::OnSpeechSoundLevelChanged(int16 level) { 241 void StartPageService::OnSpeechSoundLevelChanged(int16_t level) {
214 FOR_EACH_OBSERVER(StartPageObserver, 242 FOR_EACH_OBSERVER(StartPageObserver,
215 observers_, 243 observers_,
216 OnSpeechSoundLevelChanged(level)); 244 OnSpeechSoundLevelChanged(level));
217 } 245 }
218 246
219 void StartPageService::OnSpeechRecognitionStateChanged( 247 void StartPageService::OnSpeechRecognitionStateChanged(
220 SpeechRecognitionState new_state) { 248 SpeechRecognitionState new_state) {
249
250 if (HotwordService::IsExperimentalHotwordingEnabled() &&
251 new_state == SPEECH_RECOGNITION_READY &&
252 speech_recognizer_.get()) {
253 speech_recognizer_->Stop();
254 }
255
221 if (!InSpeechRecognition(state_) && InSpeechRecognition(new_state)) { 256 if (!InSpeechRecognition(state_) && InSpeechRecognition(new_state)) {
222 if (!speech_button_toggled_manually_ && 257 if (!speech_button_toggled_manually_ &&
223 state_ == SPEECH_RECOGNITION_HOTWORD_LISTENING) { 258 state_ == SPEECH_RECOGNITION_HOTWORD_LISTENING) {
224 RecordAction(UserMetricsAction("AppList_HotwordRecognized")); 259 RecordAction(UserMetricsAction("AppList_HotwordRecognized"));
225 } else { 260 } else {
226 RecordAction(UserMetricsAction("AppList_VoiceSearchStartedManually")); 261 RecordAction(UserMetricsAction("AppList_VoiceSearchStartedManually"));
227 } 262 }
228 } else if (InSpeechRecognition(state_) && !InSpeechRecognition(new_state) && 263 } else if (InSpeechRecognition(state_) && !InSpeechRecognition(new_state) &&
229 !speech_result_obtained_) { 264 !speech_result_obtained_) {
230 RecordAction(UserMetricsAction("AppList_VoiceSearchCanceled")); 265 RecordAction(UserMetricsAction("AppList_VoiceSearchCanceled"));
231 } 266 }
232 speech_button_toggled_manually_ = false; 267 speech_button_toggled_manually_ = false;
233 speech_result_obtained_ = false; 268 speech_result_obtained_ = false;
234 state_ = new_state; 269 state_ = new_state;
235 FOR_EACH_OBSERVER(StartPageObserver, 270 FOR_EACH_OBSERVER(StartPageObserver,
236 observers_, 271 observers_,
237 OnSpeechRecognitionStateChanged(new_state)); 272 OnSpeechRecognitionStateChanged(new_state));
238 } 273 }
239 274
275 content::WebContents* StartPageService::GetSpeechContents() {
276 return GetSpeechRecognitionContents();
277 }
278
240 void StartPageService::Shutdown() { 279 void StartPageService::Shutdown() {
241 UnloadContents(); 280 UnloadContents();
242 } 281 }
243 282
244 void StartPageService::WebUILoaded() { 283 void StartPageService::WebUILoaded() {
245 // There's a race condition between the WebUI loading, and calling its JS 284 // There's a race condition between the WebUI loading, and calling its JS
246 // functions. Specifically, calling LoadContents() doesn't mean that the page 285 // functions. Specifically, calling LoadContents() doesn't mean that the page
247 // has loaded, but several code paths make this assumption. This function 286 // has loaded, but several code paths make this assumption. This function
248 // allows us to defer calling JS functions until after the page has finished 287 // allows us to defer calling JS functions until after the page has finished
249 // loading. 288 // loading.
(...skipping 22 matching lines...) Expand all
272 ui::PAGE_TRANSITION_AUTO_TOPLEVEL, 311 ui::PAGE_TRANSITION_AUTO_TOPLEVEL,
273 std::string()); 312 std::string());
274 } 313 }
275 314
276 void StartPageService::UnloadContents() { 315 void StartPageService::UnloadContents() {
277 contents_.reset(); 316 contents_.reset();
278 webui_finished_loading_ = false; 317 webui_finished_loading_ = false;
279 } 318 }
280 319
281 } // namespace app_list 320 } // namespace app_list
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698