OLD | NEW |
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/command_line.h" | 9 #include "base/command_line.h" |
10 #include "base/memory/singleton.h" | 10 #include "base/memory/singleton.h" |
| 11 #include "base/metrics/user_metrics.h" |
11 #include "chrome/browser/chrome_notification_types.h" | 12 #include "chrome/browser/chrome_notification_types.h" |
12 #include "chrome/browser/media/media_stream_infobar_delegate.h" | 13 #include "chrome/browser/media/media_stream_infobar_delegate.h" |
13 #include "chrome/browser/profiles/profile.h" | 14 #include "chrome/browser/profiles/profile.h" |
14 #include "chrome/browser/ui/app_list/recommended_apps.h" | 15 #include "chrome/browser/ui/app_list/recommended_apps.h" |
15 #include "chrome/browser/ui/app_list/start_page_observer.h" | 16 #include "chrome/browser/ui/app_list/start_page_observer.h" |
16 #include "chrome/browser/ui/app_list/start_page_service_factory.h" | 17 #include "chrome/browser/ui/app_list/start_page_service_factory.h" |
17 #include "chrome/common/chrome_switches.h" | 18 #include "chrome/common/chrome_switches.h" |
18 #include "chrome/common/url_constants.h" | 19 #include "chrome/common/url_constants.h" |
19 #include "content/public/browser/notification_details.h" | 20 #include "content/public/browser/notification_details.h" |
20 #include "content/public/browser/notification_observer.h" | 21 #include "content/public/browser/notification_observer.h" |
21 #include "content/public/browser/notification_registrar.h" | 22 #include "content/public/browser/notification_registrar.h" |
22 #include "content/public/browser/notification_service.h" | 23 #include "content/public/browser/notification_service.h" |
23 #include "content/public/browser/notification_source.h" | 24 #include "content/public/browser/notification_source.h" |
24 #include "content/public/browser/web_contents.h" | 25 #include "content/public/browser/web_contents.h" |
25 #include "content/public/browser/web_contents_delegate.h" | 26 #include "content/public/browser/web_contents_delegate.h" |
26 #include "extensions/common/extension.h" | 27 #include "extensions/common/extension.h" |
27 #include "ui/app_list/app_list_switches.h" | 28 #include "ui/app_list/app_list_switches.h" |
28 | 29 |
| 30 using base::RecordAction; |
| 31 using base::UserMetricsAction; |
| 32 |
29 namespace app_list { | 33 namespace app_list { |
30 | 34 |
| 35 namespace { |
| 36 |
| 37 bool InSpeechRecognition(SpeechRecognitionState state) { |
| 38 return state == SPEECH_RECOGNITION_RECOGNIZING || |
| 39 state == SPEECH_RECOGNITION_IN_SPEECH; |
| 40 } |
| 41 |
| 42 } |
| 43 |
31 class StartPageService::ProfileDestroyObserver | 44 class StartPageService::ProfileDestroyObserver |
32 : public content::NotificationObserver { | 45 : public content::NotificationObserver { |
33 public: | 46 public: |
34 explicit ProfileDestroyObserver(StartPageService* service) | 47 explicit ProfileDestroyObserver(StartPageService* service) |
35 : service_(service) { | 48 : service_(service) { |
36 registrar_.Add(this, | 49 registrar_.Add(this, |
37 chrome::NOTIFICATION_PROFILE_DESTROYED, | 50 chrome::NOTIFICATION_PROFILE_DESTROYED, |
38 content::Source<Profile>(service_->profile())); | 51 content::Source<Profile>(service_->profile())); |
39 } | 52 } |
40 virtual ~ProfileDestroyObserver() {} | 53 virtual ~ProfileDestroyObserver() {} |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
75 | 88 |
76 // static | 89 // static |
77 StartPageService* StartPageService::Get(Profile* profile) { | 90 StartPageService* StartPageService::Get(Profile* profile) { |
78 return StartPageServiceFactory::GetForProfile(profile); | 91 return StartPageServiceFactory::GetForProfile(profile); |
79 } | 92 } |
80 | 93 |
81 StartPageService::StartPageService(Profile* profile) | 94 StartPageService::StartPageService(Profile* profile) |
82 : profile_(profile), | 95 : profile_(profile), |
83 profile_destroy_observer_(new ProfileDestroyObserver(this)), | 96 profile_destroy_observer_(new ProfileDestroyObserver(this)), |
84 recommended_apps_(new RecommendedApps(profile)), | 97 recommended_apps_(new RecommendedApps(profile)), |
85 state_(app_list::SPEECH_RECOGNITION_OFF) { | 98 state_(app_list::SPEECH_RECOGNITION_OFF), |
| 99 speech_button_toggled_manually_(false), |
| 100 speech_result_obtained_(false) { |
86 #if defined(OS_CHROMEOS) | 101 #if defined(OS_CHROMEOS) |
87 // Updates the default state to hotword listening, because this is | 102 // Updates the default state to hotword listening, because this is |
88 // the default behavior. This will be updated when the page is loaded and | 103 // the default behavior. This will be updated when the page is loaded and |
89 // the nacl module is loaded. | 104 // the nacl module is loaded. |
90 if (app_list::switches::IsVoiceSearchEnabled()) | 105 if (app_list::switches::IsVoiceSearchEnabled()) |
91 state_ = app_list::SPEECH_RECOGNITION_HOTWORD_LISTENING; | 106 state_ = app_list::SPEECH_RECOGNITION_HOTWORD_LISTENING; |
92 #endif | 107 #endif |
93 | 108 |
94 contents_.reset(content::WebContents::Create( | 109 contents_.reset(content::WebContents::Create( |
95 content::WebContents::CreateParams(profile_))); | 110 content::WebContents::CreateParams(profile_))); |
(...skipping 18 matching lines...) Expand all Loading... |
114 | 129 |
115 void StartPageService::AddObserver(StartPageObserver* observer) { | 130 void StartPageService::AddObserver(StartPageObserver* observer) { |
116 observers_.AddObserver(observer); | 131 observers_.AddObserver(observer); |
117 } | 132 } |
118 | 133 |
119 void StartPageService::RemoveObserver(StartPageObserver* observer) { | 134 void StartPageService::RemoveObserver(StartPageObserver* observer) { |
120 observers_.RemoveObserver(observer); | 135 observers_.RemoveObserver(observer); |
121 } | 136 } |
122 | 137 |
123 void StartPageService::ToggleSpeechRecognition() { | 138 void StartPageService::ToggleSpeechRecognition() { |
| 139 speech_button_toggled_manually_ = true; |
124 contents_->GetWebUI()->CallJavascriptFunction( | 140 contents_->GetWebUI()->CallJavascriptFunction( |
125 "appList.startPage.toggleSpeechRecognition"); | 141 "appList.startPage.toggleSpeechRecognition"); |
126 } | 142 } |
127 | 143 |
128 content::WebContents* StartPageService::GetStartPageContents() { | 144 content::WebContents* StartPageService::GetStartPageContents() { |
129 return CommandLine::ForCurrentProcess()->HasSwitch( | 145 return CommandLine::ForCurrentProcess()->HasSwitch( |
130 ::switches::kShowAppListStartPage) ? contents_.get() : NULL; | 146 ::switches::kShowAppListStartPage) ? contents_.get() : NULL; |
131 } | 147 } |
132 | 148 |
133 content::WebContents* StartPageService::GetSpeechRecognitionContents() { | 149 content::WebContents* StartPageService::GetSpeechRecognitionContents() { |
134 return app_list::switches::IsVoiceSearchEnabled() ? contents_.get() : NULL; | 150 return app_list::switches::IsVoiceSearchEnabled() ? contents_.get() : NULL; |
135 } | 151 } |
136 | 152 |
137 void StartPageService::OnSpeechResult( | 153 void StartPageService::OnSpeechResult( |
138 const base::string16& query, bool is_final) { | 154 const base::string16& query, bool is_final) { |
| 155 if (is_final) { |
| 156 speech_result_obtained_ = true; |
| 157 RecordAction(UserMetricsAction("AppList_SearchedBySpeech")); |
| 158 } |
139 FOR_EACH_OBSERVER(StartPageObserver, | 159 FOR_EACH_OBSERVER(StartPageObserver, |
140 observers_, | 160 observers_, |
141 OnSpeechResult(query, is_final)); | 161 OnSpeechResult(query, is_final)); |
142 } | 162 } |
143 | 163 |
144 void StartPageService::OnSpeechSoundLevelChanged(int16 level) { | 164 void StartPageService::OnSpeechSoundLevelChanged(int16 level) { |
145 FOR_EACH_OBSERVER(StartPageObserver, | 165 FOR_EACH_OBSERVER(StartPageObserver, |
146 observers_, | 166 observers_, |
147 OnSpeechSoundLevelChanged(level)); | 167 OnSpeechSoundLevelChanged(level)); |
148 } | 168 } |
149 | 169 |
150 void StartPageService::OnSpeechRecognitionStateChanged( | 170 void StartPageService::OnSpeechRecognitionStateChanged( |
151 SpeechRecognitionState new_state) { | 171 SpeechRecognitionState new_state) { |
| 172 if (!InSpeechRecognition(state_) && InSpeechRecognition(new_state)) { |
| 173 if (!speech_button_toggled_manually_ && |
| 174 state_ == SPEECH_RECOGNITION_HOTWORD_LISTENING) { |
| 175 RecordAction(UserMetricsAction("AppList_HotwordRecognized")); |
| 176 } else { |
| 177 RecordAction(UserMetricsAction("AppList_VoiceSearchStartedManually")); |
| 178 } |
| 179 } else if (InSpeechRecognition(state_) && !InSpeechRecognition(new_state) && |
| 180 !speech_result_obtained_) { |
| 181 RecordAction(UserMetricsAction("AppList_VoiceSearchCanceled")); |
| 182 } |
| 183 speech_button_toggled_manually_ = false; |
| 184 speech_result_obtained_ = false; |
152 state_ = new_state; | 185 state_ = new_state; |
153 FOR_EACH_OBSERVER(StartPageObserver, | 186 FOR_EACH_OBSERVER(StartPageObserver, |
154 observers_, | 187 observers_, |
155 OnSpeechRecognitionStateChanged(new_state)); | 188 OnSpeechRecognitionStateChanged(new_state)); |
156 } | 189 } |
157 | 190 |
158 void StartPageService::Shutdown() { | 191 void StartPageService::Shutdown() { |
159 contents_.reset(); | 192 contents_.reset(); |
160 } | 193 } |
161 | 194 |
162 } // namespace app_list | 195 } // namespace app_list |
OLD | NEW |