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

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: Fix timeout. 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"
20 #include "chrome/browser/ui/app_list/app_list_speech_recognizer.h"
19 #include "chrome/browser/ui/app_list/recommended_apps.h" 21 #include "chrome/browser/ui/app_list/recommended_apps.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"
(...skipping 100 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()) {
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();
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 AppListSpeechRecognizer(this, profile_locale);
191 }
192
193 speech_recognizer_->Start();
194 return;
195 }
196
197 contents_->GetWebUI()->CallJavascriptFunction(
198 "appList.startPage.toggleSpeechRecognition");
173 } 199 }
174 200
175 bool StartPageService::HotwordEnabled() { 201 bool StartPageService::HotwordEnabled() {
176 if (HotwordService::IsExperimentalHotwordingEnabled()) { 202 if (HotwordService::IsExperimentalHotwordingEnabled()) {
177 return HotwordServiceFactory::IsServiceAvailable(profile_) && 203 return HotwordServiceFactory::IsServiceAvailable(profile_) &&
178 profile_->GetPrefs()->GetBoolean(prefs::kHotwordSearchEnabled); 204 profile_->GetPrefs()->GetBoolean(prefs::kHotwordSearchEnabled);
179 } 205 }
180 #if defined(OS_CHROMEOS) 206 #if defined(OS_CHROMEOS)
181 return HotwordServiceFactory::IsServiceAvailable(profile_) && 207 return HotwordServiceFactory::IsServiceAvailable(profile_) &&
182 profile_->GetPrefs()->GetBoolean(prefs::kHotwordSearchEnabled); 208 profile_->GetPrefs()->GetBoolean(prefs::kHotwordSearchEnabled);
(...skipping 28 matching lines...) Expand all
211 } 237 }
212 238
213 void StartPageService::OnSpeechSoundLevelChanged(int16 level) { 239 void StartPageService::OnSpeechSoundLevelChanged(int16 level) {
214 FOR_EACH_OBSERVER(StartPageObserver, 240 FOR_EACH_OBSERVER(StartPageObserver,
215 observers_, 241 observers_,
216 OnSpeechSoundLevelChanged(level)); 242 OnSpeechSoundLevelChanged(level));
217 } 243 }
218 244
219 void StartPageService::OnSpeechRecognitionStateChanged( 245 void StartPageService::OnSpeechRecognitionStateChanged(
220 SpeechRecognitionState new_state) { 246 SpeechRecognitionState new_state) {
247
248 if (HotwordService::IsExperimentalHotwordingEnabled() &&
249 new_state == SPEECH_RECOGNITION_READY &&
250 speech_recognizer_.get()) {
251 speech_recognizer_->Stop();
252 }
253
221 if (!InSpeechRecognition(state_) && InSpeechRecognition(new_state)) { 254 if (!InSpeechRecognition(state_) && InSpeechRecognition(new_state)) {
222 if (!speech_button_toggled_manually_ && 255 if (!speech_button_toggled_manually_ &&
223 state_ == SPEECH_RECOGNITION_HOTWORD_LISTENING) { 256 state_ == SPEECH_RECOGNITION_HOTWORD_LISTENING) {
224 RecordAction(UserMetricsAction("AppList_HotwordRecognized")); 257 RecordAction(UserMetricsAction("AppList_HotwordRecognized"));
225 } else { 258 } else {
226 RecordAction(UserMetricsAction("AppList_VoiceSearchStartedManually")); 259 RecordAction(UserMetricsAction("AppList_VoiceSearchStartedManually"));
227 } 260 }
228 } else if (InSpeechRecognition(state_) && !InSpeechRecognition(new_state) && 261 } else if (InSpeechRecognition(state_) && !InSpeechRecognition(new_state) &&
229 !speech_result_obtained_) { 262 !speech_result_obtained_) {
230 RecordAction(UserMetricsAction("AppList_VoiceSearchCanceled")); 263 RecordAction(UserMetricsAction("AppList_VoiceSearchCanceled"));
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
272 ui::PAGE_TRANSITION_AUTO_TOPLEVEL, 305 ui::PAGE_TRANSITION_AUTO_TOPLEVEL,
273 std::string()); 306 std::string());
274 } 307 }
275 308
276 void StartPageService::UnloadContents() { 309 void StartPageService::UnloadContents() {
277 contents_.reset(); 310 contents_.reset();
278 webui_finished_loading_ = false; 311 webui_finished_loading_ = false;
279 } 312 }
280 313
281 } // namespace app_list 314 } // namespace app_list
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698